|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package table |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/aws/aws-sdk-go/aws" |
| 20 | + |
| 21 | + "github.com/aws-controllers-k8s/dynamodb-controller/apis/v1alpha1" |
| 22 | + compare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 23 | +) |
| 24 | + |
| 25 | +func Test_compareResourcePolicyDocument(t *testing.T) { |
| 26 | + type args struct { |
| 27 | + a *resource |
| 28 | + b *resource |
| 29 | + } |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + args args |
| 33 | + wantDifferent bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "both policies are nil", |
| 37 | + args: args{ |
| 38 | + a: &resource{ |
| 39 | + ko: &v1alpha1.Table{ |
| 40 | + Spec: v1alpha1.TableSpec{ |
| 41 | + ResourcePolicy: nil, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + b: &resource{ |
| 46 | + ko: &v1alpha1.Table{ |
| 47 | + Spec: v1alpha1.TableSpec{ |
| 48 | + ResourcePolicy: nil, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + wantDifferent: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "desired policy is set, latest policy is nil", |
| 57 | + args: args{ |
| 58 | + a: &resource{ |
| 59 | + ko: &v1alpha1.Table{ |
| 60 | + Spec: v1alpha1.TableSpec{ |
| 61 | + ResourcePolicy: aws.String(`{"Version": "2012-10-17"}`), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + b: &resource{ |
| 66 | + ko: &v1alpha1.Table{ |
| 67 | + Spec: v1alpha1.TableSpec{ |
| 68 | + ResourcePolicy: nil, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + wantDifferent: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "identical policy documents", |
| 77 | + args: args{ |
| 78 | + a: &resource{ |
| 79 | + ko: &v1alpha1.Table{ |
| 80 | + Spec: v1alpha1.TableSpec{ |
| 81 | + ResourcePolicy: aws.String(`{ |
| 82 | + "Version": "2012-10-17", |
| 83 | + "Statement": [ |
| 84 | + { |
| 85 | + "Effect": "Allow", |
| 86 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 87 | + "Action": ["dynamodb:GetItem", "dynamodb:Query"], |
| 88 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 89 | + } |
| 90 | + ] |
| 91 | + }`), |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + b: &resource{ |
| 96 | + ko: &v1alpha1.Table{ |
| 97 | + Spec: v1alpha1.TableSpec{ |
| 98 | + ResourcePolicy: aws.String(`{ |
| 99 | + "Version": "2012-10-17", |
| 100 | + "Statement": [ |
| 101 | + { |
| 102 | + "Effect": "Allow", |
| 103 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 104 | + "Action": ["dynamodb:GetItem", "dynamodb:Query"], |
| 105 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 106 | + } |
| 107 | + ] |
| 108 | + }`), |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + wantDifferent: false, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "same policy content with different whitespace formatting", |
| 117 | + args: args{ |
| 118 | + a: &resource{ |
| 119 | + ko: &v1alpha1.Table{ |
| 120 | + Spec: v1alpha1.TableSpec{ |
| 121 | + ResourcePolicy: aws.String(`{ |
| 122 | + "Version": "2012-10-17", |
| 123 | + "Statement": [ |
| 124 | + { |
| 125 | + "Effect": "Allow", |
| 126 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 127 | + "Action": ["dynamodb:GetItem", "dynamodb:Query"], |
| 128 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 129 | + } |
| 130 | + ] |
| 131 | + }`), |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + b: &resource{ |
| 136 | + ko: &v1alpha1.Table{ |
| 137 | + Spec: v1alpha1.TableSpec{ |
| 138 | + ResourcePolicy: aws.String(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789012:root"},"Action":["dynamodb:GetItem","dynamodb:Query"],"Resource":"arn:aws:dynamodb:us-west-2:123456789012:table/MyTable"}]}`), |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + wantDifferent: false, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "different effect in statement", |
| 147 | + args: args{ |
| 148 | + a: &resource{ |
| 149 | + ko: &v1alpha1.Table{ |
| 150 | + Spec: v1alpha1.TableSpec{ |
| 151 | + ResourcePolicy: aws.String(`{ |
| 152 | + "Version": "2012-10-17", |
| 153 | + "Statement": [ |
| 154 | + { |
| 155 | + "Effect": "Allow", |
| 156 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 157 | + "Action": ["dynamodb:GetItem"], |
| 158 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 159 | + } |
| 160 | + ] |
| 161 | + }`), |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + b: &resource{ |
| 166 | + ko: &v1alpha1.Table{ |
| 167 | + Spec: v1alpha1.TableSpec{ |
| 168 | + ResourcePolicy: aws.String(`{ |
| 169 | + "Version": "2012-10-17", |
| 170 | + "Statement": [ |
| 171 | + { |
| 172 | + "Effect": "Deny", |
| 173 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 174 | + "Action": ["dynamodb:GetItem"], |
| 175 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 176 | + } |
| 177 | + ] |
| 178 | + }`), |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + wantDifferent: true, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "different actions in statement", |
| 187 | + args: args{ |
| 188 | + a: &resource{ |
| 189 | + ko: &v1alpha1.Table{ |
| 190 | + Spec: v1alpha1.TableSpec{ |
| 191 | + ResourcePolicy: aws.String(`{ |
| 192 | + "Version": "2012-10-17", |
| 193 | + "Statement": [ |
| 194 | + { |
| 195 | + "Effect": "Allow", |
| 196 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 197 | + "Action": ["dynamodb:GetItem"], |
| 198 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 199 | + } |
| 200 | + ] |
| 201 | + }`), |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | + b: &resource{ |
| 206 | + ko: &v1alpha1.Table{ |
| 207 | + Spec: v1alpha1.TableSpec{ |
| 208 | + ResourcePolicy: aws.String(`{ |
| 209 | + "Version": "2012-10-17", |
| 210 | + "Statement": [ |
| 211 | + { |
| 212 | + "Effect": "Allow", |
| 213 | + "Principal": {"AWS": "arn:aws:iam::123456789012:root"}, |
| 214 | + "Action": ["dynamodb:Query"], |
| 215 | + "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MyTable" |
| 216 | + } |
| 217 | + ] |
| 218 | + }`), |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + }, |
| 223 | + wantDifferent: true, |
| 224 | + }, |
| 225 | + } |
| 226 | + for _, tt := range tests { |
| 227 | + t.Run(tt.name, func(t *testing.T) { |
| 228 | + delta := &compare.Delta{} |
| 229 | + compareResourcePolicyDocument(delta, tt.args.a, tt.args.b) |
| 230 | + if got := delta.DifferentAt("Spec.ResourcePolicy"); got != tt.wantDifferent { |
| 231 | + t.Errorf("compareResourcePolicyDocument() difference = %v, want %v", got, tt.wantDifferent) |
| 232 | + } |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
0 commit comments