Skip to content

Commit 94219cb

Browse files
committed
Add a environment variable credential provider package
1 parent cebedea commit 94219cb

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {CredentialError} from "@aws/credential-provider-base";
2+
import {
3+
ENV_KEY,
4+
ENV_SECRET,
5+
ENV_SESSION,
6+
fromEnv,
7+
} from "../";
8+
9+
const akid = process.env[ENV_KEY];
10+
const secret = process.env[ENV_SECRET];
11+
const token = process.env[ENV_SESSION];
12+
13+
beforeEach(() => {
14+
delete process.env[ENV_KEY];
15+
delete process.env[ENV_SECRET];
16+
delete process.env[ENV_SESSION];
17+
});
18+
19+
afterAll(() => {
20+
process.env[ENV_KEY] = akid;
21+
process.env[ENV_SECRET] = secret;
22+
process.env[ENV_SESSION] = token;
23+
});
24+
25+
describe('fromEnv', () => {
26+
it('should read credentials from known environment variables', async () => {
27+
process.env[ENV_KEY] = 'foo';
28+
process.env[ENV_SECRET] = 'bar';
29+
process.env[ENV_SESSION] = 'baz';
30+
31+
expect(await fromEnv()()).toEqual({
32+
accessKeyId: 'foo',
33+
secretAccessKey: 'bar',
34+
sessionToken: 'baz',
35+
});
36+
});
37+
38+
it('can create credentials without a session token', 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+
sessionToken: void 0,
46+
});
47+
});
48+
49+
it(
50+
'should reject the promise if no environmental credentials can be found',
51+
async () => {
52+
await fromEnv()().then(
53+
() => { throw new Error('The promise should have been rejected.'); },
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+
() => { throw new Error('The promise should have been rejected.'); },
62+
err => {
63+
expect((err as CredentialError).tryNextLink).toBe(true);
64+
}
65+
);
66+
});
67+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {CredentialProvider} from "@aws/types";
2+
import {CredentialError} from "@aws/credential-provider-base";
3+
4+
export const ENV_KEY = 'AWS_ACCESS_KEY_ID';
5+
export const ENV_SECRET = 'AWS_SECRET_ACCESS_KEY';
6+
export const ENV_SESSION = 'AWS_SESSION_TOKEN';
7+
8+
/**
9+
* Source AWS credentials from known environment variables. If either the
10+
* `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variable is not
11+
* set in this process, the provider will return a rejected promise.
12+
*/
13+
export function fromEnv(): CredentialProvider {
14+
return () => {
15+
const accessKeyId: string = process.env[ENV_KEY];
16+
const secretAccessKey: string = process.env[ENV_SECRET];
17+
if (accessKeyId && secretAccessKey) {
18+
return Promise.resolve({
19+
accessKeyId,
20+
secretAccessKey,
21+
sessionToken: process.env[ENV_SESSION],
22+
});
23+
}
24+
25+
return Promise.reject(new CredentialError(
26+
'Unable to find environment variable credentials.'
27+
));
28+
};
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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/types": "^0.0.1"
21+
},
22+
"devDependencies": {
23+
"@types/jest": "^19.2.2",
24+
"@types/node": "^7.0.12",
25+
"jest": "^19.0.2",
26+
"typescript": "^2.3"
27+
}
28+
}
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)