-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ini-loader.js
138 lines (124 loc) · 4.24 KB
/
ini-loader.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var AWS = require('../core');
var os = require('os');
var path = require('path');
function parseFile(filename) {
return AWS.util.ini.parse(AWS.util.readFileSync(filename));
}
function getProfiles(fileContent) {
var tmpContent = {};
Object.keys(fileContent).forEach(function(sectionName) {
if (/^sso-session\s/.test(sectionName)) return;
Object.defineProperty(tmpContent, sectionName.replace(/^profile\s/, ''), {
value: fileContent[sectionName],
enumerable: true
});
});
return tmpContent;
}
function getSsoSessions(fileContent) {
var tmpContent = {};
Object.keys(fileContent).forEach(function(sectionName) {
if (!/^sso-session\s/.test(sectionName)) return;
Object.defineProperty(tmpContent, sectionName.replace(/^sso-session\s/, ''), {
value: fileContent[sectionName],
enumerable: true
});
});
return tmpContent;
}
/**
* Ini file loader class the same as that used in the SDK. It loads and
* parses config and credentials files in .ini format and cache the content
* to assure files are only read once.
* Note that calling operations on the instance instantiated from this class
* won't affect the behavior of SDK since SDK uses an internal singleton of
* this class.
* @!macro nobrowser
*/
AWS.IniLoader = AWS.util.inherit({
constructor: function IniLoader() {
this.resolvedProfiles = {};
this.resolvedSsoSessions = {};
},
/** Remove all cached files. Used after config files are updated. */
clearCachedFiles: function clearCachedFiles() {
this.resolvedProfiles = {};
this.resolvedSsoSessions = {};
},
/**
* Load configurations from config/credentials files and cache them
* for later use. If no file is specified it will try to load default files.
*
* @param options [map] information describing the file
* @option options filename [String] ('~/.aws/credentials' or defined by
* AWS_SHARED_CREDENTIALS_FILE process env var or '~/.aws/config' if
* isConfig is set to true)
* path to the file to be read.
* @option options isConfig [Boolean] (false) True to read config file.
* @return [map<String,String>] object containing contents from file in key-value
* pairs.
*/
loadFrom: function loadFrom(options) {
options = options || {};
var isConfig = options.isConfig === true;
var filename = options.filename || this.getDefaultFilePath(isConfig);
if (!this.resolvedProfiles[filename]) {
var fileContent = parseFile(filename);
if (isConfig) {
Object.defineProperty(this.resolvedProfiles, filename, {
value: getProfiles(fileContent)
});
} else {
Object.defineProperty(this.resolvedProfiles, filename, { value: fileContent });
}
}
return this.resolvedProfiles[filename];
},
/**
* Load sso sessions from config/credentials files and cache them
* for later use. If no file is specified it will try to load default file.
*
* @param options [map] information describing the file
* @option options filename [String] ('~/.aws/config' or defined by
* AWS_CONFIG_FILE process env var)
* @return [map<String,String>] object containing contents from file in key-value
* pairs.
*/
loadSsoSessionsFrom: function loadSsoSessionsFrom(options) {
options = options || {};
var filename = options.filename || this.getDefaultFilePath(true);
if (!this.resolvedSsoSessions[filename]) {
var fileContent = parseFile(filename);
Object.defineProperty(this.resolvedSsoSessions, filename, {
value: getSsoSessions(fileContent)
});
}
return this.resolvedSsoSessions[filename];
},
getDefaultFilePath: function getDefaultFilePath(isConfig) {
return path.join(
this.getHomeDir(),
'.aws',
isConfig ? 'config' : 'credentials'
);
},
getHomeDir: function getHomeDir() {
var env = process.env;
var home = env.HOME ||
env.USERPROFILE ||
(env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null);
if (home) {
return home;
}
if (typeof os.homedir === 'function') {
return os.homedir();
}
throw AWS.util.error(
new Error('Cannot load credentials, HOME path not set')
);
}
});
var IniLoader = AWS.IniLoader;
module.exports = {
IniLoader: IniLoader
};