|
| 1 | +import { HttpResponse } from "@aws-sdk/protocol-http"; |
| 2 | +import { Readable } from "stream"; |
| 3 | +const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> |
| 4 | +<AssumeRoleResult> |
| 5 | + <AssumedRoleUser> |
| 6 | + <AssumedRoleId>AROAZOX2IL27GNRBJHWC2:session</AssumedRoleId> |
| 7 | + <Arn>arn:aws:sts::123:assumed-role/assume-role-test/session</Arn> |
| 8 | + </AssumedRoleUser> |
| 9 | + <Credentials> |
| 10 | + <AccessKeyId>key</AccessKeyId> |
| 11 | + <SecretAccessKey>secrete</SecretAccessKey> |
| 12 | + <SessionToken>session-token</SessionToken> |
| 13 | + <Expiration>2021-05-05T23:22:08Z</Expiration> |
| 14 | + </Credentials> |
| 15 | +</AssumeRoleResult> |
| 16 | +<ResponseMetadata> |
| 17 | + <RequestId>12345678id</RequestId> |
| 18 | +</ResponseMetadata> |
| 19 | +</AssumeRoleResponse>`; |
| 20 | +const mockHandle = jest.fn().mockResolvedValue({ |
| 21 | + response: new HttpResponse({ |
| 22 | + statusCode: 200, |
| 23 | + body: Readable.from([""]), |
| 24 | + }), |
| 25 | +}); |
| 26 | +jest.mock("@aws-sdk/node-http-handler", () => ({ |
| 27 | + NodeHttpHandler: jest.fn().mockImplementation(() => ({ |
| 28 | + destroy: () => {}, |
| 29 | + handle: mockHandle, |
| 30 | + })), |
| 31 | + streamCollector: async () => Buffer.from(assumeRoleResponse), |
| 32 | +})); |
| 33 | + |
| 34 | +import { getDefaultRoleAssumer } from "./defaultRoleAssumers"; |
| 35 | +import type { AssumeRoleCommandInput } from "./commands/AssumeRoleCommand"; |
| 36 | + |
| 37 | +describe("getDefaultRoleAssumer", () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }); |
| 41 | + it("should use supplied source credentials", async () => { |
| 42 | + const roleAssumer = getDefaultRoleAssumer(); |
| 43 | + const params: AssumeRoleCommandInput = { |
| 44 | + RoleArn: "arn:aws:foo", |
| 45 | + RoleSessionName: "session", |
| 46 | + }; |
| 47 | + const sourceCred1 = { accessKeyId: "key1", secretAccessKey: "secrete1" }; |
| 48 | + await roleAssumer(sourceCred1, params); |
| 49 | + expect(mockHandle).toBeCalledTimes(1); |
| 50 | + // Validate request is signed by sourceCred1 |
| 51 | + expect(mockHandle.mock.calls[0][0].headers?.authorization).toEqual( |
| 52 | + expect.stringContaining("AWS4-HMAC-SHA256 Credential=key1/") |
| 53 | + ); |
| 54 | + const sourceCred2 = { accessKeyId: "key2", secretAccessKey: "secrete1" }; |
| 55 | + await roleAssumer(sourceCred2, params); |
| 56 | + // Validate request is signed by sourceCred2 |
| 57 | + expect(mockHandle).toBeCalledTimes(2); |
| 58 | + expect(mockHandle.mock.calls[1][0].headers?.authorization).toEqual( |
| 59 | + expect.stringContaining("AWS4-HMAC-SHA256 Credential=key2/") |
| 60 | + ); |
| 61 | + }); |
| 62 | +}); |
0 commit comments