Skip to content
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

feat(ecs): credentialSpecs in ContainerDefinitionOptions #29085

Merged
merged 14 commits into from
Feb 16, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as cdk from 'aws-cdk-lib';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as iam from 'aws-cdk-lib/aws-iam';
import { IntegTest } from '@aws-cdk/integ-tests-alpha';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-ecs-task-definition-container-credentialspecs');

const taskExecutionRole = new iam.Role(stack, 'task-execution-role', {
roleName: 'aws-ecs-task-definition-container-credentialspecs-task-exec-role',
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com'),
});
taskExecutionRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonECSTaskExecutionRolePolicy'));
taskExecutionRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonS3ReadOnlyAccess'));

const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef', {
executionRole: taskExecutionRole,
});

taskDefinition.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/ecs-sample-image/amazon-ecs-sample:latest'),
memoryReservationMiB: 32,
memoryLimitMiB: 512,
credentialSpecs: ['credentialspecdomainless:arn:aws:s3:::bucket_name/key_name'],
});

new IntegTest(app, 'TaskDefinitionContainerCredSpecs', {
testCases: [stack],
});

app.synth();
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ export interface ContainerDefinitionOptions {
*/
readonly command?: string[];

/**
* A list of ARNs in SSM or Amazon S3 to a credential spec (`CredSpec`) file that configures the container for Active Directory authentication.
*
* We recommend that you use this parameter instead of the `dockerSecurityOptions`. The maximum number of ARNs is 1.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum number of ARNs is 1

I know you're pulling this directly from the docs themselves, but this is confusing to me. It sounds like the max number of ARNs this credentialSpecs property takes is 1, and then why type it as a string at all?

I see two scenarios:

a) I misunderstand. In that case, lets make the docs a bit clearer.
b) The max number of ARNs for now is 1, but might be more in the future. Lets document that if thats the decision behind the array type.

But given that we aren't doing any synth time checks for this limit of 1, I think we are in scenario a)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think the way the engineer team implemented this, but in the API the credentialSpecs accepts an array of strings. Still they only use the first one...
I updated the comment a little to make it clearer. I don't want to limit the L2 construct to only 1 CredSpec because if later the service team decide to use the rest of the entries, it will be a breaking change for customers.

*
* @default - No credential specs.
*/
readonly credentialSpecs?: string[];

/**
* The minimum number of CPU units to reserve for the container.
*
Expand Down Expand Up @@ -794,6 +803,7 @@ export class ContainerDefinition extends Construct {
public renderContainerDefinition(_taskDefinition?: TaskDefinition): CfnTaskDefinition.ContainerDefinitionProperty {
return {
command: this.props.command,
credentialSpecs: this.props.credentialSpecs,
cpu: this.props.cpu,
disableNetworking: this.props.disableNetworking,
dependsOn: cdk.Lazy.any({ produce: () => this.containerDependencies.map(renderContainerDependency) }, { omitEmptyArray: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ describe('container definition', () => {
memoryReservationMiB: 512,
containerName: 'Example Container',
command: ['CMD-SHELL'],
credentialSpecs: ['credentialspecdomainless:arn:aws:s3:::bucket_name/key_name'],
cpu: 128,
disableNetworking: true,
dnsSearchDomains: ['example.com'],
Expand Down Expand Up @@ -514,6 +515,9 @@ describe('container definition', () => {
'CMD-SHELL',
],
Cpu: 128,
CredentialSpecs: [
'credentialspecdomainless:arn:aws:s3:::bucket_name/key_name',
],
DisableNetworking: true,
DnsSearchDomains: [
'example.com',
Expand Down