file_system_credentials.js
2.07 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
var AWS = require('../core');
/**
* Represents credentials from a JSON file on disk.
* If the credentials expire, the SDK can {refresh} the credentials
* from the file.
*
* The format of the file should be similar to the options passed to
* {AWS.Config}:
*
* ```javascript
* {accessKeyId: 'akid', secretAccessKey: 'secret', sessionToken: 'optional'}
* ```
*
* @example Loading credentials from disk
* var creds = new AWS.FileSystemCredentials('./configuration.json');
* creds.accessKeyId == 'AKID'
*
* @!attribute filename
* @readonly
* @return [String] the path to the JSON file on disk containing the
* credentials.
* @!macro nobrowser
*/
AWS.FileSystemCredentials = AWS.util.inherit(AWS.Credentials, {
/**
* @overload AWS.FileSystemCredentials(filename)
* Creates a new FileSystemCredentials object from a filename
*
* @param filename [String] the path on disk to the JSON file to load.
*/
constructor: function FileSystemCredentials(filename) {
AWS.Credentials.call(this);
this.filename = filename;
this.get(function() {});
},
/**
* Loads the credentials from the {filename} on disk.
*
* @callback callback function(err)
* Called after the JSON file on disk is read and parsed. 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;
try {
var creds = JSON.parse(AWS.util.readFileSync(this.filename));
AWS.Credentials.call(this, creds);
if (!this.accessKeyId || !this.secretAccessKey) {
throw AWS.util.error(
new Error('Credentials not set in ' + this.filename),
{ code: 'FileSystemCredentialsProviderFailure' }
);
}
this.expired = false;
callback();
} catch (err) {
callback(err);
}
}
});