@@ -36,11 +36,24 @@ export abstract class Secret {
36
36
public static fromSecretsManager ( secret : secretsmanager . ISecret , field ?: string ) : Secret {
37
37
return {
38
38
arn : field ? `${ secret . secretArn } :${ field } ::` : secret . secretArn ,
39
+ hasField : ! ! field ,
39
40
grantRead : grantee => secret . grantRead ( grantee ) ,
40
41
} ;
41
42
}
42
43
44
+ /**
45
+ * The ARN of the secret
46
+ */
43
47
public abstract readonly arn : string ;
48
+
49
+ /**
50
+ * Whether this secret uses a specific JSON field
51
+ */
52
+ public abstract readonly hasField ?: boolean ;
53
+
54
+ /**
55
+ * Grants reading the secret to a principal
56
+ */
44
57
public abstract grantRead ( grantee : iam . IGrantable ) : iam . Grant ;
45
58
}
46
59
@@ -348,6 +361,8 @@ export class ContainerDefinition extends cdk.Construct {
348
361
349
362
private readonly imageConfig : ContainerImageConfig ;
350
363
364
+ private readonly secrets ?: CfnTaskDefinition . SecretProperty [ ] ;
365
+
351
366
/**
352
367
* Constructs a new instance of the ContainerDefinition class.
353
368
*/
@@ -369,6 +384,20 @@ export class ContainerDefinition extends cdk.Construct {
369
384
this . logDriverConfig = props . logging . bind ( this , this ) ;
370
385
}
371
386
props . taskDefinition . _linkContainer ( this ) ;
387
+
388
+ if ( props . secrets ) {
389
+ this . secrets = [ ] ;
390
+ for ( const [ name , secret ] of Object . entries ( props . secrets ) ) {
391
+ if ( this . taskDefinition . isFargateCompatible && secret . hasField ) {
392
+ throw new Error ( `Cannot specify secret JSON field for a task using the FARGATE launch type: '${ name } ' in container '${ this . node . id } '` ) ;
393
+ }
394
+ secret . grantRead ( this . taskDefinition . obtainExecutionRole ( ) ) ;
395
+ this . secrets . push ( {
396
+ name,
397
+ valueFrom : secret . arn ,
398
+ } ) ;
399
+ }
400
+ }
372
401
}
373
402
374
403
/**
@@ -519,9 +548,9 @@ export class ContainerDefinition extends cdk.Construct {
519
548
/**
520
549
* Render this container definition to a CloudFormation object
521
550
*
522
- * @param taskDefinition [disable-awslint:ref-via-interface] (made optional to avoid breaking change)
551
+ * @param _taskDefinition [disable-awslint:ref-via-interface] (unused but kept to avoid breaking change)
523
552
*/
524
- public renderContainerDefinition ( taskDefinition ?: TaskDefinition ) : CfnTaskDefinition . ContainerDefinitionProperty {
553
+ public renderContainerDefinition ( _taskDefinition ?: TaskDefinition ) : CfnTaskDefinition . ContainerDefinitionProperty {
525
554
return {
526
555
command : this . props . command ,
527
556
cpu : this . props . cpu ,
@@ -551,16 +580,7 @@ export class ContainerDefinition extends cdk.Construct {
551
580
workingDirectory : this . props . workingDirectory ,
552
581
logConfiguration : this . logDriverConfig ,
553
582
environment : this . props . environment && renderKV ( this . props . environment , 'name' , 'value' ) ,
554
- secrets : this . props . secrets && Object . entries ( this . props . secrets )
555
- . map ( ( [ k , v ] ) => {
556
- if ( taskDefinition ) {
557
- v . grantRead ( taskDefinition . obtainExecutionRole ( ) ) ;
558
- }
559
- return {
560
- name : k ,
561
- valueFrom : v . arn ,
562
- } ;
563
- } ) ,
583
+ secrets : this . secrets ,
564
584
extraHosts : this . props . extraHosts && renderKV ( this . props . extraHosts , 'hostname' , 'ipAddress' ) ,
565
585
healthCheck : this . props . healthCheck && renderHealthCheck ( this . props . healthCheck ) ,
566
586
links : cdk . Lazy . listValue ( { produce : ( ) => this . links } , { omitEmpty : true } ) ,
0 commit comments