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,294 @@
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: Mon Nov 6 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
+ const k2hr3apiutil_1 = __importDefault(require("../lib/k2hr3apiutil"));
59
+ const k2hr3resutil_1 = __importDefault(require("../lib/k2hr3resutil"));
60
+ const k2hr3tokens_1 = __importDefault(require("../lib/k2hr3tokens"));
61
+ const dbglogging_1 = __importDefault(require("../lib/dbglogging"));
62
+ const https = __importStar(require("https"));
63
+ const http = __importStar(require("http"));
64
+ const express_1 = __importDefault(require("express"));
65
+ const cacerts_1 = require("../lib/cacerts");
66
+ const router = express_1.default.Router();
67
+ //
68
+ // Debug Verify URL
69
+ //
70
+ // This router is for debugging verify url for dummy service.
71
+ // This is called only on development environment.
72
+ //
73
+ // Mountpath : '/v1/debug/verify'
74
+ //
75
+ // GET '/v1/debug/verify' : get verify for debug on version 1
76
+ // URL argument
77
+ // service : service name(default testservice)
78
+ // HEADER : X-Auth-Token => Scoped User token(without 'U=' prefix)
79
+ // response body = [ : undefined/null or resource array(if one element, allows only it not array)
80
+ // {
81
+ // name : resource name which is key name(path) for resource
82
+ // expire : undefined/null or integer
83
+ // type : resource data type(string or object), if date is null or '', this value must be string.
84
+ // data : resource data which must be string or object or null/undefined.
85
+ // keys = { : resource has keys(associative array), or null/undefined.
86
+ // 'foo': bar, : any value is allowed
87
+ // ... :
88
+ // } :
89
+ // },
90
+ // ]
91
+ //
92
+ router.get('/', (req, res, next) => {
93
+ dbglogging_1.default.dlog('CALL:', req.method, req.url);
94
+ if ('GET' !== req.method) {
95
+ // HEAD request comes here, so it should be routed to head function.
96
+ next();
97
+ return;
98
+ }
99
+ res.type('application/json; charset=utf-8');
100
+ //
101
+ // Check request
102
+ //
103
+ if (!k2hr3apiutil_1.default.isPlainObject(req) ||
104
+ !k2hr3apiutil_1.default.isSafeString(req.baseUrl) ||
105
+ !k2hr3apiutil_1.default.isSafeString(req.method) ||
106
+ !k2hr3apiutil_1.default.isSafeString(req.protocol) ||
107
+ !k2hr3apiutil_1.default.isPlainObject(req.query) ||
108
+ (!k2hr3apiutil_1.default.isSafeString(req.host) && !k2hr3apiutil_1.default.isSafeString(req.hostname)) ||
109
+ !k2hr3apiutil_1.default.isPlainObject(req.headers) ||
110
+ !k2hr3apiutil_1.default.isSafeEntity(req.headers.host)) {
111
+ dbglogging_1.default.elog('GET request or url or token is wrong');
112
+ k2hr3resutil_1.default.errResponse(req, res, 400); // 400: Bad Request
113
+ return;
114
+ }
115
+ const _req = req;
116
+ const _res = res;
117
+ //------------------------------
118
+ // check token
119
+ //------------------------------
120
+ const token_result = k2hr3tokens_1.default.checkToken(req, true, true); // scoped, user token
121
+ if (!token_result.result) {
122
+ dbglogging_1.default.elog(k2hr3apiutil_1.default.getSafeString(token_result.message));
123
+ const result = {
124
+ result: token_result.result,
125
+ message: k2hr3apiutil_1.default.getSafeString(token_result.message),
126
+ };
127
+ k2hr3resutil_1.default.errResponse(req, res, token_result.status, result);
128
+ return;
129
+ }
130
+ const _token_info = token_result.token_info;
131
+ if (!k2hr3tokens_1.default.isResTypeCheckRoleToken(_token_info)) {
132
+ const result = {
133
+ result: false,
134
+ message: 'specified wrong token or it is not scoped user token'
135
+ };
136
+ dbglogging_1.default.elog(result.message);
137
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
138
+ return;
139
+ }
140
+ //
141
+ // check arguments
142
+ //
143
+ let _service_name = 'testservice'; // [NOTE] default service name
144
+ if (k2hr3apiutil_1.default.isSafeString(req.query.service) && k2hr3apiutil_1.default.isSafeString(req.query.service.trim())) {
145
+ _service_name = req.query.service.trim();
146
+ }
147
+ //
148
+ // Check localhost information for ACR API
149
+ //
150
+ const urlobj = k2hr3apiutil_1.default.parseUrl(_req.protocol + '://' + _req.headers.host);
151
+ if (null === urlobj || !urlobj.https) {
152
+ if (!k2hr3apiutil_1.default.compareCaseString(_req.protocol, 'http')) {
153
+ dbglogging_1.default.elog('Original request url method is not as same as request method');
154
+ k2hr3resutil_1.default.errResponse(_req, _res, 400); // 400: Bad Request
155
+ return;
156
+ }
157
+ }
158
+ else {
159
+ if (!k2hr3apiutil_1.default.compareCaseString(_req.protocol, 'https')) {
160
+ dbglogging_1.default.elog('Original request url method is not as same as request method');
161
+ k2hr3resutil_1.default.errResponse(_req, _res, 400); // 400: Bad Request
162
+ return;
163
+ }
164
+ }
165
+ if (null === urlobj || (urlobj.host !== k2hr3apiutil_1.default.getSafeString(_req.host) && urlobj.host !== k2hr3apiutil_1.default.getSafeString(_req.hostname))) {
166
+ dbglogging_1.default.elog('Original request url host is not as same as request host');
167
+ k2hr3resutil_1.default.errResponse(_req, _res, 400); // 400: Bad Request
168
+ return;
169
+ }
170
+ //
171
+ // Make request data
172
+ //
173
+ const agent = k2hr3apiutil_1.default.compareCaseString('https:', _req.protocol) ? https : http;
174
+ const headers = {
175
+ 'Content-Type': 'application/json',
176
+ 'Content-Length': 0,
177
+ 'X-Auth-Token': k2hr3apiutil_1.default.getSafeString(_req.headers['x-auth-token']) // Transfer
178
+ };
179
+ const options = {
180
+ 'host': urlobj.host,
181
+ 'port': urlobj.port,
182
+ 'path': '/v1/acr/' + _service_name,
183
+ 'method': 'GET',
184
+ 'headers': headers,
185
+ 'ca': (k2hr3apiutil_1.default.compareCaseString('https:', _req.protocol) && null !== cacerts_1.ca) ? ((0, cacerts_1.ca)() ?? undefined) : undefined
186
+ };
187
+ dbglogging_1.default.dlog('request options = ' + JSON.stringify({ ...options, ca: options.ca ? '[Buffer]' : undefined }));
188
+ dbglogging_1.default.dlog('request headers = ' + JSON.stringify(headers));
189
+ //
190
+ // Send request to localhost
191
+ //
192
+ const subreq = agent.request(options, (subres) => {
193
+ let _body = '';
194
+ const _status = subres.statusCode;
195
+ const _headers = subres.headers;
196
+ dbglogging_1.default.dlog('/v1/acr/testservice response status: ' + _status);
197
+ dbglogging_1.default.dlog('/v1/acr/testservice response header: ' + JSON.stringify(_headers));
198
+ subres.setEncoding('utf8');
199
+ res.on('data', (chunk) => {
200
+ //r3logger.dlog('/v1/acr/testservice response chunk: ' + chunk);
201
+ _body += chunk;
202
+ });
203
+ res.on('end', () => {
204
+ if (300 <= (_status ?? 500)) {
205
+ const _error = new Error('got error response for verify request by status=' + String(_status ?? 500));
206
+ dbglogging_1.default.elog(_error.message);
207
+ k2hr3resutil_1.default.errResponse(_req, _res, _status, _error.message); // 4xx, 5xx
208
+ return;
209
+ }
210
+ //r3logger.dlog('/v1/acr/testservice response body: ' + _body);
211
+ //
212
+ // Check response body
213
+ //
214
+ let res_body = _body;
215
+ if (k2hr3apiutil_1.default.checkSimpleJSON(_body)) {
216
+ res_body = JSON.parse(_body);
217
+ }
218
+ if (!k2hr3apiutil_1.default.isPlainObject(res_body) ||
219
+ !k2hr3apiutil_1.default.isPlainObject(res_body.tokeninfo) ||
220
+ !k2hr3apiutil_1.default.isSafeString(res_body.tokeninfo.service) ||
221
+ !k2hr3apiutil_1.default.compareCaseString(res_body.tokeninfo.service, _service_name) ||
222
+ !k2hr3apiutil_1.default.isSafeString(res_body.tokeninfo.user) ||
223
+ !k2hr3apiutil_1.default.isSafeString(res_body.tokeninfo.tenant)) {
224
+ const _error = new Error('/v1/acr/testservice response is something wrong(' + JSON.stringify(res_body) + ').');
225
+ dbglogging_1.default.elog(_error.message);
226
+ k2hr3resutil_1.default.errResponse(_req, _res, 400, _error.message); // 400: Bad Request
227
+ return;
228
+ }
229
+ dbglogging_1.default.dlog('Call Verify URL: verified user(' + k2hr3apiutil_1.default.getSafeString(res_body.user) + ') and tenant(' + k2hr3apiutil_1.default.getSafeString(res_body.tenant) + ')');
230
+ //
231
+ // Make response body for debug
232
+ //
233
+ const resobj = [
234
+ {
235
+ name: _service_name + '_resource', // resource name
236
+ expire: null, // no expire
237
+ type: 'string', // resource is string type
238
+ data: _service_name + ' resource data for debug', // resource data(string)
239
+ keys: {
240
+ 'creator': k2hr3apiutil_1.default.getSafeString(_token_info.user),
241
+ 'owner_tenant': k2hr3apiutil_1.default.getSafeString(_token_info.tenant),
242
+ 'service_name': _service_name,
243
+ 'token': 'sample_token_value',
244
+ 'accesskey': 'sample_accesskey_value',
245
+ 'secretkey': 'sample_secretkey_value',
246
+ 'anykey': 'sample_value'
247
+ }
248
+ }
249
+ ];
250
+ //
251
+ // Return response
252
+ //
253
+ dbglogging_1.default.dlog('succeed : ' + JSON.stringify(resobj));
254
+ _res.status(200); // 200: OK
255
+ _res.send(JSON.stringify(resobj));
256
+ });
257
+ });
258
+ subreq.on('error', (exception) => {
259
+ dbglogging_1.default.elog(exception.message);
260
+ const maybeErrno = exception.errno;
261
+ const maybeCode = exception.code;
262
+ let status_code = 500;
263
+ if (k2hr3apiutil_1.default.isSafeNumber(maybeErrno)) {
264
+ status_code = maybeErrno;
265
+ }
266
+ else {
267
+ let tmpSatus = k2hr3apiutil_1.default.cvtToNumber(maybeErrno);
268
+ if (!k2hr3apiutil_1.default.isSafeNumber(tmpSatus)) {
269
+ tmpSatus = k2hr3apiutil_1.default.cvtToNumber(maybeCode);
270
+ }
271
+ if (k2hr3apiutil_1.default.isSafeNumber(tmpSatus)) {
272
+ status_code = tmpSatus;
273
+ }
274
+ }
275
+ k2hr3resutil_1.default.errResponse(_req, _res, status_code, exception.message);
276
+ return;
277
+ });
278
+ subreq.end();
279
+ });
280
+ //---------------------------------------------------------
281
+ // Exports
282
+ //---------------------------------------------------------
283
+ //
284
+ // Functions
285
+ //
286
+ exports.default = router;
287
+ /*
288
+ * Local variables:
289
+ * tab-width: 4
290
+ * c-basic-offset: 4
291
+ * End:
292
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
293
+ * vim<600: noexpandtab sw=4 ts=4
294
+ */
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ /*
3
+ * K2HR3 REST API
4
+ *
5
+ * Copyright 2018 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: Tue May 13 2020
18
+ * REVISION:
19
+ *
20
+ */
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const k2hr3apiutil_1 = __importDefault(require("../lib/k2hr3apiutil"));
26
+ const k2hr3resutil_1 = __importDefault(require("../lib/k2hr3resutil"));
27
+ const dbglogging_1 = __importDefault(require("../lib/dbglogging"));
28
+ const k2hr3extdata_1 = __importDefault(require("../lib/k2hr3extdata"));
29
+ const express_1 = __importDefault(require("express"));
30
+ const router = express_1.default.Router();
31
+ //---------------------------------------------------------
32
+ // Router GET
33
+ //---------------------------------------------------------
34
+ //
35
+ // Mountpath : '/v1/extdata/*'
36
+ //
37
+ // GET '/v1/extdata/<exturi>/<encrypted data>' : get extra(user-defined) data on version 1
38
+ // response : compressed(gzip) extdata(binary)
39
+ //
40
+ // This mount point is for getting compressed user defined extra data.
41
+ // The user can define this extra data as a template in the configuration.
42
+ // The variables can be used in templates, and those are replaced real values
43
+ // as like userdata entry point.
44
+ // The returned data is encrypted and compressed with the specified algorithm.
45
+ //
46
+ router.get('/', (req, res, next) => {
47
+ dbglogging_1.default.dlog('CALL:', req.method, req.url, req.baseUrl);
48
+ if ('GET' !== req.method) {
49
+ // HEAD request comes here, so it should be routed to head(not defined) function.
50
+ next();
51
+ return;
52
+ }
53
+ if (!k2hr3apiutil_1.default.isPlainObject(req) ||
54
+ !k2hr3apiutil_1.default.isSafeString(req.baseUrl) ||
55
+ !k2hr3apiutil_1.default.isSafeEntity(req.headers)) // Must User-Agent in header
56
+ {
57
+ const result = {
58
+ result: false,
59
+ message: 'GET request or url is wrong'
60
+ };
61
+ dbglogging_1.default.elog(result.message);
62
+ res.type('application/json; charset=utf-8');
63
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
64
+ return;
65
+ }
66
+ //------------------------------
67
+ // Check headers
68
+ //------------------------------
69
+ if (!k2hr3apiutil_1.default.isSafeEntity(req.headers['user-agent'])) {
70
+ // 'User-Agent' Must be existed
71
+ const result = {
72
+ result: false,
73
+ message: 'GET request does not have User-Agent header'
74
+ };
75
+ dbglogging_1.default.elog(result.message);
76
+ res.type('application/json; charset=utf-8');
77
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
78
+ return;
79
+ }
80
+ const userAgent = k2hr3apiutil_1.default.getSafeString(req.headers['user-agent']).toLowerCase();
81
+ const typeEncode = req.headers['accept-encoding'];
82
+ const typeContent = req.headers['content-type'];
83
+ let isGzip = false;
84
+ if (k2hr3apiutil_1.default.isSafeEntity(typeEncode)) {
85
+ if (k2hr3apiutil_1.default.hasPartString(typeEncode, ',', ['gzip', 'deflate'], true)) {
86
+ isGzip = true;
87
+ }
88
+ else if (!isGzip) {
89
+ // Accept-Encoding should have 'gzip' or 'deflate', but all type is allowed
90
+ dbglogging_1.default.dlog('Get request Accept-Encoding does not have gzip nor deflate, but continue...');
91
+ }
92
+ }
93
+ else {
94
+ //r3logger.dlog('GET request doe not have Accept-Encoding, but continue...');
95
+ }
96
+ if (k2hr3apiutil_1.default.isSafeEntity(typeContent)) {
97
+ if (!k2hr3apiutil_1.default.hasPartString(typeContent, ';', 'application/octet-stream', true)) {
98
+ // should be 'application/octet-stream', but all type is allowed
99
+ dbglogging_1.default.dlog('GET request Content-Type is not application/octet-stream, but continue...');
100
+ }
101
+ }
102
+ else {
103
+ //r3logger.dlog('GET request doe not have Content-Type, but continue...');
104
+ }
105
+ //------------------------------
106
+ // get url paths and decode
107
+ //------------------------------
108
+ // check path matching
109
+ const requestptn = new RegExp('^/v1/extdata/(.*)/(.*)'); // regex = /^\/v1\/extdata\/(.*)\/(.*)/
110
+ const reqmatchs = decodeURI(req.baseUrl).match(requestptn);
111
+ if (!k2hr3apiutil_1.default.isArray(reqmatchs) ||
112
+ !k2hr3apiutil_1.default.isNotEmptyArray(reqmatchs) ||
113
+ reqmatchs.length < 3 ||
114
+ '' === k2hr3apiutil_1.default.getSafeString(reqmatchs[1]) ||
115
+ '' === k2hr3apiutil_1.default.getSafeString(reqmatchs[2])) {
116
+ const result = {
117
+ result: false,
118
+ message: 'GET request url does not have extdata path parameter'
119
+ };
120
+ dbglogging_1.default.elog(result.message);
121
+ res.type('application/json; charset=utf-8');
122
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
123
+ return;
124
+ }
125
+ // decode and check extdata parameter
126
+ const extdataproc = new k2hr3extdata_1.default;
127
+ const suburi = k2hr3apiutil_1.default.getSafeString(reqmatchs[1]);
128
+ const roleinfo = extdataproc.decryptRoleInfo(reqmatchs[2]);
129
+ if (!k2hr3apiutil_1.default.isSafeString(suburi) || !extdataproc.checkSuburi(suburi)) {
130
+ const result = {
131
+ result: false,
132
+ message: 'GET request URL path(' + k2hr3apiutil_1.default.getSafeString(suburi) + ') does not exist'
133
+ };
134
+ dbglogging_1.default.elog(result.message);
135
+ res.type('application/json; charset=utf-8');
136
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
137
+ return;
138
+ }
139
+ if (!extdataproc.checkUserAgent(userAgent, suburi)) {
140
+ const result = {
141
+ result: false,
142
+ message: 'GET request is not allowed from your client'
143
+ };
144
+ dbglogging_1.default.elog(result.message);
145
+ res.type('application/json; charset=utf-8');
146
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
147
+ return;
148
+ }
149
+ if (!k2hr3apiutil_1.default.isValTypeRoleInfo(roleinfo)) {
150
+ const result = {
151
+ result: false,
152
+ message: 'GET /extdata/' + suburi + '/<path> is invalid'
153
+ };
154
+ dbglogging_1.default.elog(result.message);
155
+ res.type('application/json; charset=utf-8');
156
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
157
+ return;
158
+ }
159
+ const contype = extdataproc.getContentType(suburi);
160
+ //------------------------------
161
+ // Make response
162
+ //------------------------------
163
+ if (isGzip) {
164
+ // Gzip
165
+ const responsebody = extdataproc.getGzipExtdata(roleinfo, suburi);
166
+ if (null == responsebody) {
167
+ const result = {
168
+ result: false,
169
+ message: 'Could not make gzip response'
170
+ };
171
+ dbglogging_1.default.elog(result.message);
172
+ res.type('application/json; charset=utf-8');
173
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
174
+ return;
175
+ }
176
+ res.type('application/zip');
177
+ res.setHeader('Content-Encoding', 'gzip');
178
+ res.setHeader('Content-Transfer-Encoding', 'binary');
179
+ res.setHeader('Content-Disposition', 'attachment; filename=k2hr3-extdata.gz');
180
+ res.setHeader('Content-Length', responsebody.length);
181
+ dbglogging_1.default.dlog('succeed : (response body is gzip compressed)');
182
+ res.status(200); // 200: OK
183
+ res.send(responsebody.data);
184
+ }
185
+ else {
186
+ // Text
187
+ const responsebody = extdataproc.getExtdata(roleinfo, suburi);
188
+ if (null == responsebody) {
189
+ const result = {
190
+ result: false,
191
+ message: 'Could not make response'
192
+ };
193
+ dbglogging_1.default.elog(result.message);
194
+ res.type('application/json; charset=utf-8');
195
+ k2hr3resutil_1.default.errResponse(req, res, 400, result); // 400: Bad Request
196
+ return;
197
+ }
198
+ res.type((null === contype || !k2hr3apiutil_1.default.isSafeString(contype)) ? 'text/plain' : contype);
199
+ res.setHeader('Content-Length', responsebody.length);
200
+ dbglogging_1.default.dlog('succeed : (response body is not gzip compressed)');
201
+ res.status(200); // 200: OK
202
+ res.send(responsebody);
203
+ }
204
+ });
205
+ //---------------------------------------------------------
206
+ // Exports
207
+ //---------------------------------------------------------
208
+ //
209
+ // Functions
210
+ //
211
+ exports.default = router;
212
+ /*
213
+ * Local variables:
214
+ * tab-width: 4
215
+ * c-basic-offset: 4
216
+ * End:
217
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
218
+ * vim<600: noexpandtab sw=4 ts=4
219
+ */