Skip to content

Commit

Permalink
feat(credential-provider-env): refactor into modular components (#3293)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Feb 14, 2022
1 parent 3485cf0 commit e0697ed
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 96 deletions.
56 changes: 56 additions & 0 deletions packages/credential-provider-env/src/fromEnv.spec.ts
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);
}
});
});
30 changes: 30 additions & 0 deletions packages/credential-provider-env/src/fromEnv.ts
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.");
};
66 changes: 0 additions & 66 deletions packages/credential-provider-env/src/index.spec.ts

This file was deleted.

31 changes: 1 addition & 30 deletions packages/credential-provider-env/src/index.ts
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";

0 comments on commit e0697ed

Please sign in to comment.