Skip to content

Commit

Permalink
fix(credentials): used selected auth scheme identity instead of calli…
Browse files Browse the repository at this point in the history
…ng credentials provider
  • Loading branch information
kuhe committed Oct 9, 2024
1 parent fc7effc commit 055041c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
30 changes: 30 additions & 0 deletions packages/middleware-user-agent/src/check-features.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AwsHandlerExecutionContext } from "@aws-sdk/types";

import { checkFeatures } from "./check-features";

describe(checkFeatures.name, () => {
it("should not call the credentials provider to retrieve the identity", async () => {
const config = {
credentials: jest.fn(),
};

const context = {
__smithy_context: {
selectedHttpAuthScheme: {
identity: {
accountId: "123456789012",
$source: {},
},
},
},
} as AwsHandlerExecutionContext;

await checkFeatures(context, config, {
request: undefined,
input: undefined,
});

expect(config.credentials).not.toHaveBeenCalled();
expect(context.__aws_sdk_context?.features?.RESOLVED_ACCOUNT_ID).toBe("T");
});
});
24 changes: 10 additions & 14 deletions packages/middleware-user-agent/src/check-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,16 @@ export async function checkFeatures(
}
}

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

if ((identity as AttributedAwsCredentialIdentity)?.$source) {
const credentials = identity as AttributedAwsCredentialIdentity;
if (credentials.accountId) {
setFeature(context, "RESOLVED_ACCOUNT_ID", "T");
}
for (const [key, value] of Object.entries(credentials.$source ?? {})) {
setFeature(context, key as keyof AwsSdkCredentialsFeatures, value);
}
}
}

0 comments on commit 055041c

Please sign in to comment.