configuration.js
2.91 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
var AWS = require('../core');
/**
* Resolve client-side monitoring configuration from either environmental variables
* or shared config file. Configurations from environmental variables have higher priority
* than those from shared config file. The resolver will try to read the shared config file
* no matter whether the AWS_SDK_LOAD_CONFIG variable is set.
* @api private
*/
function resolveMonitoringConfig() {
var config = {
port: undefined,
clientId: undefined,
enabled: undefined,
host: undefined
};
if (fromEnvironment(config) || fromConfigFile(config)) return toJSType(config);
return toJSType(config);
}
/**
* Resolve configurations from environmental variables.
* @param {object} client side monitoring config object needs to be resolved
* @returns {boolean} whether resolving configurations is done
* @api private
*/
function fromEnvironment(config) {
config.port = config.port || process.env.AWS_CSM_PORT;
config.enabled = config.enabled || process.env.AWS_CSM_ENABLED;
config.clientId = config.clientId || process.env.AWS_CSM_CLIENT_ID;
config.host = config.host || process.env.AWS_CSM_HOST;
return config.port && config.enabled && config.clientId && config.host ||
['false', '0'].indexOf(config.enabled) >= 0; //no need to read shared config file if explicitely disabled
}
/**
* Resolve cofigurations from shared config file with specified role name
* @param {object} client side monitoring config object needs to be resolved
* @returns {boolean} whether resolving configurations is done
* @api private
*/
function fromConfigFile(config) {
var sharedFileConfig;
try {
var configFile = AWS.util.iniLoader.loadFrom({
isConfig: true,
filename: process.env[AWS.util.sharedConfigFileEnv]
});
var sharedFileConfig = configFile[
process.env.AWS_PROFILE || AWS.util.defaultProfile
];
} catch (err) {
return false;
}
if (!sharedFileConfig) return config;
config.port = config.port || sharedFileConfig.csm_port;
config.enabled = config.enabled || sharedFileConfig.csm_enabled;
config.clientId = config.clientId || sharedFileConfig.csm_client_id;
config.host = config.host || sharedFileConfig.csm_host;
return config.port && config.enabled && config.clientId && config.host;
}
/**
* Transfer the resolved configuration value to proper types: port as number, enabled
* as boolean and clientId as string. The 'enabled' flag is valued to false when set
* to 'false' or '0'.
* @param {object} resolved client side monitoring config
* @api private
*/
function toJSType(config) {
//config.XXX is either undefined or string
var falsyNotations = ['false', '0', undefined];
if (!config.enabled || falsyNotations.indexOf(config.enabled.toLowerCase()) >= 0) {
config.enabled = false;
} else {
config.enabled = true;
}
config.port = config.port ? parseInt(config.port, 10) : undefined;
return config;
}
module.exports = resolveMonitoringConfig;