config_regional_endpoint.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var AWS = require('./core');
  2. /**
  3. * @api private
  4. */
  5. function validateRegionalEndpointsFlagValue(configValue, errorOptions) {
  6. if (typeof configValue !== 'string') return undefined;
  7. else if (['legacy', 'regional'].indexOf(configValue.toLowerCase()) >= 0) {
  8. return configValue.toLowerCase();
  9. } else {
  10. throw AWS.util.error(new Error(), errorOptions);
  11. }
  12. }
  13. /**
  14. * Resolve the configuration value for regional endpoint from difference sources: client
  15. * config, environmental variable, shared config file. Value can be case-insensitive
  16. * 'legacy' or 'reginal'.
  17. * @param originalConfig user-supplied config object to resolve
  18. * @param options a map of config property names from individual configuration source
  19. * - env: name of environmental variable that refers to the config
  20. * - sharedConfig: name of shared configuration file property that refers to the config
  21. * - clientConfig: name of client configuration property that refers to the config
  22. *
  23. * @api private
  24. */
  25. function resolveRegionalEndpointsFlag(originalConfig, options) {
  26. originalConfig = originalConfig || {};
  27. //validate config value
  28. var resolved;
  29. if (originalConfig[options.clientConfig]) {
  30. resolved = validateRegionalEndpointsFlagValue(originalConfig[options.clientConfig], {
  31. code: 'InvalidConfiguration',
  32. message: 'invalid "' + options.clientConfig + '" configuration. Expect "legacy" ' +
  33. ' or "regional". Got "' + originalConfig[options.clientConfig] + '".'
  34. });
  35. if (resolved) return resolved;
  36. }
  37. if (!AWS.util.isNode()) return resolved;
  38. //validate environmental variable
  39. if (Object.prototype.hasOwnProperty.call(process.env, options.env)) {
  40. var envFlag = process.env[options.env];
  41. resolved = validateRegionalEndpointsFlagValue(envFlag, {
  42. code: 'InvalidEnvironmentalVariable',
  43. message: 'invalid ' + options.env + ' environmental variable. Expect "legacy" ' +
  44. ' or "regional". Got "' + process.env[options.env] + '".'
  45. });
  46. if (resolved) return resolved;
  47. }
  48. //validate shared config file
  49. var profile = {};
  50. try {
  51. var profiles = AWS.util.getProfilesFromSharedConfig(AWS.util.iniLoader);
  52. profile = profiles[process.env.AWS_PROFILE || AWS.util.defaultProfile];
  53. } catch (e) {};
  54. if (profile && Object.prototype.hasOwnProperty.call(profile, options.sharedConfig)) {
  55. var fileFlag = profile[options.sharedConfig];
  56. resolved = validateRegionalEndpointsFlagValue(fileFlag, {
  57. code: 'InvalidConfiguration',
  58. message: 'invalid ' + options.sharedConfig + ' profile config. Expect "legacy" ' +
  59. ' or "regional". Got "' + profile[options.sharedConfig] + '".'
  60. });
  61. if (resolved) return resolved;
  62. }
  63. return resolved;
  64. }
  65. module.exports = resolveRegionalEndpointsFlag;