Skip to content

Commit 6dddf93

Browse files
authored
Merge pull request #24 from jeskew/feature/credential-provider-env
Feature/credential provider env
2 parents ab15f1c + dcfef12 commit 6dddf93

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules/
2+
*.js
3+
*.js.map
4+
*.d.ts
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { CredentialError } from "@aws/credential-provider-base";
2+
import { ENV_KEY, ENV_SECRET, ENV_SESSION, ENV_EXPIRATION, fromEnv } from "../";
3+
4+
const akid = process.env[ENV_KEY];
5+
const secret = process.env[ENV_SECRET];
6+
const token = process.env[ENV_SESSION];
7+
const expiry = process.env[ENV_EXPIRATION];
8+
9+
beforeEach(() => {
10+
delete process.env[ENV_KEY];
11+
delete process.env[ENV_SECRET];
12+
delete process.env[ENV_SESSION];
13+
delete process.env[ENV_EXPIRATION];
14+
});
15+
16+
afterAll(() => {
17+
process.env[ENV_KEY] = akid;
18+
process.env[ENV_SECRET] = secret;
19+
process.env[ENV_SESSION] = token;
20+
process.env[ENV_EXPIRATION] = expiry;
21+
});
22+
23+
describe("fromEnv", () => {
24+
it("should read credentials from known environment variables", async () => {
25+
process.env[ENV_KEY] = "foo";
26+
process.env[ENV_SECRET] = "bar";
27+
process.env[ENV_SESSION] = "baz";
28+
process.env[ENV_EXPIRATION] = "1970-01-01T07:00:00Z";
29+
30+
expect(await fromEnv()()).toEqual({
31+
accessKeyId: "foo",
32+
secretAccessKey: "bar",
33+
sessionToken: "baz",
34+
expiration: 25200
35+
});
36+
});
37+
38+
it("can create credentials without a session token or expiration", async () => {
39+
process.env[ENV_KEY] = "foo";
40+
process.env[ENV_SECRET] = "bar";
41+
42+
expect(await fromEnv()()).toEqual({
43+
accessKeyId: "foo",
44+
secretAccessKey: "bar"
45+
});
46+
});
47+
48+
it("should reject the promise if no environmental credentials can be found", async () => {
49+
await fromEnv()().then(
50+
() => {
51+
throw new Error("The promise should have been rejected.");
52+
},
53+
() => {
54+
/* Promise rejected as expected */
55+
}
56+
);
57+
});
58+
59+
it("should flag a lack of credentials as a non-terminal error", async () => {
60+
await fromEnv()().then(
61+
() => {
62+
throw new Error("The promise should have been rejected.");
63+
},
64+
err => {
65+
expect((err as CredentialError).tryNextLink).toBe(true);
66+
}
67+
);
68+
});
69+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { CredentialProvider } from "@aws/types";
2+
import { CredentialError } from "@aws/credential-provider-base";
3+
import { epoch } from "@aws/protocol-timestamp";
4+
5+
export const ENV_KEY = "AWS_ACCESS_KEY_ID";
6+
export const ENV_SECRET = "AWS_SECRET_ACCESS_KEY";
7+
export const ENV_SESSION = "AWS_SESSION_TOKEN";
8+
export const ENV_EXPIRATION = "AWS_CREDENTIAL_EXPIRATION";
9+
10+
/**
11+
* Source AWS credentials from known environment variables. If either the
12+
* `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variable is not
13+
* set in this process, the provider will return a rejected promise.
14+
*/
15+
export function fromEnv(): CredentialProvider {
16+
return () => {
17+
const accessKeyId: string = process.env[ENV_KEY];
18+
const secretAccessKey: string = process.env[ENV_SECRET];
19+
const expiry: string | undefined = process.env[ENV_EXPIRATION];
20+
if (accessKeyId && secretAccessKey) {
21+
return Promise.resolve({
22+
accessKeyId,
23+
secretAccessKey,
24+
sessionToken: process.env[ENV_SESSION],
25+
expiration: expiry ? epoch(expiry) : undefined
26+
});
27+
}
28+
29+
return Promise.reject(
30+
new CredentialError("Unable to find environment variable credentials.")
31+
);
32+
};
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@aws/credential-provider-env",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "AWS credential provider that sources credentials from known environment variables",
6+
"main": "index.js",
7+
"scripts": {
8+
"prepublishOnly": "tsc",
9+
"pretest": "tsc",
10+
"test": "jest"
11+
},
12+
"keywords": [
13+
"aws",
14+
"credentials"
15+
],
16+
"author": "[email protected]",
17+
"license": "UNLICENSED",
18+
"dependencies": {
19+
"@aws/credential-provider-base": "^0.0.1",
20+
"@aws/protocol-timestamp": "^0.0.1",
21+
"@aws/types": "^0.0.1"
22+
},
23+
"devDependencies": {
24+
"@types/jest": "^19.2.2",
25+
"@types/node": "^7.0.12",
26+
"jest": "^19.0.2",
27+
"typescript": "^2.3"
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"declaration": true,
6+
"strict": true,
7+
"sourceMap": true,
8+
"lib": [
9+
"es5",
10+
"es2015.promise"
11+
]
12+
}
13+
}

0 commit comments

Comments
 (0)