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(stepfunctions-tasks): run task perm no longer valid #30788

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as cdk from 'aws-cdk-lib';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP, ECS_REDUCE_RUN_TASK_PERMISSIONS } from 'aws-cdk-lib/cx-api';
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP } from 'aws-cdk-lib/cx-api';
import { IntegTest } from '@aws-cdk/integ-tests-alpha';

/*
Expand All @@ -20,7 +20,6 @@ import { IntegTest } from '@aws-cdk/integ-tests-alpha';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-sfn-tasks-ecs-run-task');
stack.node.setContext(EC2_RESTRICT_DEFAULT_SECURITY_GROUP, false);
stack.node.setContext(ECS_REDUCE_RUN_TASK_PERMISSIONS, true);

const cluster = new ecs.Cluster(stack, 'Ec2Cluster');
cluster.addCapacity('DefaultAutoScalingGroup', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as cdk from 'aws-cdk-lib';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP, ECS_REDUCE_RUN_TASK_PERMISSIONS } from 'aws-cdk-lib/cx-api';
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP } from 'aws-cdk-lib/cx-api';
import { IntegTest } from '@aws-cdk/integ-tests-alpha';

/*
Expand All @@ -19,7 +19,6 @@ import { IntegTest } from '@aws-cdk/integ-tests-alpha';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-sfn-tasks-ecs-fargate-run-task');
stack.node.setContext(EC2_RESTRICT_DEFAULT_SECURITY_GROUP, false);
stack.node.setContext(ECS_REDUCE_RUN_TASK_PERMISSIONS, true);

const cluster = new ecs.Cluster(stack, 'FargateCluster');

Expand Down
33 changes: 6 additions & 27 deletions packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/ecs/run-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as ecs from '../../../aws-ecs';
import * as iam from '../../../aws-iam';
import * as sfn from '../../../aws-stepfunctions';
import * as cdk from '../../../core';
import * as cxapi from '../../../cx-api';
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';

/**
Expand Down Expand Up @@ -347,31 +346,11 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
private makePolicyStatements(): iam.PolicyStatement[] {
const stack = cdk.Stack.of(this);

const taskDefinitionFamilyArn = this.getTaskDefinitionFamilyArn();
const reduceRunTaskPermissions = cdk.FeatureFlags.of(this).isEnabled(cxapi.ECS_REDUCE_RUN_TASK_PERMISSIONS);
let policyStatements = [];

// https://docs.aws.amazon.com/step-functions/latest/dg/ecs-iam.html
if (reduceRunTaskPermissions) {
policyStatements.push(
new iam.PolicyStatement({
actions: ['ecs:RunTask'],
resources: [`${taskDefinitionFamilyArn}:*`],
}),
);
} else {
policyStatements.push(
new iam.PolicyStatement({
actions: ['ecs:RunTask'],
resources: [
taskDefinitionFamilyArn,
`${taskDefinitionFamilyArn}:*`,
],
}),
);
}

policyStatements.push(
const policyStatements = [
new iam.PolicyStatement({
actions: ['ecs:RunTask'],
resources: [`${this.getTaskDefinitionFamilyArn()}:*`],
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, this.getTaskDefinitionFamilyArn() strips away the revision number from this.props.taskDefinition.taskDefinitionArn. I am curious, would the CDK user be able to specify specific revision of the task definition if they would like to scope this down for strictest security?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's correct understanding.

I am curious, would the CDK user be able to specify specific revision of the task definition if they would like to scope this down for strictest security

Not easily, they could use escape hatch to scope it down further to a specific revision number.

}),
new iam.PolicyStatement({
actions: ['ecs:StopTask', 'ecs:DescribeTasks'],
resources: ['*'],
Expand All @@ -380,7 +359,7 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
actions: ['iam:PassRole'],
resources: this.taskExecutionRoles().map((r) => r.roleArn),
}),
);
];

if (this.integrationPattern === sfn.IntegrationPattern.RUN_JOB) {
policyStatements.push(
Expand Down
Loading
Loading