@@ -7,8 +7,11 @@ locals {
7
7
8
8
data "aws_partition" "current" {}
9
9
10
+ // The IAM policy for the scanning orchestrator allows to create resources
11
+ // such as snapshots and volumes. It is also able to cleanup these resources
12
+ // after creation. It does not allow reading the created resources.
10
13
// reference: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonec2.html
11
- data "aws_iam_policy_document" "scanning_policy_document " {
14
+ data "aws_iam_policy_document" "scanning_orchestrator_policy_document " {
12
15
statement {
13
16
sid = " DatadogAgentlessScannerResourceTagging"
14
17
effect = " Allow"
@@ -23,7 +26,7 @@ data "aws_iam_policy_document" "scanning_policy_document" {
23
26
condition {
24
27
test = " StringEquals"
25
28
variable = " ec2:CreateAction"
26
- values = [" CreateSnapshot" , " CreateVolume" ]
29
+ values = [" CreateSnapshot" , " CreateVolume" , " CopySnapshot " ]
27
30
}
28
31
}
29
32
@@ -69,19 +72,34 @@ data "aws_iam_policy_document" "scanning_policy_document" {
69
72
}
70
73
71
74
statement {
72
- sid = " DatadogAgentlessScannerSnapshotAccessAndCleanup "
75
+ sid = " DatadogAgentlessScannerCopySnapshot "
73
76
effect = " Allow"
74
77
actions = [
75
- // Allow reading created snapshots' blocks from EBS direct APIs
76
- " ebs:GetSnapshotBlock" ,
77
- " ebs:ListChangedBlocks" ,
78
- " ebs:ListSnapshotBlocks" ,
78
+ " ec2:CopySnapshot"
79
+ ]
80
+ resources = [
81
+ " arn:${ data . aws_partition . current . partition } :ec2:*:*:snapshot/*" ,
82
+ ]
83
+ // Enforcing created snapshot has DatadogAgentlessScanner tag
84
+ condition {
85
+ test = " StringEquals"
86
+ variable = " aws:RequestTag/DatadogAgentlessScanner"
87
+ values = [" true" ]
88
+ }
89
+ // Enforcing created snapshot has only tags with DatadogAgentlessScanner* prefix
90
+ condition {
91
+ test = " ForAllValues:StringLike"
92
+ variable = " aws:TagKeys"
93
+ values = [" DatadogAgentlessScanner*" ]
94
+ }
95
+ }
79
96
97
+ statement {
98
+ sid = " DatadogAgentlessScannerSnapshotCleanup"
99
+ effect = " Allow"
100
+ actions = [
80
101
// Allow deleting created snapshots and volumes
81
102
" ec2:DeleteSnapshot" ,
82
-
83
- // Allow describing created snapshots
84
- " ec2:DescribeSnapshotAttribute" ,
85
103
]
86
104
resources = [
87
105
" arn:${ data . aws_partition . current . partition } :ec2:*:*:snapshot/*" ,
@@ -202,6 +220,58 @@ data "aws_iam_policy_document" "scanning_policy_document" {
202
220
# values = ["true"]
203
221
# }
204
222
# }
223
+ }
224
+
225
+ // The IAM policy for the scanning worker allows to read created resources, as
226
+ // well as lambdas.
227
+ data "aws_iam_policy_document" "scanning_worker_policy_document" {
228
+ statement {
229
+ sid = " DatadogAgentlessScannerDescribeSnapshots"
230
+ effect = " Allow"
231
+ actions = [
232
+ // Required to be able to wait for snapshots completion and cleanup. It
233
+ // cannot be restricted.
234
+ " ec2:DescribeSnapshots" ,
235
+ ]
236
+ resources = [
237
+ " *" ,
238
+ ]
239
+ }
240
+
241
+ statement {
242
+ sid = " DatadogAgentlessScannerDescribeVolumes"
243
+ effect = " Allow"
244
+ actions = [
245
+ // Required to be able to wait for volumes completion and cleanup. It
246
+ // cannot be restricted.
247
+ " ec2:DescribeVolumes" ,
248
+ ]
249
+ resources = [
250
+ " *" ,
251
+ ]
252
+ }
253
+
254
+ statement {
255
+ sid = " DatadogAgentlessScannerSnapshotAccess"
256
+ effect = " Allow"
257
+ actions = [
258
+ // Allow reading created snapshots' blocks from EBS direct APIs
259
+ " ebs:GetSnapshotBlock" ,
260
+ " ebs:ListChangedBlocks" ,
261
+ " ebs:ListSnapshotBlocks" ,
262
+ ]
263
+ resources = [
264
+ " arn:${ data . aws_partition . current . partition } :ec2:*:*:snapshot/*" ,
265
+ ]
266
+
267
+ // Enforce that any of these actions can be performed on resources
268
+ // (volumes and snapshots) that have the DatadogAgentlessScanner tag.
269
+ condition {
270
+ test = " StringEquals"
271
+ variable = " aws:ResourceTag/DatadogAgentlessScanner"
272
+ values = [" true" ]
273
+ }
274
+ }
205
275
206
276
statement {
207
277
sid = " GetLambdaDetails"
@@ -221,10 +291,16 @@ data "aws_iam_policy_document" "scanning_policy_document" {
221
291
}
222
292
}
223
293
224
- resource "aws_iam_policy" "scanning_policy" {
294
+ resource "aws_iam_policy" "scanning_orchestrator_policy" {
295
+ name = var. iam_policy_name
296
+ path = var. iam_policy_path
297
+ policy = data. aws_iam_policy_document . scanning_orchestrator_policy_document . json
298
+ }
299
+
300
+ resource "aws_iam_policy" "scanning_worker_policy" {
225
301
name = var. iam_policy_name
226
302
path = var. iam_policy_path
227
- policy = data. aws_iam_policy_document . scanning_policy_document . json
303
+ policy = data. aws_iam_policy_document . scanning_worker_policy_document . json
228
304
}
229
305
230
306
data "aws_iam_policy_document" "assume_role_policy" {
@@ -255,7 +331,12 @@ resource "aws_iam_role" "role" {
255
331
tags = merge (var. tags , local. dd_tags )
256
332
}
257
333
258
- resource "aws_iam_role_policy_attachment" "attachment" {
259
- policy_arn = aws_iam_policy. scanning_policy . arn
334
+ resource "aws_iam_role_orchestrator_policy_attachment" "attachment" {
335
+ policy_arn = aws_iam_policy. scanning_orchestrator_policy . arn
336
+ role = aws_iam_role. role . name
337
+ }
338
+
339
+ resource "aws_iam_role_worker_policy_attachment" "attachment" {
340
+ policy_arn = aws_iam_policy. scanning_worker_policy . arn
260
341
role = aws_iam_role. role . name
261
342
}
0 commit comments