@@ -138,11 +138,10 @@ data "aws_iam_policy_document" "this" {
138
138
}
139
139
}
140
140
141
-
142
141
resource "aws_iam_policy" "this" {
143
142
for_each = local.sqs
144
- name = "test-${each.key}"
145
- policy = data.aws_iam_policy_document.this[each.key].json
143
+ name = "test-${each.key}"
144
+ policy = data.aws_iam_policy_document.this[each.key].json
146
145
}` ,
147
146
expected : []iam.Policy {
148
147
{
@@ -169,6 +168,241 @@ resource "aws_iam_policy" "this" {
169
168
},
170
169
},
171
170
},
171
+ {
172
+ name : "policy_document with source_policy_documents" ,
173
+ terraform : `
174
+ data "aws_iam_policy_document" "source" {
175
+ statement {
176
+ actions = ["ec2:*"]
177
+ resources = ["*"]
178
+ }
179
+ }
180
+
181
+ data "aws_iam_policy_document" "source_document_example" {
182
+ source_policy_documents = [data.aws_iam_policy_document.source.json]
183
+
184
+ statement {
185
+ actions = ["s3:*"]
186
+
187
+ resources = [
188
+ "arn:aws:s3:::somebucket",
189
+ "arn:aws:s3:::somebucket/*",
190
+ ]
191
+ }
192
+ }
193
+
194
+ resource "aws_iam_policy" "this" {
195
+ name = "test-policy"
196
+ policy = data.aws_iam_policy_document.source_document_example.json
197
+ }` ,
198
+ expected : []iam.Policy {
199
+ {
200
+ Name : iacTypes .String ("test-policy" , iacTypes .NewTestMetadata ()),
201
+ Builtin : iacTypes .Bool (false , iacTypes .NewTestMetadata ()),
202
+ Document : func () iam.Document {
203
+ builder := iamgo .NewPolicyBuilder ()
204
+ firstStatement := iamgo .NewStatementBuilder ().
205
+ WithActions ([]string {"ec2:*" }).
206
+ WithResources ([]string {"*" }).
207
+ WithEffect ("Allow" ).
208
+ Build ()
209
+
210
+ builder .WithStatement (firstStatement )
211
+
212
+ secondStatement := iamgo .NewStatementBuilder ().
213
+ WithActions ([]string {"s3:*" }).
214
+ WithResources ([]string {"arn:aws:s3:::somebucket" , "arn:aws:s3:::somebucket/*" }).
215
+ WithEffect ("Allow" ).
216
+ Build ()
217
+
218
+ builder .WithStatement (secondStatement )
219
+
220
+ return iam.Document {
221
+ Parsed : builder .Build (),
222
+ Metadata : iacTypes .NewTestMetadata (),
223
+ IsOffset : true ,
224
+ HasRefs : false ,
225
+ }
226
+ }(),
227
+ },
228
+ },
229
+ },
230
+ {
231
+ name : "source_policy_documents with for-each" ,
232
+ terraform : `
233
+ locals {
234
+ versions = ["2008-10-17", "2012-10-17"]
235
+ }
236
+
237
+ resource "aws_iam_policy" "test_policy" {
238
+ name = "test-policy"
239
+ policy = data.aws_iam_policy_document.policy.json
240
+ }
241
+
242
+ data "aws_iam_policy_document" "policy" {
243
+ source_policy_documents = [for s in data.aws_iam_policy_document.policy_source : s.json if s.version != "2008-10-17"]
244
+ statement {
245
+ actions = ["s3:*"]
246
+ resources = ["*"]
247
+ }
248
+ }
249
+
250
+ data "aws_iam_policy_document" "policy_source" {
251
+ for_each = toset(local.versions)
252
+ version = each.value
253
+ statement {
254
+ actions = ["s3:PutObject"]
255
+ resources = ["*"]
256
+ }
257
+ }` ,
258
+ expected : []iam.Policy {
259
+ {
260
+ Name : iacTypes .String ("test-policy" , iacTypes .NewTestMetadata ()),
261
+ Document : func () iam.Document {
262
+ builder := iamgo .NewPolicyBuilder ().
263
+ WithStatement (
264
+ iamgo .NewStatementBuilder ().
265
+ WithActions ([]string {"s3:PutObject" }).
266
+ WithResources ([]string {"*" }).
267
+ WithEffect ("Allow" ).
268
+ Build (),
269
+ ).
270
+ WithStatement (
271
+ iamgo .NewStatementBuilder ().
272
+ WithActions ([]string {"s3:*" }).
273
+ WithResources ([]string {"*" }).
274
+ WithEffect ("Allow" ).
275
+ Build (),
276
+ )
277
+
278
+ return iam.Document {
279
+ Parsed : builder .Build (),
280
+ Metadata : iacTypes .NewTestMetadata (),
281
+ IsOffset : true ,
282
+ HasRefs : false ,
283
+ }
284
+ }(),
285
+ },
286
+ },
287
+ },
288
+ {
289
+ name : "source_policy_documents with condition" ,
290
+ terraform : `
291
+ locals {
292
+ versions = ["2008-10-17", "2012-10-17"]
293
+ }
294
+
295
+ resource "aws_iam_policy" "test_policy" {
296
+ name = "test-policy"
297
+ policy = data.aws_iam_policy_document.policy.json
298
+ }
299
+
300
+ data "aws_iam_policy_document" "policy" {
301
+ source_policy_documents = true ? [data.aws_iam_policy_document.policy_source.json] : [data.aws_iam_policy_document.policy_source2.json]
302
+ }
303
+
304
+ data "aws_iam_policy_document" "policy_source" {
305
+ statement {
306
+ actions = ["s3:PutObject"]
307
+ resources = ["*"]
308
+ }
309
+ }
310
+
311
+ data "aws_iam_policy_document" "policy_source2" {
312
+ statement {
313
+ actions = ["s3:PutObject2"]
314
+ resources = ["*"]
315
+ }
316
+ }
317
+ ` ,
318
+ expected : []iam.Policy {
319
+ {
320
+ Name : iacTypes .String ("test-policy" , iacTypes .NewTestMetadata ()),
321
+ Document : func () iam.Document {
322
+ builder := iamgo .NewPolicyBuilder ().
323
+ WithStatement (
324
+ iamgo .NewStatementBuilder ().
325
+ WithActions ([]string {"s3:PutObject" }).
326
+ WithResources ([]string {"*" }).
327
+ WithEffect ("Allow" ).
328
+ Build (),
329
+ )
330
+
331
+ return iam.Document {
332
+ Parsed : builder .Build (),
333
+ Metadata : iacTypes .NewTestMetadata (),
334
+ IsOffset : true ,
335
+ HasRefs : false ,
336
+ }
337
+ }(),
338
+ },
339
+ },
340
+ },
341
+ {
342
+ name : "raw source policy" ,
343
+ terraform : `resource "aws_iam_policy" "test_policy" {
344
+ name = "test-policy"
345
+ policy = data.aws_iam_policy_document.policy.json
346
+ }
347
+
348
+ data "aws_iam_policy_document" "policy" {
349
+ source_policy_documents = [
350
+ jsonencode({
351
+ Statement = [
352
+ {
353
+ Action = [
354
+ "ec2:Describe*",
355
+ ]
356
+ Effect = "Allow"
357
+ Resource = "*"
358
+ },
359
+ ]
360
+ }),
361
+ ]
362
+ }
363
+ ` ,
364
+ expected : []iam.Policy {
365
+ {
366
+ Name : iacTypes .String ("test-policy" , iacTypes .NewTestMetadata ()),
367
+ Document : func () iam.Document {
368
+ builder := iamgo .NewPolicyBuilder ().
369
+ WithStatement (
370
+ iamgo .NewStatementBuilder ().
371
+ WithActions ([]string {"ec2:Describe*" }).
372
+ WithResources ([]string {"*" }).
373
+ WithEffect ("Allow" ).
374
+ Build (),
375
+ )
376
+
377
+ return iam.Document {
378
+ Parsed : builder .Build (),
379
+ Metadata : iacTypes .NewTestMetadata (),
380
+ IsOffset : true ,
381
+ HasRefs : false ,
382
+ }
383
+ }(),
384
+ },
385
+ },
386
+ },
387
+ {
388
+ name : "invalid `override_policy_documents` attribute" ,
389
+ terraform : `resource "aws_iam_policy" "test_policy" {
390
+ name = "test-policy"
391
+ policy = data.aws_iam_policy_document.policy.json
392
+ }
393
+
394
+ data "aws_iam_policy_document" "policy" {
395
+ source_policy_documents = data.aws_iam_policy_document.policy2.json
396
+ }` ,
397
+ expected : []iam.Policy {
398
+ {
399
+ Name : iacTypes .String ("test-policy" , iacTypes .NewTestMetadata ()),
400
+ Document : iam.Document {
401
+ IsOffset : true ,
402
+ },
403
+ },
404
+ },
405
+ },
172
406
}
173
407
174
408
for _ , test := range tests {
0 commit comments