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.
- package/config/k2hr3-init.sh.templ +4 -4
- package/dist/.gitkeep +0 -0
- package/dist/src/app.js +262 -0
- package/{bin → dist/src/bin}/run.sh +1 -1
- package/dist/src/bin/watcher.js +113 -0
- package/dist/src/bin/www.js +217 -0
- package/dist/src/lib/basicipcheck.js +392 -0
- package/dist/src/lib/cacerts.js +106 -0
- package/dist/src/lib/dbglogging.js +190 -0
- package/dist/src/lib/dummyuserapi.js +719 -0
- package/dist/src/lib/ipwatch.js +354 -0
- package/dist/src/lib/k2hr3acrutil.js +532 -0
- package/dist/src/lib/k2hr3apiutil.js +1444 -0
- package/dist/src/lib/k2hr3cliutil.js +183 -0
- package/dist/src/lib/k2hr3config.js +832 -0
- package/dist/src/lib/k2hr3cryptutil.js +258 -0
- package/dist/src/lib/k2hr3dkc.js +12121 -0
- package/dist/src/lib/k2hr3extdata.js +198 -0
- package/dist/src/lib/k2hr3keys.js +207 -0
- package/dist/src/lib/k2hr3resutil.js +111 -0
- package/dist/src/lib/k2hr3template.js +6546 -0
- package/dist/src/lib/k2hr3tokens.js +2643 -0
- package/dist/src/lib/k2hr3userdata.js +296 -0
- package/dist/src/lib/k8soidc.js +1000 -0
- package/dist/src/lib/openstackapiv2.js +695 -0
- package/dist/src/lib/openstackapiv3.js +932 -0
- package/dist/src/lib/openstackep.js +667 -0
- package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
- package/dist/src/routes/acr.js +704 -0
- package/dist/src/routes/debugVerify.js +294 -0
- package/dist/src/routes/extdata.js +219 -0
- package/dist/src/routes/list.js +264 -0
- package/dist/src/routes/policy.js +840 -0
- package/dist/src/routes/resource.js +1489 -0
- package/dist/src/routes/role.js +2627 -0
- package/dist/src/routes/service.js +908 -0
- package/dist/src/routes/tenant.js +1141 -0
- package/dist/src/routes/userTokens.js +482 -0
- package/dist/src/routes/userdata.js +212 -0
- package/dist/src/routes/version.js +103 -0
- package/package.json +152 -121
- package/ChangeLog +0 -378
- package/app.js +0 -292
- package/bin/watcher +0 -122
- package/bin/www +0 -180
- package/eslint.config.mjs +0 -68
- package/lib/basicipcheck.js +0 -376
- package/lib/cacerts.js +0 -71
- package/lib/dbglogging.js +0 -151
- package/lib/dummyuserapi.js +0 -766
- package/lib/ipwatch.js +0 -379
- package/lib/k2hr3acrutil.js +0 -516
- package/lib/k2hr3apiutil.js +0 -1494
- package/lib/k2hr3cliutil.js +0 -191
- package/lib/k2hr3config.js +0 -826
- package/lib/k2hr3cryptutil.js +0 -254
- package/lib/k2hr3dkc.js +0 -12632
- package/lib/k2hr3extdata.js +0 -198
- package/lib/k2hr3keys.js +0 -234
- package/lib/k2hr3resutil.js +0 -100
- package/lib/k2hr3template.js +0 -6925
- package/lib/k2hr3tokens.js +0 -2799
- package/lib/k2hr3userdata.js +0 -312
- package/lib/k8soidc.js +0 -1012
- package/lib/openstackapiv2.js +0 -764
- package/lib/openstackapiv3.js +0 -1032
- package/lib/openstackep.js +0 -553
- package/routes/acr.js +0 -738
- package/routes/debugVerify.js +0 -263
- package/routes/extdata.js +0 -232
- package/routes/list.js +0 -270
- package/routes/policy.js +0 -869
- package/routes/resource.js +0 -1441
- package/routes/role.js +0 -2664
- package/routes/service.js +0 -894
- package/routes/tenant.js +0 -1095
- package/routes/userTokens.js +0 -511
- package/routes/userdata.js +0 -218
- package/routes/version.js +0 -108
- package/templ/Dockerfile.templ +0 -71
- package/tests/auto_acr.js +0 -1101
- package/tests/auto_acr_spec.js +0 -79
- package/tests/auto_all_spec.js +0 -142
- package/tests/auto_control_subprocess.sh +0 -243
- package/tests/auto_extdata.js +0 -220
- package/tests/auto_extdata_spec.js +0 -79
- package/tests/auto_init_config_json.sh +0 -275
- package/tests/auto_k2hdkc_server.ini +0 -109
- package/tests/auto_k2hdkc_slave.ini +0 -83
- package/tests/auto_list.js +0 -439
- package/tests/auto_list_spec.js +0 -79
- package/tests/auto_policy.js +0 -1579
- package/tests/auto_policy_spec.js +0 -79
- package/tests/auto_resource.js +0 -10956
- package/tests/auto_resource_spec.js +0 -79
- package/tests/auto_role.js +0 -6150
- package/tests/auto_role_spec.js +0 -79
- package/tests/auto_service.js +0 -770
- package/tests/auto_service_spec.js +0 -79
- package/tests/auto_subprocesses.js +0 -114
- package/tests/auto_template.sh +0 -126
- package/tests/auto_tenant.js +0 -1100
- package/tests/auto_tenant_spec.js +0 -79
- package/tests/auto_token_util.js +0 -219
- package/tests/auto_userdata.js +0 -292
- package/tests/auto_userdata_spec.js +0 -79
- package/tests/auto_usertokens.js +0 -565
- package/tests/auto_usertokens_spec.js +0 -79
- package/tests/auto_version.js +0 -127
- package/tests/auto_version_spec.js +0 -79
- package/tests/auto_watcher.js +0 -157
- package/tests/auto_watcher_spec.js +0 -79
- package/tests/k2hdkc_test.data +0 -986
- package/tests/k2hdkc_test_load.sh +0 -255
- package/tests/k2hr3template_test.js +0 -187
- package/tests/k2hr3template_test.sh +0 -339
- package/tests/k2hr3template_test_async.js +0 -216
- package/tests/k2hr3template_test_template.result +0 -7117
- package/tests/k2hr3template_test_template.txt +0 -3608
- package/tests/k2hr3template_test_vars.js +0 -194
- package/tests/manual_acr_delete.js +0 -143
- package/tests/manual_acr_get.js +0 -297
- package/tests/manual_acr_postput.js +0 -215
- package/tests/manual_allusertenant_get.js +0 -113
- package/tests/manual_extdata_get.js +0 -191
- package/tests/manual_k2hr3keys_get.js +0 -84
- package/tests/manual_list_gethead.js +0 -230
- package/tests/manual_policy_delete.js +0 -132
- package/tests/manual_policy_gethead.js +0 -275
- package/tests/manual_policy_postput.js +0 -297
- package/tests/manual_resource_delete.js +0 -433
- package/tests/manual_resource_gethead.js +0 -423
- package/tests/manual_resource_postput.js +0 -487
- package/tests/manual_role_delete.js +0 -404
- package/tests/manual_role_gethead.js +0 -547
- package/tests/manual_role_postput.js +0 -544
- package/tests/manual_service_delete.js +0 -153
- package/tests/manual_service_gethead.js +0 -178
- package/tests/manual_service_postput.js +0 -348
- package/tests/manual_tenant_delete.js +0 -186
- package/tests/manual_tenant_gethead.js +0 -268
- package/tests/manual_tenant_postput.js +0 -293
- package/tests/manual_test.sh +0 -352
- package/tests/manual_userdata_get.js +0 -173
- package/tests/manual_usertoken_gethead.js +0 -136
- package/tests/manual_usertoken_postput.js +0 -310
- package/tests/manual_version_get.js +0 -127
- package/tests/run_local_test_k2hdkc.sh +0 -174
- 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
|
+
*/
|