-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(credential-provider-env): refactor into modular components (#3293)
- Loading branch information
Showing
4 changed files
with
87 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { CredentialsProviderError } from "@aws-sdk/property-provider"; | ||
|
||
import { ENV_EXPIRATION, ENV_KEY, ENV_SECRET, ENV_SESSION, fromEnv } from "./fromEnv"; | ||
|
||
describe(fromEnv.name, () => { | ||
const ORIGINAL_ENV = process.env; | ||
const mockAccessKeyId = "mockAccessKeyId"; | ||
const mockSecretAccessKey = "mockSecretAccessKey"; | ||
const mockSessionToken = "mockSessionToken"; | ||
const mockExpiration = new Date().toISOString(); | ||
|
||
beforeEach(() => { | ||
process.env = { | ||
...ORIGINAL_ENV, | ||
[ENV_KEY]: mockAccessKeyId, | ||
[ENV_SECRET]: mockSecretAccessKey, | ||
[ENV_SESSION]: mockSessionToken, | ||
[ENV_EXPIRATION]: mockExpiration, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = ORIGINAL_ENV; | ||
}); | ||
|
||
it("should read credentials from known environment variables", async () => { | ||
const receivedCreds = await fromEnv()(); | ||
expect(receivedCreds).toStrictEqual({ | ||
accessKeyId: mockAccessKeyId, | ||
secretAccessKey: mockSecretAccessKey, | ||
sessionToken: mockSessionToken, | ||
expiration: new Date(mockExpiration), | ||
}); | ||
}); | ||
|
||
it("can create credentials without a session token or expiration", async () => { | ||
delete process.env[ENV_SESSION]; | ||
delete process.env[ENV_EXPIRATION]; | ||
const receivedCreds = await fromEnv()(); | ||
expect(receivedCreds).toStrictEqual({ | ||
accessKeyId: mockAccessKeyId, | ||
secretAccessKey: mockSecretAccessKey, | ||
}); | ||
}); | ||
|
||
it.each([ENV_KEY, ENV_SECRET])("throws if env['%s'] is not found", async (key) => { | ||
delete process.env[key]; | ||
const expectedError = new CredentialsProviderError("Unable to find environment variable credentials."); | ||
try { | ||
await fromEnv()(); | ||
fail(`expected ${expectedError}`); | ||
} catch (error) { | ||
expect(error).toStrictEqual(expectedError); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CredentialsProviderError } from "@aws-sdk/property-provider"; | ||
import { CredentialProvider } from "@aws-sdk/types"; | ||
|
||
export const ENV_KEY = "AWS_ACCESS_KEY_ID"; | ||
export const ENV_SECRET = "AWS_SECRET_ACCESS_KEY"; | ||
export const ENV_SESSION = "AWS_SESSION_TOKEN"; | ||
export const ENV_EXPIRATION = "AWS_CREDENTIAL_EXPIRATION"; | ||
|
||
/** | ||
* Source AWS credentials from known environment variables. If either the | ||
* `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variable is not | ||
* set in this process, the provider will return a rejected promise. | ||
*/ | ||
export const fromEnv = (): CredentialProvider => async () => { | ||
const accessKeyId: string | undefined = process.env[ENV_KEY]; | ||
const secretAccessKey: string | undefined = process.env[ENV_SECRET]; | ||
const sessionToken: string | undefined = process.env[ENV_SESSION]; | ||
const expiry: string | undefined = process.env[ENV_EXPIRATION]; | ||
|
||
if (accessKeyId && secretAccessKey) { | ||
return { | ||
accessKeyId, | ||
secretAccessKey, | ||
...(sessionToken && { sessionToken }), | ||
...(expiry && { expiration: new Date(expiry) }), | ||
}; | ||
} | ||
|
||
throw new CredentialsProviderError("Unable to find environment variable credentials."); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1 @@ | ||
import { CredentialsProviderError } from "@aws-sdk/property-provider"; | ||
import { CredentialProvider } from "@aws-sdk/types"; | ||
|
||
export const ENV_KEY = "AWS_ACCESS_KEY_ID"; | ||
export const ENV_SECRET = "AWS_SECRET_ACCESS_KEY"; | ||
export const ENV_SESSION = "AWS_SESSION_TOKEN"; | ||
export const ENV_EXPIRATION = "AWS_CREDENTIAL_EXPIRATION"; | ||
|
||
/** | ||
* Source AWS credentials from known environment variables. If either the | ||
* `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variable is not | ||
* set in this process, the provider will return a rejected promise. | ||
*/ | ||
export function fromEnv(): CredentialProvider { | ||
return () => { | ||
const accessKeyId: string | undefined = process.env[ENV_KEY]; | ||
const secretAccessKey: string | undefined = process.env[ENV_SECRET]; | ||
const expiry: string | undefined = process.env[ENV_EXPIRATION]; | ||
if (accessKeyId && secretAccessKey) { | ||
return Promise.resolve({ | ||
accessKeyId, | ||
secretAccessKey, | ||
sessionToken: process.env[ENV_SESSION], | ||
expiration: expiry ? new Date(expiry) : undefined, | ||
}); | ||
} | ||
|
||
return Promise.reject(new CredentialsProviderError("Unable to find environment variable credentials.")); | ||
}; | ||
} | ||
export * from "./fromEnv"; |