k2hr3-api 1.0.41 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/config/k2hr3-init.sh.templ +2 -2
  2. package/dist/.gitkeep +0 -0
  3. package/dist/src/app.js +262 -0
  4. package/{bin → dist/src/bin}/run.sh +1 -1
  5. package/dist/src/bin/watcher.js +113 -0
  6. package/dist/src/bin/www.js +217 -0
  7. package/dist/src/lib/basicipcheck.js +392 -0
  8. package/dist/src/lib/cacerts.js +106 -0
  9. package/dist/src/lib/dbglogging.js +190 -0
  10. package/dist/src/lib/dummyuserapi.js +719 -0
  11. package/dist/src/lib/ipwatch.js +354 -0
  12. package/dist/src/lib/k2hr3acrutil.js +532 -0
  13. package/dist/src/lib/k2hr3apiutil.js +1444 -0
  14. package/dist/src/lib/k2hr3cliutil.js +183 -0
  15. package/dist/src/lib/k2hr3config.js +832 -0
  16. package/dist/src/lib/k2hr3cryptutil.js +258 -0
  17. package/dist/src/lib/k2hr3dkc.js +12121 -0
  18. package/dist/src/lib/k2hr3extdata.js +198 -0
  19. package/dist/src/lib/k2hr3keys.js +207 -0
  20. package/dist/src/lib/k2hr3resutil.js +111 -0
  21. package/dist/src/lib/k2hr3template.js +6546 -0
  22. package/dist/src/lib/k2hr3tokens.js +2643 -0
  23. package/dist/src/lib/k2hr3userdata.js +296 -0
  24. package/dist/src/lib/k8soidc.js +1000 -0
  25. package/dist/src/lib/openstackapiv2.js +695 -0
  26. package/dist/src/lib/openstackapiv3.js +932 -0
  27. package/dist/src/lib/openstackep.js +667 -0
  28. package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
  29. package/dist/src/routes/acr.js +704 -0
  30. package/dist/src/routes/debugVerify.js +294 -0
  31. package/dist/src/routes/extdata.js +219 -0
  32. package/dist/src/routes/list.js +264 -0
  33. package/dist/src/routes/policy.js +840 -0
  34. package/dist/src/routes/resource.js +1489 -0
  35. package/dist/src/routes/role.js +2627 -0
  36. package/dist/src/routes/service.js +908 -0
  37. package/dist/src/routes/tenant.js +1141 -0
  38. package/dist/src/routes/userTokens.js +482 -0
  39. package/dist/src/routes/userdata.js +212 -0
  40. package/dist/src/routes/version.js +103 -0
  41. package/package.json +152 -121
  42. package/ChangeLog +0 -372
  43. package/app.js +0 -292
  44. package/bin/watcher +0 -122
  45. package/bin/www +0 -180
  46. package/eslint.config.mjs +0 -68
  47. package/lib/basicipcheck.js +0 -376
  48. package/lib/cacerts.js +0 -71
  49. package/lib/dbglogging.js +0 -151
  50. package/lib/dummyuserapi.js +0 -766
  51. package/lib/ipwatch.js +0 -379
  52. package/lib/k2hr3acrutil.js +0 -516
  53. package/lib/k2hr3apiutil.js +0 -1494
  54. package/lib/k2hr3cliutil.js +0 -191
  55. package/lib/k2hr3config.js +0 -826
  56. package/lib/k2hr3cryptutil.js +0 -254
  57. package/lib/k2hr3dkc.js +0 -12632
  58. package/lib/k2hr3extdata.js +0 -198
  59. package/lib/k2hr3keys.js +0 -234
  60. package/lib/k2hr3resutil.js +0 -100
  61. package/lib/k2hr3template.js +0 -6925
  62. package/lib/k2hr3tokens.js +0 -2799
  63. package/lib/k2hr3userdata.js +0 -312
  64. package/lib/k8soidc.js +0 -1012
  65. package/lib/openstackapiv2.js +0 -764
  66. package/lib/openstackapiv3.js +0 -1032
  67. package/lib/openstackep.js +0 -553
  68. package/routes/acr.js +0 -738
  69. package/routes/debugVerify.js +0 -263
  70. package/routes/extdata.js +0 -232
  71. package/routes/list.js +0 -270
  72. package/routes/policy.js +0 -869
  73. package/routes/resource.js +0 -1441
  74. package/routes/role.js +0 -2664
  75. package/routes/service.js +0 -894
  76. package/routes/tenant.js +0 -1095
  77. package/routes/userTokens.js +0 -511
  78. package/routes/userdata.js +0 -218
  79. package/routes/version.js +0 -108
  80. package/templ/Dockerfile.templ +0 -71
  81. package/tests/auto_acr.js +0 -1101
  82. package/tests/auto_acr_spec.js +0 -79
  83. package/tests/auto_all_spec.js +0 -142
  84. package/tests/auto_control_subprocess.sh +0 -243
  85. package/tests/auto_extdata.js +0 -220
  86. package/tests/auto_extdata_spec.js +0 -79
  87. package/tests/auto_init_config_json.sh +0 -275
  88. package/tests/auto_k2hdkc_server.ini +0 -109
  89. package/tests/auto_k2hdkc_slave.ini +0 -83
  90. package/tests/auto_list.js +0 -439
  91. package/tests/auto_list_spec.js +0 -79
  92. package/tests/auto_policy.js +0 -1579
  93. package/tests/auto_policy_spec.js +0 -79
  94. package/tests/auto_resource.js +0 -10956
  95. package/tests/auto_resource_spec.js +0 -79
  96. package/tests/auto_role.js +0 -6150
  97. package/tests/auto_role_spec.js +0 -79
  98. package/tests/auto_service.js +0 -770
  99. package/tests/auto_service_spec.js +0 -79
  100. package/tests/auto_subprocesses.js +0 -114
  101. package/tests/auto_template.sh +0 -126
  102. package/tests/auto_tenant.js +0 -1100
  103. package/tests/auto_tenant_spec.js +0 -79
  104. package/tests/auto_token_util.js +0 -219
  105. package/tests/auto_userdata.js +0 -292
  106. package/tests/auto_userdata_spec.js +0 -79
  107. package/tests/auto_usertokens.js +0 -565
  108. package/tests/auto_usertokens_spec.js +0 -79
  109. package/tests/auto_version.js +0 -127
  110. package/tests/auto_version_spec.js +0 -79
  111. package/tests/auto_watcher.js +0 -157
  112. package/tests/auto_watcher_spec.js +0 -79
  113. package/tests/k2hdkc_test.data +0 -986
  114. package/tests/k2hdkc_test_load.sh +0 -255
  115. package/tests/k2hr3template_test.js +0 -187
  116. package/tests/k2hr3template_test.sh +0 -339
  117. package/tests/k2hr3template_test_async.js +0 -216
  118. package/tests/k2hr3template_test_template.result +0 -7117
  119. package/tests/k2hr3template_test_template.txt +0 -3608
  120. package/tests/k2hr3template_test_vars.js +0 -194
  121. package/tests/manual_acr_delete.js +0 -143
  122. package/tests/manual_acr_get.js +0 -297
  123. package/tests/manual_acr_postput.js +0 -215
  124. package/tests/manual_allusertenant_get.js +0 -113
  125. package/tests/manual_extdata_get.js +0 -191
  126. package/tests/manual_k2hr3keys_get.js +0 -84
  127. package/tests/manual_list_gethead.js +0 -230
  128. package/tests/manual_policy_delete.js +0 -132
  129. package/tests/manual_policy_gethead.js +0 -275
  130. package/tests/manual_policy_postput.js +0 -297
  131. package/tests/manual_resource_delete.js +0 -433
  132. package/tests/manual_resource_gethead.js +0 -423
  133. package/tests/manual_resource_postput.js +0 -487
  134. package/tests/manual_role_delete.js +0 -404
  135. package/tests/manual_role_gethead.js +0 -547
  136. package/tests/manual_role_postput.js +0 -544
  137. package/tests/manual_service_delete.js +0 -153
  138. package/tests/manual_service_gethead.js +0 -178
  139. package/tests/manual_service_postput.js +0 -348
  140. package/tests/manual_tenant_delete.js +0 -186
  141. package/tests/manual_tenant_gethead.js +0 -268
  142. package/tests/manual_tenant_postput.js +0 -293
  143. package/tests/manual_test.sh +0 -352
  144. package/tests/manual_userdata_get.js +0 -173
  145. package/tests/manual_usertoken_gethead.js +0 -136
  146. package/tests/manual_usertoken_postput.js +0 -310
  147. package/tests/manual_version_get.js +0 -127
  148. package/tests/run_local_test_k2hdkc.sh +0 -174
  149. package/tests/test.sh +0 -333
@@ -1,553 +0,0 @@
1
- /*
2
- * K2HR3 REST API
3
- *
4
- * Copyright 2017 Yahoo Japan Corporation.
5
- *
6
- * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
7
- * common management information for the cloud.
8
- * K2HR3 can dynamically manage information as "who", "what", "operate".
9
- * These are stored as roles, resources, policies in K2hdkc, and the
10
- * client system can dynamically read and modify these information.
11
- *
12
- * For the full copyright and license information, please view
13
- * the license file that was distributed with this source code.
14
- *
15
- * AUTHOR: Takeshi Nakatani
16
- * CREATE: Wed Jun 8 2017
17
- * REVISION:
18
- *
19
- */
20
-
21
- 'use strict';
22
-
23
- var http = require('http');
24
- var https = require('https');
25
-
26
- var cacerts = require('../lib/cacerts');
27
- var apiutil = require('./k2hr3apiutil');
28
- var k2hr3 = require('./k2hr3dkc');
29
- var r3keys = require('./k2hr3keys').getK2hr3Keys;
30
-
31
- // Debug logging objects
32
- var r3logger = require('../lib/dbglogging');
33
-
34
- //---------------------------------------------------------
35
- // Keystone api wrapper
36
- //---------------------------------------------------------
37
- var kseplist = null;
38
- var ksepobj = null;
39
-
40
- (function()
41
- {
42
- var r3Conf = require('./k2hr3config').r3ApiConfig;
43
- var apiConf = new r3Conf();
44
-
45
- if(apiConf.isKeystoneEpList()){
46
- kseplist = apiConf.getKeystoneEpList();
47
- }else if(apiConf.isKeystoneEpFile()){
48
- var ksepobj_path= './' + apiConf.getKeystoneEpFile();
49
- ksepobj = require(ksepobj_path);
50
- }
51
- }());
52
-
53
- //---------------------------------------------------------
54
- // Callback function for testing one keystone endpoint
55
- //---------------------------------------------------------
56
- // Input parameter:
57
- // err : if error is occurred, error object is set(if no error, this is null).
58
- // callback : specify the callback to be called when all epallmap is checked.
59
- // epallmap : all of keystone endpoint
60
- // region : checked region
61
- // status_code : check result
62
- //
63
- // Result: callback(error, epallmap)
64
- // error : if error is occurred, error object is set(if no error, this is null).
65
- // epallmap : all endpoint mapping
66
- //
67
- // [NOTE]
68
- // The callback function is only called when all checks of
69
- // epallmap are complete.
70
- // This assumes that the caller will call this function on all
71
- // epallmaps.
72
- //
73
- var rawTestKeystoneEpCallback = function(err, callback, epallmap, region, status_code)
74
- {
75
- var error;
76
- if(!apiutil.isSafeEntity(epallmap) || !apiutil.isSafeEntity(callback) || !apiutil.isSafeString(region) || !apiutil.isSafeEntity(epallmap[region])){
77
- error = new Error('some parameters are wrong : epallmap=' + JSON.stringify(epallmap) + ', region=' + JSON.stgingify(region) + ' epallmap[region]=' + JSON.stringify(epallmap[region]));
78
- r3logger.elog(error.message);
79
- if(apiutil.isSafeEntity(callback)){
80
- callback(error);
81
- }
82
- return;
83
- }
84
- if(!apiutil.isSafeEntity(status_code) || isNaN(status_code)){
85
- r3logger.elog('parameter is wrong : status_code=' + JSON.stringify(status_code) + ', but continue with status_code(500).');
86
- status_code = 500; // status code = internal error
87
- }
88
- if(null !== err){
89
- r3logger.elog('failed test about keystone endpoint(' + epallmap[region].url + ') for region(' + region + ') by ' + err.message + ', but continue with status_code(500).');
90
- status_code = 500; // status code = internal error
91
- }
92
-
93
- // set checked flag and status result
94
- //
95
- // [NOTE][TODO]
96
- // Under control by this flag, exclusion control is not perfect.
97
- // In other words, the callback function may be called multiple times.
98
- // (However, the callback function will be called at least once.)
99
- // Currently, exclusion control here is not performed, and the caller
100
- // recognizes multiple calls. Here is the code to fix.
101
- //
102
- epallmap[region].checked = true;
103
- epallmap[region].status = status_code;
104
-
105
- // check for finish
106
- var is_finish = true;
107
- for(var test_region in epallmap){
108
- if(!epallmap[test_region].checked){
109
- is_finish = false;
110
- break;
111
- }
112
- }
113
- if(is_finish){
114
- // checked all of endpoints, then call original callback.
115
- //
116
- // [NOTE]
117
- // Perhaps this function passes here multiple times.
118
- //
119
- callback(null);
120
- }
121
- };
122
-
123
- //---------------------------------------------------------
124
- // Test one keystone endpoint
125
- //---------------------------------------------------------
126
- // Input parameter
127
- // epallmap : all of keystone endpoint
128
- // region : target one region for checking
129
- // endpoint : target one endpoint for checking
130
- // is_v3 : keystone v3 or v2
131
- // timeout : timeout for checking one endpoint
132
- // lastest_callback : callback function which is called end of last checking endpoint.
133
- //
134
- // Result: lastest_callback(error, orgcb, epallmap, region, status_code, orgcb)
135
- // error : error object
136
- // orgcb : original callback
137
- // epallmap : all endpoint mapping
138
- // region : region
139
- // status_code : status code for request(timeout = 504)
140
- //
141
- // [NOTE]
142
- // The lastest_callback callback function is only called when
143
- // all checks of epallmap are complete.
144
- // This assumes that the caller will call this function on all
145
- // epallmaps.
146
- //
147
- function rawTestKeystoneEndpoint(epallmap, region, endpoint, is_v3, timeout, lastest_callback)
148
- {
149
- var error;
150
- if(!apiutil.isSafeEntity(epallmap) || !apiutil.isSafeEntity(lastest_callback) || !apiutil.isSafeStrings(region, endpoint)){
151
- error = new Error('some parameters are wrong : epallmap=' + JSON.stringify(epallmap) + ', region=' + JSON.stgingify(region) + ', endpoint=' + JSON.stringify(endpoint));
152
- r3logger.elog(error.message);
153
- rawTestKeystoneEpCallback(error, lastest_callback, epallmap, region, 500); // return result code = 500
154
- return;
155
- }
156
- if(!apiutil.isSafeEntity(timeout) || isNaN(timeout)){
157
- error = new Error('parameter is wrong : timeout=' + JSON.stringify(timeout));
158
- r3logger.elog(error.message);
159
- rawTestKeystoneEpCallback(error, lastest_callback, epallmap, region, 500); // return result code = 500
160
- return;
161
- }
162
- if('boolean' !== typeof is_v3){
163
- is_v3 = true;
164
- }
165
- var _epallmap = epallmap;
166
- var _region = region;
167
- var _endpoint = endpoint;
168
- var _is_v3 = is_v3;
169
- var _timeout = timeout;
170
- var _lastest_cb = lastest_callback;
171
-
172
- // Make request body data
173
- // This body failed authorization.(wrong user/passwd)
174
- //
175
- var ep = apiutil.urlParse(_endpoint);
176
- var body;
177
- var strbody;
178
- var headers;
179
- var options;
180
- if(!_is_v3){
181
- body = {
182
- 'auth': {
183
- 'tenantName': '', // unscoped token for test
184
- 'passwordCredentials': {
185
- 'username': '', // user name is empty for testing
186
- 'password': '' // unauthorized passwd
187
- }
188
- }
189
- };
190
- strbody = JSON.stringify(body);
191
- headers = {
192
- 'Content-Type': 'application/json',
193
- 'Content-Length': strbody.length
194
- };
195
- options = {
196
- 'host': ep.hostname,
197
- 'port': ep.port,
198
- 'path': '/v2.0/tokens',
199
- 'method': 'POST',
200
- 'headers': headers
201
- };
202
- }else{
203
- body = {
204
- 'auth': {
205
- 'identity': {
206
- 'password': {
207
- 'user': {
208
- 'domain': {
209
- 'id': 'default'
210
- },
211
- 'name': '', // user name is empty for testing
212
- 'password': '' // unauthorized passwd
213
- }
214
- },
215
- 'methods': ['password']
216
- }
217
- }
218
- };
219
- strbody = JSON.stringify(body);
220
- headers = {
221
- 'Content-Type': 'application/json',
222
- 'Content-Length': strbody.length
223
- };
224
- options = {
225
- 'host': ep.hostname,
226
- 'port': ep.port,
227
- 'path': '/v3/auth/tokens',
228
- 'method': 'POST',
229
- 'headers': headers
230
- };
231
- }
232
-
233
- var httpobj;
234
- if(apiutil.compareCaseString('https:', ep.protocol)){
235
- if(null !== cacerts.ca){
236
- options.ca = cacerts.ca;
237
- }
238
- options.agent = new https.Agent(options);
239
- httpobj = https;
240
- }else{
241
- options.agent = new http.Agent(options);
242
- httpobj = http;
243
- }
244
-
245
- var req = httpobj.request(options, function(res)
246
- {
247
- var body = ''; // eslint-disable-line no-unused-vars
248
- var status = res.statusCode;
249
-
250
- r3logger.dlog('response status: ' + res.statusCode);
251
- r3logger.dlog('response header: ' + JSON.stringify(res.headers));
252
- res.setEncoding('utf8');
253
-
254
- res.on('data', function(chunk)
255
- {
256
- //r3logger.dlog('response chunk: ' + chunk);
257
- body += chunk;
258
- });
259
-
260
- res.on('end', function(res) // eslint-disable-line no-unused-vars
261
- {
262
- rawTestKeystoneEpCallback(null, _lastest_cb, _epallmap, _region, status); // return result code
263
- });
264
- });
265
-
266
- req.on('error', function(err)
267
- {
268
- rawTestKeystoneEpCallback(err, _lastest_cb, _epallmap, _region, 504); // return result code = 500
269
- });
270
-
271
- req.on('socket', function(socket)
272
- {
273
- socket.setTimeout(_timeout, function()
274
- {
275
- req.abort();
276
- rawTestKeystoneEpCallback(null, _lastest_cb, _epallmap, _region, 504); // return result code = 504
277
- });
278
- });
279
-
280
- // write data to request body
281
- req.write(strbody);
282
- req.end();
283
- }
284
-
285
- //---------------------------------------------------------
286
- // Callback function for dynamic/static endpoint list
287
- //---------------------------------------------------------
288
- // Input parameter:
289
- // cbargs : parent function parameter in this object
290
- // timeout : rawGetKeystoneEndpoint function argument
291
- // is_test : rawGetKeystoneEndpoint function argument
292
- // is_v3 : rawGetKeystoneEndpoint function argument
293
- // callback : rawGetKeystoneEndpoint function argument
294
- // err : if error is occurred, error object is set(if no error, this is null).
295
- // epmap : keystone endpoint map which is result of dynamic/static list.
296
- //
297
- // Result: callback(error, keystone_ep)
298
- // error : error object
299
- // keystone_ep : url object with region string element
300
- //
301
- function rawGetKeystoneEndpointsCallback(cbargs, err, epmap)
302
- {
303
- var _cbargs = cbargs;
304
- var error;
305
-
306
- if(null !== err){
307
- error = new Error('failed to get dynamic keystone endpoints : ' + err.message);
308
- r3logger.elog(error.message);
309
- _cbargs.callback(error, null);
310
- return;
311
- }
312
- if(!apiutil.isSafeEntity(epmap)){
313
- error = new Error('getting dynamic keystone endpoints is something wrong.');
314
- r3logger.elog(error.message);
315
- _cbargs.callback(error, null);
316
- return;
317
- }
318
-
319
- //
320
- // check and register endpoints to k2hdkc
321
- //
322
- var keys = r3keys();
323
- var epallmap= {};
324
- for(var region in epmap){
325
- if(!apiutil.isSafeString(epmap[region])){
326
- // wrong data
327
- r3logger.wlog('dynamic keystone endpoint for ' + region + ' is something wrong(' + JSON.stringify(epmap[region]) + '), thus skip it.');
328
- continue;
329
- }
330
- // register only https!
331
- var ep = apiutil.urlParse(epmap[region]);
332
- if(!apiutil.compareCaseString('https:', ep.protocol)){
333
- // not https
334
- r3logger.wlog('dynamic keystone endpoint for ' + region + ' is not https(' + ep.protocol + '), it is not good endpoint.');
335
- }
336
-
337
- // add to temporary
338
- var tmpmap ={
339
- 'url': epmap[region],
340
- 'checked': false,
341
- 'status': 0
342
- };
343
- epallmap[region] = tmpmap;
344
- }
345
-
346
- if(!_cbargs.is_test){
347
- // not need to test(only updates), finish here
348
- //
349
- // [NOTE]
350
- // keystone endpoint is null
351
- //
352
- _cbargs.callback(null, null);
353
- return;
354
- }
355
-
356
- // test all endpoints(asynchronous)
357
- for(region in epallmap){
358
- // check each endpoint
359
- //
360
- // [NOTE]
361
- // The callback function is called only when all the elements of
362
- // epallmap are checked.
363
- //
364
- rawTestKeystoneEndpoint(epallmap, region, epallmap[region].url, _cbargs.is_v3, _cbargs.timeout, function(err)
365
- {
366
- var _callback = _cbargs.callback;
367
- var _epallmap = epallmap;
368
-
369
- if(null !== err){
370
- var error = new Error('failed to check keystone endpoints : ' + err.message);
371
- r3logger.elog(error.message);
372
- _callback(error, null);
373
- return;
374
- }
375
-
376
- // set all endpoint map
377
- for(var region2 in _epallmap){
378
- // register endpoint(type should be VALUE_KEYSTONE_NORMAL)
379
- //
380
- // [NOTE][TODO]
381
- // Now, we do not distinguish v2 and v3 keystone, we are registering it into k2hdkc.
382
- // This may possibly cause problems.
383
- //
384
- var res_ep = k2hr3.setKeystoneEndpointAll(region2, _epallmap[region2].url, keys.VALUE_KEYSTONE_NORMAL, 0);
385
- if(!res_ep.result){
386
- r3logger.elog('could not set keystone endpoint(' + _epallmap[region2].url + ') for region2(' + region2 + ') into k2hdkc, but continue...');
387
- }else{
388
- r3logger.mlog('add new keystone endpoint(' + _epallmap[region2].url + ') for region2(' + region2 + ') into k2hdkc.');
389
- }
390
- }
391
-
392
- // call ownself with not reentrant flag
393
- rawGetKeystoneEndpoint(function(err, keystone_ep)
394
- {
395
- if(null !== err){
396
- var error = new Error('failed to get keystone endpoint : ' + err.message);
397
- r3logger.elog(error.message);
398
- _callback(error, null);
399
- return;
400
- }
401
-
402
- //
403
- // returns keystone endpoint after remaking all endpoint
404
- //
405
- _callback(null, keystone_ep);
406
-
407
- }, _cbargs.is_v3, _cbargs.is_test, _cbargs.timeout, false);
408
- });
409
- }
410
- }
411
-
412
- //---------------------------------------------------------
413
- // Get one of endpoint for keystone from static urls
414
- //---------------------------------------------------------
415
- // Input parameter
416
- // callback : specify callback function
417
- // is_v3 : keystone api v3 or v2(default v3)
418
- // is_test : specify whether to test each keystone
419
- // endpoint after unable to find a valid
420
- // keystone endpoint and re-creating it.
421
- // (default true)
422
- // timeout : specify the timeout required to check
423
- // each keystone endpoint.
424
- // (default 30s)
425
- // is_remake_keystone_ep : if keystone endpoint is not registered,
426
- // it specifies whether to recreate it.
427
- // (default false)
428
- //
429
- // Result: callback(error, keystone_ep)
430
- // error : error object
431
- // keystone_ep : url object with region string element
432
- //
433
- var last_region = null;
434
- var last_endpoint = null;
435
-
436
- function rawGetKeystoneEndpoint(callback, is_v3, is_test, timeout, is_remake_keystone_ep)
437
- {
438
- var error;
439
- if(!apiutil.isSafeEntity(timeout) || isNaN(timeout)){
440
- timeout = 30000; // default 30s
441
- }
442
- if('boolean' !== typeof is_test){
443
- is_test = true; // default true
444
- }
445
- if('boolean' !== typeof is_v3){
446
- is_v3 = true; // default v3
447
- }
448
- if('boolean' !== typeof is_remake_keystone_ep){
449
- is_remake_keystone_ep = false;
450
- }
451
-
452
- // [NOTE]
453
- // This object is inherited from getDynamicKeystoneEndpoints
454
- // to rawGetKeystoneEndpointsCallback.
455
- // Ultimately, this function is called recursively and receives
456
- // the data of this object as an argument.
457
- //
458
- var cbargs = {
459
- timeout: timeout,
460
- is_test: is_test,
461
- is_v3: is_v3,
462
- callback: callback
463
- };
464
-
465
- var keystone_ep;
466
- if(apiutil.isSafeString(last_region) && apiutil.isSafeString(last_endpoint)){
467
- // there is a cache for endpoint/region
468
- keystone_ep = apiutil.urlParse(last_endpoint);
469
- keystone_ep.region = last_region;
470
- cbargs.callback(null, keystone_ep);
471
- return;
472
- }
473
- // there is no cache for endpoint/region
474
-
475
- var allres = k2hr3.getKeystoneEndpointAll();
476
- if(!allres.result){
477
- r3logger.elog('failed to get all keystone endpoint : ' + allres.message + 'but continue for recovering.');
478
- }
479
- var keystones = apiutil.isSafeEntity(allres.keystones) ? allres.keystones : {};
480
-
481
- // search OK status from all
482
- var keys = r3keys();
483
- for(var region in keystones){
484
- if( apiutil.isSafeEntity(keystones[region]) &&
485
- apiutil.isSafeString(keystones[region].url) &&
486
- apiutil.isSafeString(keystones[region].type) &&
487
- keys.VALUE_KEYSTONE_NORMAL === keystones[region].type &&
488
- apiutil.isSafeEntity(keystones[region].status) &&
489
- keystones[region].status < 500 ) // allow 0, 2xx, 3xx, 4xx
490
- {
491
- // found reachable endpoint, then set cache and result
492
- last_region = region;
493
- last_endpoint = keystones[region].url;
494
- keystone_ep = apiutil.urlParse(last_endpoint);
495
- keystone_ep.region = last_region;
496
-
497
- cbargs.callback(null, keystone_ep);
498
- return;
499
- }
500
- }
501
-
502
- //
503
- // There are no available endpoints
504
- //
505
- if(!is_remake_keystone_ep){
506
- error = new Error('there is no safe keystone endpoints.');
507
- r3logger.elog(error.message);
508
- cbargs.callback(error, null);
509
- return;
510
- }
511
-
512
- //
513
- // try to remake keystone endpoint
514
- //
515
- if(apiutil.isSafeEntity(kseplist)){
516
- //
517
- // Get a static keystone endpoint.
518
- //
519
- rawGetKeystoneEndpointsCallback(cbargs, null, kseplist);
520
-
521
- }else if(apiutil.isSafeEntity(ksepobj)){
522
- //
523
- // Get keystone endpoint list dynamically.
524
- //
525
- ksepobj.getDynamicKeystoneEndpoints(cbargs, rawGetKeystoneEndpointsCallback);
526
-
527
- }else{
528
- //
529
- // Wrong configuration
530
- //
531
- error = new Error('keystone endpoint configuration is something wrong.');
532
- r3logger.elog(error.message);
533
- cbargs.callback(error, null);
534
- return;
535
- }
536
- }
537
-
538
- //---------------------------------------------------------
539
- // Exports
540
- //---------------------------------------------------------
541
- exports.getKeystoneEndpoint = function(callback, is_v3, is_test, timeout)
542
- {
543
- return rawGetKeystoneEndpoint(callback, is_v3, is_test, timeout, true);
544
- };
545
-
546
- /*
547
- * Local variables:
548
- * tab-width: 4
549
- * c-basic-offset: 4
550
- * End:
551
- * vim600: noexpandtab sw=4 ts=4 fdm=marker
552
- * vim<600: noexpandtab sw=4 ts=4
553
- */