Skip to content

Commit c4dc3bc

Browse files
authored
feat(ecr): add imageTagMutability prop (#10557)
This property allows setting tag mutability on ECR repositoes. Tag mutability is useful to ensure image integrity and can prevent supply chain attacks. Closes #4640 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6c3d407 commit c4dc3bc

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

packages/@aws-cdk/aws-ecr/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ ecr.PublicGalleryAuthorizationToken.grantRead(user);
7474

7575
This user can then proceed to login to the registry using one of the [authentication methods](https://docs.aws.amazon.com/AmazonECR/latest/public/public-registries.html#public-registry-auth).
7676

77+
### Image tag immutability
78+
79+
You can set tag immutability on images in our repository using the `imageTagMutability` construct prop.
80+
81+
```ts
82+
new ecr.Repository(stack, 'Repo', { imageTagMutability: ecr.TagMutability.IMMUTABLE });
83+
```
84+
7785
## Automatically clean up repositories
7886

7987
You can set life cycle rules to automatically clean up old images from your

packages/@aws-cdk/aws-ecr/lib/repository.ts

+24
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ export interface RepositoryProps {
354354
* @default false
355355
*/
356356
readonly imageScanOnPush?: boolean;
357+
358+
/**
359+
* The tag mutability setting for the repository. If this parameter is omitted, the default setting of MUTABLE will be used which will allow image tags to be overwritten.
360+
*
361+
* @default TagMutability.MUTABLE
362+
*/
363+
readonly imageTagMutability?: TagMutability;
357364
}
358365

359366
export interface RepositoryAttributes {
@@ -452,6 +459,7 @@ export class Repository extends RepositoryBase {
452459
imageScanningConfiguration: !props.imageScanOnPush ? undefined : {
453460
ScanOnPush: true,
454461
},
462+
imageTagMutability: props.imageTagMutability || undefined,
455463
});
456464

457465
resource.applyRemovalPolicy(props.removalPolicy);
@@ -610,3 +618,19 @@ const enum CountType {
610618
*/
611619
SINCE_IMAGE_PUSHED = 'sinceImagePushed',
612620
}
621+
622+
/**
623+
* The tag mutability setting for your repository.
624+
*/
625+
export enum TagMutability {
626+
/**
627+
* allow image tags to be overwritten.
628+
*/
629+
MUTABLE = 'MUTABLE',
630+
631+
/**
632+
* all image tags within the repository will be immutable which will prevent them from being overwritten.
633+
*/
634+
IMMUTABLE = 'IMMUTABLE',
635+
636+
}

packages/@aws-cdk/aws-ecr/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"import:@aws-cdk/aws-ecr.Repository",
104104
"construct-base-is-private:@aws-cdk/aws-ecr.RepositoryBase",
105105
"docs-public-apis:@aws-cdk/aws-ecr.Repository.fromRepositoryArn",
106+
"docs-public-apis:@aws-cdk/aws-ecr.Repository.imageTagMutability",
106107
"docs-public-apis:@aws-cdk/aws-ecr.Repository.fromRepositoryName",
107108
"props-default-doc:@aws-cdk/aws-ecr.LifecycleRule.maxImageAge",
108109
"props-default-doc:@aws-cdk/aws-ecr.LifecycleRule.maxImageCount",

packages/@aws-cdk/aws-ecr/test/test.repository.ts

+14
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ export = {
6363
test.done();
6464
},
6565

66+
67+
'image tag mutability can be set'(test: Test) {
68+
// GIVEN
69+
const stack = new cdk.Stack();
70+
new ecr.Repository(stack, 'Repo', { imageTagMutability: ecr.TagMutability.IMMUTABLE });
71+
72+
// THEN
73+
expect(stack).to(haveResource('AWS::ECR::Repository', {
74+
ImageTagMutability: 'IMMUTABLE',
75+
}));
76+
77+
test.done();
78+
},
79+
6680
'add day-based lifecycle policy'(test: Test) {
6781
// GIVEN
6882
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)