k2hr3-api 1.0.41 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/config/k2hr3-init.sh.templ +2 -2
  2. package/dist/.gitkeep +0 -0
  3. package/dist/src/app.js +262 -0
  4. package/{bin → dist/src/bin}/run.sh +1 -1
  5. package/dist/src/bin/watcher.js +113 -0
  6. package/dist/src/bin/www.js +217 -0
  7. package/dist/src/lib/basicipcheck.js +392 -0
  8. package/dist/src/lib/cacerts.js +106 -0
  9. package/dist/src/lib/dbglogging.js +190 -0
  10. package/dist/src/lib/dummyuserapi.js +719 -0
  11. package/dist/src/lib/ipwatch.js +354 -0
  12. package/dist/src/lib/k2hr3acrutil.js +532 -0
  13. package/dist/src/lib/k2hr3apiutil.js +1444 -0
  14. package/dist/src/lib/k2hr3cliutil.js +183 -0
  15. package/dist/src/lib/k2hr3config.js +832 -0
  16. package/dist/src/lib/k2hr3cryptutil.js +258 -0
  17. package/dist/src/lib/k2hr3dkc.js +12121 -0
  18. package/dist/src/lib/k2hr3extdata.js +198 -0
  19. package/dist/src/lib/k2hr3keys.js +207 -0
  20. package/dist/src/lib/k2hr3resutil.js +111 -0
  21. package/dist/src/lib/k2hr3template.js +6546 -0
  22. package/dist/src/lib/k2hr3tokens.js +2643 -0
  23. package/dist/src/lib/k2hr3userdata.js +296 -0
  24. package/dist/src/lib/k8soidc.js +1000 -0
  25. package/dist/src/lib/openstackapiv2.js +695 -0
  26. package/dist/src/lib/openstackapiv3.js +932 -0
  27. package/dist/src/lib/openstackep.js +667 -0
  28. package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
  29. package/dist/src/routes/acr.js +704 -0
  30. package/dist/src/routes/debugVerify.js +294 -0
  31. package/dist/src/routes/extdata.js +219 -0
  32. package/dist/src/routes/list.js +264 -0
  33. package/dist/src/routes/policy.js +840 -0
  34. package/dist/src/routes/resource.js +1489 -0
  35. package/dist/src/routes/role.js +2627 -0
  36. package/dist/src/routes/service.js +908 -0
  37. package/dist/src/routes/tenant.js +1141 -0
  38. package/dist/src/routes/userTokens.js +482 -0
  39. package/dist/src/routes/userdata.js +212 -0
  40. package/dist/src/routes/version.js +103 -0
  41. package/package.json +152 -121
  42. package/ChangeLog +0 -372
  43. package/app.js +0 -292
  44. package/bin/watcher +0 -122
  45. package/bin/www +0 -180
  46. package/eslint.config.mjs +0 -68
  47. package/lib/basicipcheck.js +0 -376
  48. package/lib/cacerts.js +0 -71
  49. package/lib/dbglogging.js +0 -151
  50. package/lib/dummyuserapi.js +0 -766
  51. package/lib/ipwatch.js +0 -379
  52. package/lib/k2hr3acrutil.js +0 -516
  53. package/lib/k2hr3apiutil.js +0 -1494
  54. package/lib/k2hr3cliutil.js +0 -191
  55. package/lib/k2hr3config.js +0 -826
  56. package/lib/k2hr3cryptutil.js +0 -254
  57. package/lib/k2hr3dkc.js +0 -12632
  58. package/lib/k2hr3extdata.js +0 -198
  59. package/lib/k2hr3keys.js +0 -234
  60. package/lib/k2hr3resutil.js +0 -100
  61. package/lib/k2hr3template.js +0 -6925
  62. package/lib/k2hr3tokens.js +0 -2799
  63. package/lib/k2hr3userdata.js +0 -312
  64. package/lib/k8soidc.js +0 -1012
  65. package/lib/openstackapiv2.js +0 -764
  66. package/lib/openstackapiv3.js +0 -1032
  67. package/lib/openstackep.js +0 -553
  68. package/routes/acr.js +0 -738
  69. package/routes/debugVerify.js +0 -263
  70. package/routes/extdata.js +0 -232
  71. package/routes/list.js +0 -270
  72. package/routes/policy.js +0 -869
  73. package/routes/resource.js +0 -1441
  74. package/routes/role.js +0 -2664
  75. package/routes/service.js +0 -894
  76. package/routes/tenant.js +0 -1095
  77. package/routes/userTokens.js +0 -511
  78. package/routes/userdata.js +0 -218
  79. package/routes/version.js +0 -108
  80. package/templ/Dockerfile.templ +0 -71
  81. package/tests/auto_acr.js +0 -1101
  82. package/tests/auto_acr_spec.js +0 -79
  83. package/tests/auto_all_spec.js +0 -142
  84. package/tests/auto_control_subprocess.sh +0 -243
  85. package/tests/auto_extdata.js +0 -220
  86. package/tests/auto_extdata_spec.js +0 -79
  87. package/tests/auto_init_config_json.sh +0 -275
  88. package/tests/auto_k2hdkc_server.ini +0 -109
  89. package/tests/auto_k2hdkc_slave.ini +0 -83
  90. package/tests/auto_list.js +0 -439
  91. package/tests/auto_list_spec.js +0 -79
  92. package/tests/auto_policy.js +0 -1579
  93. package/tests/auto_policy_spec.js +0 -79
  94. package/tests/auto_resource.js +0 -10956
  95. package/tests/auto_resource_spec.js +0 -79
  96. package/tests/auto_role.js +0 -6150
  97. package/tests/auto_role_spec.js +0 -79
  98. package/tests/auto_service.js +0 -770
  99. package/tests/auto_service_spec.js +0 -79
  100. package/tests/auto_subprocesses.js +0 -114
  101. package/tests/auto_template.sh +0 -126
  102. package/tests/auto_tenant.js +0 -1100
  103. package/tests/auto_tenant_spec.js +0 -79
  104. package/tests/auto_token_util.js +0 -219
  105. package/tests/auto_userdata.js +0 -292
  106. package/tests/auto_userdata_spec.js +0 -79
  107. package/tests/auto_usertokens.js +0 -565
  108. package/tests/auto_usertokens_spec.js +0 -79
  109. package/tests/auto_version.js +0 -127
  110. package/tests/auto_version_spec.js +0 -79
  111. package/tests/auto_watcher.js +0 -157
  112. package/tests/auto_watcher_spec.js +0 -79
  113. package/tests/k2hdkc_test.data +0 -986
  114. package/tests/k2hdkc_test_load.sh +0 -255
  115. package/tests/k2hr3template_test.js +0 -187
  116. package/tests/k2hr3template_test.sh +0 -339
  117. package/tests/k2hr3template_test_async.js +0 -216
  118. package/tests/k2hr3template_test_template.result +0 -7117
  119. package/tests/k2hr3template_test_template.txt +0 -3608
  120. package/tests/k2hr3template_test_vars.js +0 -194
  121. package/tests/manual_acr_delete.js +0 -143
  122. package/tests/manual_acr_get.js +0 -297
  123. package/tests/manual_acr_postput.js +0 -215
  124. package/tests/manual_allusertenant_get.js +0 -113
  125. package/tests/manual_extdata_get.js +0 -191
  126. package/tests/manual_k2hr3keys_get.js +0 -84
  127. package/tests/manual_list_gethead.js +0 -230
  128. package/tests/manual_policy_delete.js +0 -132
  129. package/tests/manual_policy_gethead.js +0 -275
  130. package/tests/manual_policy_postput.js +0 -297
  131. package/tests/manual_resource_delete.js +0 -433
  132. package/tests/manual_resource_gethead.js +0 -423
  133. package/tests/manual_resource_postput.js +0 -487
  134. package/tests/manual_role_delete.js +0 -404
  135. package/tests/manual_role_gethead.js +0 -547
  136. package/tests/manual_role_postput.js +0 -544
  137. package/tests/manual_service_delete.js +0 -153
  138. package/tests/manual_service_gethead.js +0 -178
  139. package/tests/manual_service_postput.js +0 -348
  140. package/tests/manual_tenant_delete.js +0 -186
  141. package/tests/manual_tenant_gethead.js +0 -268
  142. package/tests/manual_tenant_postput.js +0 -293
  143. package/tests/manual_test.sh +0 -352
  144. package/tests/manual_userdata_get.js +0 -173
  145. package/tests/manual_usertoken_gethead.js +0 -136
  146. package/tests/manual_usertoken_postput.js +0 -310
  147. package/tests/manual_version_get.js +0 -127
  148. package/tests/run_local_test_k2hdkc.sh +0 -174
  149. package/tests/test.sh +0 -333
@@ -1,826 +0,0 @@
1
- /*
2
- * K2HR3 REST API
3
- *
4
- * Copyright 2017 Yahoo Japan Corporation.
5
- *
6
- * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
7
- * common management information for the cloud.
8
- * K2HR3 can dynamically manage information as "who", "what", "operate".
9
- * These are stored as roles, resources, policies in K2hdkc, and the
10
- * client system can dynamically read and modify these information.
11
- *
12
- * For the full copyright and license information, please view
13
- * the license file that was distributed with this source code.
14
- *
15
- * AUTHOR: Takeshi Nakatani
16
- * CREATE: Wed Oct 31 2018
17
- * REVISION:
18
- *
19
- */
20
-
21
- 'use strict';
22
-
23
- var path = require('path');
24
- var rotatefs = require('rotating-file-stream'); // [NOTICE] rotating-file-stream is using fsevents optionally.
25
- var apiutil = require('./k2hr3apiutil');
26
-
27
- //---------------------------------------------------------
28
- // load configuration for API
29
- //---------------------------------------------------------
30
- //
31
- // For IP Addresses checker(watcher) type
32
- //
33
- const chkipconfigType = {
34
- CHECKER_TYPE_LISTENER: 'Listener',
35
- CHECKER_TYPE_FUNCTION: 'Function',
36
- CHECKER_TYPE_BASIC_OR: 'BasicOr',
37
- CHECKER_TYPE_BASIC_AND: 'BasicAnd',
38
- CHECKER_TYPE_NOCHECK: 'NoCheck'
39
- };
40
-
41
- //
42
- // Reserved key name in config
43
- //
44
- const reserved_key_list = [
45
- 'keystone',
46
- 'k2hdkc',
47
- 'corsips',
48
- 'multiproc',
49
- 'scheme',
50
- 'port',
51
- 'runuser',
52
- 'privatekey',
53
- 'cert',
54
- 'ca',
55
- 'logdir',
56
- 'accesslogname',
57
- 'accesslogform',
58
- 'consolelogname',
59
- 'watcherlogname',
60
- 'watchertimeform',
61
- 'wconsolelogname',
62
- 'logrotateopt',
63
- 'userdata',
64
- 'extdata',
65
- 'k2hr3admin',
66
- 'localtenants',
67
- 'confirmtenant',
68
- 'chkipconfig',
69
- 'allowcredauth',
70
- 'expiration'
71
- ];
72
-
73
- var loadedConfig = (function()
74
- {
75
- var config = require('config');
76
- var data = {
77
- // [NOTE]
78
- // Set keystone type and others for openstack keystone.
79
- //
80
- // Specify the keystone.type module to handle keystone authentication.
81
- // We prepare two of keystone V2/V3 for this value by k2hr3. They are
82
- // lib/openstackapiv2.js and lib/openstackapiv3.js.
83
- // Please specify module name specified for keystone.type without
84
- // extension. The matching file name is automatically searched from
85
- // the lib directory.
86
- // If you want to use your own module, prepare the file in the lib
87
- // directory and specify the file name. To create your own, please
88
- // prepare the module exporting the getKeystoneEndpoint(callback, is_v3, is_test, timeout)
89
- // function. For details of the getKeystoneEndpoint function, please
90
- // refer to openstackep.js.
91
- //
92
- // Please specify 'list' or 'file' as keystone.eptype.
93
- // If 'list' is specified, please specify one or more {'region name': 'keystone endpoint URI'}
94
- // in keystone.eplist.
95
- // If you specify 'file', specify keystone.epfile with your own module
96
- // name in the lib directory. This module specifies the file exporting
97
- // the getDynamicKeystoneEndpoints(cbargs, callback) function.
98
- // The getDynamicKeystoneEndpoints function takes callback(cbargs, error, endpoint_mapping)
99
- // as a callback function as an argument. 'endpoint_mapping' argument
100
- // of this callback function should return data of the same type as
101
- // keystone.eplist.
102
- // cbargs specifies the cbargs passed to the getDynamicKeystoneEndpoints
103
- // function as is.
104
- //
105
- keystone: { // User authentication type
106
- type: 'openstackapiv3', // module name in lib for openstack keystone access
107
- eptype: 'list', // type of openstack keystone endpoint
108
- epfile: null,
109
- eplist: {
110
- myregion: 'https://dummy.keystone.openstack/'
111
- }
112
- },
113
-
114
- k2hdkc: { // Slave configuration to K2HDKC cluster
115
- config: '/etc/k2hdkc/slave.ini', // Configuration file path for k2hdkc(chmpx) slave
116
- port: '8031', // Control port number for k2hdkc(chmpx) slave
117
- cuk: null // CUK for k2hdkc(chmpx) slave
118
- },
119
-
120
- corsips: [ // CORS IP Addresses
121
- '::ffff:127.0.0.1',
122
- '127.0.0.1'
123
- ],
124
-
125
- scheme: 'http', // Scheme
126
- port: 80, // Port
127
- multiproc: true, // Multi processing
128
- runuser: '', // Username for process owner
129
- privatekey: '', // Privatekey file path
130
- cert: '', // Certification file path
131
- ca: '', // CA
132
-
133
- logdir: null, // Path for logging directory
134
- fixedlogdir: null, // Fixed log directory
135
- accesslogname: 'access.log', // Access log name
136
- accesslogform: 'combined', // Access log format by morgan
137
- consolelogname: null, // Console(Error)/Debug log name
138
- watcherlogname: 'watcher.log', // Watcher log name
139
- watchertimeform: 'yyyy/mm/dd HH:MM:ss', // Watcher log time format by dateformat
140
- wconsolelogname: null, // Console(Error)/Debug log name by watcher
141
- logrotateopt: { // rotating-file-stream option object
142
- compress: 'gzip', // gzip : compression method of rotated files.
143
- interval: '6h', // 6 hour : the time interval to rotate the file.
144
- initialRotation: true, // true : initial rotation based on not-rotated file timestamp.
145
- path: null // null : the base path for files.(* this value is replace by 'logdir')
146
- /*
147
- * [NOTE] following option is not specified now.
148
- *
149
- rotationTime: true, // true : makes rotated file name with time of rotation.
150
- highWaterMark: null, // null : proxy to new stream.
151
- history: null, // null : the history filename.
152
- immutable: null, // null : never mutates file names.
153
- maxFiles: null, // null : the maximum number of rotated files to keep.
154
- maxSize: null, // null : the maximum size of rotated files to keep.
155
- mode: null, // null : proxy to fs.createWriteStream
156
- rotate: null, // null : enables the classical UNIX logrotate behaviour.
157
- size: null // null : the file size to rotate the file.
158
- */
159
- },
160
-
161
- userdata: { // Userdata for Openstack
162
- baseuri: 'https://localhost', // URI
163
- cc_templ: 'config/k2hr3-cloud-config.txt.templ', // Template for Cloud Config part
164
- script_templ: 'config/k2hr3-init.sh.templ', // Template for Shell part
165
- errscript_templ: 'config/k2hr3-init-error.sh.templ', // Template for common shell if error
166
- algorithm: 'aes-256-cbc', // Encrypt type
167
- passphrase: 'k2hr3_regpass' // Default passphrase
168
- },
169
-
170
- extdata: { // Additional extra data
171
- /*
172
- * // [NOTE]
173
- * // This Extra data is the data unique to the user who performs
174
- * // the same operation as User data.
175
- * // The data is encrypted and compressed.
176
- * // The encryption uses the algorithm and passphrase specified
177
- * // in userdata.
178
- *
179
- dummy: { // Extra data API(key=suburi) for trove k2hdkc
180
- baseuri: 'https://localhost', // URI
181
- template: 'config/extdata-dummy.sh.templ', // Template for Shell part
182
- useragent: 'dummy-client' // Allowed user-agent(can be omitted: default is allowed all)
183
- contenttype: 'text/x-shellscript; charset="us-ascii"'// Response Content-Type(can be omitted: default is 'text/plain')
184
- }
185
- */
186
- },
187
-
188
- k2hr3admin: { // K2HR3 Admin information for example is removing IP Addresses
189
- tenant: 'admintenant', // Admin tenant name
190
- delhostrole: 'delhostrole' // Admin Role name
191
- },
192
-
193
- localtenants: true, // Whether to allow K2HR3 cluster local tenants
194
- confirmtenant: false, // Whichever confirm tenant when adding service member
195
-
196
- chkipconfig: { // IP Addresses checker(watcher) type
197
- type: chkipconfigType.CHECKER_TYPE_LISTENER, // Listener / Function / Basic{Or|And} / NoCheck
198
- funcmod: null, // Module name(path) for Function type
199
- pendingsec: 864000, // Limit for removing IP which is not alive : 10 * 24 * 60 * 60 = 10 days
200
- intervalms: 4320000, // Interval ms for checking IP address : 12 * 60 * 60 * 1000 = 12 hour
201
- parallelcnt: 32, // Parallel processing count
202
- command4: 'ping', // Basic IP address check use this command for IPv4 : ping command
203
- command6: 'ping6', // Basic IP address check use this command for IPv6
204
- params: '-c 1', // Common ping command parameters
205
- timeoutparam: '-W', // Timeout parameter name for ping command
206
- timeoutms: 5000 // Timeout millisecond for each checking : 5000ms
207
- },
208
- allowcredauth: true, // allow CORS access for authorization by credential
209
-
210
- expiration: { // Expiration for Tokens
211
- roletoken: 86400, // Expire time(sec) for RoleToken : 24 * 60 * 60 = 1 day
212
- regroletoken: 315360000 // Expire time(sec) for register host : 10 * 356 * 24 * 60 * 60 = 10 years(no expire)
213
- }
214
- };
215
-
216
- if(apiutil.isSafeEntity(config)){
217
- var tmp;
218
-
219
- // Keystone type
220
- if(apiutil.isSafeEntity(config.keystone)){
221
- if(apiutil.isSafeString(config.keystone.type)){
222
- data.keystone.type = config.keystone.type;
223
- }
224
- if(apiutil.compareCaseString('list', config.keystone.eptype)){
225
- if(apiutil.isSafeEntity(config.keystone.eplist)){
226
- var eplist = {};
227
- for(var region in config.keystone.eplist){
228
- if(apiutil.isSafeUrl(config.keystone.eplist[region])){
229
- eplist[region] = config.keystone.eplist[region];
230
- }
231
- }
232
- data.keystone.eptype = config.keystone.eptype;
233
- data.keystone.epfile = null;
234
- data.keystone.eplist = eplist;
235
- }else{
236
- // Wrong value
237
- }
238
- }else if(apiutil.compareCaseString('file', config.keystone.eptype)){
239
- if(apiutil.isSafeString(config.keystone.epfile)){
240
- data.keystone.eptype = config.keystone.eptype;
241
- data.keystone.epfile = config.keystone.epfile;
242
- data.keystone.eplist = null;
243
- }else{
244
- // Wrong value
245
- }
246
- }else{
247
- // Unknown value
248
- }
249
- }
250
-
251
- // K2HDKC configuration
252
- if(apiutil.isSafeEntity(config.k2hdkc)){
253
- if(apiutil.isSafeString(config.k2hdkc.config)){
254
- data.k2hdkc.config = config.k2hdkc.config;
255
- }
256
- if(apiutil.isSafeEntity(config.k2hdkc.port) && !isNaN(config.k2hdkc.port)){
257
- data.k2hdkc.port = config.k2hdkc.port;
258
- }
259
- if(apiutil.isSafeString(config.k2hdkc.cuk)){
260
- data.k2hdkc.cuk = config.k2hdkc.cuk;
261
- }
262
- }
263
-
264
- // CORS
265
- if(!apiutil.isEmptyArray(config.corsips)){
266
- apiutil.mergeArray(data.corsips, config.corsips);
267
- }
268
-
269
- // multi processes
270
- if(apiutil.isSafeEntity(config.multiproc) && 'boolean' == typeof config.multiproc){
271
- data.multiproc = config.multiproc;
272
- }
273
-
274
- // scheme & port
275
- if(apiutil.isSafeString(config.scheme)){
276
- data.scheme = apiutil.getSafeString(config.scheme);
277
- if(apiutil.compareCaseString('http', data.scheme)){
278
- data.port = 80;
279
- }else if(apiutil.compareCaseString('https', data.scheme)){
280
- data.port = 443;
281
- }
282
- }
283
- if(apiutil.isSafeEntity(config.port)){
284
- tmp = normalizePort(config.port);
285
- if(false !== tmp){
286
- data.port = tmp;
287
- }
288
- }else if(apiutil.isSafeEntity(process.env.PORT)){ // Get port from environment
289
- tmp = normalizePort(process.env.PORT);
290
- if(false !== tmp){
291
- data.port = tmp;
292
- }
293
- }
294
- // run user
295
- if(apiutil.isSafeString(config.runuser) || null === config.runuser){
296
- data.runuser = apiutil.getSafeString(config.runuser);
297
- }
298
- // private key & cert & ca
299
- if(apiutil.isSafeString(config.privatekey) || null === config.privatekey){
300
- data.privatekey = apiutil.getSafeString(config.privatekey);
301
- }
302
- if(apiutil.isSafeString(config.cert) || null === config.cert){
303
- data.cert = apiutil.getSafeString(config.cert);
304
- }
305
- if(apiutil.isSafeString(config.ca) || null === config.ca){
306
- data.ca = apiutil.getSafeString(config.ca);
307
- }
308
-
309
- // log directory
310
- if(apiutil.isSafeString(config.logdir) || null === config.logdir){
311
- data.logdir = apiutil.getSafeString(config.logdir);
312
- }
313
- // access log file name
314
- if(apiutil.isSafeString(config.accesslogname) || null === config.accesslogname){
315
- data.accesslogname = apiutil.getSafeString(config.accesslogname);
316
- }
317
- // access log format
318
- if(apiutil.isSafeString(config.accesslogform)){
319
- data.accesslogform = apiutil.getSafeString(config.accesslogform);
320
- }
321
- // console(error) log file name
322
- if(apiutil.isSafeString(config.consolelogname) || null === config.consolelogname){
323
- data.consolelogname = apiutil.getSafeString(config.consolelogname);
324
- }
325
- // watcher log file name
326
- if(apiutil.isSafeString(config.watcherlogname) || null === config.watcherlogname){
327
- data.watcherlogname = apiutil.getSafeString(config.watcherlogname);
328
- }
329
- // watcher log time format
330
- if(apiutil.isSafeString(config.watchertimeform)){
331
- data.watchertimeform= apiutil.getSafeString(config.watchertimeform);
332
- }
333
- // watcher console(error) log file name
334
- if(apiutil.isSafeString(config.wconsolelogname) || null === config.wconsolelogname){
335
- data.wconsolelogname= apiutil.getSafeString(config.wconsolelogname);
336
- }
337
- // log rotation option
338
- if(apiutil.isSafeEntity(config.logrotateopt) && 'object' == typeof config.logrotateopt && !apiutil.isArray(config.logrotateopt)){
339
- Object.keys(config.logrotateopt).forEach(function(key){
340
- if(apiutil.isSafeEntity(config.logrotateopt[key])){
341
- data.logrotateopt[key] = config.logrotateopt[key];
342
- }else{
343
- // [NOTE] Not allow keyname
344
- }
345
- });
346
- }
347
-
348
- // Userdata
349
- if(apiutil.isSafeEntity(config.userdata)){
350
- if(apiutil.isSafeString(config.userdata.baseuri)){
351
- data.userdata.baseuri = config.userdata.baseuri;
352
- }
353
- if(apiutil.isSafeString(config.userdata.cc_templ)){
354
- data.userdata.cc_templ = config.userdata.cc_templ;
355
- }
356
- if(apiutil.isSafeString(config.userdata.script_templ)){
357
- data.userdata.script_templ = config.userdata.script_templ;
358
- }
359
- if(apiutil.isSafeString(config.userdata.errscript_templ)){
360
- data.userdata.errscript_templ = config.userdata.errscript_templ;
361
- }
362
- if(apiutil.isSafeString(config.userdata.algorithm)){
363
- data.userdata.algorithm = config.userdata.algorithm;
364
- }
365
- if(apiutil.isSafeString(config.userdata.passphrase)){
366
- data.userdata.passphrase = config.userdata.passphrase;
367
- }
368
- }
369
-
370
- // Extdata
371
- if(apiutil.isSafeEntity(config.extdata)){
372
- Object.keys(config.extdata).forEach(function(extkey){
373
- var extradata = config.extdata[extkey];
374
-
375
- if(apiutil.isSafeString(extradata.baseuri) && apiutil.isSafeString(extradata.template)){
376
- data.extdata[extkey] = {};
377
- data.extdata[extkey].baseuri = extradata.baseuri;
378
- data.extdata[extkey].template = extradata.template;
379
- data.extdata[extkey].useragent = apiutil.isSafeString(extradata.useragent) ? apiutil.getSafeString(extradata.useragent) : null;
380
- data.extdata[extkey].contenttype= apiutil.isSafeString(extradata.contenttype) ? apiutil.getSafeString(extradata.contenttype) : 'text/plain';
381
- }else{
382
- // [NOTE] Some required parameters could not be detected, then skip this.
383
- }
384
- });
385
- }
386
-
387
- // Admin
388
- if(apiutil.isSafeEntity(config.k2hr3admin)){
389
- if(apiutil.isSafeString(config.k2hr3admin.tenant)){
390
- data.k2hr3admin.tenant = config.k2hr3admin.tenant;
391
- }
392
- if(apiutil.isSafeString(config.k2hr3admin.delhostrole)){
393
- data.k2hr3admin.delhostrole = config.k2hr3admin.delhostrole;
394
- }
395
- }
396
-
397
- // Local tenants
398
- if(apiutil.isSafeEntity(config.localtenants) && 'boolean' == typeof config.localtenants){
399
- data.localtenants = config.localtenants;
400
- }
401
-
402
- // Confirm tenant mode for adding service member
403
- if(apiutil.isSafeEntity(config.confirmtenant) && 'boolean' == typeof config.confirmtenant){
404
- data.confirmtenant = config.confirmtenant;
405
- }
406
-
407
- // IP Addresses checker(watcher) config
408
- if(apiutil.isSafeEntity(config.chkipconfig)){
409
- if( apiutil.isSafeString(config.chkipconfig.type) &&
410
- ( chkipconfigType.CHECKER_TYPE_LISTENER === config.chkipconfig.type ||
411
- chkipconfigType.CHECKER_TYPE_FUNCTION === config.chkipconfig.type ||
412
- chkipconfigType.CHECKER_TYPE_BASIC_OR === config.chkipconfig.type ||
413
- chkipconfigType.CHECKER_TYPE_BASIC_AND === config.chkipconfig.type ||
414
- chkipconfigType.CHECKER_TYPE_NOCHECK === config.chkipconfig.type )
415
- ){
416
- data.chkipconfig.type = config.chkipconfig.type;
417
- }
418
- if(apiutil.isSafeString(config.chkipconfig.funcmod)){
419
- data.chkipconfig.funcmod = config.chkipconfig.funcmod;
420
- }
421
- if(apiutil.isSafeEntity(config.chkipconfig.pendingsec) && !isNaN(config.chkipconfig.pendingsec)){
422
- data.chkipconfig.pendingsec = config.chkipconfig.pendingsec;
423
- }
424
- if(apiutil.isSafeEntity(config.chkipconfig.intervalms) && !isNaN(config.chkipconfig.intervalms)){
425
- data.chkipconfig.intervalms = config.chkipconfig.intervalms;
426
- }
427
- if(apiutil.isSafeEntity(config.chkipconfig.parallelcnt) && !isNaN(config.chkipconfig.parallelcnt)){
428
- data.chkipconfig.parallelcnt = config.chkipconfig.parallelcnt;
429
- }
430
- if(apiutil.isSafeString(config.chkipconfig.command4)){
431
- data.chkipconfig.command4 = config.chkipconfig.command4;
432
- }
433
- if(apiutil.isSafeString(config.chkipconfig.command6)){
434
- data.chkipconfig.command6 = config.chkipconfig.command6;
435
- }
436
- if(apiutil.isSafeString(config.chkipconfig.params)){
437
- data.chkipconfig.params = config.chkipconfig.params;
438
- }
439
- if(apiutil.isSafeString(config.chkipconfig.timeoutparam)){
440
- data.chkipconfig.timeoutparam = config.chkipconfig.timeoutparam;
441
- }
442
- if(apiutil.isSafeEntity(config.chkipconfig.timeoutms) && !isNaN(config.chkipconfig.timeoutms)){
443
- data.chkipconfig.timeoutms = config.chkipconfig.timeoutms;
444
- }
445
- }
446
- if(apiutil.isSafeEntity(config.allowcredauth) && 'boolean' == typeof config.allowcredauth && config.allowcredauth){
447
- data.allowcredauth = config.allowcredauth;
448
- }
449
-
450
- // Expiration for Tokens
451
- if(apiutil.isSafeEntity(config.expiration)){
452
- if(apiutil.isSafeEntity(config.expiration.roletoken) && !isNaN(config.expiration.roletoken)){
453
- data.expiration.roletoken = parseInt(config.expiration.roletoken);
454
- }
455
- if(apiutil.isSafeEntity(config.expiration.regroletoken) && !isNaN(config.expiration.regroletoken)){
456
- data.expiration.regroletoken = parseInt(config.expiration.regroletoken);
457
- }
458
- }
459
-
460
- // Other objects
461
- Object.keys(config).forEach(function(key){
462
- if(!apiutil.findStringInArray(reserved_key_list, key)){
463
- // not found key in reserved key name list, then add(replace) this object to data.
464
- data[key] = config[key];
465
- }
466
- });
467
- }
468
- return data;
469
- }());
470
-
471
- //
472
- // Normalize a port into a number, string, or false.
473
- //
474
- function normalizePort(val)
475
- {
476
- if(isNaN(val)){
477
- // named pipe
478
- if(!apiutil.isSafeString(val)){
479
- return false;
480
- }
481
- return val;
482
- }
483
- var port = parseInt(val, 10);
484
- if(0 <= port){
485
- // port number
486
- return port;
487
- }
488
- return false;
489
- }
490
-
491
- //---------------------------------------------------------
492
- // Configuration Class
493
- //---------------------------------------------------------
494
- var R3ApiConfig = (function()
495
- {
496
- //
497
- // Constructor
498
- //
499
- var R3ApiConfig = function()
500
- {
501
- this.loadedConfig = loadedConfig;
502
- this.consolelog = null;
503
- };
504
-
505
- var proto = R3ApiConfig.prototype;
506
-
507
- //
508
- // Methods
509
- //
510
- proto.getKeystoneConfig = function()
511
- {
512
- return this.loadedConfig.keystone;
513
- };
514
-
515
- proto.getKeystoneType = function()
516
- {
517
- return this.loadedConfig.keystone.type;
518
- };
519
-
520
- proto.getKeystoneEpType = function()
521
- {
522
- return this.loadedConfig.keystone.eptype;
523
- };
524
-
525
- proto.isKeystoneEpList = function()
526
- {
527
- return apiutil.compareCaseString('list', this.loadedConfig.keystone.eptype);
528
- };
529
-
530
- proto.getKeystoneEpList = function()
531
- {
532
- if(!apiutil.compareCaseString('list', this.loadedConfig.keystone.eptype)){
533
- return null;
534
- }
535
- return this.loadedConfig.keystone.eplist; // [NOTE] return reference
536
- };
537
-
538
- proto.isKeystoneEpFile = function()
539
- {
540
- return apiutil.compareCaseString('file', this.loadedConfig.keystone.eptype);
541
- };
542
-
543
- proto.getKeystoneEpFile = function()
544
- {
545
- if(!apiutil.compareCaseString('file', this.loadedConfig.keystone.eptype)){
546
- return null;
547
- }
548
- return this.loadedConfig.keystone.epfile;
549
- };
550
-
551
- proto.getK2hdkcConfig = function()
552
- {
553
- return this.loadedConfig.k2hdkc.config;
554
- };
555
-
556
- proto.getK2hdkcPort = function()
557
- {
558
- return this.loadedConfig.k2hdkc.port;
559
- };
560
-
561
- proto.getK2hdkcCuk = function()
562
- {
563
- return this.loadedConfig.k2hdkc.cuk;
564
- };
565
-
566
- proto.getCORSIPs = function()
567
- {
568
- return this.loadedConfig.corsips;
569
- };
570
-
571
- proto.isMultiProc = function()
572
- {
573
- return this.loadedConfig.multiproc;
574
- };
575
-
576
- proto.getScheme = function()
577
- {
578
- return this.loadedConfig.scheme;
579
- };
580
-
581
- proto.getPort = function()
582
- {
583
- return this.loadedConfig.port;
584
- };
585
-
586
- proto.getRunUser = function()
587
- {
588
- return this.loadedConfig.runuser;
589
- };
590
-
591
- proto.getPrivateKey = function()
592
- {
593
- return this.loadedConfig.privatekey;
594
- };
595
-
596
- proto.getCert = function()
597
- {
598
- return this.loadedConfig.cert;
599
- };
600
-
601
- proto.getCA = function()
602
- {
603
- return this.loadedConfig.ca;
604
- };
605
-
606
- proto.updateLogDir = function(basepath)
607
- {
608
- var dirpath = null;
609
- if(null !== this.loadedConfig.logdir){
610
- if(0 === this.loadedConfig.logdir.indexOf('/')){
611
- dirpath = path.join(this.loadedConfig.logdir); // logdir is full path
612
- }else{
613
- if(apiutil.isSafeString(basepath)){
614
- if(0 === basepath.indexOf('/')){
615
- dirpath = path.join(basepath, this.loadedConfig.logdir);
616
- }else{
617
- dirpath = path.join(__dirname, '../..', basepath, this.loadedConfig.logdir);// from top directory
618
- }
619
- }else{
620
- dirpath = path.join(__dirname, '../..', this.loadedConfig.logdir); // from top directory
621
- }
622
- }
623
- }else{
624
- // logdir is null, it means not putting log to file.
625
- }
626
-
627
- // update log directory
628
- this.loadedConfig.fixedlogdir = dirpath;
629
- if(apiutil.isSafeString(dirpath)){
630
- // check log directory and make it if not exists
631
- if(null !== dirpath && !apiutil.checkMakeDir(dirpath)){
632
- console.warn('Log directory(' + dirpath + ') is not existed, and could not create it.');
633
- dirpath = null; // continue with no log directory
634
- }else{
635
- // set dir path to log rotation option
636
- this.loadedConfig.logrotateopt['path'] = dirpath;
637
- }
638
- }
639
-
640
- return dirpath;
641
- };
642
-
643
- proto.getAccessLogName = function()
644
- {
645
- return this.loadedConfig.accesslogname;
646
- };
647
-
648
- proto.getAccessLogFormat = function()
649
- {
650
- return this.loadedConfig.accesslogform;
651
- };
652
-
653
- proto.getConsoleLogName = function()
654
- {
655
- return this.loadedConfig.consolelogname;
656
- };
657
-
658
- proto.getWatcherLogName = function()
659
- {
660
- return this.loadedConfig.watcherlogname;
661
- };
662
-
663
- proto.getWatcherTimeFormat = function()
664
- {
665
- return this.loadedConfig.watchertimeform;
666
- };
667
-
668
- proto.getWatcherConsoleLogName = function()
669
- {
670
- return this.loadedConfig.wconsolelogname;
671
- };
672
-
673
- proto.getLogRotateOption = function()
674
- {
675
- return this.loadedConfig.logrotateopt;
676
- };
677
-
678
- proto.getRotateLogStream = function(basedir, filename)
679
- {
680
- var logstream = null;
681
- var logdir = this.updateLogDir(basedir);
682
- if(null == logdir){
683
- return logstream;
684
- }
685
- if(!apiutil.isSafeString(filename)){
686
- return logstream;
687
- }
688
- try{
689
- logstream = rotatefs.createStream(filename, this.loadedConfig.logrotateopt);
690
- }catch(error){
691
- console.warn('Could not create log rotate option by : ' + JSON.stringify(error.message));
692
- logstream = null;
693
- }
694
- return logstream;
695
- };
696
-
697
- proto.getMorganLoggerOption = function(basedir)
698
- {
699
- var loggeropt = null;
700
- var logstream = this.getRotateLogStream(basedir, this.loadedConfig.accesslogname);
701
- if(null !== logstream){
702
- loggeropt = {
703
- stream: logstream
704
- };
705
- }
706
- return loggeropt;
707
- };
708
-
709
- proto.getWatcherLoggingStream = function(basedir)
710
- {
711
- var logstream = this.getRotateLogStream(basedir, this.loadedConfig.watcherlogname);
712
- if(null == logstream){
713
- // [NOTE]
714
- // Default stream is stdout
715
- //
716
- logstream = process.stdout;
717
- }
718
- return logstream;
719
- };
720
-
721
- proto.setConsoleLogging = function(basedir, is_watcher)
722
- {
723
- var logname = this.loadedConfig.consolelogname;
724
- if(apiutil.isSafeEntity(is_watcher) && 'boolean' == typeof is_watcher && is_watcher){
725
- logname = this.loadedConfig.wconsolelogname;
726
- }
727
- var logstream = this.getRotateLogStream(basedir, logname);
728
- if(null !== logstream){
729
- this.consolelog = new console.Console(logstream, logstream);
730
- global.console.error= this.consolelog.error;
731
- global.console.warn = this.consolelog.warn;
732
- global.console.log = this.consolelog.log;
733
- global.console.debug= this.consolelog.debug;
734
- global.console.info = this.consolelog.info;
735
- }
736
- return true;
737
- };
738
-
739
- proto.getUserdataConfig = function()
740
- {
741
- return this.loadedConfig.userdata;
742
- };
743
-
744
- proto.getUserdataCryptConfig = function()
745
- {
746
- var udCryptConfig = {};
747
- udCryptConfig.algorithm = this.loadedConfig.userdata.algorithm;
748
- udCryptConfig.passphrase= this.loadedConfig.userdata.passphrase;
749
-
750
- return udCryptConfig;
751
- };
752
-
753
- proto.getExtdataConfigCount = function()
754
- {
755
- return Object.keys(this.loadedConfig.extdata).length;
756
- };
757
-
758
- proto.getAllExtdataConfig = function()
759
- {
760
- return this.loadedConfig.extdata;
761
- };
762
-
763
- proto.getK2hr3AdminConfig = function()
764
- {
765
- return this.loadedConfig.k2hr3admin;
766
- };
767
-
768
- proto.isLocalTenants = function()
769
- {
770
- return this.loadedConfig.localtenants;
771
- };
772
-
773
- proto.isConfirmTenantForService = function()
774
- {
775
- return this.loadedConfig.confirmtenant;
776
- };
777
-
778
- proto.getCheckIPConfig = function()
779
- {
780
- return this.loadedConfig.chkipconfig;
781
- };
782
-
783
- proto.isAllowedCredentialAccess = function()
784
- {
785
- return this.loadedConfig.allowcredauth;
786
- };
787
-
788
- proto.getExpireTimeRoleToken = function()
789
- {
790
- return this.loadedConfig.expiration.roletoken;
791
- };
792
-
793
- proto.getExpireTimeRegRoleToken = function()
794
- {
795
- return this.loadedConfig.expiration.regroletoken;
796
- };
797
-
798
- proto.getOtherObject = function(key)
799
- {
800
- if(!apiutil.isSafeString(key)){
801
- return null;
802
- }
803
- if(!apiutil.isSafeEntity(this.loadedConfig[key])){
804
- return null;
805
- }
806
- return this.loadedConfig[key];
807
- };
808
-
809
- return R3ApiConfig;
810
- })();
811
-
812
- //---------------------------------------------------------
813
- // Exports
814
- //---------------------------------------------------------
815
- exports.chkipType = chkipconfigType;
816
-
817
- exports.r3ApiConfig = R3ApiConfig;
818
-
819
- /*
820
- * Local variables:
821
- * tab-width: 4
822
- * c-basic-offset: 4
823
- * End:
824
- * vim600: noexpandtab sw=4 ts=4 fdm=marker
825
- * vim<600: noexpandtab sw=4 ts=4
826
- */