Skip to content

Commit 1cbf57b

Browse files
authored
fix(client-sts): use outer client region before default STS global region (#5800)
1 parent ef1203b commit 1cbf57b

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

clients/client-sts/src/defaultStsRoleAssumers.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ const ASSUME_ROLE_DEFAULT_REGION = "us-east-1";
3333
/**
3434
* @internal
3535
*
36-
* Default to the us-east-1 region for aws partition,
37-
* or default to the parent client region otherwise.
36+
* Default to the parent client region or us-east-1 if no region is specified.
3837
*/
3938
const resolveRegion = async (
4039
_region: string | Provider<string> | undefined,
@@ -44,24 +43,14 @@ const resolveRegion = async (
4443
const region: string | undefined = typeof _region === "function" ? await _region() : _region;
4544
const parentRegion: string | undefined = typeof _parentRegion === "function" ? await _parentRegion() : _parentRegion;
4645

47-
if (!parentRegion || partition(parentRegion).name === "aws") {
48-
credentialProviderLogger?.debug?.(
49-
"@aws-sdk/client-sts::resolveRegion",
50-
"accepting first of:",
51-
`${region} (provider)`,
52-
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
53-
);
54-
return region ?? ASSUME_ROLE_DEFAULT_REGION;
55-
} else {
56-
credentialProviderLogger?.debug?.(
57-
"@aws-sdk/client-sts::resolveRegion",
58-
"accepting first of:",
59-
`${region} (provider)`,
60-
`${parentRegion} (parent client)`,
61-
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
62-
);
63-
return region ?? parentRegion ?? ASSUME_ROLE_DEFAULT_REGION;
64-
}
46+
credentialProviderLogger?.debug?.(
47+
"@aws-sdk/client-sts::resolveRegion",
48+
"accepting first of:",
49+
`${region} (provider)`,
50+
`${parentRegion} (parent client)`,
51+
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
52+
);
53+
return region ?? parentRegion ?? ASSUME_ROLE_DEFAULT_REGION;
6554
};
6655

6756
/**

codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultStsRoleAssumers.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const ASSUME_ROLE_DEFAULT_REGION = "us-east-1";
3030
/**
3131
* @internal
3232
*
33-
* Default to the us-east-1 region for aws partition,
34-
* or default to the parent client region otherwise.
33+
* Default to the parent client region or us-east-1 if no region is specified.
3534
*/
3635
const resolveRegion = async (
3736
_region: string | Provider<string> | undefined,
@@ -41,24 +40,14 @@ const resolveRegion = async (
4140
const region: string | undefined = typeof _region === "function" ? await _region() : _region;
4241
const parentRegion: string | undefined = typeof _parentRegion === "function" ? await _parentRegion() : _parentRegion;
4342

44-
if (!parentRegion || partition(parentRegion).name === "aws") {
45-
credentialProviderLogger?.debug?.(
46-
"@aws-sdk/client-sts::resolveRegion",
47-
"accepting first of:",
48-
`${region} (provider)`,
49-
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
50-
);
51-
return region ?? ASSUME_ROLE_DEFAULT_REGION;
52-
} else {
53-
credentialProviderLogger?.debug?.(
54-
"@aws-sdk/client-sts::resolveRegion",
55-
"accepting first of:",
56-
`${region} (provider)`,
57-
`${parentRegion} (parent client)`,
58-
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
59-
);
60-
return region ?? parentRegion ?? ASSUME_ROLE_DEFAULT_REGION;
61-
}
43+
credentialProviderLogger?.debug?.(
44+
"@aws-sdk/client-sts::resolveRegion",
45+
"accepting first of:",
46+
`${region} (provider)`,
47+
`${parentRegion} (parent client)`,
48+
`${ASSUME_ROLE_DEFAULT_REGION} (STS default)`
49+
);
50+
return region ?? parentRegion ?? ASSUME_ROLE_DEFAULT_REGION;
6251
};
6352

6453
/**

packages/credential-provider-node/src/credential-provider-node.integ.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,35 @@ describe("credential-provider-node integration test", () => {
342342
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
343343
sessionToken: "STS_AR_SESSION_TOKEN",
344344
expiration: new Date("3000-01-01T00:00:00.000Z"),
345-
credentialScope: "us-stsar-1__us-east-1",
345+
credentialScope: "us-stsar-1__us-west-2",
346+
});
347+
});
348+
349+
it("should use the outer client's region for STS when the partition is AWS", async () => {
350+
sts = new STS({
351+
region: "eu-west-1",
352+
requestHandler: mockRequestHandler,
353+
});
354+
iniProfileData.assume = {
355+
region: "eu-west-1",
356+
aws_access_key_id: "ASSUME_STATIC_ACCESS_KEY",
357+
aws_secret_access_key: "ASSUME_STATIC_SECRET_KEY",
358+
};
359+
Object.assign(iniProfileData.default, {
360+
region: "eu-west-1",
361+
role_arn: "ROLE_ARN",
362+
role_session_name: "ROLE_SESSION_NAME",
363+
external_id: "EXTERNAL_ID",
364+
source_profile: "assume",
365+
});
366+
await sts.getCallerIdentity({});
367+
const credentials = await sts.config.credentials();
368+
expect(credentials).toEqual({
369+
accessKeyId: "STS_AR_ACCESS_KEY_ID",
370+
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
371+
sessionToken: "STS_AR_SESSION_TOKEN",
372+
expiration: new Date("3000-01-01T00:00:00.000Z"),
373+
credentialScope: "us-stsar-1__eu-west-1",
346374
});
347375
});
348376

@@ -390,7 +418,7 @@ describe("credential-provider-node integration test", () => {
390418
secretAccessKey: "STS_ARWI_SECRET_ACCESS_KEY",
391419
sessionToken: "STS_ARWI_SESSION_TOKEN",
392420
expiration: new Date("3000-01-01T00:00:00.000Z"),
393-
credentialScope: "us-stsarwi-1__us-east-1",
421+
credentialScope: "us-stsarwi-1__us-west-2",
394422
});
395423
});
396424

@@ -484,7 +512,7 @@ describe("credential-provider-node integration test", () => {
484512
secretAccessKey: "STS_ARWI_SECRET_ACCESS_KEY",
485513
sessionToken: "STS_ARWI_SESSION_TOKEN",
486514
expiration: new Date("3000-01-01T00:00:00.000Z"),
487-
credentialScope: "us-stsarwi-1__us-east-1",
515+
credentialScope: "us-stsarwi-1__us-west-2",
488516
});
489517
});
490518
});

0 commit comments

Comments
 (0)