Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware-sdk-ec2): use hostname from regionInfoProvider #3673

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
11 changes: 10 additions & 1 deletion packages/middleware-sdk-ec2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MetadataBearer,
Pluggable,
Provider,
RegionInfoProvider,
} from "@aws-sdk/types";
import { formatUrl } from "@aws-sdk/util-format-url";

Expand All @@ -22,6 +23,7 @@ interface PreviouslyResolved {
region: Provider<string>;
sha256: HashConstructor;
signingEscapePath: boolean;
regionInfoProvider?: RegionInfoProvider;
}

const version = "2016-11-15";
Expand All @@ -34,7 +36,14 @@ export function copySnapshotPresignedUrlMiddleware(options: PreviouslyResolved):
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