Skip to content

Commit 89117c0

Browse files
authored
Add missing CopySnapshot permissions for scanning AMIs (#56)
* Add missing CopySnapshot permissions for scanning AMIs * Split delegate role policies in two: orchestrator (write) and worker (read) * Update CHANGELOG
1 parent 0e89a12 commit 89117c0

File tree

3 files changed

+112
-24
lines changed

3 files changed

+112
-24
lines changed

CHANGELOG.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## TBD
3+
## Version TBD
4+
5+
### Terraform
6+
7+
- Add missing CopySnapshot permissions to allow AMI scanning
48

59
### CloudFormation
610

@@ -12,11 +16,11 @@
1216
- Add support for offline mode to scan without remote-config (deactived by default)
1317
- AutoScalingGroup update policy replacing instances as the launch template is being updated
1418

15-
## Terraform 0.9.1
19+
## Version 0.9.1
1620

1721
- Adds missing nbd module activation in cloud init
1822

19-
## Terraform 0.9.0
23+
## Version 0.9.0
2024

2125
### agentless-scanner 2024022201
2226

@@ -28,7 +32,7 @@
2832
- Split agentless binary in dedicated package
2933
- Improve performance of OS SBOMs generation
3034

31-
## Terraform 0.8.0
35+
## Version 0.8.0
3236

3337
### agentless-scanner 2024020101
3438

@@ -45,20 +49,20 @@
4549
- AWS volume attach: reduce the number of DeleteVolume requests when cleaning up a scan
4650
- NBD attach: fix occasional crashes when closing the NBD server
4751

48-
## Terraform 0.7.0
52+
## Version 0.7.0
4953

5054
### agentless-scanner 2024011701
5155

5256
- Execute Trivy scans in dedicated processes.
5357

54-
## Terraform 0.6.0
58+
## Version 0.6.0
5559

5660
### agentless-scanner 2024011501
5761

5862
- Clean up downloaded AWS Lambdas on startup.
5963
- Increase timeout while downloading AWS Lambda functions.
6064

61-
## Terraform 0.5.0
65+
## Version 0.5.0
6266

6367
### agentless-scanner 2023122001
6468

modules/scanning-delegate-role/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ No modules.
2020

2121
| Name | Type |
2222
|------|------|
23-
| [aws_iam_policy.scanning_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
23+
| [aws_iam_policy.scanning_orchestrator_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
24+
| [aws_iam_policy.scanning_worker_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
2425
| [aws_iam_role.role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
25-
| [aws_iam_role_policy_attachment.attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
26+
| [aws_iam_role_orchestrator_policy_attachment.attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_orchestrator_policy_attachment) | resource |
27+
| [aws_iam_role_worker_policy_attachment.attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_worker_policy_attachment) | resource |
2628
| [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
27-
| [aws_iam_policy_document.scanning_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
29+
| [aws_iam_policy_document.scanning_orchestrator_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
30+
| [aws_iam_policy_document.scanning_worker_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
2831
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
2932

3033
## Inputs

modules/scanning-delegate-role/main.tf

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ locals {
77

88
data "aws_partition" "current" {}
99

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.
1013
// 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" {
1215
statement {
1316
sid = "DatadogAgentlessScannerResourceTagging"
1417
effect = "Allow"
@@ -23,7 +26,7 @@ data "aws_iam_policy_document" "scanning_policy_document" {
2326
condition {
2427
test = "StringEquals"
2528
variable = "ec2:CreateAction"
26-
values = ["CreateSnapshot", "CreateVolume"]
29+
values = ["CreateSnapshot", "CreateVolume", "CopySnapshot"]
2730
}
2831
}
2932

@@ -69,19 +72,34 @@ data "aws_iam_policy_document" "scanning_policy_document" {
6972
}
7073

7174
statement {
72-
sid = "DatadogAgentlessScannerSnapshotAccessAndCleanup"
75+
sid = "DatadogAgentlessScannerCopySnapshot"
7376
effect = "Allow"
7477
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+
}
7996

97+
statement {
98+
sid = "DatadogAgentlessScannerSnapshotCleanup"
99+
effect = "Allow"
100+
actions = [
80101
// Allow deleting created snapshots and volumes
81102
"ec2:DeleteSnapshot",
82-
83-
// Allow describing created snapshots
84-
"ec2:DescribeSnapshotAttribute",
85103
]
86104
resources = [
87105
"arn:${data.aws_partition.current.partition}:ec2:*:*:snapshot/*",
@@ -202,6 +220,58 @@ data "aws_iam_policy_document" "scanning_policy_document" {
202220
# values = ["true"]
203221
# }
204222
# }
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+
}
205275

206276
statement {
207277
sid = "GetLambdaDetails"
@@ -221,10 +291,16 @@ data "aws_iam_policy_document" "scanning_policy_document" {
221291
}
222292
}
223293

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" {
225301
name = var.iam_policy_name
226302
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
228304
}
229305

230306
data "aws_iam_policy_document" "assume_role_policy" {
@@ -255,7 +331,12 @@ resource "aws_iam_role" "role" {
255331
tags = merge(var.tags, local.dd_tags)
256332
}
257333

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
260341
role = aws_iam_role.role.name
261342
}

0 commit comments

Comments
 (0)