Skip to content

Commit 64b7f75

Browse files
antonu17iankhou
authored andcommitted
feat(cli): add sts:TagSession permission to trusted accounts on bootstrap (#762)
## Description Accounts bootstrapped with `--trust` or `--trust-for-lookup` need `sts:TagSession` permissions in AssumeRolePolicy. I got errors during `cdk deploy` run in CD pipelines executed on EKS cluster on the trusted account. Error message: Could not assume role in target account using current credentials (which are for account `<TRUSTED_ACCOUT>`) User: `arn:aws:sts::<TRUSTED_ACCOUT>:assumed-role/<eks-pod-role>` is not authorized to perform: `sts:TagSession` on resource: `arn:aws:iam::<TARGET_ACCOUNT>:role/cdk-hnb659fds-lookup-role-<TARGET_ACCOUNT>-us-east-1` Troubleshooting revealed that DeploymentActionRole, FilePublishingRole, ImagePublishingRole, LookupRole don't have `sts:TagSession`. After updating AssumeRolePolicy `cdk deploy` worked normally. Fixes aws/aws-cdk#31557 --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 50d80a8 commit 64b7f75

File tree

2 files changed

+106
-5
lines changed

2 files changed

+106
-5
lines changed

packages/@aws-cdk/toolkit-lib/test/api/bootstrap/bootstrap2.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import * as deployStack from '../../../lib/api/deployments/deploy-stack';
44
import type { Stack } from '@aws-sdk/client-cloudformation';
55
import { CreatePolicyCommand, GetPolicyCommand } from '@aws-sdk/client-iam';
6+
import { Match, Template } from 'aws-cdk-lib/assertions';
7+
68
import {
79
mockBootstrapStack,
810
mockIAMClient,
@@ -642,4 +644,93 @@ describe('Bootstrapping v2', () => {
642644
},
643645
);
644646
});
647+
648+
describe('contains sts:TagSession on trusted accounts', () => {
649+
let template: Template;
650+
651+
const iamRoleName = (name: string) => {
652+
return {
653+
'Fn::Sub': `cdk-\${Qualifier}-${name}-\${AWS::AccountId}-\${AWS::Region}`,
654+
};
655+
};
656+
657+
const statementWithCondition = (conditionName: string) => {
658+
return Match.objectLike({
659+
'Fn::If': Match.arrayWith([
660+
conditionName,
661+
Match.objectLike({
662+
Action: Match.arrayWith(['sts:AssumeRole', 'sts:TagSession']),
663+
}),
664+
]),
665+
});
666+
};
667+
668+
beforeEach(async () => {
669+
let rawTemplate: any;
670+
mockDeployStack.mockImplementation((args: deployStack.DeployStackOptions) => {
671+
rawTemplate = args.stack.template;
672+
return Promise.resolve({
673+
type: 'did-deploy-stack',
674+
noOp: false,
675+
outputs: {},
676+
stackArn: 'arn:stack',
677+
});
678+
});
679+
await bootstrapper.bootstrapEnvironment(env, sdk, {});
680+
template = Template.fromJSON(rawTemplate);
681+
});
682+
683+
test('in the FilePublishingRole', async () => {
684+
template.hasResource('AWS::IAM::Role', {
685+
Properties: {
686+
RoleName: iamRoleName('file-publishing-role'),
687+
AssumeRolePolicyDocument: {
688+
Statement: Match.arrayWith([
689+
statementWithCondition('HasTrustedAccounts'),
690+
]),
691+
},
692+
},
693+
});
694+
});
695+
696+
test('in the ImagePublishingRole', async () => {
697+
template.hasResource('AWS::IAM::Role', {
698+
Properties: {
699+
RoleName: iamRoleName('image-publishing-role'),
700+
AssumeRolePolicyDocument: {
701+
Statement: Match.arrayWith([
702+
statementWithCondition('HasTrustedAccounts'),
703+
]),
704+
},
705+
},
706+
});
707+
});
708+
709+
test('in the LookupRole', async () => {
710+
template.hasResource('AWS::IAM::Role', {
711+
Properties: {
712+
RoleName: iamRoleName('lookup-role'),
713+
AssumeRolePolicyDocument: {
714+
Statement: Match.arrayWith([
715+
statementWithCondition('HasTrustedAccountsForLookup'),
716+
statementWithCondition('HasTrustedAccounts'),
717+
]),
718+
},
719+
},
720+
});
721+
});
722+
723+
test('in the DeploymentActionRole', async () => {
724+
template.hasResource('AWS::IAM::Role', {
725+
Properties: {
726+
RoleName: iamRoleName('deploy-role'),
727+
AssumeRolePolicyDocument: {
728+
Statement: Match.arrayWith([
729+
statementWithCondition('HasTrustedAccounts'),
730+
]),
731+
},
732+
},
733+
});
734+
});
735+
});
645736
});

packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ Resources:
310310
Ref: AWS::AccountId
311311
- Fn::If:
312312
- HasTrustedAccounts
313-
- Action: sts:AssumeRole
313+
- Action:
314+
- sts:AssumeRole
315+
- sts:TagSession
314316
Effect: Allow
315317
Principal:
316318
AWS:
@@ -340,7 +342,9 @@ Resources:
340342
Ref: AWS::AccountId
341343
- Fn::If:
342344
- HasTrustedAccounts
343-
- Action: sts:AssumeRole
345+
- Action:
346+
- sts:AssumeRole
347+
- sts:TagSession
344348
Effect: Allow
345349
Principal:
346350
AWS:
@@ -370,15 +374,19 @@ Resources:
370374
Ref: AWS::AccountId
371375
- Fn::If:
372376
- HasTrustedAccountsForLookup
373-
- Action: sts:AssumeRole
377+
- Action:
378+
- sts:AssumeRole
379+
- sts:TagSession
374380
Effect: Allow
375381
Principal:
376382
AWS:
377383
Ref: TrustedAccountsForLookup
378384
- Ref: AWS::NoValue
379385
- Fn::If:
380386
- HasTrustedAccounts
381-
- Action: sts:AssumeRole
387+
- Action:
388+
- sts:AssumeRole
389+
- sts:TagSession
382390
Effect: Allow
383391
Principal:
384392
AWS:
@@ -485,7 +493,9 @@ Resources:
485493
Ref: AWS::AccountId
486494
- Fn::If:
487495
- HasTrustedAccounts
488-
- Action: sts:AssumeRole
496+
- Action:
497+
- sts:AssumeRole
498+
- sts:TagSession
489499
Effect: Allow
490500
Principal:
491501
AWS:

0 commit comments

Comments
 (0)