Skip to content

Commit 5478913

Browse files
authored
fix(aws-cdk): fix profile use in non-'aws' partitions (#1283)
Properly pass on the default region to the STS call we make to discover the default AWS credentials. Also, there is no way to make use of AssumeRole profiles without the AWS_SDK_LOAD_CONFIG flag being set, so reintroduce setting that flag if we discover the file to exist. Fixes #1262 and #1109.
1 parent c298a7c commit 5478913

File tree

1 file changed

+18
-3
lines changed
  • packages/aws-cdk/lib/api/util

1 file changed

+18
-3
lines changed

packages/aws-cdk/lib/api/util/sdk.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class SDK {
7272
});
7373
}
7474

75-
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider);
75+
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider, getCLICompatibleDefaultRegion(this.profile));
7676
this.credentialsCache = new CredentialsCache(this.defaultAwsAccount, defaultCredentialProvider);
7777
}
7878

@@ -206,7 +206,9 @@ class DefaultAWSAccount {
206206
private defaultAccountId?: string = undefined;
207207
private readonly accountCache = new AccountAccessKeyCache();
208208

209-
constructor(private readonly defaultCredentialsProvider: Promise<AWS.CredentialProviderChain>) {
209+
constructor(
210+
private readonly defaultCredentialsProvider: Promise<AWS.CredentialProviderChain>,
211+
private readonly region: Promise<string | undefined>) {
210212
}
211213

212214
/**
@@ -222,6 +224,10 @@ class DefaultAWSAccount {
222224

223225
private async lookupDefaultAccount(): Promise<string | undefined> {
224226
try {
227+
// There just is *NO* way to do AssumeRole credentials as long as AWS_SDK_LOAD_CONFIG is not set. The SDK
228+
// crash if the file does not exist though. So set the environment variable if we can find that file.
229+
await setConfigVariable();
230+
225231
debug('Resolving default credentials');
226232
const credentialProvider = await this.defaultCredentialsProvider;
227233
const creds = await credentialProvider.resolvePromise();
@@ -234,7 +240,7 @@ class DefaultAWSAccount {
234240
const accountId = await this.accountCache.fetch(creds.accessKeyId, async () => {
235241
// if we don't have one, resolve from STS and store in cache.
236242
debug('Looking up default account ID from STS');
237-
const result = await new AWS.STS({ credentials: creds }).getCallerIdentity().promise();
243+
const result = await new AWS.STS({ credentials: creds, region: await this.region }).getCallerIdentity().promise();
238244
const aid = result.Account;
239245
if (!aid) {
240246
debug('STS didn\'t return an account ID');
@@ -389,6 +395,15 @@ async function hasEc2Credentials() {
389395
return instance;
390396
}
391397

398+
async function setConfigVariable() {
399+
const homeDir = process.env.HOME || process.env.USERPROFILE
400+
|| (process.env.HOMEPATH ? ((process.env.HOMEDRIVE || 'C:/') + process.env.HOMEPATH) : null) || os.homedir();
401+
402+
if (await fs.pathExists(path.resolve(homeDir, '.aws', 'config'))) {
403+
process.env.AWS_SDK_LOAD_CONFIG = '1';
404+
}
405+
}
406+
392407
async function readIfPossible(filename: string): Promise<string | undefined> {
393408
try {
394409
if (!await fs.pathExists(filename)) { return undefined; }

0 commit comments

Comments
 (0)