k2hr3-api 1.0.42 → 2.0.1

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 +4 -4
  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 -378
  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
@@ -0,0 +1,695 @@
1
+ "use strict";
2
+ /*
3
+ * K2HR3 REST API
4
+ *
5
+ * Copyright 2017 Yahoo Japan Corporation.
6
+ *
7
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
8
+ * common management information for the cloud.
9
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
10
+ * These are stored as roles, resources, policies in K2hdkc, and the
11
+ * client system can dynamically read and modify these information.
12
+ *
13
+ * For the full copyright and license information, please view
14
+ * the license file that was distributed with this source code.
15
+ *
16
+ * AUTHOR: Takeshi Nakatani
17
+ * CREATE: Wed Jun 8 2017
18
+ * REVISION:
19
+ *
20
+ */
21
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ var desc = Object.getOwnPropertyDescriptor(m, k);
24
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
25
+ desc = { enumerable: true, get: function() { return m[k]; } };
26
+ }
27
+ Object.defineProperty(o, k2, desc);
28
+ }) : (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ o[k2] = m[k];
31
+ }));
32
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
33
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
34
+ }) : function(o, v) {
35
+ o["default"] = v;
36
+ });
37
+ var __importStar = (this && this.__importStar) || (function () {
38
+ var ownKeys = function(o) {
39
+ ownKeys = Object.getOwnPropertyNames || function (o) {
40
+ var ar = [];
41
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
42
+ return ar;
43
+ };
44
+ return ownKeys(o);
45
+ };
46
+ return function (mod) {
47
+ if (mod && mod.__esModule) return mod;
48
+ var result = {};
49
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
50
+ __setModuleDefault(result, mod);
51
+ return result;
52
+ };
53
+ })();
54
+ var __importDefault = (this && this.__importDefault) || function (mod) {
55
+ return (mod && mod.__esModule) ? mod : { "default": mod };
56
+ };
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.openstackapiv2 = void 0;
59
+ const https = __importStar(require("https"));
60
+ const http = __importStar(require("http"));
61
+ const k2hr3apiutil_1 = __importDefault(require("./k2hr3apiutil"));
62
+ const dbglogging_1 = __importDefault(require("./dbglogging"));
63
+ const openstackep_1 = __importDefault(require("./openstackep")); // OpenStack KeyStone EndPoint(=osksep)
64
+ const cacerts_1 = require("./cacerts");
65
+ //
66
+ // Get Unscoped token by user name and passwd from Openstack identity v2 API
67
+ //
68
+ // Document: https://developer.openstack.org/api-ref/identity/v2/?expanded=authenticate-detail
69
+ //
70
+ // Request: {
71
+ // "auth": {
72
+ // "tenantName": "",
73
+ // "passwordCredentials": {
74
+ // "username": "user",
75
+ // "password": "passphrase"
76
+ // }
77
+ // }
78
+ // }
79
+ //
80
+ // Response: {
81
+ // "access": {
82
+ // "metadata": {
83
+ // "is_admin": 0,
84
+ // "roles": []
85
+ // },
86
+ // "serviceCatalog": [],
87
+ // "token": {
88
+ // "audit_ids": [
89
+ // "######################"
90
+ // ],
91
+ // "expires": "2017-06-16T07:12:01Z", (*2)
92
+ // "id": "*************************......", (*1)
93
+ // "issued_at": "2017-06-15T07:12:01.822673"
94
+ // },
95
+ // "user": {
96
+ // "id": "************************.....", (*4)
97
+ // "name": "name", (*3)
98
+ // "roles": [],
99
+ // "roles_links": [],
100
+ // "username": "user"
101
+ // }
102
+ // }
103
+ // }
104
+ //
105
+ // callback(error, result):
106
+ // result = {
107
+ // user: user name (*3)
108
+ // userid: user id (*4)
109
+ // scoped: false (always false)
110
+ // token: token string(id) (*1)
111
+ // expire: expire string (*2)
112
+ // region: region string (region name for keystone endpoint)
113
+ // token_seed: seed ({publisher: 'OPENSTACKV2'})
114
+ // }
115
+ //
116
+ const rawGetUserUnscopedTokenV2 = (uname, passwd, callback) => {
117
+ if (!k2hr3apiutil_1.default.isSafeString(uname)) {
118
+ const error = new Error('some parameters are wrong : uname=' + JSON.stringify(uname));
119
+ dbglogging_1.default.elog(error.message);
120
+ callback(error, null);
121
+ return;
122
+ }
123
+ if (!k2hr3apiutil_1.default.isSafeString(passwd)) {
124
+ passwd = null;
125
+ }
126
+ const _uname = uname;
127
+ const _passwd = passwd;
128
+ const _callback = callback;
129
+ // get end points for keystone
130
+ openstackep_1.default.getKeystoneEndpoint((err, keystone_ep) => {
131
+ if (k2hr3apiutil_1.default.isSafeEntity(err) || !k2hr3apiutil_1.default.isSafeEntity(keystone_ep)) {
132
+ const error = new Error('could not get keystone end point by ' + (k2hr3apiutil_1.default.isSafeEntity(err) ? k2hr3apiutil_1.default.getSafeString(err.message) : ''));
133
+ dbglogging_1.default.elog(error.message);
134
+ _callback(error, null);
135
+ return;
136
+ }
137
+ // got safe endpoint for keystone
138
+ //r3logger.dlog(keystone_ep);
139
+ // build parameters for request
140
+ const body = {
141
+ 'auth': {
142
+ 'tenantName': '', // unscoped token by no tenant name
143
+ 'passwordCredentials': {
144
+ 'username': _uname,
145
+ 'password': _passwd
146
+ }
147
+ }
148
+ };
149
+ const agent = k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) ? https : http;
150
+ const strbody = JSON.stringify(body);
151
+ const headers = {
152
+ 'Content-Type': 'application/json',
153
+ 'Content-Length': strbody.length
154
+ };
155
+ const options = {
156
+ 'host': keystone_ep.hostname ?? '',
157
+ 'port': keystone_ep.port ?? 0,
158
+ 'path': keystone_ep.pathname + '/v2.0/tokens',
159
+ 'method': 'POST',
160
+ 'headers': headers,
161
+ 'ca': (k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) && null !== cacerts_1.ca) ? ((0, cacerts_1.ca)() ?? undefined) : undefined
162
+ };
163
+ // send request
164
+ const req = agent.request(options, (res) => {
165
+ let body = '';
166
+ const status = res.statusCode;
167
+ dbglogging_1.default.dlog('response status: ' + res.statusCode);
168
+ dbglogging_1.default.dlog('response header: ' + JSON.stringify(res.headers));
169
+ res.setEncoding('utf8');
170
+ res.on('data', (chunk) => {
171
+ //r3logger.dlog('response chunk: ' + chunk);
172
+ body += chunk;
173
+ });
174
+ res.on('end', () => {
175
+ if (300 <= (status ?? 500)) {
176
+ const error = new Error('could not get unscoped token by status=' + String(status ?? 500));
177
+ dbglogging_1.default.elog(error.message);
178
+ _callback(error, null);
179
+ return;
180
+ }
181
+ //r3logger.dlog('response body: ' + body);
182
+ let res_body = body;
183
+ if (k2hr3apiutil_1.default.checkSimpleJSON(body)) {
184
+ res_body = JSON.parse(body);
185
+ }
186
+ if (!k2hr3apiutil_1.default.isPlainObject(res_body) ||
187
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access) ||
188
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access.user) ||
189
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.user.name) ||
190
+ !k2hr3apiutil_1.default.compareCaseString(res_body.access.user.name, _uname) ||
191
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.user.id) ||
192
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access.token) ||
193
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.token.id) ||
194
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.token.expires)) {
195
+ const error = new Error('could not get unscoped token by something wrong response body');
196
+ dbglogging_1.default.elog(error.message);
197
+ _callback(error, null);
198
+ return;
199
+ }
200
+ // convert openstack user id(16 bytes hex string) to UUID(not UUID4)
201
+ const user_id_uuid4 = k2hr3apiutil_1.default.getSafeString(k2hr3apiutil_1.default.cvtNumberStringToUuid4(res_body.access.user.id, 16));
202
+ // build result
203
+ const resobj = {
204
+ user: res_body.access.user.name.toLowerCase(),
205
+ userid: user_id_uuid4,
206
+ scoped: false,
207
+ token: res_body.access.token.id.toLowerCase(),
208
+ expire: res_body.access.token.expires,
209
+ region: k2hr3apiutil_1.default.getSafeString(keystone_ep.region).toLowerCase(),
210
+ token_seed: JSON.stringify({ publisher: 'OPENSTACKV2' })
211
+ };
212
+ _callback(null, resobj);
213
+ });
214
+ });
215
+ req.on('error', (exception) => {
216
+ dbglogging_1.default.elog('problem with request: ' + exception.message);
217
+ _callback(exception, null);
218
+ return;
219
+ });
220
+ // write data to request body
221
+ req.write(strbody);
222
+ req.end();
223
+ }, false);
224
+ };
225
+ //
226
+ // Get Scoped token by unscoped token and tenant name from Openstack identity v2 API
227
+ //
228
+ // Document: https://developer.openstack.org/api-ref/identity/v2/?expanded=authenticate-detail
229
+ //
230
+ // Request: {
231
+ // "auth": {
232
+ // "tenantName": "tenant",
233
+ // "token" : {
234
+ // "id": "**********************....."
235
+ // }
236
+ // }
237
+ // }
238
+ //
239
+ // Response: {
240
+ // "access": {
241
+ // "metadata": {
242
+ // "is_admin": 0,
243
+ // "roles": [
244
+ // "************************......."
245
+ // ]
246
+ // },
247
+ // "serviceCatalog": [
248
+ // {
249
+ // "endpoints": [
250
+ // {
251
+ // "adminURL": "http://xxx.yahoo.co.jp:8776/v1/3b737f48168d32c23928b....",
252
+ // "id": "*****************.......",
253
+ // "internalURL": "http://xxx.yahoo.co.jp:8776/v1/3b737f48168d32c23928b....",
254
+ // "publicURL": "https://yyy.yahoo.co.jp:8776/v1/3b737f48168d32c23928b55f....",
255
+ // "region": "region name" (*4)
256
+ // }
257
+ // ],
258
+ // "endpoints_links": [],
259
+ // "name": "cinder",
260
+ // "type": "identity" (*4)
261
+ // },
262
+ // ...
263
+ // ],
264
+ // "token": {
265
+ // "audit_ids": [
266
+ // "######################",
267
+ // "######################"
268
+ // ],
269
+ // "expires": "2017-06-16T07:06:31Z", (*2)
270
+ // "id": "********************************", (*1)
271
+ // "issued_at": "2017-06-15T07:31:12.027688",
272
+ // "tenant": {
273
+ // "description": null,
274
+ // "enabled": true,
275
+ // "id": "************************...",
276
+ // "name": "tenant name" (*5: tenant name)
277
+ // }
278
+ // },
279
+ // "user": {
280
+ // "id": "************************...", (*4)
281
+ // "name": "user", (*3)
282
+ // "roles": [
283
+ // {
284
+ // "name": "_member_"
285
+ // }
286
+ // ],
287
+ // "roles_links": [],
288
+ // "username": "user name"
289
+ // }
290
+ // }
291
+ // }
292
+ //
293
+ // callback(error, result):
294
+ // result = {
295
+ // user: user name (*3)
296
+ // userid: user id (*4)
297
+ // scoped: true (always true)
298
+ // token: token string(id) (*1)
299
+ // expire: expire string (*2)
300
+ // region: region string (identity's *4 for region name)
301
+ // token_seed: seed ({publisher: 'OPENSTACKV2'})
302
+ // }
303
+ //
304
+ const rawGetUserScopedTokenV2 = (unscopedtoken, tenant, callback) => {
305
+ if (!k2hr3apiutil_1.default.isSafeString(unscopedtoken) || !k2hr3apiutil_1.default.isSafeString(tenant)) {
306
+ const error = new Error('some parameters are wrong : unscopedtoken=' + JSON.stringify(unscopedtoken) + ', tenant=' + JSON.stringify(tenant));
307
+ dbglogging_1.default.elog(error.message);
308
+ callback(error, null);
309
+ return;
310
+ }
311
+ const _unscopedtoken = unscopedtoken;
312
+ const _tenant = tenant;
313
+ const _callback = callback;
314
+ // get end points for keystone
315
+ openstackep_1.default.getKeystoneEndpoint((err, keystone_ep) => {
316
+ if (null !== err || null === keystone_ep) {
317
+ const error = new Error('could not get keystone end point by ' + (err?.message ?? ''));
318
+ dbglogging_1.default.elog(error.message);
319
+ _callback(error, null);
320
+ return;
321
+ }
322
+ // got safe endpoint for keystone
323
+ //r3logger.dlog(keystone_ep);
324
+ // build parameters for request
325
+ const body = {
326
+ 'auth': {
327
+ 'tenantName': _tenant,
328
+ 'token': {
329
+ 'id': _unscopedtoken
330
+ }
331
+ }
332
+ };
333
+ const agent = k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) ? https : http;
334
+ const strbody = JSON.stringify(body);
335
+ const headers = {
336
+ 'Content-Type': 'application/json',
337
+ 'Content-Length': strbody.length
338
+ };
339
+ const options = {
340
+ 'host': keystone_ep.hostname ?? '',
341
+ 'port': keystone_ep.port ?? 0,
342
+ 'path': keystone_ep.pathname + '/v2.0/tokens',
343
+ 'method': 'POST',
344
+ 'headers': headers,
345
+ 'ca': (k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) && null !== cacerts_1.ca) ? ((0, cacerts_1.ca)() ?? undefined) : undefined
346
+ };
347
+ // send request
348
+ const req = agent.request(options, (res) => {
349
+ let body = '';
350
+ const status = res.statusCode;
351
+ dbglogging_1.default.dlog('response status: ' + res.statusCode);
352
+ dbglogging_1.default.dlog('response header: ' + JSON.stringify(res.headers));
353
+ res.setEncoding('utf8');
354
+ res.on('data', (chunk) => {
355
+ //r3logger.dlog('response chunk: ' + chunk);
356
+ body += chunk;
357
+ });
358
+ res.on('end', () => {
359
+ if (300 <= (status ?? 500)) {
360
+ const error = new Error('could not get scoped token by status=' + String(status ?? 500));
361
+ dbglogging_1.default.elog(error.message);
362
+ _callback(error, null);
363
+ return;
364
+ }
365
+ //r3logger.dlog('response body: ' + body);
366
+ let res_body = body;
367
+ if (k2hr3apiutil_1.default.checkSimpleJSON(body)) {
368
+ res_body = JSON.parse(body);
369
+ }
370
+ if (!k2hr3apiutil_1.default.isPlainObject(res_body) ||
371
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access) ||
372
+ !k2hr3apiutil_1.default.isNotEmptyArray(res_body.access.serviceCatalog) ||
373
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access.user) ||
374
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.user.name) ||
375
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.user.id) ||
376
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access.token) ||
377
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.token.id) ||
378
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.token.expires) ||
379
+ !k2hr3apiutil_1.default.isPlainObject(res_body.access.token.tenant) ||
380
+ !k2hr3apiutil_1.default.isSafeString(res_body.access.token.tenant.name) ||
381
+ !k2hr3apiutil_1.default.compareCaseString(res_body.access.token.tenant.name, _tenant)) {
382
+ const error = new Error('could not get scoped token by something wrong response body');
383
+ dbglogging_1.default.elog(error.message);
384
+ _callback(error, null);
385
+ return;
386
+ }
387
+ // check & get region
388
+ let region = null;
389
+ for (let cnt = 0; cnt < (res_body?.access?.serviceCatalog?.length ?? 0) && null === region; ++cnt) {
390
+ const tmpCatalog = res_body.access.serviceCatalog[cnt];
391
+ if (!k2hr3apiutil_1.default.isPlainObject(tmpCatalog) ||
392
+ !k2hr3apiutil_1.default.isNotEmptyArray(tmpCatalog.endpoints) ||
393
+ !k2hr3apiutil_1.default.isSafeString(tmpCatalog.type)) {
394
+ dbglogging_1.default.wlog('one of response for scoped token is something wrong : ' + JSON.stringify(res_body));
395
+ continue;
396
+ }
397
+ // target region by type=identity
398
+ if (!k2hr3apiutil_1.default.compareCaseString('identity', tmpCatalog.type)) {
399
+ continue;
400
+ }
401
+ // check region
402
+ for (let cnt2 = 0; cnt2 < tmpCatalog.endpoints.length; ++cnt2) {
403
+ const tmpEp = tmpCatalog.endpoints[cnt2];
404
+ if (k2hr3apiutil_1.default.isPlainObject(tmpEp) &&
405
+ k2hr3apiutil_1.default.isSafeString(tmpEp.region)) {
406
+ if (k2hr3apiutil_1.default.compareCaseString(tmpEp.region, keystone_ep.region)) {
407
+ // found
408
+ region = tmpEp.region;
409
+ break;
410
+ }
411
+ else {
412
+ dbglogging_1.default.wlog('unknown region(' + tmpEp.region + '), we need to find region(' + keystone_ep.region + '), so skip this');
413
+ }
414
+ }
415
+ else {
416
+ dbglogging_1.default.wlog('one of response endpoint for scoped token is something wrong : ' + JSON.stringify(tmpEp));
417
+ }
418
+ }
419
+ }
420
+ if (!k2hr3apiutil_1.default.isString(region)) {
421
+ const error = new Error('could not find request region in result.');
422
+ dbglogging_1.default.elog(error.message);
423
+ _callback(error, null);
424
+ return;
425
+ }
426
+ // convert openstack user id(16 bytes hex string) to UUID(not UUID4)
427
+ const user_id_uuid4 = k2hr3apiutil_1.default.getSafeString(k2hr3apiutil_1.default.cvtNumberStringToUuid4(res_body.access.user.id, 16));
428
+ // build result
429
+ const resobj = {
430
+ user: res_body.access.user.name.toLowerCase(),
431
+ userid: user_id_uuid4,
432
+ scoped: false,
433
+ token: res_body.access.token.id.toLowerCase(),
434
+ expire: res_body.access.token.expires,
435
+ region: region.toLowerCase(),
436
+ token_seed: JSON.stringify({ publisher: 'OPENSTACKV2' })
437
+ };
438
+ _callback(null, resobj);
439
+ });
440
+ });
441
+ req.on('error', (exception) => {
442
+ dbglogging_1.default.elog('problem with request: ' + exception.message);
443
+ _callback(exception, null);
444
+ return;
445
+ });
446
+ // write data to request body
447
+ req.write(strbody);
448
+ req.end();
449
+ }, false);
450
+ };
451
+ //
452
+ // Verify User Token for OpenStack V2
453
+ //
454
+ // user : target user name for token
455
+ // token : check token
456
+ // token_seed : token seed data
457
+ //
458
+ // result : {
459
+ // result: true/false
460
+ // message: null or error message string
461
+ // }
462
+ //
463
+ const rawVerifyUserTokenPublisherV2 = (token_seed) => {
464
+ if (!k2hr3apiutil_1.default.isSafeString(token_seed)) {
465
+ return false;
466
+ }
467
+ // parse seed
468
+ if (!k2hr3apiutil_1.default.checkSimpleJSON(token_seed)) {
469
+ return false;
470
+ }
471
+ const seed = k2hr3apiutil_1.default.parseJSON(token_seed);
472
+ if (!k2hr3apiutil_1.default.isValTypeTokenSeed(seed)) {
473
+ return false;
474
+ }
475
+ if (!k2hr3apiutil_1.default.isSafeString(seed.publisher) ||
476
+ (seed.publisher != 'OPENSTACKV2')) // publisher must be 'OPENSTACKV2'
477
+ {
478
+ return false;
479
+ }
480
+ return true;
481
+ };
482
+ const rawWrapVerifyUserTokenPublisherV2 = (token_seed) => {
483
+ const resobj = { result: true, message: null };
484
+ if (!rawVerifyUserTokenPublisherV2(token_seed)) {
485
+ resobj.result = false;
486
+ resobj.message = 'token_seed(not printable) is not safe entity.';
487
+ dbglogging_1.default.elog(resobj.message);
488
+ return resobj;
489
+ }
490
+ return resobj;
491
+ };
492
+ const rawVerifyUserTokenV2 = (user, token, token_seed) => {
493
+ const resobj = { result: true, message: null };
494
+ if (!k2hr3apiutil_1.default.isSafeString(user) || !k2hr3apiutil_1.default.isSafeString(token) || !k2hr3apiutil_1.default.isSafeString(token_seed)) {
495
+ resobj.result = false;
496
+ resobj.message = 'some parameters are wrong : token=' + JSON.stringify(token) + ', token_seed=<not printable>, user=' + JSON.stringify(user);
497
+ dbglogging_1.default.elog(resobj.message);
498
+ return resobj;
499
+ }
500
+ // check seed
501
+ if (!rawVerifyUserTokenPublisherV2(token_seed)) {
502
+ resobj.result = false;
503
+ resobj.message = 'token_seed(not printable) is not safe entity.';
504
+ dbglogging_1.default.elog(resobj.message);
505
+ return resobj;
506
+ }
507
+ return resobj;
508
+ };
509
+ //
510
+ // Get tenant list by unscoped token from Openstack identity v2 API
511
+ //
512
+ // Document: https://developer.openstack.org/api-ref/identity/v2/?expanded=list-tenants-detail
513
+ //
514
+ // Request: X-Auth-Token: <unscoped token>
515
+ //
516
+ // Response: {
517
+ // "tenants": [
518
+ // {
519
+ // "description": null, (*1)
520
+ // "enabled": true,
521
+ // "id": "***************************.....", (*2)
522
+ // "name": "tenant name" (*3)
523
+ // },
524
+ // {
525
+ // "description": null,
526
+ // "enabled": true,
527
+ // "id": "**************************......",
528
+ // "name": "tenant name"
529
+ // }
530
+ // ],
531
+ // "tenants_links": []
532
+ // }
533
+ //
534
+ // callback(error, result):
535
+ // result = [
536
+ // {
537
+ // name: tenant name (*3)
538
+ // id: tenant id (*2)
539
+ // description: tenant description (*1)
540
+ // display: display name (*3)
541
+ // },
542
+ // ...
543
+ // ]
544
+ //
545
+ // [TODO]
546
+ // Should not we use "tenant id" instead of "tenant name" for "name"?
547
+ // We need this consideration.
548
+ //
549
+ const rawGetUserTenantListV2 = (unscopedtoken, callback) => {
550
+ if (!k2hr3apiutil_1.default.isSafeString(unscopedtoken)) {
551
+ const error = new Error('parameter is wrong : unscopedtoken=' + JSON.stringify(unscopedtoken));
552
+ dbglogging_1.default.elog(error.message);
553
+ callback(error, null);
554
+ return;
555
+ }
556
+ const _unscopedtoken = unscopedtoken;
557
+ const _callback = callback;
558
+ // get end points for keystone
559
+ openstackep_1.default.getKeystoneEndpoint((err, keystone_ep) => {
560
+ if (null !== err || null === keystone_ep) {
561
+ const error = new Error('could not get keystone end point by ' + (err?.message ?? ''));
562
+ dbglogging_1.default.elog(error.message);
563
+ _callback(error, null);
564
+ return;
565
+ }
566
+ // got safe endpoint for keystone
567
+ //r3logger.dlog(keystone_ep);
568
+ // build parameters for request
569
+ const agent = k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) ? https : http;
570
+ const headers = {
571
+ 'Content-Type': 'application/json',
572
+ 'X-Auth-Token': _unscopedtoken,
573
+ 'Content-Length': 0
574
+ };
575
+ const options = {
576
+ 'host': keystone_ep.hostname ?? '',
577
+ 'port': keystone_ep.port ?? 0,
578
+ 'path': keystone_ep.pathname + '/v2.0/tenants',
579
+ 'method': 'GET',
580
+ 'headers': headers,
581
+ 'ca': (k2hr3apiutil_1.default.compareCaseString('https:', keystone_ep.protocol) && null !== cacerts_1.ca) ? ((0, cacerts_1.ca)() ?? undefined) : undefined
582
+ };
583
+ // send request
584
+ const req = agent.request(options, (res) => {
585
+ let body = '';
586
+ const status = res.statusCode;
587
+ dbglogging_1.default.dlog('response status: ' + res.statusCode);
588
+ dbglogging_1.default.dlog('response header: ' + JSON.stringify(res.headers));
589
+ res.setEncoding('utf8');
590
+ res.on('data', (chunk) => {
591
+ //r3logger.dlog('response chunk: ' + chunk);
592
+ body += chunk;
593
+ });
594
+ res.on('end', () => {
595
+ if (300 <= (status ?? 500)) {
596
+ const error = new Error('could not get scoped token by status=' + String(status ?? 500));
597
+ dbglogging_1.default.elog(error.message);
598
+ _callback(error, null);
599
+ return;
600
+ }
601
+ //r3logger.dlog('response body: ' + body);
602
+ let res_body = body;
603
+ if (k2hr3apiutil_1.default.checkSimpleJSON(body)) {
604
+ res_body = JSON.parse(body);
605
+ }
606
+ if (!k2hr3apiutil_1.default.isPlainObject(res_body) ||
607
+ !k2hr3apiutil_1.default.isNotEmptyArray(res_body.tenants)) {
608
+ const error = new Error('failed to get tenant list by unscoped token.');
609
+ dbglogging_1.default.elog(error.message);
610
+ _callback(error, null);
611
+ return;
612
+ }
613
+ // convert result array
614
+ const resobj = [];
615
+ for (let cnt = 0; cnt < res_body.tenants.length; ++cnt) {
616
+ const tmpTenant = res_body.tenants[cnt];
617
+ if (!k2hr3apiutil_1.default.isPlainObject(tmpTenant) ||
618
+ !k2hr3apiutil_1.default.isSafeString(tmpTenant.id) ||
619
+ !k2hr3apiutil_1.default.isSafeString(tmpTenant.name)) {
620
+ dbglogging_1.default.wlog('one of response for tenant list is something wrong : ' + JSON.stringify(tmpTenant));
621
+ continue;
622
+ }
623
+ const tenant = {
624
+ name: tmpTenant.name.toLowerCase(),
625
+ id: tmpTenant.id.toLowerCase(),
626
+ description: k2hr3apiutil_1.default.getSafeString(tmpTenant.description),
627
+ display: tmpTenant.name
628
+ };
629
+ resobj.push(tenant);
630
+ }
631
+ if (0 === resobj.length) {
632
+ const error = new Error('could not get any tenant list by unscoped token.');
633
+ dbglogging_1.default.elog(error.message);
634
+ _callback(error, null);
635
+ return;
636
+ }
637
+ _callback(null, resobj);
638
+ });
639
+ });
640
+ req.on('error', (exception) => {
641
+ dbglogging_1.default.elog('problem with request: ' + exception.message);
642
+ callback(exception, null);
643
+ return;
644
+ });
645
+ }, false);
646
+ };
647
+ //---------------------------------------------------------
648
+ // Exports
649
+ //---------------------------------------------------------
650
+ exports.openstackapiv2 = {
651
+ getUserUnscopedToken: rawGetUserUnscopedTokenV2,
652
+ //
653
+ // update token : not implemented
654
+ //
655
+ getUserUnscopedTokenByToken: (token, callback) => {
656
+ const error = new Error('getUserUnscopedTokenByToken is not implemented');
657
+ dbglogging_1.default.elog(error.message);
658
+ callback(error, null);
659
+ },
660
+ //
661
+ // tenantname : for keystone v2 api
662
+ // tenantid : not used
663
+ //
664
+ getUserScopedToken: rawGetUserScopedTokenV2,
665
+ //
666
+ // Verify seed publisher type
667
+ //
668
+ verifyUserTokenPublisher: rawWrapVerifyUserTokenPublisherV2,
669
+ //
670
+ // Verify token
671
+ //
672
+ // tenant is not used.
673
+ //
674
+ verifyUserToken: (dkcobj_permanent, user, tenant, token, token_seed) => {
675
+ return rawVerifyUserTokenV2(user, token, token_seed);
676
+ },
677
+ //
678
+ // userid : not used
679
+ //
680
+ getUserTenantList: (unscopedtoken, userid, callback) => {
681
+ rawGetUserTenantListV2(unscopedtoken, callback);
682
+ }
683
+ };
684
+ //
685
+ // Default
686
+ //
687
+ exports.default = exports.openstackapiv2;
688
+ /*
689
+ * Local variables:
690
+ * tab-width: 4
691
+ * c-basic-offset: 4
692
+ * End:
693
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
694
+ * vim<600: noexpandtab sw=4 ts=4
695
+ */