k2hr3-api 1.0.42 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/config/k2hr3-init.sh.templ +2 -2
  2. package/dist/.gitkeep +0 -0
  3. package/dist/src/app.js +262 -0
  4. package/{bin → dist/src/bin}/run.sh +1 -1
  5. package/dist/src/bin/watcher.js +113 -0
  6. package/dist/src/bin/www.js +217 -0
  7. package/dist/src/lib/basicipcheck.js +392 -0
  8. package/dist/src/lib/cacerts.js +106 -0
  9. package/dist/src/lib/dbglogging.js +190 -0
  10. package/dist/src/lib/dummyuserapi.js +719 -0
  11. package/dist/src/lib/ipwatch.js +354 -0
  12. package/dist/src/lib/k2hr3acrutil.js +532 -0
  13. package/dist/src/lib/k2hr3apiutil.js +1444 -0
  14. package/dist/src/lib/k2hr3cliutil.js +183 -0
  15. package/dist/src/lib/k2hr3config.js +832 -0
  16. package/dist/src/lib/k2hr3cryptutil.js +258 -0
  17. package/dist/src/lib/k2hr3dkc.js +12121 -0
  18. package/dist/src/lib/k2hr3extdata.js +198 -0
  19. package/dist/src/lib/k2hr3keys.js +207 -0
  20. package/dist/src/lib/k2hr3resutil.js +111 -0
  21. package/dist/src/lib/k2hr3template.js +6546 -0
  22. package/dist/src/lib/k2hr3tokens.js +2643 -0
  23. package/dist/src/lib/k2hr3userdata.js +296 -0
  24. package/dist/src/lib/k8soidc.js +1000 -0
  25. package/dist/src/lib/openstackapiv2.js +695 -0
  26. package/dist/src/lib/openstackapiv3.js +932 -0
  27. package/dist/src/lib/openstackep.js +667 -0
  28. package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
  29. package/dist/src/routes/acr.js +704 -0
  30. package/dist/src/routes/debugVerify.js +294 -0
  31. package/dist/src/routes/extdata.js +219 -0
  32. package/dist/src/routes/list.js +264 -0
  33. package/dist/src/routes/policy.js +840 -0
  34. package/dist/src/routes/resource.js +1489 -0
  35. package/dist/src/routes/role.js +2627 -0
  36. package/dist/src/routes/service.js +908 -0
  37. package/dist/src/routes/tenant.js +1141 -0
  38. package/dist/src/routes/userTokens.js +482 -0
  39. package/dist/src/routes/userdata.js +212 -0
  40. package/dist/src/routes/version.js +103 -0
  41. package/package.json +152 -121
  42. package/ChangeLog +0 -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
@@ -234,13 +234,13 @@ check_create_file()
234
234
  _LOCAL_CC_MODE=0644
235
235
  fi
236
236
 
237
- if [ -f "${_LOCAL_CC_FILE}" ]; then
237
+ if [ -n "${_LOCAL_CC_FILE}" ] && [ -f "${_LOCAL_CC_FILE}" ]; then
238
238
  _LOCAL_CC_TMP=$(tr -d '\n' < "${_LOCAL_CC_FILE}" 2>/dev/null)
239
239
 
240
240
  # [NOTE]
241
241
  # Since the condition becomes complicated, use "X"(temporary word).
242
242
  #
243
- if [ "X${_LOCAL_CC_TMP}" = "X${_LOCAL_CC_VALUE}" ]; then
243
+ if [ -n "${_LOCAL_CC_VALUE}" ] && [ -n "${_LOCAL_CC_VALUE}" ] && [ "${_LOCAL_CC_TMP}" = "${_LOCAL_CC_VALUE}" ]; then
244
244
  # Same value, thus nothing to do
245
245
  return 0
246
246
  fi
package/dist/.gitkeep ADDED
File without changes
@@ -0,0 +1,262 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ //---------------------------------------------------------
26
+ // Imports
27
+ //---------------------------------------------------------
28
+ const path_1 = __importDefault(require("path"));
29
+ const morgan_1 = __importDefault(require("morgan"));
30
+ const cookie_parser_1 = __importDefault(require("cookie-parser"));
31
+ const body_parser_1 = __importDefault(require("body-parser"));
32
+ const k2hr3apiutil_1 = __importDefault(require("./lib/k2hr3apiutil"));
33
+ const k2hr3resutil_1 = __importDefault(require("./lib/k2hr3resutil"));
34
+ const express_1 = __importDefault(require("express"));
35
+ //
36
+ // Load Configuration
37
+ //
38
+ const k2hr3config_1 = require("./lib/k2hr3config");
39
+ const apiConf = new k2hr3config_1.r3ApiConfig();
40
+ //
41
+ // Load variables
42
+ //
43
+ // - Local Tenants
44
+ // - Load CORS(Cross-Origin Resource Sharing) Setting
45
+ //
46
+ // [NOTE][TODO]
47
+ // It specifies a web development machine for temporary debugging.
48
+ // In future we plan to specify with K2HR3 role.
49
+ //
50
+ let is_localtenants = true;
51
+ const cors_ips = [];
52
+ (() => {
53
+ is_localtenants = apiConf.isLocalTenants();
54
+ if (k2hr3apiutil_1.default.isSafeEntity(apiConf) && k2hr3apiutil_1.default.isNotEmptyArray(apiConf.getCORSIPs())) {
55
+ k2hr3apiutil_1.default.mergeArray(cors_ips, apiConf.getCORSIPs());
56
+ }
57
+ })();
58
+ //---------------------------------------------------------
59
+ // Environments
60
+ //---------------------------------------------------------
61
+ // NODE_ENV(development or production)
62
+ // NODE_LOGGER(if 'no', not logging by morgan)
63
+ //
64
+ const is_product = k2hr3apiutil_1.default.compareCaseString(k2hr3apiutil_1.default.getSafeString(process.env.NODE_ENV), 'production');
65
+ const is_logging = !k2hr3apiutil_1.default.compareCaseString(k2hr3apiutil_1.default.getSafeString(process.env.NODE_LOGGER), 'no');
66
+ //---------------------------------------------------------
67
+ // Routes
68
+ //---------------------------------------------------------
69
+ const version_1 = __importDefault(require("./routes/version"));
70
+ const userTokens_1 = __importDefault(require("./routes/userTokens"));
71
+ const policy_1 = __importDefault(require("./routes/policy"));
72
+ const resource_1 = __importDefault(require("./routes/resource"));
73
+ const role_1 = __importDefault(require("./routes/role"));
74
+ const service_1 = __importDefault(require("./routes/service"));
75
+ const acr_1 = __importDefault(require("./routes/acr"));
76
+ const list_1 = __importDefault(require("./routes/list"));
77
+ const userdata_1 = __importDefault(require("./routes/userdata"));
78
+ const extdata_1 = __importDefault(require("./routes/extdata"));
79
+ const tenant_1 = __importDefault(require("./routes/tenant"));
80
+ const debugVerify_1 = __importDefault(require("./routes/debugVerify"));
81
+ let tenant = null;
82
+ if (is_localtenants) {
83
+ tenant = tenant_1.default;
84
+ }
85
+ let verify = null;
86
+ if (!is_product) {
87
+ verify = debugVerify_1.default;
88
+ }
89
+ //
90
+ // Express objects
91
+ //
92
+ const app = (0, express_1.default)();
93
+ const userExp = (0, express_1.default)();
94
+ const policyExp = (0, express_1.default)();
95
+ const resourceExp = (0, express_1.default)();
96
+ const roleExp = (0, express_1.default)();
97
+ const serviceExp = (0, express_1.default)();
98
+ const acrExp = (0, express_1.default)();
99
+ const listExp = (0, express_1.default)();
100
+ const userdataExp = (0, express_1.default)();
101
+ const extdataExp = (0, express_1.default)();
102
+ let tenantExp = null;
103
+ if (is_localtenants) {
104
+ tenantExp = (0, express_1.default)();
105
+ }
106
+ let verifyExp = null;
107
+ if (!is_product) {
108
+ verifyExp = (0, express_1.default)();
109
+ }
110
+ //---------------------------------------------------------
111
+ // Trusted proxy
112
+ //---------------------------------------------------------
113
+ // [NOTE][TODO]
114
+ // We set trusted proxy as only loopback now.
115
+ // Here, we need to add CDN/Proxy for our NW, but pending now.
116
+ //
117
+ app.set('trust proxy', 'loopback');
118
+ userExp.set('trust proxy', 'loopback');
119
+ policyExp.set('trust proxy', 'loopback');
120
+ resourceExp.set('trust proxy', 'loopback');
121
+ roleExp.set('trust proxy', 'loopback');
122
+ serviceExp.set('trust proxy', 'loopback');
123
+ acrExp.set('trust proxy', 'loopback');
124
+ listExp.set('trust proxy', 'loopback');
125
+ userdataExp.set('trust proxy', 'loopback');
126
+ extdataExp.set('trust proxy', 'loopback');
127
+ if (tenantExp) {
128
+ tenantExp.set('trust proxy', 'loopback');
129
+ }
130
+ if (verifyExp) {
131
+ verifyExp.set('trust proxy', 'loopback');
132
+ }
133
+ //
134
+ // CORS(Cross-Origin Resource Sharing) Controller
135
+ //
136
+ app.use((req, res, next) => {
137
+ //
138
+ // Do not allow CORS for userToken without tenant name(=put/post unscoped token)
139
+ //
140
+ const userTokenUrlExp = new RegExp('^/v1/user/tokens(.*)');
141
+ if (req.socket.localAddress !== req.socket.remoteAddress && k2hr3apiutil_1.default.isNotEmptyArray(k2hr3apiutil_1.default.getSafeString(req.url).match(userTokenUrlExp))) {
142
+ //
143
+ // case of POST/PUT userToken
144
+ //
145
+ if (!k2hr3apiutil_1.default.findStringInArray(apiConf.getCORSIPs(), req.socket.remoteAddress) && !k2hr3apiutil_1.default.findStringInArray(apiConf.getCORSIPs(), '*')) {
146
+ // [NOTE]
147
+ // If allowcredauth is true in configuration and password is specified on PUT method,
148
+ // it allows authorization by credential(username/password).
149
+ // This case is used for accessing keystone directly.
150
+ // (The password is empty is allowed.)
151
+ //
152
+ if ((k2hr3apiutil_1.default.compareCaseString(req.method, 'PUT') && k2hr3apiutil_1.default.isSafeEntity(req.query) && k2hr3apiutil_1.default.isSafeString(req.query.username) && !(apiConf.isAllowedCredentialAccess() && k2hr3apiutil_1.default.isSafeEntity(req.query.password))) ||
153
+ (k2hr3apiutil_1.default.compareCaseString(req.method, 'POST') && k2hr3apiutil_1.default.isSafeEntity(req.body) && k2hr3apiutil_1.default.isSafeEntity(req.body.auth) && k2hr3apiutil_1.default.isSafeEntity(req.body.auth.passwordCredentials))) {
154
+ //
155
+ // case of specified user credentials(except specified unscoped token)
156
+ //
157
+ const result = {
158
+ result: false,
159
+ message: 'not allow CORS(cross-origin resource sharing) to /v1/user/tokens'
160
+ };
161
+ k2hr3resutil_1.default.errResponse(req, res, 405, result, 'application/json; charset=utf-8');
162
+ return;
163
+ }
164
+ }
165
+ }
166
+ //
167
+ // Origin is specified, allow it.
168
+ //
169
+ if (k2hr3apiutil_1.default.isSafeString(req.headers.origin)) {
170
+ res.header('Access-Control-Allow-Origin', req.headers.origin);
171
+ res.header('Access-Control-Allow-Headers', 'Origin,X-Requested-With,X-HTTP-Method-Override,Content-Type,Accept,X-Auth-Token,x-k2hr3-debug');
172
+ res.header('Access-Control-Allow-Methods', 'HEAD,POST,GET,PUT,DELETE,OPTIONS');
173
+ res.header('Access-Control-Expose-Headers', 'X-Auth-Token,x-k2hr3-error');
174
+ res.header('Access-Control-Allow-Credentials', 'true');
175
+ res.header('Access-Control-Max-Age', '86400');
176
+ }
177
+ next();
178
+ });
179
+ //---------------------------------------------------------
180
+ // Express
181
+ //---------------------------------------------------------
182
+ //
183
+ // Setting for express
184
+ //
185
+ if (is_logging) {
186
+ //
187
+ // Setup Log
188
+ //
189
+ const format = apiConf.getAccessLogFormat() ?? 'combined';
190
+ const options = apiConf.getMorganLoggerOption(__dirname) ?? undefined;
191
+ app.use((0, morgan_1.default)(format, options));
192
+ }
193
+ app.use(body_parser_1.default.json());
194
+ app.use(body_parser_1.default.urlencoded({ extended: false }));
195
+ app.use((0, cookie_parser_1.default)());
196
+ app.use(express_1.default.static(path_1.default.join(__dirname, 'public')));
197
+ app.use('/status.html', express_1.default.static(__dirname + '/public/status.html'));
198
+ //
199
+ // Route mapping
200
+ //
201
+ app.use('/', version_1.default); // '/'
202
+ app.use('/v1', version_1.default); // '/v1'
203
+ userExp.use('/', userTokens_1.default); // '/v1/user/tokens'
204
+ policyExp.use('/', policy_1.default); // '/v1/policy'
205
+ resourceExp.use('/', resource_1.default); // '/v1/resource'
206
+ roleExp.use('/', role_1.default); // '/v1/role'
207
+ serviceExp.use('/', service_1.default); // '/v1/service'
208
+ acrExp.use('/', acr_1.default); // '/v1/acr'
209
+ listExp.use('/', list_1.default); // '/v1/list'
210
+ userdataExp.use('/', userdata_1.default); // '/v1/userdata'
211
+ extdataExp.use('/', extdata_1.default); // '/v1/extdata'
212
+ if (tenantExp && tenant) {
213
+ tenantExp.use('/', tenant); // '/v1/tenant'
214
+ }
215
+ if (verifyExp && verify) {
216
+ verifyExp.use('/', verify); // '/v1/debug/verify*'
217
+ }
218
+ app.use(/^\/v1\/user\/tokens(?:\/.*)?$/, userExp); // mountpath: '/v1/user/tokens*'
219
+ app.use(/^\/v1\/policy(?:\/.*)?$/, policyExp); // mountpath: '/v1/policy*'
220
+ app.use(/^\/v1\/resource(?:\/.*)?$/, resourceExp); // mountpath: '/v1/resource*'
221
+ app.use(/^\/v1\/role(?:\/.*)?$/, roleExp); // mountpath: '/v1/role*'
222
+ app.use(/^\/v1\/service(?:\/.*)?$/, serviceExp); // mountpath: '/v1/service*'
223
+ app.use(/^\/v1\/acr(?:\/.*)?$/, acrExp); // mountpath: '/v1/acr*'
224
+ app.use(/^\/v1\/list(?:\/.*)?$/, listExp); // mountpath: '/v1/list*'
225
+ app.use(/^\/v1\/userdata(?:\/.*)?$/, userdataExp); // mountpath: '/v1/userdata*'
226
+ app.use(/^\/v1\/extdata(?:\/.*)?$/, extdataExp); // mountpath: '/v1/extdata*'
227
+ if (tenantExp) {
228
+ app.use(/^\/v1\/tenant(?:\/.*)?$/, tenantExp); // mountpath: '/v1/tenant*'
229
+ }
230
+ if (verifyExp) {
231
+ app.use(/^\/v1\/debug\/verify(?:\/.*)?$/, verifyExp); // mountpath: '/v1/debug/verify*'
232
+ }
233
+ app.use((req, res, next) => {
234
+ const err = new Error('Not Found');
235
+ err.status = 404;
236
+ next(err);
237
+ });
238
+ //
239
+ // error handler
240
+ //
241
+ app.use((err, req, res, _next) => {
242
+ // set locals, only providing error in development
243
+ res.locals.message = err.message;
244
+ res.locals.error = req.app.get('env') === 'development' ? err : {};
245
+ const result = {
246
+ result: false,
247
+ message: 'Internal server error'
248
+ };
249
+ k2hr3resutil_1.default.errResponse(req, res, (err.status || 500), result, 'application/json; charset=utf-8');
250
+ });
251
+ //---------------------------------------------------------
252
+ // Exports
253
+ //---------------------------------------------------------
254
+ exports.default = app;
255
+ /*
256
+ * Local variables:
257
+ * tab-width: 4
258
+ * c-basic-offset: 4
259
+ * End:
260
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
261
+ * vim<600: noexpandtab sw=4 ts=4
262
+ */
@@ -32,7 +32,7 @@ SRCTOP=$(cd "${SCRIPTDIR}/.." || exit 1; pwd)
32
32
  CMDLINE_COMMAND="$0"
33
33
  CMDLINE_PARAMETERS="$*"
34
34
 
35
- LOCAL_HOSTNAME="$(hostname | tr -d '\n')"
35
+ LOCAL_HOSTNAME="$(hostname -f | tr -d '\n')"
36
36
  PID_FILE_BASEDIR="/var/run/antpickax"
37
37
  PID_FILE_TMPDIR="/tmp"
38
38
 
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /*
4
+ * K2HR3 REST API
5
+ *
6
+ * Copyright 2017 Yahoo Japan Corporation.
7
+ *
8
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
9
+ * common management information for the cloud.
10
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
11
+ * These are stored as roles, resources, policies in K2hdkc, and the
12
+ * client system can dynamically read and modify these information.
13
+ *
14
+ * For the full copyright and license information, please view
15
+ * the license file that was distributed with this source code.
16
+ *
17
+ * AUTHOR: Takeshi Nakatani
18
+ * CREATE: Wed Oct 24 2018
19
+ * REVISION:
20
+ *
21
+ */
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ //
27
+ // Module dependencies.
28
+ //
29
+ const k2hr3apiutil_1 = __importDefault(require("../lib/k2hr3apiutil"));
30
+ const ipwatch_1 = __importDefault(require("../lib/ipwatch"));
31
+ const dbglogging_1 = __importDefault(require("../lib/dbglogging"));
32
+ const k2hr3config_1 = require("../lib/k2hr3config");
33
+ const apiConf = new k2hr3config_1.r3ApiConfig();
34
+ //
35
+ // Setup console logging
36
+ //
37
+ apiConf.setConsoleLogging(__dirname + '/..', true); // replace output from stdout/stderr to file if set in config
38
+ // Globals
39
+ let intervalobj = null;
40
+ //
41
+ // Process/Signal handlering
42
+ //
43
+ const procSignal = (reason, code) => {
44
+ if (k2hr3apiutil_1.default.compareCaseString(reason, 'exit')) {
45
+ dbglogging_1.default.elog('K2HR3 watcher exit with status code : ' + (k2hr3apiutil_1.default.isSafeNumber(code) ? code.toString() : 'unknown'));
46
+ }
47
+ else {
48
+ dbglogging_1.default.elog('K2HR3 watcher caught signal : ' + k2hr3apiutil_1.default.getSafeString(reason));
49
+ }
50
+ if (intervalobj) {
51
+ clearInterval(intervalobj);
52
+ intervalobj = null;
53
+ }
54
+ };
55
+ process.on('exit', (code) => { procSignal('exit', code); });
56
+ process.on('SIGHUP', () => { procSignal('SIGHUP'); });
57
+ process.on('SIGINT', () => { procSignal('SIGINT'); });
58
+ process.on('SIGBREAK', () => { procSignal('SIGBREAK'); });
59
+ process.on('SIGTERM', () => { procSignal('SIGTERM'); });
60
+ //
61
+ // OneShot callback
62
+ //
63
+ const oneshotCallback = (result) => {
64
+ if (k2hr3apiutil_1.default.isBoolean(result) && result) {
65
+ dbglogging_1.default.dlog('K2HR3 watcher oneshot result : Succeed');
66
+ process.exit(0);
67
+ }
68
+ else {
69
+ dbglogging_1.default.elog('K2HR3 watcher oneshot result : Failed');
70
+ process.exit(1);
71
+ }
72
+ };
73
+ //
74
+ // Process parameter
75
+ //
76
+ let oneshotCB = null;
77
+ if (k2hr3apiutil_1.default.isNotEmptyArray(process.argv)) {
78
+ for (let cnt = 2; cnt < process.argv.length; ++cnt) { // argv[0] = 'node', argv[1] = 'this program'
79
+ if (k2hr3apiutil_1.default.isSafeString(process.argv[cnt]) ||
80
+ k2hr3apiutil_1.default.compareCaseString(process.argv[cnt], '--oneshot') ||
81
+ k2hr3apiutil_1.default.compareCaseString(process.argv[cnt], '-os')) {
82
+ oneshotCB = oneshotCallback;
83
+ }
84
+ }
85
+ }
86
+ //
87
+ // Check wathcer type
88
+ //
89
+ if (ipwatch_1.default.isNocheckType() || ipwatch_1.default.isListenerType()) {
90
+ dbglogging_1.default.elog('K2HR3 watcher type defined ' + ipwatch_1.default.getType() + ' in configuration, thus could not run watcher.');
91
+ process.exit(1);
92
+ }
93
+ //
94
+ // Run watcher interval loop
95
+ //
96
+ intervalobj = ipwatch_1.default.watchAddressesAlive(oneshotCB);
97
+ if (!intervalobj) {
98
+ if (null != oneshotCB) {
99
+ // nothig to do here
100
+ }
101
+ else {
102
+ dbglogging_1.default.elog('K2HR3 watcher could not run.');
103
+ process.exit(1);
104
+ }
105
+ }
106
+ /*
107
+ * Local variables:
108
+ * tab-width: 4
109
+ * c-basic-offset: 4
110
+ * End:
111
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
112
+ * vim<600: noexpandtab sw=4 ts=4
113
+ */
@@ -0,0 +1,217 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /*
4
+ * K2HR3 REST API
5
+ *
6
+ * Copyright 2017 Yahoo Japan Corporation.
7
+ *
8
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
9
+ * common management information for the cloud.
10
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
11
+ * These are stored as roles, resources, policies in K2hdkc, and the
12
+ * client system can dynamically read and modify these information.
13
+ *
14
+ * For the full copyright and license information, please view
15
+ * the license file that was distributed with this source code.
16
+ *
17
+ * AUTHOR: Takeshi Nakatani
18
+ * CREATE: Wed Jun 8 2017
19
+ * REVISION:
20
+ *
21
+ */
22
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ var desc = Object.getOwnPropertyDescriptor(m, k);
25
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
26
+ desc = { enumerable: true, get: function() { return m[k]; } };
27
+ }
28
+ Object.defineProperty(o, k2, desc);
29
+ }) : (function(o, m, k, k2) {
30
+ if (k2 === undefined) k2 = k;
31
+ o[k2] = m[k];
32
+ }));
33
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
34
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
35
+ }) : function(o, v) {
36
+ o["default"] = v;
37
+ });
38
+ var __importStar = (this && this.__importStar) || (function () {
39
+ var ownKeys = function(o) {
40
+ ownKeys = Object.getOwnPropertyNames || function (o) {
41
+ var ar = [];
42
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
43
+ return ar;
44
+ };
45
+ return ownKeys(o);
46
+ };
47
+ return function (mod) {
48
+ if (mod && mod.__esModule) return mod;
49
+ var result = {};
50
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
51
+ __setModuleDefault(result, mod);
52
+ return result;
53
+ };
54
+ })();
55
+ var __importDefault = (this && this.__importDefault) || function (mod) {
56
+ return (mod && mod.__esModule) ? mod : { "default": mod };
57
+ };
58
+ Object.defineProperty(exports, "__esModule", { value: true });
59
+ //
60
+ // Module dependencies.
61
+ //
62
+ const app_1 = __importDefault(require("../app"));
63
+ const debug_1 = __importDefault(require("debug"));
64
+ const fs_1 = __importDefault(require("fs"));
65
+ const os_1 = __importDefault(require("os"));
66
+ const cluster_1 = __importDefault(require("cluster"));
67
+ const https = __importStar(require("https"));
68
+ const http = __importStar(require("http"));
69
+ const k2hr3apiutil_1 = __importDefault(require("../lib/k2hr3apiutil"));
70
+ const k2hr3config_1 = require("../lib/k2hr3config");
71
+ const dbg = (0, debug_1.default)('k2hr3-api:server');
72
+ const numCPUs = os_1.default.cpus().length;
73
+ const apiConf = new k2hr3config_1.r3ApiConfig();
74
+ const key = apiConf.getPrivateKey(); // allow empty
75
+ const cert = apiConf.getCert(); // allow empty
76
+ const ca = apiConf.getCA(); // allow empty
77
+ const user = apiConf.getRunUser(); // allow empty
78
+ const port = apiConf.getPort();
79
+ let server;
80
+ //
81
+ // Event listener for HTTP server "error" event.
82
+ //
83
+ const onError = (error) => {
84
+ if ('listen' !== error.syscall) {
85
+ throw error;
86
+ }
87
+ const bind = k2hr3apiutil_1.default.isString(port) ? ('Pipe ' + port) : ('Port ' + String(port));
88
+ // handle specific listen errors with friendly messages
89
+ switch (error.code) {
90
+ case 'EACCES':
91
+ console.error(bind + ' requires elevated privileges');
92
+ process.exit(1);
93
+ break;
94
+ case 'EADDRINUSE':
95
+ console.error(bind + ' is already in use');
96
+ process.exit(1);
97
+ break;
98
+ default:
99
+ throw error;
100
+ }
101
+ };
102
+ //
103
+ // Event listener for HTTP server "listening" event.
104
+ //
105
+ const onListening = () => {
106
+ const addr = server.address();
107
+ if (!k2hr3apiutil_1.default.isSafeEntity(addr)) {
108
+ dbg('Server address is null');
109
+ }
110
+ else {
111
+ if (k2hr3apiutil_1.default.isString(addr)) {
112
+ const bind = 'pipe ' + addr;
113
+ dbg('Listening on ' + bind);
114
+ }
115
+ else if (k2hr3apiutil_1.default.isSafeNumeric(addr.port)) {
116
+ const bind = 'port ' + String(addr.port);
117
+ dbg('Listening on ' + bind);
118
+ }
119
+ else {
120
+ dbg('Server address is unknown');
121
+ }
122
+ }
123
+ };
124
+ //
125
+ // Setup console logging
126
+ //
127
+ apiConf.setConsoleLogging(__dirname + '/..', false); // replace output from stdout/stderr to file if set in config
128
+ if (cluster_1.default.isPrimary && (!k2hr3apiutil_1.default.isSafeEntity(apiConf.isMultiProc()) || false !== apiConf.isMultiProc())) {
129
+ console.log(`Master ${process.pid} is running`);
130
+ // Fork workers.
131
+ for (let cnt = 0; cnt < numCPUs; ++cnt) {
132
+ cluster_1.default.fork();
133
+ }
134
+ cluster_1.default.on('exit', (worker, code, signal) => {
135
+ if (k2hr3apiutil_1.default.isString(signal)) {
136
+ console.log(`worker was killed by signal: ${signal}`);
137
+ }
138
+ else if (0 !== code) {
139
+ console.log(`worker exited with error code: ${code}`);
140
+ }
141
+ else {
142
+ console.log(`worker ${worker.process.pid} died`);
143
+ }
144
+ });
145
+ }
146
+ else {
147
+ //
148
+ // Get port from environment and store in Express.
149
+ //
150
+ let options = {};
151
+ let secure = false;
152
+ //
153
+ // scheme
154
+ //
155
+ if ('https' == apiConf.getScheme() || 'HTTPS' == apiConf.getScheme()) {
156
+ secure = true;
157
+ options = {
158
+ key: fs_1.default.readFileSync(key),
159
+ cert: fs_1.default.readFileSync(cert),
160
+ ca: fs_1.default.readFileSync(ca)
161
+ };
162
+ }
163
+ else if ('http' == apiConf.getScheme() || 'HTTP' == apiConf.getScheme()) {
164
+ secure = false;
165
+ }
166
+ else {
167
+ console.log('scheme value(' + apiConf.getScheme() + ') in config is wrong');
168
+ process.exit(1);
169
+ }
170
+ //
171
+ // Others
172
+ //
173
+ const hostname = os_1.default.hostname() || '127.0.0.1';
174
+ //
175
+ // Store in Express.
176
+ //
177
+ app_1.default.set('port', port);
178
+ //
179
+ // Create HTTP server.
180
+ //
181
+ if (secure) {
182
+ server = https.createServer(options, app_1.default);
183
+ }
184
+ else {
185
+ server = http.createServer(app_1.default);
186
+ }
187
+ //
188
+ // Listen on provided port, on all network interfaces.
189
+ //
190
+ server.listen(port, () => {
191
+ if (k2hr3apiutil_1.default.isSafeString(user)) {
192
+ console.log('Attempting setuid to user "' + user + '"...');
193
+ if (k2hr3apiutil_1.default.isFunction(process.setuid)) {
194
+ try {
195
+ process.setuid(user);
196
+ console.log('Succeeded to setuid');
197
+ }
198
+ catch (err) {
199
+ console.log('Failed to setuid', JSON.stringify(err));
200
+ process.exit(1);
201
+ }
202
+ }
203
+ }
204
+ });
205
+ server.on('error', onError);
206
+ server.on('listening', onListening);
207
+ console.log('Server running at ' + apiConf.getScheme() + '://' + hostname + ':' + port + '/');
208
+ console.log(`Worker ${process.pid} started`);
209
+ }
210
+ /*
211
+ * Local variables:
212
+ * tab-width: 4
213
+ * c-basic-offset: 4
214
+ * End:
215
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
216
+ * vim<600: noexpandtab sw=4 ts=4
217
+ */