Skip to content

Commit 27f462b

Browse files
authored
fix(credentials): used selected auth scheme identity instead of calling credentials provider (#6555)
* fix(credentials): used selected auth scheme identity instead of calling credentials provider * test(middleware-user-agent): add unit test for empty config/context
1 parent fc7effc commit 27f462b

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { AwsHandlerExecutionContext } from "@aws-sdk/types";
2+
3+
import { checkFeatures } from "./check-features";
4+
5+
describe(checkFeatures.name, () => {
6+
it("should not call the credentials provider to retrieve the identity", async () => {
7+
const config = {
8+
credentials: jest.fn(),
9+
};
10+
11+
const context = {
12+
__smithy_context: {
13+
selectedHttpAuthScheme: {
14+
identity: {
15+
accountId: "123456789012",
16+
$source: {},
17+
},
18+
},
19+
},
20+
} as AwsHandlerExecutionContext;
21+
22+
await checkFeatures(context, config, {
23+
request: undefined,
24+
input: undefined,
25+
});
26+
27+
expect(config.credentials).not.toHaveBeenCalled();
28+
expect(context.__aws_sdk_context?.features?.RESOLVED_ACCOUNT_ID).toBe("T");
29+
});
30+
31+
it("should not throw an error if no fields are present", async () => {
32+
await checkFeatures({}, {}, {} as any);
33+
});
34+
});

packages/middleware-user-agent/src/check-features.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ export async function checkFeatures(
4242
}
4343
}
4444

45-
if (typeof config.credentials === "function") {
46-
try {
47-
const credentials: AttributedAwsCredentialIdentity = await config.credentials?.();
48-
if (credentials.accountId) {
49-
setFeature(context, "RESOLVED_ACCOUNT_ID", "T");
50-
}
51-
for (const [key, value] of Object.entries(credentials.$source ?? {})) {
52-
setFeature(context, key as keyof AwsSdkCredentialsFeatures, value);
53-
}
54-
} catch (e: unknown) {
55-
// Sometimes config.credentials is a function but only throws
56-
// as a way of informing users that something is missing.
57-
// That error and any other credential retrieval errors are
58-
// not relevant for feature-checking and should be ignored.
45+
// TODO: later version of @smithy/types has explicit typing for this.
46+
const identity = (context.__smithy_context?.selectedHttpAuthScheme as any)?.identity;
47+
48+
if ((identity as AttributedAwsCredentialIdentity)?.$source) {
49+
const credentials = identity as AttributedAwsCredentialIdentity;
50+
if (credentials.accountId) {
51+
setFeature(context, "RESOLVED_ACCOUNT_ID", "T");
52+
}
53+
for (const [key, value] of Object.entries(credentials.$source ?? {})) {
54+
setFeature(context, key as keyof AwsSdkCredentialsFeatures, value);
5955
}
6056
}
6157
}

0 commit comments

Comments
 (0)