From 346f13990eda7c828985843ab4fe9217cc8e4be9 Mon Sep 17 00:00:00 2001 From: AllanZhengYP Date: Fri, 3 Jun 2022 09:30:26 -0700 Subject: [PATCH] chore(middleware-sdk-s3): remove deprecated regional endpoints middleware (#3656) * chore(middleware-sdk-s3): remove deprecated regional endpoints mw * test(s3): add test for regional endpoints --- clients/client-s3/src/S3Client.ts | 3 +- clients/client-s3/test/S3.spec.ts | 39 +++++++++++- .../aws/typescript/codegen/AddS3Config.java | 5 -- packages/middleware-sdk-s3/src/index.ts | 1 - .../src/use-regional-endpoint.spec.ts | 62 ------------------- .../src/use-regional-endpoint.ts | 52 ---------------- 6 files changed, 39 insertions(+), 123 deletions(-) delete mode 100644 packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts delete mode 100644 packages/middleware-sdk-s3/src/use-regional-endpoint.ts diff --git a/clients/client-s3/src/S3Client.ts b/clients/client-s3/src/S3Client.ts index e8aa84ae1486..15dd66285f9f 100644 --- a/clients/client-s3/src/S3Client.ts +++ b/clients/client-s3/src/S3Client.ts @@ -27,7 +27,7 @@ import { } from "@aws-sdk/middleware-host-header"; import { getLoggerPlugin } from "@aws-sdk/middleware-logger"; import { getRetryPlugin, resolveRetryConfig, RetryInputConfig, RetryResolvedConfig } from "@aws-sdk/middleware-retry"; -import { getUseRegionalEndpointPlugin, getValidateBucketNamePlugin } from "@aws-sdk/middleware-sdk-s3"; +import { getValidateBucketNamePlugin } from "@aws-sdk/middleware-sdk-s3"; import { AwsAuthInputConfig, AwsAuthResolvedConfig, @@ -739,7 +739,6 @@ export class S3Client extends __Client< this.middlewareStack.use(getLoggerPlugin(this.config)); this.middlewareStack.use(getAwsAuthPlugin(this.config)); this.middlewareStack.use(getValidateBucketNamePlugin(this.config)); - this.middlewareStack.use(getUseRegionalEndpointPlugin(this.config)); this.middlewareStack.use(getAddExpectContinuePlugin(this.config)); this.middlewareStack.use(getUserAgentPlugin(this.config)); } diff --git a/clients/client-s3/test/S3.spec.ts b/clients/client-s3/test/S3.spec.ts index 4bbcc8b0303a..62ddb76fb74d 100644 --- a/clients/client-s3/test/S3.spec.ts +++ b/clients/client-s3/test/S3.spec.ts @@ -1,6 +1,6 @@ /// import { HttpRequest } from "@aws-sdk/protocol-http"; -import { BuildMiddleware, SerializeMiddleware } from "@aws-sdk/types"; +import { BuildMiddleware, FinalizeRequestMiddleware, SerializeMiddleware } from "@aws-sdk/types"; import chai from "chai"; import chaiAsPromised from "chai-as-promised"; import { PassThrough } from "stream"; @@ -171,3 +171,40 @@ describe("Throw 200 response", () => { ); }); }); + +describe("regional endpoints", () => { + const endpointValidator: FinalizeRequestMiddleware = (next, context) => (args) => { + // middleware intercept the request and return it early + const request = args.request as HttpRequest; + return Promise.resolve({ + output: { + $metadata: { attempts: 0, httpStatusCode: 200 }, + request, + context, + } as any, + response: {} as any, + }); + }; + + it("should use regional endpoints if region is us-east-1", async () => { + const client = new S3({ region: "us-east-1" }); + client.middlewareStack.add(endpointValidator, { step: "finalizeRequest", priority: "low" }); + const result: any = await client.putObject({ + Bucket: "bucket", + Key: "key", + Body: "body", + }); + expect(result.request.hostname).to.eql("bucket.s3.us-east-1.amazonaws.com"); + }); + + it("should use global endpoints if region is aws-global", async () => { + const client = new S3({ region: "aws-global" }); + client.middlewareStack.add(endpointValidator, { step: "finalizeRequest", priority: "low" }); + const result: any = await client.putObject({ + Bucket: "bucket", + Key: "key", + Body: "body", + }); + expect(result.request.hostname).to.eql("bucket.s3.amazonaws.com"); + }); +}); diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddS3Config.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddS3Config.java index dd6e85a0bac3..5099d0927fc8 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddS3Config.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddS3Config.java @@ -165,11 +165,6 @@ public List getClientPlugins() { HAS_MIDDLEWARE) .operationPredicate((m, s, o) -> testServiceId(s) && o.getId().getName(s).equals("PutObject")) .build(), - RuntimeClientPlugin.builder() - .withConventions(AwsDependency.S3_MIDDLEWARE.dependency, "UseRegionalEndpoint", - HAS_MIDDLEWARE) - .servicePredicate((m, s) -> testServiceId(s)) - .build(), RuntimeClientPlugin.builder() .withConventions(AwsDependency.S3_MIDDLEWARE.dependency, "throw200Exceptions", HAS_MIDDLEWARE) diff --git a/packages/middleware-sdk-s3/src/index.ts b/packages/middleware-sdk-s3/src/index.ts index 818cf25fbb39..7bb22f7e8302 100644 --- a/packages/middleware-sdk-s3/src/index.ts +++ b/packages/middleware-sdk-s3/src/index.ts @@ -1,4 +1,3 @@ export * from "./check-content-length-header"; export * from "./throw-200-exceptions"; -export * from "./use-regional-endpoint"; export * from "./validate-bucket-name"; diff --git a/packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts b/packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts deleted file mode 100644 index e35f7961e3a3..000000000000 --- a/packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { HttpRequest } from "@aws-sdk/protocol-http"; - -import { useRegionalEndpointMiddleware } from "./use-regional-endpoint"; - -describe("useRegionalEndpointMiddleware", () => { - const mockNextHandler = jest.fn(); - beforeEach(() => { - jest.clearAllMocks(); - }); - - it("should accept any endpoint if set by customer", async () => { - const config = { isCustomEndpoint: true, region: async () => "foo-region" }; - const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); - await handler({ - input: {}, - request: new HttpRequest({ - hostname: "s3.us-east-1.amazonaws.com", - }), - }); - expect(mockNextHandler.mock.calls.length).toBe(1); - expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-east-1.amazonaws.com"); - }); - - it("should modify the hostname if it's global endpoint", async () => { - const config = { isCustomEndpoint: false, region: async () => "foo-region" }; - const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); - await handler({ - input: {}, - request: new HttpRequest({ - hostname: "s3.amazonaws.com", - }), - }); - expect(mockNextHandler.mock.calls.length).toBe(1); - expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-east-1.amazonaws.com"); - }); - - it("should not modify the hostname if it's regional endpoint", async () => { - const config = { isCustomEndpoint: false, region: async () => "foo-region" }; - const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); - await handler({ - input: {}, - request: new HttpRequest({ - hostname: "s3.us-west-2.amazonaws.com", - }), - }); - expect(mockNextHandler.mock.calls.length).toBe(1); - expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-west-2.amazonaws.com"); - }); - - it("should use global endpoint if region is set to 'aws-global'", async () => { - const config = { isCustomEndpoint: false, region: async () => "aws-global" }; - const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); - await handler({ - input: {}, - request: new HttpRequest({ - hostname: "s3.aws-global.amazonaws.com", - }), - }); - expect(mockNextHandler.mock.calls.length).toBe(1); - expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.amazonaws.com"); - }); -}); diff --git a/packages/middleware-sdk-s3/src/use-regional-endpoint.ts b/packages/middleware-sdk-s3/src/use-regional-endpoint.ts deleted file mode 100644 index 0448e0d3ae83..000000000000 --- a/packages/middleware-sdk-s3/src/use-regional-endpoint.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { HttpRequest } from "@aws-sdk/protocol-http"; -import { - BuildHandler, - BuildHandlerArguments, - BuildHandlerOptions, - BuildHandlerOutput, - BuildMiddleware, - MetadataBearer, - Pluggable, - Provider, -} from "@aws-sdk/types"; - -type PreviouslyResolved = { - region: Provider; - isCustomEndpoint: boolean; -}; - -/** - * @internal - */ -export const useRegionalEndpointMiddleware = - (config: PreviouslyResolved): BuildMiddleware => - (next: BuildHandler): BuildHandler => - async (args: BuildHandlerArguments): Promise> => { - const { request } = args; - if (!HttpRequest.isInstance(request) || config.isCustomEndpoint) return next({ ...args }); - if (request.hostname === "s3.amazonaws.com") { - request.hostname = "s3.us-east-1.amazonaws.com"; - } else if ("aws-global" === (await config.region())) { - request.hostname = "s3.amazonaws.com"; - } - return next({ ...args }); - }; - -/** - * @internal - */ -export const useRegionalEndpointMiddlewareOptions: BuildHandlerOptions = { - step: "build", - tags: ["USE_REGIONAL_ENDPOINT", "S3"], - name: "useRegionalEndpointMiddleware", - override: true, -}; - -/** - * @internal - */ -export const getUseRegionalEndpointPlugin = (config: PreviouslyResolved): Pluggable => ({ - applyToStack: (clientStack) => { - clientStack.add(useRegionalEndpointMiddleware(config), useRegionalEndpointMiddlewareOptions); - }, -});