|
| 1 | +import { fromEnv } from "@aws-sdk/credential-provider-env"; |
| 2 | +import { fromIni } from "@aws-sdk/credential-provider-ini"; |
| 3 | +import { remoteProvider } from "@aws-sdk/credential-provider-node/src/remoteProvider"; |
| 4 | +import { fromProcess } from "@aws-sdk/credential-provider-process"; |
| 5 | +import { fromSSO } from "@aws-sdk/credential-provider-sso"; |
| 6 | +import { fromTokenFile } from "@aws-sdk/credential-provider-web-identity"; |
| 7 | +import { CredentialsProviderError } from "@smithy/property-provider"; |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 9 | + |
| 10 | +import { fromAwsCliV2CompatibleProviderChain } from "./fromAwsCliV2CompatibleProviderChain"; |
| 11 | + |
| 12 | +vi.mock("@aws-sdk/credential-provider-env"); |
| 13 | +vi.mock("@aws-sdk/credential-provider-ini"); |
| 14 | +vi.mock("@aws-sdk/credential-provider-process"); |
| 15 | +vi.mock("@aws-sdk/credential-provider-sso"); |
| 16 | +vi.mock("@aws-sdk/credential-provider-web-identity"); |
| 17 | +vi.mock("@aws-sdk/credential-provider-node/src/remoteProvider"); |
| 18 | + |
| 19 | +describe("fromAwsCliV2CompatibleProviderChain", () => { |
| 20 | + const mockCreds = { |
| 21 | + accessKeyId: "mockAccessKeyId", |
| 22 | + secretAccessKey: "mockSecretAccessKey", |
| 23 | + }; |
| 24 | + |
| 25 | + const mockInit = { |
| 26 | + profile: "mockProfile", |
| 27 | + logger: { |
| 28 | + debug: vi.fn(), |
| 29 | + info: vi.fn(), |
| 30 | + warn: vi.fn(), |
| 31 | + error: vi.fn(), |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + const credentials = () => { |
| 36 | + throw new CredentialsProviderError("test", true); |
| 37 | + }; |
| 38 | + |
| 39 | + const finalCredentials = () => { |
| 40 | + return mockCreds; |
| 41 | + }; |
| 42 | + |
| 43 | + const mockEnvFn = vi.fn().mockImplementation(() => credentials()); |
| 44 | + const mockSsoFn = vi.fn().mockImplementation(() => credentials()); |
| 45 | + const mockIniFn = vi.fn().mockImplementation(() => credentials()); |
| 46 | + const mockProcessFn = vi.fn().mockImplementation(() => credentials()); |
| 47 | + const mockTokenFileFn = vi.fn().mockImplementation(() => credentials()); |
| 48 | + const mockRemoteProviderFn = vi.fn().mockImplementation(() => finalCredentials()); |
| 49 | + |
| 50 | + const ORIGINAL_ENV = { ...process.env }; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + process.env = { ...ORIGINAL_ENV }; |
| 54 | + delete process.env.AWS_PROFILE; |
| 55 | + delete process.env.AWS_ACCESS_KEY_ID; |
| 56 | + delete process.env.AWS_SECRET_ACCESS_KEY; |
| 57 | + |
| 58 | + vi.mocked(fromEnv).mockReturnValue(mockEnvFn); |
| 59 | + vi.mocked(fromSSO).mockReturnValue(mockSsoFn); |
| 60 | + vi.mocked(fromIni).mockReturnValue(mockIniFn); |
| 61 | + vi.mocked(fromProcess).mockReturnValue(mockProcessFn); |
| 62 | + vi.mocked(fromTokenFile).mockReturnValue(mockTokenFileFn); |
| 63 | + vi.mocked(remoteProvider).mockReturnValue(mockRemoteProviderFn); |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + vi.clearAllMocks(); |
| 68 | + process.env = ORIGINAL_ENV; |
| 69 | + }); |
| 70 | + |
| 71 | + describe("when explicit credentials are provided", () => { |
| 72 | + it("should return the provided credentials", async () => { |
| 73 | + const credentials = await fromAwsCliV2CompatibleProviderChain({ |
| 74 | + accessKeyId: "explicitAccessKey", |
| 75 | + secretAccessKey: "explicitSecretKey", |
| 76 | + })(); |
| 77 | + |
| 78 | + expect(credentials).toEqual({ |
| 79 | + accessKeyId: "explicitAccessKey", |
| 80 | + secretAccessKey: "explicitSecretKey", |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("when profile is provided", () => { |
| 86 | + it("should use fromIni to resolve credentials", async () => { |
| 87 | + await fromAwsCliV2CompatibleProviderChain({ profile: "mockProfile" })(); |
| 88 | + expect(fromIni).toHaveBeenCalledWith({ profile: "mockProfile" }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("credential provider chain resolution", () => { |
| 93 | + it("should call providers in correct order and return valid credentials", async () => { |
| 94 | + const provider = fromAwsCliV2CompatibleProviderChain(mockInit); |
| 95 | + |
| 96 | + const receivedCreds = await provider(); |
| 97 | + expect(receivedCreds).toEqual(mockCreds); |
| 98 | + |
| 99 | + expect(fromEnv).toHaveBeenCalled(); |
| 100 | + expect(fromSSO).toHaveBeenCalled(); |
| 101 | + expect(fromIni).toHaveBeenCalledWith(mockInit); |
| 102 | + expect(fromProcess).toHaveBeenCalledWith(mockInit); |
| 103 | + expect(fromTokenFile).toHaveBeenCalledWith(mockInit); |
| 104 | + expect(remoteProvider).toHaveBeenCalledWith(mockInit); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should skip fromEnv when profile is explicitly provided", async () => { |
| 108 | + await fromAwsCliV2CompatibleProviderChain({ profile: "mockProfile" })(); |
| 109 | + expect(fromEnv).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should throw an error if no credentials are found", async () => { |
| 113 | + vi.mocked(fromEnv).mockImplementation(() => credentials()); |
| 114 | + vi.mocked(fromSSO).mockImplementation(() => credentials()); |
| 115 | + vi.mocked(fromIni).mockImplementation(() => credentials()); |
| 116 | + vi.mocked(fromProcess).mockImplementation(() => credentials()); |
| 117 | + vi.mocked(fromTokenFile).mockImplementation(() => credentials()); |
| 118 | + vi.mocked(remoteProvider).mockImplementation(() => credentials()); |
| 119 | + |
| 120 | + const provider = fromAwsCliV2CompatibleProviderChain(mockInit); |
| 121 | + await expect(provider()).rejects.toThrow("Could not load credentials from any providers"); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("when AWS_PROFILE is set in the environment", () => { |
| 126 | + it("should use fromIni with the environment profile", async () => { |
| 127 | + process.env.AWS_PROFILE = "envProfile"; |
| 128 | + |
| 129 | + await fromAwsCliV2CompatibleProviderChain({})(); |
| 130 | + |
| 131 | + expect(fromIni).toHaveBeenCalledWith({ profile: "envProfile" }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe("when credentials are expired or near expiration", () => { |
| 136 | + it("should detect expired credentials", () => { |
| 137 | + const expiration = new Date(Date.now() - 24 * 60 * 60 * 1000); // Expired |
| 138 | + expect(credentialsTreatedAsExpired({ ...mockCreds, expiration })).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should detect credentials that expire in less than 5 minutes", () => { |
| 142 | + const expiration = new Date(Date.now() + 299 * 1000); |
| 143 | + expect(credentialsTreatedAsExpired({ ...mockCreds, expiration })).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should not mark credentials as expired if expiration is more than 5 minutes away", () => { |
| 147 | + const expiration = new Date(Date.now() + 301 * 1000); |
| 148 | + expect(credentialsTreatedAsExpired({ ...mockCreds, expiration })).toBe(false); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should assume credentials need refresh if expiration is not set", () => { |
| 152 | + expect(credentialsWillNeedRefresh({ ...mockCreds })).toBe(true); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments