-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ecr): autoDeleteImages fails when repository is renamed #26742
Changes from 5 commits
f0f912f
e344391
ae6cffa
21db9b8
22b20c9
596ae0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,11 @@ import { | |
CustomResource, | ||
CustomResourceProvider, | ||
CustomResourceProviderRuntime, | ||
Aws, | ||
} from '../../core'; | ||
|
||
const AUTO_DELETE_IMAGES_RESOURCE_TYPE = 'Custom::ECRAutoDeleteImages'; | ||
const AUTO_DELETE_IMAGES_TAG = 'aws-cdk:auto-delete-images'; | ||
const REPO_ARN_SYMBOL = Symbol.for('@aws-cdk/aws-ecr.RepoArns'); | ||
|
||
/** | ||
* Represents an ECR repository. | ||
|
@@ -867,12 +867,8 @@ export class Repository extends RepositoryBase { | |
}); | ||
|
||
if (firstTime) { | ||
const repoArns = [this._resource.attrArn]; | ||
(provider as any)[REPO_ARN_SYMBOL] = repoArns; | ||
|
||
// Use a iam policy to allow the custom resource to list & delete | ||
// images in the repository and the ability to get all repositories to find the arn needed on delete. | ||
// We lazily produce a list of repositories associated with this custom resource provider. | ||
provider.addToRolePolicy({ | ||
Effect: 'Allow', | ||
Action: [ | ||
|
@@ -881,10 +877,13 @@ export class Repository extends RepositoryBase { | |
'ecr:ListImages', | ||
'ecr:ListTagsForResource', | ||
], | ||
Resource: Lazy.list({ produce: () => repoArns }), | ||
Resource: [`arn:${Aws.PARTITION}:ecr:${Stack.of(this).region}:${Stack.of(this).account}:repository/*`], | ||
Condition: { | ||
StringEquals: { | ||
['ecr:ResourceTag/' + AUTO_DELETE_IMAGES_TAG]: 'true', | ||
}, | ||
}, | ||
}); | ||
} else { | ||
(provider as any)[REPO_ARN_SYMBOL].push(this._resource.attrArn); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The const role = iam.Role.fromRoleArn(this, `${this.repositoryName}Role`, provider.roleArn);
// Use a iam policy to allow the custom resource to list & delete
// images in the repository and the ability to get all repositories to find the arn needed on delete.
new iam.Policy(this, `${this.repositoryName}Policy`, {
roles: [role],
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecr:BatchDeleteImage',
'ecr:DescribeRepositories',
'ecr:ListImages',
'ecr:ListTagsForResource',
],
resources: [this._resource.attrArn],
}),
],
});
In that case, is this the way to go? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used However, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, you can't use |
||
|
||
const customResource = new CustomResource(this, 'AutoDeleteImagesCustomResource', { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rix0rrr
I implemented tag-based access control with a Condition for now.