Skip to content

Commit bd36c6c

Browse files
iamhopaul123mergify[bot]
authored andcommitted
feat(ecs): Add warning message when pulling ECR image (#4334)
* Add warning message when pulling ECR image using fromRegistry() * Refactor the regex to bind method, add token validation, and add more test case
1 parent 53db8bc commit bd36c6c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/@aws-cdk/aws-ecs/lib/images/repository.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import secretsmanager = require('@aws-cdk/aws-secretsmanager');
2-
import { Construct } from '@aws-cdk/core';
2+
import { Construct, Token } from '@aws-cdk/core';
33
import { ContainerDefinition } from "../container-definition";
44
import { ContainerImage, ContainerImageConfig } from "../container-image";
55

6+
/**
7+
* Regex pattern to check if it is an ECR image URL.
8+
*
9+
* @experimental
10+
*/
11+
const ECR_IMAGE_REGEX = /(^[a-zA-Z0-9][a-zA-Z0-9-_]*).dkr.ecr.([a-zA-Z0-9][a-zA-Z0-9-_]*).amazonaws.com(.cn)?\/.*/;
12+
613
/**
714
* The properties for an image hosted in a public or private repository.
815
*/
@@ -27,7 +34,12 @@ export class RepositoryImage extends ContainerImage {
2734
super();
2835
}
2936

30-
public bind(_scope: Construct, containerDefinition: ContainerDefinition): ContainerImageConfig {
37+
public bind(scope: Construct, containerDefinition: ContainerDefinition): ContainerImageConfig {
38+
// name could be a Token - in that case, skip validation altogether
39+
if (!Token.isUnresolved(this.imageName) && ECR_IMAGE_REGEX.test(this.imageName)) {
40+
scope.node.addWarning("Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'.");
41+
}
42+
3143
if (this.props.credentials) {
3244
this.props.credentials.grantRead(containerDefinition.taskDefinition.obtainExecutionRole());
3345
}

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,43 @@ export = {
472472
test.done();
473473
},
474474

475+
"warns when setting containers from ECR repository using fromRegistry method"(test: Test) {
476+
// GIVEN
477+
const stack = new cdk.Stack();
478+
479+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
480+
481+
// WHEN
482+
const container = taskDefinition.addContainer("web", {
483+
image: ecs.ContainerImage.fromRegistry("ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY"),
484+
memoryLimitMiB: 512
485+
});
486+
487+
// THEN
488+
test.deepEqual(container.node.metadata[0].data, "Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'.");
489+
test.done();
490+
},
491+
492+
"warns when setting containers from ECR repository by creating a RepositoryImage class"(test: Test) {
493+
// GIVEN
494+
const stack = new cdk.Stack();
495+
496+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
497+
498+
const repo = new ecs.RepositoryImage("ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY");
499+
500+
// WHEN
501+
const container = taskDefinition.addContainer("web", {
502+
image: repo,
503+
memoryLimitMiB: 512
504+
});
505+
506+
// THEN
507+
test.deepEqual(container.node.metadata[0].data, "Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'.");
508+
509+
test.done();
510+
},
511+
475512
"correctly sets containers from asset using default props"(test: Test) {
476513
// GIVEN
477514
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)