Skip to content

Commit d243542

Browse files
committed
chore(middleware-user-agent): use authScheme identity instead of credentials provider
1 parent fc7effc commit d243542

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type PreviouslyResolved = Partial<{
1616
accountIdEndpointMode?: Provider<AccountIdEndpointMode>;
1717
}>;
1818

19+
/**
20+
* @internal
21+
*/
22+
const ACCOUNT_ID_ENDPOINT_REGEX = /\d{12}\.ddb/;
23+
1924
/**
2025
* @internal
2126
* Check for features that don't have a middleware activation site but
@@ -29,6 +34,10 @@ export async function checkFeatures(
2934
// eslint-disable-next-line
3035
const request = args.request as IHttpRequest;
3136
if (typeof config.accountIdEndpointMode === "function") {
37+
const endpointV2 = context.endpointV2;
38+
if (String(endpointV2?.url?.hostname).match(ACCOUNT_ID_ENDPOINT_REGEX)) {
39+
setFeature(context, "ACCOUNT_ID_ENDPOINT", "O");
40+
}
3241
switch (await config.accountIdEndpointMode?.()) {
3342
case "disabled":
3443
setFeature(context, "ACCOUNT_ID_MODE_DISABLED", "Q");
@@ -42,20 +51,14 @@ export async function checkFeatures(
4251
}
4352
}
4453

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.
54+
const identity = context.__smithy_context?.selectedHttpAuthScheme?.identity;
55+
if ((identity as AttributedAwsCredentialIdentity).$source) {
56+
const credentials = identity as AttributedAwsCredentialIdentity;
57+
if (credentials.accountId) {
58+
setFeature(context, "RESOLVED_ACCOUNT_ID", "T");
59+
}
60+
for (const [key, value] of Object.entries(credentials.$source ?? {})) {
61+
setFeature(context, key as keyof AwsSdkCredentialsFeatures, value);
5962
}
6063
}
6164
}

packages/middleware-user-agent/src/middleware-user-agent.integ.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,25 @@ describe("middleware-user-agent", () => {
2727
});
2828

2929
describe("features", () => {
30-
it("should detect DDB mapper, and account id mode", async () => {
30+
it("should detect DDB mapper, account id, and account id mode", async () => {
3131
const client = new DynamoDB({
32-
credentials: {
33-
accessKeyId: "",
34-
secretAccessKey: "",
35-
accountId: "123",
36-
},
37-
accountIdEndpointMode: async () => "preferred" as const,
32+
accountIdEndpointMode: async () => "required" as const,
3833
});
3934

4035
const doc = DynamoDBDocument.from(client);
4136

4237
requireRequestsFrom(doc).toMatch({
4338
headers: {
44-
"user-agent": /(.*?) m\/d,P$/,
39+
"user-agent": /(.*?) m\/d,O,R$/,
4540
},
4641
});
4742

43+
client.config.credentials = async () => ({
44+
accessKeyId: "",
45+
secretAccessKey: "",
46+
accountId: "123456789012",
47+
});
48+
4849
await doc.get({
4950
TableName: "table",
5051
Key: {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { getUserAgentPrefix } from "@aws-sdk/util-endpoints";
33
import { HttpRequest } from "@smithy/protocol-http";
44
import {
55
AbsoluteLocation,
6-
BuildHandler,
7-
BuildHandlerArguments,
8-
BuildHandlerOptions,
9-
BuildHandlerOutput,
6+
FinalizeHandler,
7+
FinalizeHandlerArguments,
8+
FinalizeHandlerOutput,
9+
FinalizeRequestHandlerOptions,
1010
HandlerExecutionContext,
1111
MetadataBearer,
1212
Pluggable,
@@ -41,10 +41,10 @@ import { encodeFeatures } from "./encode-features";
4141
export const userAgentMiddleware =
4242
(options: UserAgentResolvedConfig) =>
4343
<Output extends MetadataBearer>(
44-
next: BuildHandler<any, any>,
44+
next: FinalizeHandler<any, any>,
4545
context: HandlerExecutionContext | AwsHandlerExecutionContext
46-
): BuildHandler<any, any> =>
47-
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
46+
): FinalizeHandler<any, any> =>
47+
async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<Output>> => {
4848
const { request } = args;
4949
if (!HttpRequest.isInstance(request)) {
5050
return next(args);
@@ -130,9 +130,9 @@ const escapeUserAgent = (userAgentPair: UserAgentPair): string => {
130130
}, "") as string;
131131
};
132132

133-
export const getUserAgentMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {
133+
export const getUserAgentMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {
134134
name: "getUserAgentMiddleware",
135-
step: "build",
135+
step: "finalizeRequest",
136136
priority: "low",
137137
tags: ["SET_USER_AGENT", "USER_AGENT"],
138138
override: true,

0 commit comments

Comments
 (0)