File tree Expand file tree Collapse file tree 2 files changed +51
-2
lines changed
packages/@aws-cdk/aws-ecs Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Original file line number Diff line number Diff line change 1
1
import secretsmanager = require( '@aws-cdk/aws-secretsmanager' ) ;
2
- import { Construct } from '@aws-cdk/core' ;
2
+ import { Construct , Token } from '@aws-cdk/core' ;
3
3
import { ContainerDefinition } from "../container-definition" ;
4
4
import { ContainerImage , ContainerImageConfig } from "../container-image" ;
5
5
6
+ /**
7
+ * Regex pattern to check if it is an ECR image URL.
8
+ *
9
+ * @experimental
10
+ */
11
+ const ECR_IMAGE_REGEX = / ( ^ [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 - _ ] * ) .d k r .e c r .( [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 - _ ] * ) .a m a z o n a w s .c o m ( .c n ) ? \/ .* / ;
12
+
6
13
/**
7
14
* The properties for an image hosted in a public or private repository.
8
15
*/
@@ -27,7 +34,12 @@ export class RepositoryImage extends ContainerImage {
27
34
super ( ) ;
28
35
}
29
36
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
+
31
43
if ( this . props . credentials ) {
32
44
this . props . credentials . grantRead ( containerDefinition . taskDefinition . obtainExecutionRole ( ) ) ;
33
45
}
Original file line number Diff line number Diff line change @@ -472,6 +472,43 @@ export = {
472
472
test . done ( ) ;
473
473
} ,
474
474
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
+
475
512
"correctly sets containers from asset using default props" ( test : Test ) {
476
513
// GIVEN
477
514
const stack = new cdk . Stack ( ) ;
You can’t perform that action at this time.
0 commit comments