Skip to content

Commit dce662c

Browse files
authored
fix(stepfunctions-tasks): fix IAM policy statements for step functions API calls (#22959)
Fix IAM policy statements for step functions API calls. The service name of the Step Functions IAM policy action is different from the service name of the task resource name and must be translated. - IAM policy action: `states:[apiAction]` - Task resource name: `arn:aws:states:::aws-sdk:sfn:[apiAction]` ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 15ced88 commit dce662c

File tree

12 files changed

+2395
-1
lines changed

12 files changed

+2395
-1
lines changed

packages/@aws-cdk/aws-stepfunctions-tasks/lib/aws-sdk/call-aws-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,17 @@ export class CallAwsService extends sfn.TaskStateBase {
8080
throw new Error('The RUN_JOB integration pattern is not supported for CallAwsService');
8181
}
8282

83+
const iamServiceMap: Record<string, string> = {
84+
sfn: 'states',
85+
};
86+
const iamService = iamServiceMap[props.service] ?? props.service;
87+
8388
this.taskPolicies = [
8489
new iam.PolicyStatement({
8590
resources: props.iamResources,
8691
// The prefix and the action name are case insensitive
8792
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_action.html
88-
actions: [props.iamAction ?? `${props.service}:${props.action}`],
93+
actions: [props.iamAction ?? `${iamService}:${props.action}`],
8994
}),
9095
...props.additionalIamStatements ?? [],
9196
];

packages/@aws-cdk/aws-stepfunctions-tasks/test/aws-sdk/call-aws-service.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,34 @@ test('can pass additional IAM statements', () => {
198198
},
199199
});
200200
});
201+
202+
test('IAM policy for sfn', () => {
203+
// WHEN
204+
const task = new tasks.CallAwsService(stack, 'SendTaskSuccess', {
205+
service: 'sfn',
206+
action: 'sendTaskSuccess',
207+
iamResources: ['*'],
208+
parameters: {
209+
Output: sfn.JsonPath.objectAt('$.output'),
210+
TaskToken: sfn.JsonPath.stringAt('$.taskToken'),
211+
},
212+
});
213+
214+
new sfn.StateMachine(stack, 'StateMachine', {
215+
definition: task,
216+
});
217+
218+
// THEN
219+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
220+
PolicyDocument: {
221+
Statement: [
222+
{
223+
Action: 'states:sendTaskSuccess',
224+
Effect: 'Allow',
225+
Resource: '*',
226+
},
227+
],
228+
Version: '2012-10-17',
229+
},
230+
});
231+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "21.0.0",
3+
"files": {
4+
"b54b99043c35bd080b9d9d1afce31e3541cf15b679799ba980ed40c837dcb03b": {
5+
"source": {
6+
"path": "asset.b54b99043c35bd080b9d9d1afce31e3541cf15b679799ba980ed40c837dcb03b.bundle",
7+
"packaging": "zip"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "b54b99043c35bd080b9d9d1afce31e3541cf15b679799ba980ed40c837dcb03b.zip",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
},
17+
"53ea1c76e8a088a3e3455a07f903c3cdc7054d8399d75bc242655e2569ec4dbe": {
18+
"source": {
19+
"path": "IntegTestDefaultTestDeployAssertE3E7D2A4.template.json",
20+
"packaging": "file"
21+
},
22+
"destinations": {
23+
"current_account-current_region": {
24+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
25+
"objectKey": "53ea1c76e8a088a3e3455a07f903c3cdc7054d8399d75bc242655e2569ec4dbe.json",
26+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
27+
}
28+
}
29+
}
30+
},
31+
"dockerImages": {}
32+
}

0 commit comments

Comments
 (0)