Skip to content

Commit 6eb0c29

Browse files
authored
Merge pull request #24 from jeskew/feature/credential-provider-env
Feature/credential provider env
2 parents 5a2a01e + 6fb34c8 commit 6eb0c29

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {CredentialError} from "@aws/credential-provider-base";
2+
import {
3+
ENV_KEY,
4+
ENV_SECRET,
5+
ENV_SESSION,
6+
ENV_EXPIRATION,
7+
fromEnv,
8+
} from "../";
9+
10+
const akid = process.env[ENV_KEY];
11+
const secret = process.env[ENV_SECRET];
12+
const token = process.env[ENV_SESSION];
13+
const expiry = process.env[ENV_EXPIRATION];
14+
15+
beforeEach(() => {
16+
delete process.env[ENV_KEY];
17+
delete process.env[ENV_SECRET];
18+
delete process.env[ENV_SESSION];
19+
delete process.env[ENV_EXPIRATION];
20+
});
21+
22+
afterAll(() => {
23+
process.env[ENV_KEY] = akid;
24+
process.env[ENV_SECRET] = secret;
25+
process.env[ENV_SESSION] = token;
26+
process.env[ENV_EXPIRATION] = expiry;
27+
});
28+
29+
describe('fromEnv', () => {
30+
it('should read credentials from known environment variables', async () => {
31+
process.env[ENV_KEY] = 'foo';
32+
process.env[ENV_SECRET] = 'bar';
33+
process.env[ENV_SESSION] = 'baz';
34+
process.env[ENV_EXPIRATION] = '1970-01-01T07:00:00Z';
35+
36+
expect(await fromEnv()()).toEqual({
37+
accessKeyId: 'foo',
38+
secretAccessKey: 'bar',
39+
sessionToken: 'baz',
40+
expiration: 25200,
41+
});
42+
});
43+
44+
it('can create credentials without a session token or expiration', async () => {
45+
process.env[ENV_KEY] = 'foo';
46+
process.env[ENV_SECRET] = 'bar';
47+
48+
expect(await fromEnv()()).toEqual({
49+
accessKeyId: 'foo',
50+
secretAccessKey: 'bar',
51+
});
52+
});
53+
54+
it(
55+
'should reject the promise if no environmental credentials can be found',
56+
async () => {
57+
await fromEnv()().then(
58+
() => { throw new Error('The promise should have been rejected.'); },
59+
() => { /* Promise rejected as expected */ }
60+
);
61+
}
62+
);
63+
64+
it('should flag a lack of credentials as a non-terminal error', async () => {
65+
await fromEnv()().then(
66+
() => { throw new Error('The promise should have been rejected.'); },
67+
err => {
68+
expect((err as CredentialError).tryNextLink).toBe(true);
69+
}
70+
);
71+
});
72+
});
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(new CredentialError(
30+
'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)