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

fix(event-targets): ecsTask uses invalid task definition arn in policy #30484

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,18 @@ export class EcsTask implements events.IRuleTarget {
}

private createEventRolePolicyStatements(): iam.PolicyStatement[] {
// check if there is a taskdefinition revision (arn will end with : followed by digits) included in the arn already
let needsRevisionWildcard = false;
if ( !cdk.Token.isUnresolved(this.taskDefinition.taskDefinitionArn)) {
const revisionAtEndPattern = /:[0-9]+$/;
const hasRevision = revisionAtEndPattern.test(this.taskDefinition.taskDefinitionArn);
needsRevisionWildcard = !hasRevision;
}

const policyStatements = [
new iam.PolicyStatement({
actions: ['ecs:RunTask'],
resources: [this.taskDefinition.taskDefinitionArn],
resources: [`${this.taskDefinition.taskDefinitionArn}${needsRevisionWildcard ? ':*' : ''}`],
conditions: {
ArnEquals: { 'ecs:cluster': this.cluster.clusterArn },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as iam from '../../../aws-iam';
import * as sqs from '../../../aws-sqs';
import * as cdk from '../../../core';
import * as targets from '../../lib';
import { EcsTask } from '../../lib';

let stack: cdk.Stack;
let vpc: ec2.Vpc;
Expand Down Expand Up @@ -1095,3 +1096,100 @@ test.each([
],
});
});

test('Non-imported TaskDefinition role is targeting by Ref', () => {
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromRegistry('henk'),
});

const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(
new EcsTask({
cluster: cluster,
taskDefinition: taskDefinition,
}),
);

const policyMatch = Match.objectLike({
PolicyDocument: {
Statement: Match.arrayWith([
Match.objectLike({
Action: 'ecs:RunTask',
Resource: {
Ref: 'TaskDef54694570',
},
}),
]),
},
});
const template = Template.fromStack(stack);
template.hasResource('AWS::IAM::Policy', { Properties: policyMatch });
});

test('Imported task definition without revision adds wildcard to policy resource', () => {
const taskDefinition = ecs.FargateTaskDefinition.fromFargateTaskDefinitionAttributes(stack, 'TaskDefImport', {
taskDefinitionArn: 'arn:aws:ecs:us-west-2:012345678901:task-definition/MyTask',
taskRole: iam.Role.fromRoleArn(stack, 'RoleImport', 'arn:aws:iam::012345678901:role/MyTaskRole'),
networkMode: ecs.NetworkMode.AWS_VPC,
});

const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(
new EcsTask({
cluster: cluster,
taskDefinition: taskDefinition,
}),
);

const policyMatch = Match.objectLike({
PolicyDocument: {
Statement: Match.arrayWith([
Match.objectLike({
Action: 'ecs:RunTask',
Resource: `${taskDefinition.taskDefinitionArn}:*`,
}),
]),
},
});
const template = Template.fromStack(stack);
template.hasResource('AWS::IAM::Policy', { Properties: policyMatch });
});

test('Imported task definition with revision uses original arn for policy resource', () => {
const taskDefinition = ecs.FargateTaskDefinition.fromFargateTaskDefinitionAttributes(stack, 'TaskDefImport', {
taskDefinitionArn: 'arn:aws:ecs:us-west-2:012345678901:task-definition/MyTask:1',
taskRole: iam.Role.fromRoleArn(stack, 'RoleImport', 'arn:aws:iam::012345678901:role/MyTaskRole'),
networkMode: ecs.NetworkMode.AWS_VPC,
});

const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(
new EcsTask({
cluster: cluster,
taskDefinition: taskDefinition,
}),
);

const policyMatch = Match.objectLike({
PolicyDocument: {
Statement: Match.arrayWith([
Match.objectLike({
Action: 'ecs:RunTask',
Resource: taskDefinition.taskDefinitionArn,
}),
]),
},
});
const template = Template.fromStack(stack);
template.hasResource('AWS::IAM::Policy', { Properties: policyMatch });
});
Loading