Skip to content

Commit 04df491

Browse files
RanVakninRan Vakninkuhe
authoredJan 31, 2024
fix(s3-request-presigner): identify correct authscheme for mrap (#5742)
* fix(s3-request-presigner): adjust signing region based on authScheme for sigv4a * fix(s3-request-presigner): identify correct authscheme for mrap * fix(s3-request-presigner): small refactor * chore: remove unused import --------- Co-authored-by: Ran Vaknin <rvaknin@dev-dsk-rvaknin2-2a-e69e90c1.us-west-2.amazon.com> Co-authored-by: RanVaknin <RanVaknin@users.noreply.github.com> Co-authored-by: George Fu <kuhe@users.noreply.github.com>
1 parent d054fe1 commit 04df491

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
 

‎packages/s3-request-presigner/src/getSignedUrl.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ import { RequestPresigningArguments } from "@smithy/types";
2727

2828
import { getSignedUrl } from "./getSignedUrl";
2929

30+
jest.mock("@smithy/middleware-endpoint", () => {
31+
const originalModule = jest.requireActual("@smithy/middleware-endpoint");
32+
return {
33+
...originalModule,
34+
getEndpointFromInstructions: jest.fn(() =>
35+
Promise.resolve({
36+
properties: {
37+
authSchemes: [{ name: "sigv4a", signingRegionSet: ["*"] }],
38+
},
39+
})
40+
),
41+
};
42+
});
43+
3044
describe("getSignedUrl", () => {
3145
const clientParams = {
3246
region: "us-foo-1",
@@ -141,6 +155,23 @@ describe("getSignedUrl", () => {
141155
expect(mockPresign.mock.calls[0][0].headers[header]).toBeUndefined();
142156
}
143157
);
158+
it("should set region to * when sigv4a is the auth scheme", async () => {
159+
const mockPresigned = "a presigned url";
160+
mockPresign.mockReturnValue(mockPresigned);
161+
162+
const client = new S3Client(clientParams);
163+
const command = new GetObjectCommand({
164+
Bucket: "Bucket",
165+
Key: "Key",
166+
});
167+
168+
await getSignedUrl(client, command);
169+
const presignerArgs = mockPresigner.mock.calls[0][0];
170+
const region = await presignerArgs.region();
171+
172+
expect(region).toBe("*");
173+
expect(mockPresign).toBeCalled();
174+
});
144175

145176
// TODO(endpointsv2) fix this test
146177
it.skip("should presign request with MRAP ARN", async () => {

‎packages/s3-request-presigner/src/getSignedUrl.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@ export const getSignedUrl = async <
2626
): Promise<string> => {
2727
let s3Presigner: S3RequestPresigner;
2828

29+
let region: string | undefined;
2930
if (typeof client.config.endpointProvider === "function") {
3031
const endpointV2: EndpointV2 = await getEndpointFromInstructions(
3132
command.input as Record<string, unknown>,
3233
command.constructor as EndpointParameterInstructionsSupplier,
3334
client.config
3435
);
3536
const authScheme = endpointV2.properties?.authSchemes?.[0];
36-
37+
if (authScheme?.name === "sigv4a") {
38+
region = authScheme?.signingRegionSet?.join(",");
39+
} else {
40+
region = authScheme?.signingRegion;
41+
}
3742
s3Presigner = new S3RequestPresigner({
3843
...client.config,
3944
signingName: authScheme?.signingName,
40-
region: async () => authScheme?.signingRegion,
45+
region: async () => region,
4146
});
4247
} else {
4348
s3Presigner = new S3RequestPresigner(client.config);
@@ -58,7 +63,7 @@ export const getSignedUrl = async <
5863
let presigned: IHttpRequest;
5964
const presignerOptions = {
6065
...options,
61-
signingRegion: options.signingRegion ?? context["signing_region"],
66+
signingRegion: options.signingRegion ?? context["signing_region"] ?? region,
6267
signingService: options.signingService ?? context["signing_service"],
6368
};
6469

0 commit comments

Comments
 (0)
Please sign in to comment.