1 | var AWS = require('./core');
|
2 | var util = require('./util');
|
3 | var endpointDiscoveryEnabledEnvs = ['AWS_ENABLE_ENDPOINT_DISCOVERY', 'AWS_ENDPOINT_DISCOVERY_ENABLED'];
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function getCacheKey(request) {
|
15 | var service = request.service;
|
16 | var api = service.api || {};
|
17 | var operations = api.operations;
|
18 | var identifiers = {};
|
19 | if (service.config.region) {
|
20 | identifiers.region = service.config.region;
|
21 | }
|
22 | if (api.serviceId) {
|
23 | identifiers.serviceId = api.serviceId;
|
24 | }
|
25 | if (service.config.credentials.accessKeyId) {
|
26 | identifiers.accessKeyId = service.config.credentials.accessKeyId;
|
27 | }
|
28 | return identifiers;
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function marshallCustomIdentifiersHelper(result, params, shape) {
|
37 | if (!shape || params === undefined || params === null) return;
|
38 | if (shape.type === 'structure' && shape.required && shape.required.length > 0) {
|
39 | util.arrayEach(shape.required, function(name) {
|
40 | var memberShape = shape.members[name];
|
41 | if (memberShape.endpointDiscoveryId === true) {
|
42 | var locationName = memberShape.isLocationName ? memberShape.name : name;
|
43 | result[locationName] = String(params[name]);
|
44 | } else {
|
45 | marshallCustomIdentifiersHelper(result, params[name], memberShape);
|
46 | }
|
47 | });
|
48 | }
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function marshallCustomIdentifiers(request, shape) {
|
59 | var identifiers = {};
|
60 | marshallCustomIdentifiersHelper(identifiers, request.params, shape);
|
61 | return identifiers;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function optionalDiscoverEndpoint(request) {
|
73 | var service = request.service;
|
74 | var api = service.api;
|
75 | var operationModel = api.operations ? api.operations[request.operation] : undefined;
|
76 | var inputShape = operationModel ? operationModel.input : undefined;
|
77 |
|
78 | var identifiers = marshallCustomIdentifiers(request, inputShape);
|
79 | var cacheKey = getCacheKey(request);
|
80 | if (Object.keys(identifiers).length > 0) {
|
81 | cacheKey = util.update(cacheKey, identifiers);
|
82 | if (operationModel) cacheKey.operation = operationModel.name;
|
83 | }
|
84 | var endpoints = AWS.endpointCache.get(cacheKey);
|
85 | if (endpoints && endpoints.length === 1 && endpoints[0].Address === '') {
|
86 |
|
87 |
|
88 | return;
|
89 | } else if (endpoints && endpoints.length > 0) {
|
90 |
|
91 | request.httpRequest.updateEndpoint(endpoints[0].Address);
|
92 | } else {
|
93 |
|
94 | var endpointRequest = service.makeRequest(api.endpointOperation, {
|
95 | Operation: operationModel.name,
|
96 | Identifiers: identifiers,
|
97 | });
|
98 | addApiVersionHeader(endpointRequest);
|
99 | endpointRequest.removeListener('validate', AWS.EventListeners.Core.VALIDATE_PARAMETERS);
|
100 | endpointRequest.removeListener('retry', AWS.EventListeners.Core.RETRY_CHECK);
|
101 |
|
102 |
|
103 | AWS.endpointCache.put(cacheKey, [{
|
104 | Address: '',
|
105 | CachePeriodInMinutes: 1
|
106 | }]);
|
107 | endpointRequest.send(function(err, data) {
|
108 | if (data && data.Endpoints) {
|
109 | AWS.endpointCache.put(cacheKey, data.Endpoints);
|
110 | } else if (err) {
|
111 | AWS.endpointCache.put(cacheKey, [{
|
112 | Address: '',
|
113 | CachePeriodInMinutes: 1
|
114 | }]);
|
115 | }
|
116 | });
|
117 | }
|
118 | }
|
119 |
|
120 | var requestQueue = {};
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function requiredDiscoverEndpoint(request, done) {
|
132 | var service = request.service;
|
133 | var api = service.api;
|
134 | var operationModel = api.operations ? api.operations[request.operation] : undefined;
|
135 | var inputShape = operationModel ? operationModel.input : undefined;
|
136 |
|
137 | var identifiers = marshallCustomIdentifiers(request, inputShape);
|
138 | var cacheKey = getCacheKey(request);
|
139 | if (Object.keys(identifiers).length > 0) {
|
140 | cacheKey = util.update(cacheKey, identifiers);
|
141 | if (operationModel) cacheKey.operation = operationModel.name;
|
142 | }
|
143 | var cacheKeyStr = AWS.EndpointCache.getKeyString(cacheKey);
|
144 | var endpoints = AWS.endpointCache.get(cacheKeyStr);
|
145 | if (endpoints && endpoints.length === 1 && endpoints[0].Address === '') {
|
146 |
|
147 |
|
148 | if (!requestQueue[cacheKeyStr]) requestQueue[cacheKeyStr] = [];
|
149 | requestQueue[cacheKeyStr].push({request: request, callback: done});
|
150 | return;
|
151 | } else if (endpoints && endpoints.length > 0) {
|
152 | request.httpRequest.updateEndpoint(endpoints[0].Address);
|
153 | done();
|
154 | } else {
|
155 | var endpointRequest = service.makeRequest(api.endpointOperation, {
|
156 | Operation: operationModel.name,
|
157 | Identifiers: identifiers,
|
158 | });
|
159 | endpointRequest.removeListener('validate', AWS.EventListeners.Core.VALIDATE_PARAMETERS);
|
160 | addApiVersionHeader(endpointRequest);
|
161 |
|
162 |
|
163 |
|
164 | AWS.endpointCache.put(cacheKeyStr, [{
|
165 | Address: '',
|
166 | CachePeriodInMinutes: 60
|
167 | }]);
|
168 | endpointRequest.send(function(err, data) {
|
169 | if (err) {
|
170 | var errorParams = {
|
171 | code: 'EndpointDiscoveryException',
|
172 | message: 'Request cannot be fulfilled without specifying an endpoint',
|
173 | retryable: false
|
174 | };
|
175 | request.response.error = util.error(err, errorParams);
|
176 | AWS.endpointCache.remove(cacheKey);
|
177 |
|
178 |
|
179 | if (requestQueue[cacheKeyStr]) {
|
180 | var pendingRequests = requestQueue[cacheKeyStr];
|
181 | util.arrayEach(pendingRequests, function(requestContext) {
|
182 | requestContext.request.response.error = util.error(err, errorParams);
|
183 | requestContext.callback();
|
184 | });
|
185 | delete requestQueue[cacheKeyStr];
|
186 | }
|
187 | } else if (data) {
|
188 | AWS.endpointCache.put(cacheKeyStr, data.Endpoints);
|
189 | request.httpRequest.updateEndpoint(data.Endpoints[0].Address);
|
190 |
|
191 |
|
192 | if (requestQueue[cacheKeyStr]) {
|
193 | var pendingRequests = requestQueue[cacheKeyStr];
|
194 | util.arrayEach(pendingRequests, function(requestContext) {
|
195 | requestContext.request.httpRequest.updateEndpoint(data.Endpoints[0].Address);
|
196 | requestContext.callback();
|
197 | });
|
198 | delete requestQueue[cacheKeyStr];
|
199 | }
|
200 | }
|
201 | done();
|
202 | });
|
203 | }
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function addApiVersionHeader(endpointRequest) {
|
211 | var api = endpointRequest.service.api;
|
212 | var apiVersion = api.apiVersion;
|
213 | if (apiVersion && !endpointRequest.httpRequest.headers['x-amz-api-version']) {
|
214 | endpointRequest.httpRequest.headers['x-amz-api-version'] = apiVersion;
|
215 | }
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | function invalidateCachedEndpoints(response) {
|
224 | var error = response.error;
|
225 | var httpResponse = response.httpResponse;
|
226 | if (error &&
|
227 | (error.code === 'InvalidEndpointException' || httpResponse.statusCode === 421)
|
228 | ) {
|
229 | var request = response.request;
|
230 | var operations = request.service.api.operations || {};
|
231 | var inputShape = operations[request.operation] ? operations[request.operation].input : undefined;
|
232 | var identifiers = marshallCustomIdentifiers(request, inputShape);
|
233 | var cacheKey = getCacheKey(request);
|
234 | if (Object.keys(identifiers).length > 0) {
|
235 | cacheKey = util.update(cacheKey, identifiers);
|
236 | if (operations[request.operation]) cacheKey.operation = operations[request.operation].name;
|
237 | }
|
238 | AWS.endpointCache.remove(cacheKey);
|
239 | }
|
240 | }
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function hasCustomEndpoint(client) {
|
248 |
|
249 | if (client._originalConfig && client._originalConfig.endpoint && client._originalConfig.endpointDiscoveryEnabled === true) {
|
250 | throw util.error(new Error(), {
|
251 | code: 'ConfigurationException',
|
252 | message: 'Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.'
|
253 | });
|
254 | };
|
255 | var svcConfig = AWS.config[client.serviceIdentifier] || {};
|
256 | return Boolean(AWS.config.endpoint || svcConfig.endpoint || (client._originalConfig && client._originalConfig.endpoint));
|
257 | }
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | function isFalsy(value) {
|
263 | return ['false', '0'].indexOf(value) >= 0;
|
264 | }
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function isEndpointDiscoveryEnabled(request) {
|
277 | var service = request.service || {};
|
278 | if (service.config.endpointDiscoveryEnabled === true) return true;
|
279 |
|
280 |
|
281 |
|
282 | if (util.isBrowser()) return false;
|
283 |
|
284 | for (var i = 0; i < endpointDiscoveryEnabledEnvs.length; i++) {
|
285 | var env = endpointDiscoveryEnabledEnvs[i];
|
286 | if (Object.prototype.hasOwnProperty.call(process.env, env)) {
|
287 | if (process.env[env] === '' || process.env[env] === undefined) {
|
288 | throw util.error(new Error(), {
|
289 | code: 'ConfigurationException',
|
290 | message: 'environmental variable ' + env + ' cannot be set to nothing'
|
291 | });
|
292 | }
|
293 | if (!isFalsy(process.env[env])) return true;
|
294 | }
|
295 | }
|
296 |
|
297 | var configFile = {};
|
298 | try {
|
299 | configFile = AWS.util.iniLoader ? AWS.util.iniLoader.loadFrom({
|
300 | isConfig: true,
|
301 | filename: process.env[AWS.util.sharedConfigFileEnv]
|
302 | }) : {};
|
303 | } catch (e) {}
|
304 | var sharedFileConfig = configFile[
|
305 | process.env.AWS_PROFILE || AWS.util.defaultProfile
|
306 | ] || {};
|
307 | if (Object.prototype.hasOwnProperty.call(sharedFileConfig, 'endpoint_discovery_enabled')) {
|
308 | if (sharedFileConfig.endpoint_discovery_enabled === undefined) {
|
309 | throw util.error(new Error(), {
|
310 | code: 'ConfigurationException',
|
311 | message: 'config file entry \'endpoint_discovery_enabled\' cannot be set to nothing'
|
312 | });
|
313 | }
|
314 | if (!isFalsy(sharedFileConfig.endpoint_discovery_enabled)) return true;
|
315 | }
|
316 | return false;
|
317 | }
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | function discoverEndpoint(request, done) {
|
325 | var service = request.service || {};
|
326 | if (hasCustomEndpoint(service) || request.isPresigned()) return done();
|
327 |
|
328 | var operations = service.api.operations || {};
|
329 | var operationModel = operations[request.operation];
|
330 | var isEndpointDiscoveryRequired = operationModel ? operationModel.endpointDiscoveryRequired : 'NULL';
|
331 | var isEnabled = isEndpointDiscoveryEnabled(request);
|
332 |
|
333 | if (!isEnabled) {
|
334 |
|
335 | if (isEndpointDiscoveryRequired === 'REQUIRED') {
|
336 | throw util.error(new Error(), {
|
337 | code: 'ConfigurationException',
|
338 | message: 'Endpoint Discovery is not enabled but this operation requires it.'
|
339 | });
|
340 | }
|
341 | return done();
|
342 | }
|
343 |
|
344 | request.httpRequest.appendToUserAgent('endpoint-discovery');
|
345 | switch (isEndpointDiscoveryRequired) {
|
346 | case 'OPTIONAL':
|
347 | optionalDiscoverEndpoint(request);
|
348 | request.addNamedListener('INVALIDATE_CACHED_ENDPOINTS', 'extractError', invalidateCachedEndpoints);
|
349 | done();
|
350 | break;
|
351 | case 'REQUIRED':
|
352 | request.addNamedListener('INVALIDATE_CACHED_ENDPOINTS', 'extractError', invalidateCachedEndpoints);
|
353 | requiredDiscoverEndpoint(request, done);
|
354 | break;
|
355 | case 'NULL':
|
356 | default:
|
357 | done();
|
358 | break;
|
359 | }
|
360 | }
|
361 |
|
362 | module.exports = {
|
363 | discoverEndpoint: discoverEndpoint,
|
364 | requiredDiscoverEndpoint: requiredDiscoverEndpoint,
|
365 | optionalDiscoverEndpoint: optionalDiscoverEndpoint,
|
366 | marshallCustomIdentifiers: marshallCustomIdentifiers,
|
367 | getCacheKey: getCacheKey,
|
368 | invalidateCachedEndpoint: invalidateCachedEndpoints,
|
369 | };
|