discover_endpoint.js
13.3 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
var AWS = require('./core');
var util = require('./util');
var endpointDiscoveryEnabledEnvs = ['AWS_ENABLE_ENDPOINT_DISCOVERY', 'AWS_ENDPOINT_DISCOVERY_ENABLED'];
/**
* Generate key (except resources and operation part) to index the endpoints in the cache
* If input shape has endpointdiscoveryid trait then use
* accessKey + operation + resources + region + service as cache key
* If input shape doesn't have endpointdiscoveryid trait then use
* accessKey + region + service as cache key
* @return [map<String,String>] object with keys to index endpoints.
* @api private
*/
function getCacheKey(request) {
var service = request.service;
var api = service.api || {};
var operations = api.operations;
var identifiers = {};
if (service.config.region) {
identifiers.region = service.config.region;
}
if (api.serviceId) {
identifiers.serviceId = api.serviceId;
}
if (service.config.credentials.accessKeyId) {
identifiers.accessKeyId = service.config.credentials.accessKeyId;
}
return identifiers;
}
/**
* Recursive helper for marshallCustomIdentifiers().
* Looks for required string input members that have 'endpointdiscoveryid' trait.
* @api private
*/
function marshallCustomIdentifiersHelper(result, params, shape) {
if (!shape || params === undefined || params === null) return;
if (shape.type === 'structure' && shape.required && shape.required.length > 0) {
util.arrayEach(shape.required, function(name) {
var memberShape = shape.members[name];
if (memberShape.endpointDiscoveryId === true) {
var locationName = memberShape.isLocationName ? memberShape.name : name;
result[locationName] = String(params[name]);
} else {
marshallCustomIdentifiersHelper(result, params[name], memberShape);
}
});
}
}
/**
* Get custom identifiers for cache key.
* Identifies custom identifiers by checking each shape's `endpointDiscoveryId` trait.
* @param [object] request object
* @param [object] input shape of the given operation's api
* @api private
*/
function marshallCustomIdentifiers(request, shape) {
var identifiers = {};
marshallCustomIdentifiersHelper(identifiers, request.params, shape);
return identifiers;
}
/**
* Call endpoint discovery operation when it's optional.
* When endpoint is available in cache then use the cached endpoints. If endpoints
* are unavailable then use regional endpoints and call endpoint discovery operation
* asynchronously. This is turned off by default.
* @param [object] request object
* @api private
*/
function optionalDiscoverEndpoint(request) {
var service = request.service;
var api = service.api;
var operationModel = api.operations ? api.operations[request.operation] : undefined;
var inputShape = operationModel ? operationModel.input : undefined;
var identifiers = marshallCustomIdentifiers(request, inputShape);
var cacheKey = getCacheKey(request);
if (Object.keys(identifiers).length > 0) {
cacheKey = util.update(cacheKey, identifiers);
if (operationModel) cacheKey.operation = operationModel.name;
}
var endpoints = AWS.endpointCache.get(cacheKey);
if (endpoints && endpoints.length === 1 && endpoints[0].Address === '') {
//endpoint operation is being made but response not yet received
//or endpoint operation just failed in 1 minute
return;
} else if (endpoints && endpoints.length > 0) {
//found endpoint record from cache
request.httpRequest.updateEndpoint(endpoints[0].Address);
} else {
//endpoint record not in cache or outdated. make discovery operation
var endpointRequest = service.makeRequest(api.endpointOperation, {
Operation: operationModel.name,
Identifiers: identifiers,
});
addApiVersionHeader(endpointRequest);
endpointRequest.removeListener('validate', AWS.EventListeners.Core.VALIDATE_PARAMETERS);
endpointRequest.removeListener('retry', AWS.EventListeners.Core.RETRY_CHECK);
//put in a placeholder for endpoints already requested, prevent
//too much in-flight calls
AWS.endpointCache.put(cacheKey, [{
Address: '',
CachePeriodInMinutes: 1
}]);
endpointRequest.send(function(err, data) {
if (data && data.Endpoints) {
AWS.endpointCache.put(cacheKey, data.Endpoints);
} else if (err) {
AWS.endpointCache.put(cacheKey, [{
Address: '',
CachePeriodInMinutes: 1 //not to make more endpoint operation in next 1 minute
}]);
}
});
}
}
var requestQueue = {};
/**
* Call endpoint discovery operation when it's required.
* When endpoint is available in cache then use cached ones. If endpoints are
* unavailable then SDK should call endpoint operation then use returned new
* endpoint for the api call. SDK will automatically attempt to do endpoint
* discovery. This is turned off by default
* @param [object] request object
* @api private
*/
function requiredDiscoverEndpoint(request, done) {
var service = request.service;
var api = service.api;
var operationModel = api.operations ? api.operations[request.operation] : undefined;
var inputShape = operationModel ? operationModel.input : undefined;
var identifiers = marshallCustomIdentifiers(request, inputShape);
var cacheKey = getCacheKey(request);
if (Object.keys(identifiers).length > 0) {
cacheKey = util.update(cacheKey, identifiers);
if (operationModel) cacheKey.operation = operationModel.name;
}
var cacheKeyStr = AWS.EndpointCache.getKeyString(cacheKey);
var endpoints = AWS.endpointCache.get(cacheKeyStr); //endpoint cache also accepts string keys
if (endpoints && endpoints.length === 1 && endpoints[0].Address === '') {
//endpoint operation is being made but response not yet received
//push request object to a pending queue
if (!requestQueue[cacheKeyStr]) requestQueue[cacheKeyStr] = [];
requestQueue[cacheKeyStr].push({request: request, callback: done});
return;
} else if (endpoints && endpoints.length > 0) {
request.httpRequest.updateEndpoint(endpoints[0].Address);
done();
} else {
var endpointRequest = service.makeRequest(api.endpointOperation, {
Operation: operationModel.name,
Identifiers: identifiers,
});
endpointRequest.removeListener('validate', AWS.EventListeners.Core.VALIDATE_PARAMETERS);
addApiVersionHeader(endpointRequest);
//put in a placeholder for endpoints already requested, prevent
//too much in-flight calls
AWS.endpointCache.put(cacheKeyStr, [{
Address: '',
CachePeriodInMinutes: 60 //long-live cache
}]);
endpointRequest.send(function(err, data) {
if (err) {
var errorParams = {
code: 'EndpointDiscoveryException',
message: 'Request cannot be fulfilled without specifying an endpoint',
retryable: false
};
request.response.error = util.error(err, errorParams);
AWS.endpointCache.remove(cacheKey);
//fail all the pending requests in batch
if (requestQueue[cacheKeyStr]) {
var pendingRequests = requestQueue[cacheKeyStr];
util.arrayEach(pendingRequests, function(requestContext) {
requestContext.request.response.error = util.error(err, errorParams);
requestContext.callback();
});
delete requestQueue[cacheKeyStr];
}
} else if (data) {
AWS.endpointCache.put(cacheKeyStr, data.Endpoints);
request.httpRequest.updateEndpoint(data.Endpoints[0].Address);
//update the endpoint for all the pending requests in batch
if (requestQueue[cacheKeyStr]) {
var pendingRequests = requestQueue[cacheKeyStr];
util.arrayEach(pendingRequests, function(requestContext) {
requestContext.request.httpRequest.updateEndpoint(data.Endpoints[0].Address);
requestContext.callback();
});
delete requestQueue[cacheKeyStr];
}
}
done();
});
}
}
/**
* add api version header to endpoint operation
* @api private
*/
function addApiVersionHeader(endpointRequest) {
var api = endpointRequest.service.api;
var apiVersion = api.apiVersion;
if (apiVersion && !endpointRequest.httpRequest.headers['x-amz-api-version']) {
endpointRequest.httpRequest.headers['x-amz-api-version'] = apiVersion;
}
}
/**
* If api call gets invalid endpoint exception, SDK should attempt to remove the invalid
* endpoint from cache.
* @api private
*/
function invalidateCachedEndpoints(response) {
var error = response.error;
var httpResponse = response.httpResponse;
if (error &&
(error.code === 'InvalidEndpointException' || httpResponse.statusCode === 421)
) {
var request = response.request;
var operations = request.service.api.operations || {};
var inputShape = operations[request.operation] ? operations[request.operation].input : undefined;
var identifiers = marshallCustomIdentifiers(request, inputShape);
var cacheKey = getCacheKey(request);
if (Object.keys(identifiers).length > 0) {
cacheKey = util.update(cacheKey, identifiers);
if (operations[request.operation]) cacheKey.operation = operations[request.operation].name;
}
AWS.endpointCache.remove(cacheKey);
}
}
/**
* If endpoint is explicitly configured, SDK should not do endpoint discovery in anytime.
* @param [object] client Service client object.
* @api private
*/
function hasCustomEndpoint(client) {
//if set endpoint is set for specific client, enable endpoint discovery will raise an error.
if (client._originalConfig && client._originalConfig.endpoint && client._originalConfig.endpointDiscoveryEnabled === true) {
throw util.error(new Error(), {
code: 'ConfigurationException',
message: 'Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.'
});
};
var svcConfig = AWS.config[client.serviceIdentifier] || {};
return Boolean(AWS.config.endpoint || svcConfig.endpoint || (client._originalConfig && client._originalConfig.endpoint));
}
/**
* @api private
*/
function isFalsy(value) {
return ['false', '0'].indexOf(value) >= 0;
}
/**
* If endpoint discovery should perform for this request when endpoint discovery is optional.
* SDK performs config resolution in order like below:
* 1. If turned on client configuration(default to off) then turn on endpoint discovery.
* 2. If turned on in env AWS_ENABLE_ENDPOINT_DISCOVERY then turn on endpoint discovery.
* 3. If turned on in shared ini config file with key 'endpoint_discovery_enabled', then
* turn on endpoint discovery.
* @param [object] request request object.
* @api private
*/
function isEndpointDiscoveryApplicable(request) {
var service = request.service || {};
if (service.config.endpointDiscoveryEnabled === true) return true;
//shared ini file is only available in Node
//not to check env in browser
if (util.isBrowser()) return false;
for (var i = 0; i < endpointDiscoveryEnabledEnvs.length; i++) {
var env = endpointDiscoveryEnabledEnvs[i];
if (Object.prototype.hasOwnProperty.call(process.env, env)) {
if (process.env[env] === '' || process.env[env] === undefined) {
throw util.error(new Error(), {
code: 'ConfigurationException',
message: 'environmental variable ' + env + ' cannot be set to nothing'
});
}
if (!isFalsy(process.env[env])) return true;
}
}
var configFile = {};
try {
configFile = AWS.util.iniLoader ? AWS.util.iniLoader.loadFrom({
isConfig: true,
filename: process.env[AWS.util.sharedConfigFileEnv]
}) : {};
} catch (e) {}
var sharedFileConfig = configFile[
process.env.AWS_PROFILE || AWS.util.defaultProfile
] || {};
if (Object.prototype.hasOwnProperty.call(sharedFileConfig, 'endpoint_discovery_enabled')) {
if (sharedFileConfig.endpoint_discovery_enabled === undefined) {
throw util.error(new Error(), {
code: 'ConfigurationException',
message: 'config file entry \'endpoint_discovery_enabled\' cannot be set to nothing'
});
}
if (!isFalsy(sharedFileConfig.endpoint_discovery_enabled)) return true;
}
return false;
}
/**
* attach endpoint discovery logic to request object
* @param [object] request
* @api private
*/
function discoverEndpoint(request, done) {
var service = request.service || {};
if (hasCustomEndpoint(service) || request.isPresigned()) return done();
if (!isEndpointDiscoveryApplicable(request)) return done();
request.httpRequest.appendToUserAgent('endpoint-discovery');
var operations = service.api.operations || {};
var operationModel = operations[request.operation];
var isEndpointDiscoveryRequired = operationModel ? operationModel.endpointDiscoveryRequired : 'NULL';
switch (isEndpointDiscoveryRequired) {
case 'OPTIONAL':
optionalDiscoverEndpoint(request);
request.addNamedListener('INVALIDATE_CACHED_ENDPOINTS', 'extractError', invalidateCachedEndpoints);
done();
break;
case 'REQUIRED':
request.addNamedListener('INVALIDATE_CACHED_ENDPOINTS', 'extractError', invalidateCachedEndpoints);
requiredDiscoverEndpoint(request, done);
break;
case 'NULL':
default:
done();
break;
}
}
module.exports = {
discoverEndpoint: discoverEndpoint,
requiredDiscoverEndpoint: requiredDiscoverEndpoint,
optionalDiscoverEndpoint: optionalDiscoverEndpoint,
marshallCustomIdentifiers: marshallCustomIdentifiers,
getCacheKey: getCacheKey,
invalidateCachedEndpoint: invalidateCachedEndpoints,
};