Skip to content

Commit

Permalink
fix(middleware-sdk-ec2): use hostname from regionInfoProvider (#3673)
Browse files Browse the repository at this point in the history
* fix(middleware-sdk-ec2): use hostname from regionInfoProvider

* fix(middleware-sdk-ec2): remove circular dep on client-ec2

* fix(middleware-sdk-ec2): restore interface order
  • Loading branch information
kuhe committed Jun 8, 2022
1 parent 543a0ce commit 5103554
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 5103554

Please sign in to comment.