|
| 1 | +import { FinalizeRequestMiddleware } from "@smithy/types"; |
| 2 | +import { describe, expect, test as it } from "vitest"; |
| 3 | + |
| 4 | +import { S3 } from "@aws-sdk/client-s3"; |
| 5 | +import "@aws-sdk/signature-v4-crt"; |
| 6 | + |
| 7 | +describe("authSchemePreference", () => { |
| 8 | + const credentials = { accessKeyId: "key", secretAccessKey: "secret" }; |
| 9 | + const Bucket = `arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-01234567890123456/accesspoint/abc-111`; |
| 10 | + const Key = "key"; |
| 11 | + const Body = "body"; |
| 12 | + |
| 13 | + const SIGNATURE_PREFIX: Record<string, string> = { |
| 14 | + sigv4: "AWS4-HMAC-SHA256", |
| 15 | + sigv4a: "AWS4-ECDSA-P256-SHA256", |
| 16 | + }; |
| 17 | + |
| 18 | + const interceptorMiddleware: FinalizeRequestMiddleware<any, any> = (next, context) => (args) => { |
| 19 | + // middleware intercept the request and return it early |
| 20 | + const request = args.request; |
| 21 | + return Promise.resolve({ |
| 22 | + output: { $metadata: { attempts: 0, httpStatusCode: 200 }, request, context }, |
| 23 | + response: {}, |
| 24 | + }); |
| 25 | + }; |
| 26 | + |
| 27 | + const getAuthorizationHeader = (headers: Record<string, string>) => |
| 28 | + headers["authorization"] || headers["Authorization"]; |
| 29 | + |
| 30 | + it.each([ |
| 31 | + ["sigv4a", undefined], |
| 32 | + ["sigv4a", ["sigv3"]], |
| 33 | + ["sigv4", ["sigv4"]], |
| 34 | + ["sigv4", ["sigv4", "sigv4a"]], |
| 35 | + ["sigv4a", ["sigv4a"]], |
| 36 | + ["sigv4a", ["sigv4a", "sigv4"]], |
| 37 | + ])("selects '%s' when authSchemePreference: %s", async (output, authSchemePreference) => { |
| 38 | + const client = new S3({ credentials, authSchemePreference }); |
| 39 | + client.middlewareStack.add(interceptorMiddleware, { step: "finalizeRequest", priority: "low" }); |
| 40 | + const result: any = await client.putObject({ Bucket, Key, Body }); |
| 41 | + |
| 42 | + const authorizationHeader = getAuthorizationHeader(result.request.headers); |
| 43 | + expect(authorizationHeader.startsWith(SIGNATURE_PREFIX[output])).toBe(true); |
| 44 | + }); |
| 45 | +}); |
0 commit comments