File tree Expand file tree Collapse file tree 4 files changed +25
-9
lines changed
cdk-plugin-assume-role/src Expand file tree Collapse file tree 4 files changed +25
-9
lines changed Original file line number Diff line number Diff line change @@ -31,10 +31,6 @@ import { promises as fsp } from 'fs';
3131// Set debug logging
3232setLogLevel ( 1 ) ;
3333
34- // Register the assume role plugin
35- const assumeRolePlugin = new AssumeProfilePlugin ( ) ;
36- assumeRolePlugin . init ( PluginHost . instance ) ;
37-
3834export interface CdkToolkitProps {
3935 assemblies : CloudAssembly [ ] ;
4036 configuration : Configuration ;
@@ -198,6 +194,9 @@ export class CdkToolkit {
198194 }
199195
200196 async deployStack ( stack : CloudFormationStackArtifact , retries : number = 0 ) : Promise < StackOutput [ ] > {
197+ // Register the assume role plugin
198+ const assumeRolePlugin = new AssumeProfilePlugin ( { region : stack . environment . region } ) ;
199+ await assumeRolePlugin . init ( PluginHost . instance ) ;
201200 this . deploymentLog ( stack , 'Deploying Stack' ) ;
202201 const stackExists = await this . cloudFormation . stackExists ( { stack } ) ;
203202 this . deploymentLog ( stack , `Stack Exists: ${ stackExists } ` ) ;
Original file line number Diff line number Diff line change @@ -17,13 +17,14 @@ import { AssumeRoleProviderSource } from './assume-role-provider-source';
1717export class AssumeProfilePlugin implements Plugin {
1818 readonly version = '1' ;
1919
20- constructor ( private readonly props : { assumeRoleName ?: string ; assumeRoleDuration ?: number } = { } ) { }
20+ constructor ( private readonly props : { assumeRoleName ?: string ; assumeRoleDuration ?: number ; region ?: string } = { } ) { }
2121
2222 init ( host : PluginHost ) : void {
2323 const source = new AssumeRoleProviderSource ( {
2424 name : 'cdk-assume-role-plugin' ,
2525 assumeRoleName : this . props . assumeRoleName ?? AssumeProfilePlugin . getDefaultAssumeRoleName ( ) ,
2626 assumeRoleDuration : this . props . assumeRoleDuration ?? AssumeProfilePlugin . getDefaultAssumeRoleDuration ( ) ,
27+ region : this . props . region ,
2728 } ) ;
2829 host . registerCredentialProviderSource ( source ) ;
2930 }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ export interface AssumeRoleProviderSourceProps {
2121 name : string ;
2222 assumeRoleName : string ;
2323 assumeRoleDuration : number ;
24+ region : string | undefined ;
2425}
2526
2627export class AssumeRoleProviderSource implements CredentialProviderSource {
@@ -64,9 +65,13 @@ export class AssumeRoleProviderSource implements CredentialProviderSource {
6465 protected async assumeRole ( accountId : string , duration : number ) : Promise < aws . STS . AssumeRoleResponse > {
6566 const roleArn = `arn:aws:iam::${ accountId } :role/${ this . props . assumeRoleName } ` ;
6667 console . log ( `Assuming role ${ green ( roleArn ) } for ${ duration } seconds` ) ;
67-
68- const sts = new aws . STS ( ) ;
69- return throttlingBackOff ( ( ) =>
68+ const region = this . props . region ;
69+ let endpoint ;
70+ if ( region ) {
71+ endpoint = `sts.${ region } .amazonaws.com` ;
72+ }
73+ const sts = new aws . STS ( { endpoint, region } ) ;
74+ const assumeRoleResponse = await throttlingBackOff ( ( ) =>
7075 sts
7176 . assumeRole ( {
7277 RoleArn : roleArn ,
@@ -75,5 +80,8 @@ export class AssumeRoleProviderSource implements CredentialProviderSource {
7580 } )
7681 . promise ( ) ,
7782 ) ;
83+
84+ console . log ( assumeRoleResponse ) ;
85+ return assumeRoleResponse ;
7886 }
7987}
Original file line number Diff line number Diff line change 1414import aws from './aws-client' ;
1515import * as sts from 'aws-sdk/clients/sts' ;
1616import { throttlingBackOff } from './backoff' ;
17-
1817export class STS {
1918 private readonly client : aws . STS ;
2019 private readonly cache : { [ roleArn : string ] : aws . Credentials } = { } ;
2120
2221 constructor ( credentials ?: aws . Credentials ) {
22+ let region ;
23+ let endpoint ;
24+ if ( process . env . AWS_REGION ) {
25+ region = process . env . AWS_REGION ;
26+ endpoint = `sts.${ process . env . AWS_REGION } .amazonaws.com` ;
27+ }
28+
2329 this . client = new aws . STS ( {
2430 credentials,
31+ region,
32+ endpoint,
2533 } ) ;
2634 }
2735
You can’t perform that action at this time.
0 commit comments