Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import * as deployStack from '../../../lib/api/deployments/deploy-stack';
import type { Stack } from '@aws-sdk/client-cloudformation';
import { CreatePolicyCommand, GetPolicyCommand } from '@aws-sdk/client-iam';
import { Match, Template } from 'aws-cdk-lib/assertions';

import {
mockBootstrapStack,
mockIAMClient,
Expand Down Expand Up @@ -642,4 +644,93 @@ describe('Bootstrapping v2', () => {
},
);
});

describe('contains sts:TagSession on trusted accounts', () => {
let template: Template;

const iamRoleName = (name: string) => {
return {
'Fn::Sub': `cdk-\${Qualifier}-${name}-\${AWS::AccountId}-\${AWS::Region}`,
};
};

const statementWithCondition = (conditionName: string) => {
return Match.objectLike({
'Fn::If': Match.arrayWith([
conditionName,
Match.objectLike({
Action: Match.arrayWith(['sts:AssumeRole', 'sts:TagSession']),
}),
]),
});
};

beforeEach(async () => {
let rawTemplate: any;
mockDeployStack.mockImplementation((args: deployStack.DeployStackOptions) => {
rawTemplate = args.stack.template;
return Promise.resolve({
type: 'did-deploy-stack',
noOp: false,
outputs: {},
stackArn: 'arn:stack',
});
});
await bootstrapper.bootstrapEnvironment(env, sdk, {});
template = Template.fromJSON(rawTemplate);
});

test('in the FilePublishingRole', async () => {
template.hasResource('AWS::IAM::Role', {
Properties: {
RoleName: iamRoleName('file-publishing-role'),
AssumeRolePolicyDocument: {
Statement: Match.arrayWith([
statementWithCondition('HasTrustedAccounts'),
]),
},
},
});
});

test('in the ImagePublishingRole', async () => {
template.hasResource('AWS::IAM::Role', {
Properties: {
RoleName: iamRoleName('image-publishing-role'),
AssumeRolePolicyDocument: {
Statement: Match.arrayWith([
statementWithCondition('HasTrustedAccounts'),
]),
},
},
});
});

test('in the LookupRole', async () => {
template.hasResource('AWS::IAM::Role', {
Properties: {
RoleName: iamRoleName('lookup-role'),
AssumeRolePolicyDocument: {
Statement: Match.arrayWith([
statementWithCondition('HasTrustedAccountsForLookup'),
statementWithCondition('HasTrustedAccounts'),
]),
},
},
});
});

test('in the DeploymentActionRole', async () => {
template.hasResource('AWS::IAM::Role', {
Properties: {
RoleName: iamRoleName('deploy-role'),
AssumeRolePolicyDocument: {
Statement: Match.arrayWith([
statementWithCondition('HasTrustedAccounts'),
]),
},
},
});
});
});
});
20 changes: 15 additions & 5 deletions packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ Resources:
Ref: AWS::AccountId
- Fn::If:
- HasTrustedAccounts
- Action: sts:AssumeRole
- Action:
- sts:AssumeRole
- sts:TagSession
Effect: Allow
Principal:
AWS:
Expand Down Expand Up @@ -340,7 +342,9 @@ Resources:
Ref: AWS::AccountId
- Fn::If:
- HasTrustedAccounts
- Action: sts:AssumeRole
- Action:
- sts:AssumeRole
- sts:TagSession
Effect: Allow
Principal:
AWS:
Expand Down Expand Up @@ -370,15 +374,19 @@ Resources:
Ref: AWS::AccountId
- Fn::If:
- HasTrustedAccountsForLookup
- Action: sts:AssumeRole
- Action:
- sts:AssumeRole
- sts:TagSession
Effect: Allow
Principal:
AWS:
Ref: TrustedAccountsForLookup
- Ref: AWS::NoValue
- Fn::If:
- HasTrustedAccounts
- Action: sts:AssumeRole
- Action:
- sts:AssumeRole
- sts:TagSession
Effect: Allow
Principal:
AWS:
Expand Down Expand Up @@ -485,7 +493,9 @@ Resources:
Ref: AWS::AccountId
- Fn::If:
- HasTrustedAccounts
- Action: sts:AssumeRole
- Action:
- sts:AssumeRole
- sts:TagSession
Effect: Allow
Principal:
AWS:
Expand Down
Loading