Skip to content

Commit dcfef12

Browse files
committed
Add support for the AWS_CREDENTIAL_EXPIRATION environment variable
1 parent 1865767 commit dcfef12

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

packages/credential-provider-env/__tests__/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
import { CredentialError } from "@aws/credential-provider-base";
2-
import { ENV_KEY, ENV_SECRET, ENV_SESSION, fromEnv } from "../";
2+
import { ENV_KEY, ENV_SECRET, ENV_SESSION, ENV_EXPIRATION, fromEnv } from "../";
33

44
const akid = process.env[ENV_KEY];
55
const secret = process.env[ENV_SECRET];
66
const token = process.env[ENV_SESSION];
7+
const expiry = process.env[ENV_EXPIRATION];
78

89
beforeEach(() => {
910
delete process.env[ENV_KEY];
1011
delete process.env[ENV_SECRET];
1112
delete process.env[ENV_SESSION];
13+
delete process.env[ENV_EXPIRATION];
1214
});
1315

1416
afterAll(() => {
1517
process.env[ENV_KEY] = akid;
1618
process.env[ENV_SECRET] = secret;
1719
process.env[ENV_SESSION] = token;
20+
process.env[ENV_EXPIRATION] = expiry;
1821
});
1922

2023
describe("fromEnv", () => {
2124
it("should read credentials from known environment variables", async () => {
2225
process.env[ENV_KEY] = "foo";
2326
process.env[ENV_SECRET] = "bar";
2427
process.env[ENV_SESSION] = "baz";
28+
process.env[ENV_EXPIRATION] = "1970-01-01T07:00:00Z";
2529

2630
expect(await fromEnv()()).toEqual({
2731
accessKeyId: "foo",
2832
secretAccessKey: "bar",
29-
sessionToken: "baz"
33+
sessionToken: "baz",
34+
expiration: 25200
3035
});
3136
});
3237

33-
it("can create credentials without a session token", async () => {
38+
it("can create credentials without a session token or expiration", async () => {
3439
process.env[ENV_KEY] = "foo";
3540
process.env[ENV_SECRET] = "bar";
3641

3742
expect(await fromEnv()()).toEqual({
3843
accessKeyId: "foo",
39-
secretAccessKey: "bar",
40-
sessionToken: void 0
44+
secretAccessKey: "bar"
4145
});
4246
});
4347

packages/credential-provider-env/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { CredentialProvider } from "@aws/types";
22
import { CredentialError } from "@aws/credential-provider-base";
3+
import { epoch } from "@aws/protocol-timestamp";
34

45
export const ENV_KEY = "AWS_ACCESS_KEY_ID";
56
export const ENV_SECRET = "AWS_SECRET_ACCESS_KEY";
67
export const ENV_SESSION = "AWS_SESSION_TOKEN";
8+
export const ENV_EXPIRATION = "AWS_CREDENTIAL_EXPIRATION";
79

810
/**
911
* Source AWS credentials from known environment variables. If either the
@@ -14,11 +16,13 @@ export function fromEnv(): CredentialProvider {
1416
return () => {
1517
const accessKeyId: string = process.env[ENV_KEY];
1618
const secretAccessKey: string = process.env[ENV_SECRET];
19+
const expiry: string | undefined = process.env[ENV_EXPIRATION];
1720
if (accessKeyId && secretAccessKey) {
1821
return Promise.resolve({
1922
accessKeyId,
2023
secretAccessKey,
21-
sessionToken: process.env[ENV_SESSION]
24+
sessionToken: process.env[ENV_SESSION],
25+
expiration: expiry ? epoch(expiry) : undefined
2226
});
2327
}
2428

packages/credential-provider-env/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"license": "UNLICENSED",
1818
"dependencies": {
1919
"@aws/credential-provider-base": "^0.0.1",
20+
"@aws/protocol-timestamp": "^0.0.1",
2021
"@aws/types": "^0.0.1"
2122
},
2223
"devDependencies": {

0 commit comments

Comments
 (0)