Skip to content

Commit 6c06ca0

Browse files
authoredApr 27, 2023
Merge pull request #19 from DNXLabs/hotfix/fix-ecr-policy-issue-when-not-using-trust-accounts-option
fix ecr policy issue when not using trust accounts option
2 parents 2868fe6 + ff1b061 commit 6c06ca0

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed
 

‎ecr-policies.tf

+60-53
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,73 @@
1+
12
resource "aws_ecr_repository_policy" "default" {
23
repository = aws_ecr_repository.default.name
4+
policy = data.aws_iam_policy_document.default.json
5+
}
6+
7+
data "aws_iam_policy_document" "default" {
8+
dynamic "statement" {
9+
for_each = length(try(var.trust_accounts, [])) > 0 ? [1] : []
10+
11+
content {
12+
sid = "AllowPull"
13+
effect = "Allow"
14+
15+
principals {
16+
type = "AWS"
17+
identifiers = formatlist("arn:aws:iam::%s:root", var.trust_accounts)
18+
}
319

4-
policy = <<EOF
5-
{
6-
"Version": "2008-10-17",
7-
"Statement": [
8-
{
9-
"Sid": "AllowPull",
10-
"Effect": "Allow",
11-
"Principal": {
12-
"AWS": [
13-
${join(",", formatlist("\"arn:aws:iam::%s:root\"", var.trust_accounts))}
14-
]
15-
},
16-
"Action": [
20+
actions = [
1721
"ecr:GetDownloadUrlForLayer",
1822
"ecr:BatchGetImage",
1923
"ecr:BatchCheckLayerAvailability",
2024
"ecr:DescribeImageScanFindings"
2125
]
22-
},
23-
{
24-
"Sid": "AllowWriteMgmt",
25-
"Effect": "Allow",
26-
"Principal": {
27-
"AWS": [
28-
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
29-
]
30-
},
31-
"Action": [
32-
"ecr:GetDownloadUrlForLayer",
33-
"ecr:BatchGetImage",
34-
"ecr:BatchCheckLayerAvailability",
35-
"ecr:PutImage",
36-
"ecr:InitiateLayerUpload",
37-
"ecr:UploadLayerPart",
38-
"ecr:CompleteLayerUpload"
39-
]
40-
},
41-
{
42-
"Sid": "LambdaECRImageCrossAccountRetrievalPolicy",
43-
"Effect": "Allow",
44-
"Principal": {
45-
"Service": [
46-
"lambda.amazonaws.com"
47-
]
48-
},
49-
"Action": [
26+
}
27+
}
28+
29+
statement {
30+
sid = "AllowWriteMgmt"
31+
effect = "Allow"
32+
33+
principals {
34+
type = "AWS"
35+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
36+
}
37+
38+
actions = [
39+
"ecr:GetDownloadUrlForLayer",
40+
"ecr:BatchGetImage",
41+
"ecr:BatchCheckLayerAvailability",
42+
"ecr:PutImage",
43+
"ecr:InitiateLayerUpload",
44+
"ecr:UploadLayerPart",
45+
"ecr:CompleteLayerUpload"
46+
]
47+
}
48+
49+
dynamic "statement" {
50+
for_each = length(try(var.trust_accounts, [])) > 0 ? [1] : []
51+
52+
content {
53+
sid = "LambdaECRImageCrossAccountRetrievalPolicy"
54+
effect = "Allow"
55+
56+
principals {
57+
type = "Service"
58+
identifiers = ["lambda.amazonaws.com"]
59+
}
60+
61+
actions = [
5062
"ecr:GetDownloadUrlForLayer",
5163
"ecr:BatchGetImage"
52-
],
53-
"Condition": {
54-
"StringLike": {
55-
"aws:sourceArn": [
56-
${join(",", formatlist("\"arn:aws:lambda:%s:%s:function:*\"", data.aws_region.current.name, var.trust_accounts))}
57-
]
58-
}
64+
]
65+
66+
condition {
67+
test = "StringLike"
68+
variable = "aws:sourceArn"
69+
values = formatlist("arn:aws:lambda:%s:%s:function:*", data.aws_region.current.name, var.trust_accounts)
5970
}
6071
}
61-
]
62-
}
63-
EOF
64-
65-
depends_on = [aws_ecr_repository.default]
72+
}
6673
}

0 commit comments

Comments
 (0)
Please sign in to comment.