config_regional_endpoint.js
2.7 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
var AWS = require('./core');
/**
* @api private
*/
function validateRegionalEndpointsFlagValue(configValue, errorOptions) {
if (typeof configValue !== 'string') return undefined;
else if (['legacy', 'regional'].indexOf(configValue.toLowerCase()) >= 0) {
return configValue.toLowerCase();
} else {
throw AWS.util.error(new Error(), errorOptions);
}
}
/**
* Resolve the configuration value for regional endpoint from difference sources: client
* config, environmental variable, shared config file. Value can be case-insensitive
* 'legacy' or 'reginal'.
* @param originalConfig user-supplied config object to resolve
* @param options a map of config property names from individual configuration source
* - env: name of environmental variable that refers to the config
* - sharedConfig: name of shared configuration file property that refers to the config
* - clientConfig: name of client configuration property that refers to the config
*
* @api private
*/
function resolveRegionalEndpointsFlag(originalConfig, options) {
originalConfig = originalConfig || {};
//validate config value
var resolved;
if (originalConfig[options.clientConfig]) {
resolved = validateRegionalEndpointsFlagValue(originalConfig[options.clientConfig], {
code: 'InvalidConfiguration',
message: 'invalid "' + options.clientConfig + '" configuration. Expect "legacy" ' +
' or "regional". Got "' + originalConfig[options.clientConfig] + '".'
});
if (resolved) return resolved;
}
if (!AWS.util.isNode()) return resolved;
//validate environmental variable
if (Object.prototype.hasOwnProperty.call(process.env, options.env)) {
var envFlag = process.env[options.env];
resolved = validateRegionalEndpointsFlagValue(envFlag, {
code: 'InvalidEnvironmentalVariable',
message: 'invalid ' + options.env + ' environmental variable. Expect "legacy" ' +
' or "regional". Got "' + process.env[options.env] + '".'
});
if (resolved) return resolved;
}
//validate shared config file
var profile = {};
try {
var profiles = AWS.util.getProfilesFromSharedConfig(AWS.util.iniLoader);
profile = profiles[process.env.AWS_PROFILE || AWS.util.defaultProfile];
} catch (e) {};
if (profile && Object.prototype.hasOwnProperty.call(profile, options.sharedConfig)) {
var fileFlag = profile[options.sharedConfig];
resolved = validateRegionalEndpointsFlagValue(fileFlag, {
code: 'InvalidConfiguration',
message: 'invalid ' + options.sharedConfig + ' profile config. Expect "legacy" ' +
' or "regional". Got "' + profile[options.sharedConfig] + '".'
});
if (resolved) return resolved;
}
return resolved;
}
module.exports = resolveRegionalEndpointsFlag;