Skip to content

Commit

Permalink
chore(middleware-user-agent): use authScheme identity instead of cred…
Browse files Browse the repository at this point in the history
…entials provider
  • Loading branch information
kuhe committed Oct 9, 2024
1 parent 27f462b commit 58ca4e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
9 changes: 9 additions & 0 deletions packages/middleware-user-agent/src/check-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type PreviouslyResolved = Partial<{
accountIdEndpointMode?: Provider<AccountIdEndpointMode>;
}>;

/**
* @internal
*/
const ACCOUNT_ID_ENDPOINT_REGEX = /\d{12}\.ddb/;

/**
* @internal
* Check for features that don't have a middleware activation site but
Expand All @@ -29,6 +34,10 @@ export async function checkFeatures(
// eslint-disable-next-line
const request = args.request as IHttpRequest;
if (typeof config.accountIdEndpointMode === "function") {
const endpointV2 = context.endpointV2;
if (String(endpointV2?.url?.hostname).match(ACCOUNT_ID_ENDPOINT_REGEX)) {
setFeature(context, "ACCOUNT_ID_ENDPOINT", "O");
}
switch (await config.accountIdEndpointMode?.()) {
case "disabled":
setFeature(context, "ACCOUNT_ID_MODE_DISABLED", "Q");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ describe("middleware-user-agent", () => {
});

describe("features", () => {
it("should detect DDB mapper, and account id mode", async () => {
it("should detect DDB mapper, account id, and account id mode", async () => {
const client = new DynamoDB({
credentials: {
accessKeyId: "",
secretAccessKey: "",
accountId: "123",
},
accountIdEndpointMode: async () => "preferred" as const,
accountIdEndpointMode: async () => "required" as const,
});

const doc = DynamoDBDocument.from(client);

requireRequestsFrom(doc).toMatch({
headers: {
"user-agent": /(.*?) m\/d,P$/,
"user-agent": /(.*?) m\/d,O,R$/,
},
});

client.config.credentials = async () => ({
accessKeyId: "",
secretAccessKey: "",
accountId: "123456789012",
});

await doc.get({
TableName: "table",
Key: {
Expand Down
18 changes: 9 additions & 9 deletions packages/middleware-user-agent/src/user-agent-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { getUserAgentPrefix } from "@aws-sdk/util-endpoints";
import { HttpRequest } from "@smithy/protocol-http";
import {
AbsoluteLocation,
BuildHandler,
BuildHandlerArguments,
BuildHandlerOptions,
BuildHandlerOutput,
FinalizeHandler,
FinalizeHandlerArguments,
FinalizeHandlerOutput,
FinalizeRequestHandlerOptions,
HandlerExecutionContext,
MetadataBearer,
Pluggable,
Expand Down Expand Up @@ -41,10 +41,10 @@ import { encodeFeatures } from "./encode-features";
export const userAgentMiddleware =
(options: UserAgentResolvedConfig) =>
<Output extends MetadataBearer>(
next: BuildHandler<any, any>,
next: FinalizeHandler<any, any>,
context: HandlerExecutionContext | AwsHandlerExecutionContext
): BuildHandler<any, any> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
): FinalizeHandler<any, any> =>
async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<Output>> => {
const { request } = args;
if (!HttpRequest.isInstance(request)) {
return next(args);
Expand Down Expand Up @@ -130,9 +130,9 @@ const escapeUserAgent = (userAgentPair: UserAgentPair): string => {
}, "") as string;
};

export const getUserAgentMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {
export const getUserAgentMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {
name: "getUserAgentMiddleware",
step: "build",
step: "finalizeRequest",
priority: "low",
tags: ["SET_USER_AGENT", "USER_AGENT"],
override: true,
Expand Down

0 comments on commit 58ca4e6

Please sign in to comment.