|
1 | 1 | var AWS = require('../core'); |
2 | | -var regionConfig = require('../region_config'); |
| 2 | +var resolveRegionalEndpointsFlag = require('../config_regional_endpoint'); |
3 | 3 | var ENV_REGIONAL_ENDPOINT_ENABLED = 'AWS_STS_REGIONAL_ENDPOINTS'; |
4 | 4 | var CONFIG_REGIONAL_ENDPOINT_ENABLED = 'sts_regional_endpoints'; |
5 | 5 |
|
@@ -51,77 +51,38 @@ AWS.util.update(AWS.STS.prototype, { |
51 | 51 | /** |
52 | 52 | * @api private |
53 | 53 | */ |
54 | | - validateRegionalEndpointsFlagValue: function validateRegionalEndpointsFlagValue(configValue, errorOptions) { |
55 | | - if (typeof configValue === 'string' && ['legacy', 'regional'].indexOf(configValue.toLowerCase()) >= 0) { |
56 | | - this.config.stsRegionalEndpoints = configValue.toLowerCase(); |
57 | | - return; |
58 | | - } else { |
59 | | - throw AWS.util.error(new Error(), errorOptions); |
60 | | - } |
| 54 | + setupRequestListeners: function setupRequestListeners(request) { |
| 55 | + request.addListener('validate', this.optInRegionalEndpoint, true); |
61 | 56 | }, |
62 | 57 |
|
63 | 58 | /** |
64 | 59 | * @api private |
65 | 60 | */ |
66 | | - validateRegionalEndpointsFlag: function validateRegionalEndpointsFlag() { |
67 | | - //validate config value |
68 | | - var config = this.config; |
69 | | - if (config.stsRegionalEndpoints) { |
70 | | - this.validateRegionalEndpointsFlagValue(config.stsRegionalEndpoints, { |
71 | | - code: 'InvalidConfiguration', |
72 | | - message: 'invalid "stsRegionalEndpoints" configuration. Expect "legacy" ' + |
73 | | - ' or "regional". Got "' + config.stsRegionalEndpoints + '".' |
74 | | - }); |
75 | | - } |
76 | | - if (!AWS.util.isNode()) return; |
77 | | - //validate environmental variable |
78 | | - if (Object.prototype.hasOwnProperty.call(process.env, ENV_REGIONAL_ENDPOINT_ENABLED)) { |
79 | | - var envFlag = process.env[ENV_REGIONAL_ENDPOINT_ENABLED]; |
80 | | - this.validateRegionalEndpointsFlagValue(envFlag, { |
81 | | - code: 'InvalidEnvironmentalVariable', |
82 | | - message: 'invalid ' + ENV_REGIONAL_ENDPOINT_ENABLED + ' environmental variable. Expect "legacy" ' + |
83 | | - ' or "regional". Got "' + process.env[ENV_REGIONAL_ENDPOINT_ENABLED] + '".' |
84 | | - }); |
85 | | - } |
86 | | - //validate shared config file |
87 | | - var profile = {}; |
88 | | - try { |
89 | | - var profiles = AWS.util.getProfilesFromSharedConfig(AWS.util.iniLoader); |
90 | | - profile = profiles[process.env.AWS_PROFILE || AWS.util.defaultProfile]; |
91 | | - } catch (e) {}; |
92 | | - if (profile && Object.prototype.hasOwnProperty.call(profile, CONFIG_REGIONAL_ENDPOINT_ENABLED)) { |
93 | | - var fileFlag = profile[CONFIG_REGIONAL_ENDPOINT_ENABLED]; |
94 | | - this.validateRegionalEndpointsFlagValue(fileFlag, { |
95 | | - code: 'InvalidConfiguration', |
96 | | - message: 'invalid '+CONFIG_REGIONAL_ENDPOINT_ENABLED+' profile config. Expect "legacy" ' + |
97 | | - ' or "regional". Got "' + profile[CONFIG_REGIONAL_ENDPOINT_ENABLED] + '".' |
98 | | - }); |
99 | | - } |
100 | | - }, |
101 | | - |
102 | | - /** |
103 | | - * @api private |
104 | | - */ |
105 | | - optInRegionalEndpoint: function optInRegionalEndpoint() { |
106 | | - this.validateRegionalEndpointsFlag(); |
107 | | - var config = this.config; |
108 | | - if (config.stsRegionalEndpoints === 'regional') { |
109 | | - regionConfig(this); |
110 | | - if (!this.isGlobalEndpoint) return; |
111 | | - this.isGlobalEndpoint = false; |
| 61 | + optInRegionalEndpoint: function optInRegionalEndpoint(req) { |
| 62 | + var service = req.service; |
| 63 | + var config = service.config; |
| 64 | + config.stsRegionalEndpoints = resolveRegionalEndpointsFlag(service._originalConfig, { |
| 65 | + env: ENV_REGIONAL_ENDPOINT_ENABLED, |
| 66 | + sharedConfig: CONFIG_REGIONAL_ENDPOINT_ENABLED, |
| 67 | + clientConfig: 'stsRegionalEndpoints' |
| 68 | + }); |
| 69 | + if ( |
| 70 | + config.stsRegionalEndpoints === 'regional' && |
| 71 | + service.isGlobalEndpoint |
| 72 | + ) { |
112 | 73 | //client will throw if region is not supplied; request will be signed with specified region |
113 | 74 | if (!config.region) { |
114 | 75 | throw AWS.util.error(new Error(), |
115 | 76 | {code: 'ConfigError', message: 'Missing region in config'}); |
116 | 77 | } |
117 | 78 | var insertPoint = config.endpoint.indexOf('.amazonaws.com'); |
118 | | - config.endpoint = config.endpoint.substring(0, insertPoint) + |
| 79 | + regionalEndpoint = config.endpoint.substring(0, insertPoint) + |
119 | 80 | '.' + config.region + config.endpoint.substring(insertPoint); |
| 81 | + var endpoint = req.httpRequest.endpoint; |
| 82 | + endpoint.hostname = regionalEndpoint; |
| 83 | + endpoint.host = regionalEndpoint; |
| 84 | + req.httpRequest.region = config.region; |
120 | 85 | } |
121 | | - }, |
122 | | - |
123 | | - validateService: function validateService() { |
124 | | - this.optInRegionalEndpoint(); |
125 | 86 | } |
126 | 87 |
|
127 | 88 | }); |
0 commit comments