Skip to content

Commit

Permalink
chore(middleware-sdk-s3): split region redirect middlewares in differ…
Browse files Browse the repository at this point in the history
…ent files
  • Loading branch information
siddsriv committed Sep 25, 2023
1 parent e133bb7 commit 8ed442b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
HandlerExecutionContext,
MetadataBearer,
Pluggable,
RelativeMiddlewareOptions,
SerializeHandler,
SerializeHandlerArguments,
SerializeHandlerOutput,
SerializeMiddleware,
} from "@smithy/types";

import { PreviouslyResolved } from "./region-redirect-middleware";

/**
* @internal
*/
export const regionRedirectEndpointMiddleware = (config: PreviouslyResolved): SerializeMiddleware<any, any> => {
return <Output extends MetadataBearer>(
next: SerializeHandler<any, Output>,
context: HandlerExecutionContext
): SerializeHandler<any, Output> =>
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => {
const originalRegion = await config.region();
if (context.__s3RegionRedirect) {
config.region = async () => {
return context.__s3RegionRedirect;
};
}
const result = await next(args);
if (context.__s3RegionRedirect) {
const region = await config.region();
if (originalRegion !== region) {
throw new Error("Region was not restored following S3 region redirect.");
}
}
return result;
};
};

/**
* @internal
*/
export const regionRedirectEndpointMiddlewareOptions: RelativeMiddlewareOptions = {
tags: ["REGION_REDIRECT", "S3"],
name: "regionRedirectEndpointMiddleware",
override: true,
relation: "before",
toMiddleware: "endpointV2Middleware",
};

/**
* @internal
*/
export const getRegionRedirectMiddlewarePlugin = (clientConfig: PreviouslyResolved): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.addRelativeTo(regionRedirectEndpointMiddleware(clientConfig), regionRedirectEndpointMiddlewareOptions);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { STS } from "@aws-sdk/client-sts";

const regionConfigs = [
{ region: "us-east-1", followRegionRedirects: true },
{ region: "eu-west-1", followRegionRedirects: true },
{ region: "us-west-2", followRegionRedirects: true },
{ region: "us-west-1", followRegionRedirects: true },
];

const s3Clients = regionConfigs.map((config) => new S3Client(config));
Expand All @@ -23,7 +23,7 @@ async function testS3GlobalClient() {

const callerID = await stsClient.getCallerIdentity({});

const bucketNames = regionConfigs.map((config) => `${callerID.Account}-redirect-testing-${config.region}`);
const bucketNames = regionConfigs.map((config) => `${callerID.Account}-redirect-${config.region}`);
await Promise.all(
bucketNames.map((bucketName, index) => s3Clients[index].send(new CreateBucketCommand({ Bucket: bucketName })))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe(regionRedirectMiddleware.name, () => {
if (call === 0) {
call++;
throw Object.assign(new Error(), {
Code: "PermanentRedirect",
name: "PermanentRedirect",
$metadata: { httpStatusCode: 301 },
$response: { headers: { "x-amz-bucket-region": redirectRegion } },
});
Expand Down
47 changes: 1 addition & 46 deletions packages/middleware-sdk-s3/src/region-redirect-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import {
MetadataBearer,
Pluggable,
Provider,
RelativeMiddlewareOptions,
SerializeHandler,
SerializeHandlerArguments,
SerializeHandlerOutput,
SerializeMiddleware,
} from "@smithy/types";

/**
* @internal
*/
interface PreviouslyResolved {
export interface PreviouslyResolved {
region: Provider<string>;
followRegionRedirects: boolean;
}
Expand Down Expand Up @@ -55,34 +50,6 @@ export function regionRedirectMiddleware(clientConfig: PreviouslyResolved): Init
};
}

/**
* @internal
*/
export const regionRedirectEndpointMiddleware = (config: PreviouslyResolved): SerializeMiddleware<any, any> => {
return <Output extends MetadataBearer>(
next: SerializeHandler<any, Output>,
context: HandlerExecutionContext
): SerializeHandler<any, Output> =>
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => {
const originalRegion = await config.region();
if (context.__s3RegionRedirect) {
const regionProviderRef = config.region;
config.region = async () => {
config.region = regionProviderRef;
return context.__s3RegionRedirect;
};
}
const result = await next(args);
if (context.__s3RegionRedirect) {
const region = await config.region();
if (originalRegion !== region) {
throw new Error("Region was not restored following S3 region redirect.");
}
}
return result;
};
};

/**
* @internal
*/
Expand All @@ -93,23 +60,11 @@ export const regionRedirectMiddlewareOptions: InitializeHandlerOptions = {
override: true,
};

/**
* @internal
*/
export const regionRedirectEndpointMiddlewareOptions: RelativeMiddlewareOptions = {
tags: ["REGION_REDIRECT", "S3"],
name: "regionRedirectEndpointMiddleware",
override: true,
relation: "before",
toMiddleware: "endpointV2Middleware",
};

/**
* @internal
*/
export const getRegionRedirectMiddlewarePlugin = (clientConfig: PreviouslyResolved): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.add(regionRedirectMiddleware(clientConfig), regionRedirectMiddlewareOptions);
clientStack.addRelativeTo(regionRedirectEndpointMiddleware(clientConfig), regionRedirectEndpointMiddlewareOptions);
},
});

0 comments on commit 8ed442b

Please sign in to comment.