Skip to content

Commit b11b158

Browse files
fixed sts endpoint (#997)
* fixed sts endpoint * fixed assume role plugin for sts regional endpoints Co-authored-by: hickeydh-aws <hickeydh@amazon.com> Co-authored-by: Brian969 <56414362+Brian969@users.noreply.github.com>
1 parent 994a8f4 commit b11b158

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/deployments/cdk/toolkit.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ import { promises as fsp } from 'fs';
3131
// Set debug logging
3232
setLogLevel(1);
3333

34-
// Register the assume role plugin
35-
const assumeRolePlugin = new AssumeProfilePlugin();
36-
assumeRolePlugin.init(PluginHost.instance);
37-
3834
export 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}`);

src/lib/cdk-plugin-assume-role/src/assume-role-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import { AssumeRoleProviderSource } from './assume-role-provider-source';
1717
export 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
}

src/lib/cdk-plugin-assume-role/src/assume-role-provider-source.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface AssumeRoleProviderSourceProps {
2121
name: string;
2222
assumeRoleName: string;
2323
assumeRoleDuration: number;
24+
region: string | undefined;
2425
}
2526

2627
export 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
}

src/lib/common/src/aws/sts.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414
import aws from './aws-client';
1515
import * as sts from 'aws-sdk/clients/sts';
1616
import { throttlingBackOff } from './backoff';
17-
1817
export 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

0 commit comments

Comments
 (0)