Skip to content

Commit

Permalink
fix(middleware-sdk-ec2): use hostname from regionInfoProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 8, 2022
1 parent 9a97d29 commit 4e98582
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/middleware-sdk-ec2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@aws-sdk/signature-v4": "*",
"@aws-sdk/types": "*",
"@aws-sdk/util-format-url": "*",
"@aws-sdk/client-ec2": "*",
"tslib": "^2.3.1"
},
"engines": {
Expand Down
8 changes: 6 additions & 2 deletions packages/middleware-sdk-ec2/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const handler = copySnapshotPresignedUrlMiddleware({
region,
sha256: MockSha256,
signingEscapePath: true,
})(nextHandler, {} as any);
regionInfoProvider: async (...args) => ({
hostname: "ec2.src-region.test-host.com",
}),
} as any)(nextHandler, {} as any);

describe("middleware-sdk-ec2", () => {
beforeEach(() => {
Expand All @@ -27,7 +30,8 @@ describe("middleware-sdk-ec2", () => {
expect(middlewareOutput.input.SourceSnapshotId).toEqual(params.SourceSnapshotId);
expect(middlewareOutput.input.DestinationRegion).toEqual(await region());
const presignedUrl = middlewareOutput.input.PresignedUrl;
expect(presignedUrl).toMatch(/https\:\/\/ec2.src-region.amazonaws.com\/\?/);
expect(presignedUrl).not.toMatch(/https\:\/\/ec2.src-region.amazonaws.com\/\?/);
expect(presignedUrl).toMatch(/https\:\/\/ec2.src-region.test-host.com\/\?/);
expect(presignedUrl).toMatch(/Action\=CopySnapshot/);
expect(presignedUrl).toMatch(/Version\=2016\-11\-15/);
expect(presignedUrl).toMatch(
Expand Down
27 changes: 11 additions & 16 deletions packages/middleware-sdk-ec2/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
import { EC2ClientResolvedConfig } from "@aws-sdk/client-ec2";
import { HttpRequest } from "@aws-sdk/protocol-http";
import { SignatureV4 } from "@aws-sdk/signature-v4";
import {
Credentials,
Endpoint,
HashConstructor,
InitializeHandler,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
InitializeMiddleware,
MemoizedProvider,
MetadataBearer,
Pluggable,
Provider,
} from "@aws-sdk/types";
import { formatUrl } from "@aws-sdk/util-format-url";

interface PreviouslyResolved {
credentials: MemoizedProvider<Credentials>;
endpoint: Provider<Endpoint>;
region: Provider<string>;
sha256: HashConstructor;
signingEscapePath: boolean;
}

const version = "2016-11-15";

//an initialize middleware to add PresignUrl to input
export function copySnapshotPresignedUrlMiddleware(options: PreviouslyResolved): InitializeMiddleware<any, any> {
export function copySnapshotPresignedUrlMiddleware(options: EC2ClientResolvedConfig): InitializeMiddleware<any, any> {
return <Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
const { input } = args;
if (!input.PresignedUrl) {
const region = await options.region();
const resolvedEndpoint = await options.endpoint();
resolvedEndpoint.hostname = `ec2.${input.SourceRegion}.amazonaws.com`;

if (typeof options.regionInfoProvider === "function") {
const regionInfo = await options.regionInfoProvider(input.SourceRegion);
resolvedEndpoint.hostname = regionInfo?.hostname || `ec2.${input.SourceRegion}.amazonaws.com`;
} else {
resolvedEndpoint.hostname = `ec2.${input.SourceRegion}.amazonaws.com`;
}

const request = new HttpRequest({
...resolvedEndpoint,
protocol: "https",
Expand Down Expand Up @@ -81,7 +76,7 @@ export const copySnapshotPresignedUrlMiddlewareOptions: InitializeHandlerOptions
override: true,
};

export const getCopySnapshotPresignedUrlPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({
export const getCopySnapshotPresignedUrlPlugin = (config: EC2ClientResolvedConfig): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.add(copySnapshotPresignedUrlMiddleware(config), copySnapshotPresignedUrlMiddlewareOptions);
},
Expand Down

0 comments on commit 4e98582

Please sign in to comment.