configuration.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var AWS = require('../core');
  2. /**
  3. * Resolve client-side monitoring configuration from either environmental variables
  4. * or shared config file. Configurations from environmental variables have higher priority
  5. * than those from shared config file. The resolver will try to read the shared config file
  6. * no matter whether the AWS_SDK_LOAD_CONFIG variable is set.
  7. * @api private
  8. */
  9. function resolveMonitoringConfig() {
  10. var config = {
  11. port: undefined,
  12. clientId: undefined,
  13. enabled: undefined,
  14. host: undefined
  15. };
  16. if (fromEnvironment(config) || fromConfigFile(config)) return toJSType(config);
  17. return toJSType(config);
  18. }
  19. /**
  20. * Resolve configurations from environmental variables.
  21. * @param {object} client side monitoring config object needs to be resolved
  22. * @returns {boolean} whether resolving configurations is done
  23. * @api private
  24. */
  25. function fromEnvironment(config) {
  26. config.port = config.port || process.env.AWS_CSM_PORT;
  27. config.enabled = config.enabled || process.env.AWS_CSM_ENABLED;
  28. config.clientId = config.clientId || process.env.AWS_CSM_CLIENT_ID;
  29. config.host = config.host || process.env.AWS_CSM_HOST;
  30. return config.port && config.enabled && config.clientId && config.host ||
  31. ['false', '0'].indexOf(config.enabled) >= 0; //no need to read shared config file if explicitely disabled
  32. }
  33. /**
  34. * Resolve cofigurations from shared config file with specified role name
  35. * @param {object} client side monitoring config object needs to be resolved
  36. * @returns {boolean} whether resolving configurations is done
  37. * @api private
  38. */
  39. function fromConfigFile(config) {
  40. var sharedFileConfig;
  41. try {
  42. var configFile = AWS.util.iniLoader.loadFrom({
  43. isConfig: true,
  44. filename: process.env[AWS.util.sharedConfigFileEnv]
  45. });
  46. var sharedFileConfig = configFile[
  47. process.env.AWS_PROFILE || AWS.util.defaultProfile
  48. ];
  49. } catch (err) {
  50. return false;
  51. }
  52. if (!sharedFileConfig) return config;
  53. config.port = config.port || sharedFileConfig.csm_port;
  54. config.enabled = config.enabled || sharedFileConfig.csm_enabled;
  55. config.clientId = config.clientId || sharedFileConfig.csm_client_id;
  56. config.host = config.host || sharedFileConfig.csm_host;
  57. return config.port && config.enabled && config.clientId && config.host;
  58. }
  59. /**
  60. * Transfer the resolved configuration value to proper types: port as number, enabled
  61. * as boolean and clientId as string. The 'enabled' flag is valued to false when set
  62. * to 'false' or '0'.
  63. * @param {object} resolved client side monitoring config
  64. * @api private
  65. */
  66. function toJSType(config) {
  67. //config.XXX is either undefined or string
  68. var falsyNotations = ['false', '0', undefined];
  69. if (!config.enabled || falsyNotations.indexOf(config.enabled.toLowerCase()) >= 0) {
  70. config.enabled = false;
  71. } else {
  72. config.enabled = true;
  73. }
  74. config.port = config.port ? parseInt(config.port, 10) : undefined;
  75. return config;
  76. }
  77. module.exports = resolveMonitoringConfig;