Skip to content

Commit cce10b1

Browse files
authoredJun 25, 2024
fix(apprunner): auto deployment fails after new container image pushed due to lack of a permission (aws#30630)
### Issue # (if applicable) Closes aws#26640 ### Reason for this change According to the [docs](https://docs.aws.amazon.com/apprunner/latest/dg/security_iam_service-with-iam.html#security_iam_service-with-iam-roles), required permissions for an App Runner's AccessRole to access images in ECR repository are the followings: 1. "ecr:GetDownloadUrlForLayer", 2. "ecr:BatchCheckLayerAvailability", 3. "ecr:BatchGetImage", 4. "ecr:DescribeImages", 5. "ecr:GetAuthorizationToken" No.1~3 are granted by the [grantPull](https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ecr/lib/repository.ts#L385) method of `ecr.Repository`. https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-apprunner-alpha/lib/service.ts#L1303 Permission for No.5 is granted by the following. **Note** : It is correct that the resources here is set to `*`(Ref: [docs](https://docs.aws.amazon.com/apprunner/latest/dg/security_iam_service-with-iam.html#security_iam_service-with-iam-roles)) > If you create your own custom policy for your access role, be sure to specify "Resource": "*" for the ecr:GetAuthorizationToken action. Tokens can be used to access any Amazon ECR registry that you have access to. https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-apprunner-alpha/lib/service.ts#L1368 At the moment, No.4 permission is missing. So we need to add. ### Description of changes Add a `ecr:DescribeImages` permisison to the AccessRole. ### Description of how you validated changes Update a unit test and a integ test. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 326580a commit cce10b1

File tree

9 files changed

+278
-104
lines changed

9 files changed

+278
-104
lines changed
 

‎packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1311,9 +1311,11 @@ export class Service extends cdk.Resource implements iam.IGrantable {
13111311
} : undefined,
13121312
});
13131313

1314-
// grant required privileges for the role
1314+
// grant required privileges for the role to access an image in Amazon ECR
1315+
// See https://docs.aws.amazon.com/apprunner/latest/dg/security_iam_service-with-iam.html#security_iam_service-with-iam-roles
13151316
if (this.source.ecrRepository && this.accessRole) {
13161317
this.source.ecrRepository.grantPull(this.accessRole);
1318+
this.source.ecrRepository.grant(this.accessRole, 'ecr:DescribeImages');
13171319
}
13181320

13191321
this.serviceArn = resource.attrServiceArn;

‎packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-ecr.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-ecr.js.snapshot/integ-apprunner.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-ecr.js.snapshot/integ-apprunner.template.json

+62-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
{
22
"Resources": {
3+
"Service3InstanceRoleD40BEE82": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "tasks.apprunner.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
}
18+
}
19+
},
320
"Service3AccessRole3ACBAAA0": {
421
"Type": "AWS::IAM::Role",
522
"Properties": {
@@ -31,6 +48,7 @@
3148
"Action": [
3249
"ecr:BatchCheckLayerAvailability",
3350
"ecr:BatchGetImage",
51+
"ecr:DescribeImages",
3452
"ecr:GetDownloadUrlForLayer"
3553
],
3654
"Effect": "Allow",
@@ -72,6 +90,19 @@
7290
"Service342D067F2": {
7391
"Type": "AWS::AppRunner::Service",
7492
"Properties": {
93+
"InstanceConfiguration": {
94+
"InstanceRoleArn": {
95+
"Fn::GetAtt": [
96+
"Service3InstanceRoleD40BEE82",
97+
"Arn"
98+
]
99+
}
100+
},
101+
"NetworkConfiguration": {
102+
"EgressConfiguration": {
103+
"EgressType": "DEFAULT"
104+
}
105+
},
75106
"SourceConfiguration": {
76107
"AuthenticationConfiguration": {
77108
"AccessRoleArn": {
@@ -91,19 +122,23 @@
91122
},
92123
"ImageRepositoryType": "ECR"
93124
}
94-
},
95-
"InstanceConfiguration": {
96-
"InstanceRoleArn": {
97-
"Fn::GetAtt": [
98-
"Service3InstanceRoleD40BEE82",
99-
"Arn"
100-
]
125+
}
126+
}
127+
},
128+
"Service2InstanceRole3F57F2AA": {
129+
"Type": "AWS::IAM::Role",
130+
"Properties": {
131+
"AssumeRolePolicyDocument": {
132+
"Statement": [
133+
{
134+
"Action": "sts:AssumeRole",
135+
"Effect": "Allow",
136+
"Principal": {
137+
"Service": "tasks.apprunner.amazonaws.com"
138+
}
101139
}
102-
},
103-
"NetworkConfiguration": {
104-
"EgressConfiguration": {
105-
"EgressType": "DEFAULT"
106-
}
140+
],
141+
"Version": "2012-10-17"
107142
}
108143
}
109144
},
@@ -138,6 +173,7 @@
138173
"Action": [
139174
"ecr:BatchCheckLayerAvailability",
140175
"ecr:BatchGetImage",
176+
"ecr:DescribeImages",
141177
"ecr:GetDownloadUrlForLayer"
142178
],
143179
"Effect": "Allow",
@@ -179,6 +215,19 @@
179215
"Service2AB4D14D8": {
180216
"Type": "AWS::AppRunner::Service",
181217
"Properties": {
218+
"InstanceConfiguration": {
219+
"InstanceRoleArn": {
220+
"Fn::GetAtt": [
221+
"Service2InstanceRole3F57F2AA",
222+
"Arn"
223+
]
224+
}
225+
},
226+
"NetworkConfiguration": {
227+
"EgressConfiguration": {
228+
"EgressType": "DEFAULT"
229+
}
230+
},
182231
"SourceConfiguration": {
183232
"AuthenticationConfiguration": {
184233
"AccessRoleArn": {
@@ -217,56 +266,9 @@
217266
},
218267
"ImageRepositoryType": "ECR"
219268
}
220-
},
221-
"InstanceConfiguration": {
222-
"InstanceRoleArn": {
223-
"Fn::GetAtt": [
224-
"Service2InstanceRole3F57F2AA",
225-
"Arn"
226-
]
227-
}
228-
},
229-
"NetworkConfiguration": {
230-
"EgressConfiguration": {
231-
"EgressType": "DEFAULT"
232-
}
233-
}
234-
}
235-
},
236-
"Service3InstanceRoleD40BEE82": {
237-
"Type": "AWS::IAM::Role",
238-
"Properties": {
239-
"AssumeRolePolicyDocument": {
240-
"Statement": [
241-
{
242-
"Action": "sts:AssumeRole",
243-
"Effect": "Allow",
244-
"Principal": {
245-
"Service": "tasks.apprunner.amazonaws.com"
246-
}
247-
}
248-
],
249-
"Version": "2012-10-17"
250-
}
251-
}
252-
},
253-
"Service2InstanceRole3F57F2AA": {
254-
"Type": "AWS::IAM::Role",
255-
"Properties": {
256-
"AssumeRolePolicyDocument": {
257-
"Statement": [
258-
{
259-
"Action": "sts:AssumeRole",
260-
"Effect": "Allow",
261-
"Principal": {
262-
"Service": "tasks.apprunner.amazonaws.com"
263-
}
264-
}
265-
],
266-
"Version": "2012-10-17"
267-
}
268269
}
269270
}
271+
}
270272
},
271273
"Outputs": {
272274
"URL3": {

‎packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-ecr.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-ecr.js.snapshot/manifest.json

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)