environment_credentials.js
2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var AWS = require('../core');
/**
* Represents credentials from the environment.
*
* By default, this class will look for the matching environment variables
* prefixed by a given {envPrefix}. The un-prefixed environment variable names
* for each credential value is listed below:
*
* ```javascript
* accessKeyId: ACCESS_KEY_ID
* secretAccessKey: SECRET_ACCESS_KEY
* sessionToken: SESSION_TOKEN
* ```
*
* With the default prefix of 'AWS', the environment variables would be:
*
* AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
*
* @!attribute envPrefix
* @readonly
* @return [String] the prefix for the environment variable names excluding
* the separating underscore ('_').
*/
AWS.EnvironmentCredentials = AWS.util.inherit(AWS.Credentials, {
/**
* Creates a new EnvironmentCredentials class with a given variable
* prefix {envPrefix}. For example, to load credentials using the 'AWS'
* prefix:
*
* ```javascript
* var creds = new AWS.EnvironmentCredentials('AWS');
* creds.accessKeyId == 'AKID' // from AWS_ACCESS_KEY_ID env var
* ```
*
* @param envPrefix [String] the prefix to use (e.g., 'AWS') for environment
* variables. Do not include the separating underscore.
*/
constructor: function EnvironmentCredentials(envPrefix) {
AWS.Credentials.call(this);
this.envPrefix = envPrefix;
this.get(function() {});
},
/**
* Loads credentials from the environment using the prefixed
* environment variables.
*
* @callback callback function(err)
* Called after the (prefixed) ACCESS_KEY_ID, SECRET_ACCESS_KEY, and
* SESSION_TOKEN environment variables are read. When this callback is
* called with no error, it means that the credentials information has
* been loaded into the object (as the `accessKeyId`, `secretAccessKey`,
* and `sessionToken` properties).
* @param err [Error] if an error occurred, this value will be filled
* @see get
*/
refresh: function refresh(callback) {
if (!callback) callback = AWS.util.fn.callback;
if (!process || !process.env) {
callback(AWS.util.error(
new Error('No process info or environment variables available'),
{ code: 'EnvironmentCredentialsProviderFailure' }
));
return;
}
var keys = ['ACCESS_KEY_ID', 'SECRET_ACCESS_KEY', 'SESSION_TOKEN'];
var values = [];
for (var i = 0; i < keys.length; i++) {
var prefix = '';
if (this.envPrefix) prefix = this.envPrefix + '_';
values[i] = process.env[prefix + keys[i]];
if (!values[i] && keys[i] !== 'SESSION_TOKEN') {
callback(AWS.util.error(
new Error('Variable ' + prefix + keys[i] + ' not set.'),
{ code: 'EnvironmentCredentialsProviderFailure' }
));
return;
}
}
this.expired = false;
AWS.Credentials.apply(this, values);
callback();
}
});