UNPKG

k2hr3-api

Version:

K2HR3 REST API is K2hdkc based Resource and Roles and policy Rules

833 lines (832 loc) 34.7 kB
"use strict"; /* * K2HR3 REST API * * Copyright 2017 Yahoo Japan Corporation. * * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers * common management information for the cloud. * K2HR3 can dynamically manage information as "who", "what", "operate". * These are stored as roles, resources, policies in K2hdkc, and the * client system can dynamically read and modify these information. * * For the full copyright and license information, please view * the license file that was distributed with this source code. * * AUTHOR: Takeshi Nakatani * CREATE: Wed Oct 31 2018 * REVISION: * */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.chkipType = exports.r3ApiConfig = exports.loadedConfig = exports.chkipconfigType = void 0; const path = __importStar(require("path")); const rotatefs = __importStar(require("rotating-file-stream")); const config_1 = __importDefault(require("config")); const k2hr3apiutil_1 = __importDefault(require("./k2hr3apiutil")); const openstackep_1 = __importDefault(require("./openstackep")); const toIntervalNumber = (val) => { if (/^\d+d$/.test(val)) { return val; } if (/^\d+s$/.test(val)) { return val; } if (/^\d+M$/.test(val)) { return val; } if (/^\d+h$/.test(val)) { return val; } if (/^\d+m$/.test(val)) { return val; } return '1d'; }; // // Utility: copy(overwrite) LoadedConfig // const setLoadedConfig = (base, data) => { if (!k2hr3apiutil_1.default.isPlainObject(data)) { return null; } // Keystone type if (k2hr3apiutil_1.default.isPlainObject(data.keystone)) { if (k2hr3apiutil_1.default.isSafeString(data.keystone.type)) { base.keystone.type = data.keystone.type; } if (k2hr3apiutil_1.default.isSafeString(data.keystone.eptype) && k2hr3apiutil_1.default.compareCaseString('list', data.keystone.eptype)) { if (openstackep_1.default.isValTypeKeystoneEndpointMap(data.keystone.eplist)) { const eplist = {}; for (const region in data.keystone.eplist) { const tmpRegion = data.keystone.eplist[region]; if (k2hr3apiutil_1.default.isString(tmpRegion)) { if (k2hr3apiutil_1.default.isSafeUrl(tmpRegion)) { eplist[region] = tmpRegion; } } } base.keystone.eptype = data.keystone.eptype; base.keystone.epfile = null; base.keystone.eplist = eplist; } else { // Wrong value } } else if (k2hr3apiutil_1.default.isSafeString(data.keystone.eptype) && k2hr3apiutil_1.default.compareCaseString('file', data.keystone.eptype)) { if (k2hr3apiutil_1.default.isSafeString(data.keystone.epfile)) { base.keystone.eptype = data.keystone.eptype; base.keystone.epfile = data.keystone.epfile; base.keystone.eplist = null; } else { // Wrong value } } else { // Unknown value } } // K2HDKC configuration if (k2hr3apiutil_1.default.isPlainObject(data.k2hdkc)) { if (k2hr3apiutil_1.default.isSafeString(data.k2hdkc.config)) { base.k2hdkc.config = data.k2hdkc.config; } if (k2hr3apiutil_1.default.isSafeNumeric(data.k2hdkc.port)) { const tmpPort = k2hr3apiutil_1.default.cvtToNumber(data.k2hdkc.port); if (k2hr3apiutil_1.default.isSafeNumber(tmpPort)) { base.k2hdkc.port = tmpPort; } } if (null === data.k2hdkc.cuk || k2hr3apiutil_1.default.isString(data.k2hdkc.cuk)) { base.k2hdkc.cuk = data.k2hdkc.cuk; } } // CORS if (k2hr3apiutil_1.default.isStringArray(data.corsips)) { base.corsips = data.corsips; } // multi processes if (k2hr3apiutil_1.default.isBoolean(data.multiproc)) { base.multiproc = data.multiproc; } // scheme & port if (k2hr3apiutil_1.default.isString(data.scheme)) { base.scheme = k2hr3apiutil_1.default.getSafeString(data.scheme); if (k2hr3apiutil_1.default.compareCaseString('http', data.scheme)) { base.port = 80; } else if (k2hr3apiutil_1.default.compareCaseString('https', data.scheme)) { base.port = 443; } } if (k2hr3apiutil_1.default.isSafeEntity(data.port)) { const tmpPort = k2hr3apiutil_1.default.cvtToNumber(data.port); if (k2hr3apiutil_1.default.isSafeNumber(tmpPort)) { base.port = tmpPort; } } else if (k2hr3apiutil_1.default.isSafeNumeric(process.env.PORT)) { // Get port from environment const tmpPort = k2hr3apiutil_1.default.cvtToNumber(process.env.PORT); if (k2hr3apiutil_1.default.isSafeNumber(tmpPort)) { base.port = tmpPort; } } // run user if (null === data.runuser || k2hr3apiutil_1.default.isString(data.runuser)) { base.runuser = k2hr3apiutil_1.default.getSafeString(data.runuser); } // private key & cert & ca if (null === data.privatekey || k2hr3apiutil_1.default.isString(data.privatekey)) { base.privatekey = k2hr3apiutil_1.default.getSafeString(data.privatekey); } if (null === data.cert || k2hr3apiutil_1.default.isString(data.cert)) { base.cert = k2hr3apiutil_1.default.getSafeString(data.cert); } if (null === data.ca || k2hr3apiutil_1.default.isString(data.ca)) { base.ca = k2hr3apiutil_1.default.getSafeString(data.ca); } // log directory if (null === data.logdir || k2hr3apiutil_1.default.isString(data.logdir)) { base.logdir = k2hr3apiutil_1.default.getSafeString(data.logdir); } // access log file name if (null === data.accesslogname || k2hr3apiutil_1.default.isString(data.accesslogname)) { base.accesslogname = k2hr3apiutil_1.default.getSafeString(data.accesslogname); } if (null === data.accesslogform || k2hr3apiutil_1.default.isString(data.accesslogform)) { base.accesslogform = k2hr3apiutil_1.default.getSafeString(data.accesslogform); } if (null === data.consolelogname || k2hr3apiutil_1.default.isString(data.consolelogname)) { base.consolelogname = k2hr3apiutil_1.default.getSafeString(data.consolelogname); } if (null === data.watcherlogname || k2hr3apiutil_1.default.isString(data.watcherlogname)) { base.watcherlogname = k2hr3apiutil_1.default.getSafeString(data.watcherlogname); } if (null === data.watchertimeform || k2hr3apiutil_1.default.isString(data.watchertimeform)) { base.watchertimeform = k2hr3apiutil_1.default.getSafeString(data.watchertimeform); } if (null === data.wconsolelogname || k2hr3apiutil_1.default.isString(data.wconsolelogname)) { base.wconsolelogname = k2hr3apiutil_1.default.getSafeString(data.wconsolelogname); } // log rotation option if (k2hr3apiutil_1.default.isPlainObject(data.logrotateopt)) { if (k2hr3apiutil_1.default.isSafeString(data.logrotateopt.compress)) { base.logrotateopt.compress = data.logrotateopt.compress; } if (k2hr3apiutil_1.default.isSafeString(data.logrotateopt.interval)) { base.logrotateopt.interval = data.logrotateopt.interval; } if (k2hr3apiutil_1.default.isBoolean(data.logrotateopt.initialRotation)) { base.logrotateopt.initialRotation = data.logrotateopt.initialRotation; } if (k2hr3apiutil_1.default.isSafeString(data.logrotateopt.path)) { base.logrotateopt.path = data.logrotateopt.path; } const _logrotateopt = data.logrotateopt; Object.keys(_logrotateopt).forEach((key) => { const val = _logrotateopt[key]; if (null === val || k2hr3apiutil_1.default.isSafeString(val) || k2hr3apiutil_1.default.isBoolean(val)) { base.logrotateopt[key] = val; } }); } // Userdata if (k2hr3apiutil_1.default.isPlainObject(data.userdata)) { if (k2hr3apiutil_1.default.isSafeString(data.userdata.baseuri)) { base.userdata.baseuri = data.userdata.baseuri; } if (k2hr3apiutil_1.default.isSafeString(data.userdata.cc_templ)) { base.userdata.cc_templ = data.userdata.cc_templ; } if (k2hr3apiutil_1.default.isSafeString(data.userdata.script_templ)) { base.userdata.script_templ = data.userdata.script_templ; } if (k2hr3apiutil_1.default.isSafeString(data.userdata.errscript_templ)) { base.userdata.errscript_templ = data.userdata.errscript_templ; } if (k2hr3apiutil_1.default.isSafeString(data.userdata.algorithm)) { base.userdata.algorithm = data.userdata.algorithm; } if (k2hr3apiutil_1.default.isSafeString(data.userdata.passphrase)) { base.userdata.passphrase = data.userdata.passphrase; } } // Extdata if (k2hr3apiutil_1.default.isPlainObject(data.extdata)) { const _extdata = data.extdata; Object.keys(_extdata).forEach((key) => { const one_extdata = _extdata[key]; if (k2hr3apiutil_1.default.isPlainObject(one_extdata) && k2hr3apiutil_1.default.isSafeString(one_extdata.baseuri) && k2hr3apiutil_1.default.isSafeString(one_extdata.template)) { base.extdata[key] = {}; base.extdata[key].baseuri = one_extdata.baseuri; base.extdata[key].template = one_extdata.template; base.extdata[key].useragent = k2hr3apiutil_1.default.isSafeString(one_extdata.useragent) ? k2hr3apiutil_1.default.getSafeString(one_extdata.useragent) : null; base.extdata[key].contenttype = k2hr3apiutil_1.default.isSafeString(one_extdata.contenttype) ? k2hr3apiutil_1.default.getSafeString(one_extdata.contenttype) : 'text/plain'; } else { // [NOTE] Some required parameters could not be detected, then skip this. } }); } // Admin if (k2hr3apiutil_1.default.isPlainObject(data.k2hr3admin)) { if (k2hr3apiutil_1.default.isSafeString(data.k2hr3admin.tenant)) { base.k2hr3admin.tenant = data.k2hr3admin.tenant; } if (k2hr3apiutil_1.default.isSafeString(data.k2hr3admin.delhostrole)) { base.k2hr3admin.delhostrole = data.k2hr3admin.delhostrole; } } // Local tenants if (k2hr3apiutil_1.default.isBoolean(data.localtenants)) { base.localtenants = data.localtenants; } // Confirm tenant mode for adding service member if (k2hr3apiutil_1.default.isBoolean(data.confirmtenant)) { base.confirmtenant = data.confirmtenant; } // IP Addresses checker(watcher) config if (k2hr3apiutil_1.default.isPlainObject(data.chkipconfig)) { if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.type) && (exports.chkipconfigType.CHECKER_TYPE_LISTENER === data.chkipconfig.type || exports.chkipconfigType.CHECKER_TYPE_FUNCTION === data.chkipconfig.type || exports.chkipconfigType.CHECKER_TYPE_BASIC_OR === data.chkipconfig.type || exports.chkipconfigType.CHECKER_TYPE_BASIC_AND === data.chkipconfig.type || exports.chkipconfigType.CHECKER_TYPE_NOCHECK === data.chkipconfig.type)) { base.chkipconfig.type = data.chkipconfig.type; } if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.funcmod)) { base.chkipconfig.funcmod = data.chkipconfig.funcmod; } if (k2hr3apiutil_1.default.isSafeNumeric(data.chkipconfig.pendingsec)) { const tmpTime = k2hr3apiutil_1.default.cvtToNumber(data.chkipconfig.pendingsec); if (k2hr3apiutil_1.default.isSafeNumber(tmpTime)) { base.chkipconfig.pendingsec = tmpTime; } } if (k2hr3apiutil_1.default.isSafeNumeric(data.chkipconfig.intervalms)) { const tmpTime = k2hr3apiutil_1.default.cvtToNumber(data.chkipconfig.intervalms); if (k2hr3apiutil_1.default.isSafeNumber(tmpTime)) { base.chkipconfig.intervalms = tmpTime; } } if (k2hr3apiutil_1.default.isSafeNumeric(data.chkipconfig.parallelcnt)) { const tmpCnt = k2hr3apiutil_1.default.cvtToNumber(data.chkipconfig.parallelcnt); if (k2hr3apiutil_1.default.isSafeNumber(tmpCnt)) { base.chkipconfig.parallelcnt = tmpCnt; } } if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.command4)) { base.chkipconfig.command4 = data.chkipconfig.command4; } if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.command6)) { base.chkipconfig.command6 = data.chkipconfig.command6; } if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.params)) { base.chkipconfig.params = data.chkipconfig.params; } if (k2hr3apiutil_1.default.isSafeString(data.chkipconfig.timeoutparam)) { base.chkipconfig.timeoutparam = data.chkipconfig.timeoutparam; } if (k2hr3apiutil_1.default.isSafeNumeric(data.chkipconfig.timeoutms)) { const tmpTime = k2hr3apiutil_1.default.cvtToNumber(data.chkipconfig.timeoutms); if (k2hr3apiutil_1.default.isSafeNumber(tmpTime)) { base.chkipconfig.timeoutms = tmpTime; } } } if (k2hr3apiutil_1.default.isBoolean(data.allowcredauth)) { base.allowcredauth = data.allowcredauth; } // Expiration for Tokens if (k2hr3apiutil_1.default.isPlainObject(data.expiration)) { if (k2hr3apiutil_1.default.isSafeNumeric(data.expiration.roletoken)) { const tmpNum = k2hr3apiutil_1.default.cvtToNumber(data.expiration.roletoken); if (k2hr3apiutil_1.default.isSafeNumber(tmpNum)) { base.expiration.roletoken = tmpNum; } } if (k2hr3apiutil_1.default.isSafeNumeric(data.expiration.regroletoken)) { const tmpNum = k2hr3apiutil_1.default.cvtToNumber(data.expiration.regroletoken); if (k2hr3apiutil_1.default.isSafeNumber(tmpNum)) { base.expiration.regroletoken = tmpNum; } } } // Other objects Object.keys(config_1.default).forEach((key) => { if (!k2hr3apiutil_1.default.findStringInArray(reserved_key_list, key)) { // not found key in reserved key name list, then add(replace) this object to data. base[key] = data[key]; } }); return base; }; // // [NOTE] // About type assertion // // The config package loads settings dynamically and does not // guarantee type safety by default. // To address this, we define our own LoadedConfig type for // project-specific configuration, and use a type assertion(as // LoadedConfig) on the config object. // This allows us to access configuration values with type safety. // // Note: Type assertion does not perform runtime checks. TypeScript // will assume the config object matches LoadedConfig, // even if the actual config content differs. Be careful to keep // the LoadedConfig definition in sync with the configuration file. // const k2hr3Config = config_1.default; // // For IP Addresses checker(watcher) type // exports.chkipconfigType = { CHECKER_TYPE_LISTENER: 'Listener', CHECKER_TYPE_FUNCTION: 'Function', CHECKER_TYPE_BASIC_OR: 'BasicOr', CHECKER_TYPE_BASIC_AND: 'BasicAnd', CHECKER_TYPE_NOCHECK: 'NoCheck' }; // // Reserved key name in config // const reserved_key_list = [ 'keystone', 'k2hdkc', 'corsips', 'multiproc', 'scheme', 'port', 'runuser', 'privatekey', 'cert', 'ca', 'logdir', 'accesslogname', 'accesslogform', 'consolelogname', 'watcherlogname', 'watchertimeform', 'wconsolelogname', 'logrotateopt', 'userdata', 'extdata', 'k2hr3admin', 'localtenants', 'confirmtenant', 'chkipconfig', 'allowcredauth', 'expiration' ]; // // LoadedConfig // exports.loadedConfig = (() => { // Default values let data = { // [NOTE] // Set keystone type and others for openstack keystone. // // Specify the keystone.type module to handle keystone authentication. // We prepare two of keystone V2/V3 for this value by k2hr3. They are // lib/openstackapiv2.js and lib/openstackapiv3.js. // Please specify module name specified for keystone.type without // extension. The matching file name is automatically searched from // the lib directory. // If you want to use your own module, prepare the file in the lib // directory and specify the file name. To create your own, please // prepare the module exporting the getKeystoneEndpoint(callback, is_v3, is_test, timeout) // function. For details of the getKeystoneEndpoint function, please // refer to openstackep.js. // // Please specify 'list' or 'file' as keystone.eptype. // If 'list' is specified, please specify one or more {'region name': 'keystone endpoint URI'} // in keystone.eplist. // If you specify 'file', specify keystone.epfile with your own module // name in the lib directory. This module specifies the file exporting // the getDynamicKeystoneEndpoints(cbargs, callback) function. // The getDynamicKeystoneEndpoints function takes callback(cbargs, error, endpoint_mapping) // as a callback function as an argument. 'endpoint_mapping' argument // of this callback function should return data of the same type as // keystone.eplist. // cbargs specifies the cbargs passed to the getDynamicKeystoneEndpoints // function as is. // keystone: { type: 'openstackapiv3', // module name in lib for openstack keystone access eptype: 'list', // type of openstack keystone endpoint epfile: null, eplist: { myregion: 'https://dummy.keystone.openstack/' } }, k2hdkc: { config: '/etc/k2hdkc/slave.ini', // Configuration file path for k2hdkc(chmpx) slave port: '8031', // Control port number for k2hdkc(chmpx) slave cuk: null // CUK for k2hdkc(chmpx) slave }, corsips: [ '::ffff:127.0.0.1', '127.0.0.1' ], scheme: 'http', // Scheme port: 80, // Port multiproc: true, // Multi processing runuser: '', // Username for process owner privatekey: '', // Privatekey file path cert: '', // Certification file path ca: '', // CA logdir: null, // Path for logging directory fixedlogdir: null, // Fixed log directory accesslogname: 'access.log', // Access log name accesslogform: 'combined', // Access log format by morgan consolelogname: null, // Console(Error)/Debug log name watcherlogname: 'watcher.log', // Watcher log name watchertimeform: 'yyyy/mm/dd HH:MM:ss', // Watcher log time format by dateformat wconsolelogname: null, // Console(Error)/Debug log name by watcher logrotateopt: { compress: 'gzip', // gzip : compression method of rotated files. interval: '6h', // 6 hour : the time interval to rotate the file. initialRotation: true, // true : initial rotation based on not-rotated file timestamp. path: null // null : the base path for files.(* this value is replace by 'logdir') /* * [NOTE] following option is not specified now. * rotationTime: true, // true : makes rotated file name with time of rotation. highWaterMark: null, // null : proxy to new stream. history: null, // null : the history filename. immutable: null, // null : never mutates file names. maxFiles: null, // null : the maximum number of rotated files to keep. maxSize: null, // null : the maximum size of rotated files to keep. mode: null, // null : proxy to fs.createWriteStream rotate: null, // null : enables the classical UNIX logrotate behaviour. size: null // null : the file size to rotate the file. */ }, userdata: { baseuri: 'https://localhost', // URI cc_templ: 'config/k2hr3-cloud-config.txt.templ', // Template for Cloud Config part script_templ: 'config/k2hr3-init.sh.templ', // Template for Shell part errscript_templ: 'config/k2hr3-init-error.sh.templ', // Template for common shell if error algorithm: 'aes-256-cbc', // Encrypt type passphrase: 'k2hr3_regpass' // Default passphrase }, extdata: { // Additional extra data /* * // [NOTE] * // This Extra data is the data unique to the user who performs * // the same operation as User data. * // The data is encrypted and compressed. * // The encryption uses the algorithm and passphrase specified * // in userdata. * dummy: { // Extra data API(key=suburi) for trove k2hdkc baseuri: 'https://localhost', // URI template: 'config/extdata-dummy.sh.templ', // Template for Shell part useragent: 'dummy-client' // Allowed user-agent(can be omitted: default is allowed all) contenttype: 'text/x-shellscript; charset="us-ascii"'// Response Content-Type(can be omitted: default is 'text/plain') } */ }, k2hr3admin: { tenant: 'admintenant', // Admin tenant name delhostrole: 'delhostrole' // Admin Role name }, localtenants: true, // Whether to allow K2HR3 cluster local tenants confirmtenant: false, // Whichever confirm tenant when adding service member chkipconfig: { type: exports.chkipconfigType.CHECKER_TYPE_LISTENER, // Listener / Function / Basic{Or|And} / NoCheck funcmod: null, // Module name(path) for Function type pendingsec: 864000, // Limit for removing IP which is not alive : 10 * 24 * 60 * 60 = 10 days intervalms: 4320000, // Interval ms for checking IP address : 12 * 60 * 60 * 1000 = 12 hour parallelcnt: 32, // Parallel processing count command4: 'ping', // Basic IP address check use this command for IPv4 : ping command command6: 'ping6', // Basic IP address check use this command for IPv6 params: '-c 1', // Common ping command parameters timeoutparam: '-W', // Timeout parameter name for ping command timeoutms: 5000 // Timeout millisecond for each checking : 5000ms }, allowcredauth: true, // allow CORS access for authorization by credential expiration: { roletoken: 86400, // Expire time(sec) for RoleToken : 24 * 60 * 60 = 1 day regroletoken: 315360000 // Expire time(sec) for register host : 10 * 356 * 24 * 60 * 60 = 10 years(no expire) } }; // Set(overwrite) from loaded config const mergeConfig = setLoadedConfig(data, k2hr3Config); if (null !== mergeConfig) { data = mergeConfig; } else { console.warn('Failed loading configuration file. Please check config file.'); } return data; })(); //--------------------------------------------------------- // Configuration Class //--------------------------------------------------------- class r3ApiConfig { loadedConfig; consolelog; // // Constructor // constructor() { this.loadedConfig = exports.loadedConfig; this.consolelog = null; } // // Methods // getKeystoneConfig = () => { return this.loadedConfig.keystone; }; getKeystoneType = () => { return this.loadedConfig.keystone.type; }; getKeystoneEpType = () => { return this.loadedConfig.keystone.eptype; }; isKeystoneEpList = () => { return k2hr3apiutil_1.default.compareCaseString('list', this.loadedConfig.keystone.eptype); }; getKeystoneEpList = () => { if (!k2hr3apiutil_1.default.compareCaseString('list', this.loadedConfig.keystone.eptype)) { return null; } return this.loadedConfig.keystone.eplist ?? null; }; isKeystoneEpFile = () => { return k2hr3apiutil_1.default.compareCaseString('file', this.loadedConfig.keystone.eptype); }; getKeystoneEpFile = () => { if (!k2hr3apiutil_1.default.compareCaseString('file', this.loadedConfig.keystone.eptype)) { return null; } return this.loadedConfig.keystone.epfile; }; getK2hdkcConfig = () => { return this.loadedConfig.k2hdkc.config; }; getK2hdkcPort = () => { return this.loadedConfig.k2hdkc.port; }; getK2hdkcCuk = () => { return this.loadedConfig.k2hdkc.cuk; }; getCORSIPs = () => { return this.loadedConfig.corsips; }; isMultiProc = () => { return this.loadedConfig.multiproc; }; getScheme = () => { return this.loadedConfig.scheme; }; getPort = () => { return this.loadedConfig.port; }; getRunUser = () => { return this.loadedConfig.runuser; }; getPrivateKey = () => { return this.loadedConfig.privatekey; }; getCert = () => { return this.loadedConfig.cert; }; getCA = () => { return this.loadedConfig.ca; }; updateLogDir = (basepath) => { let dirpath = null; if (null !== this.loadedConfig.logdir) { if (0 === this.loadedConfig.logdir.indexOf('/')) { dirpath = path.join(this.loadedConfig.logdir); // logdir is full path } else { if (null !== basepath && k2hr3apiutil_1.default.isSafeString(basepath)) { if (null !== basepath && 0 === basepath.indexOf('/')) { dirpath = path.join(basepath, this.loadedConfig.logdir); } else { dirpath = path.join(__dirname, '../..', basepath, this.loadedConfig.logdir); // from top directory } } else { dirpath = path.join(__dirname, '../..', this.loadedConfig.logdir); // from top directory } } } else { // logdir is null, it means not putting log to file. } // update log directory this.loadedConfig.fixedlogdir = dirpath; if (k2hr3apiutil_1.default.isSafeString(dirpath)) { // check log directory and make it if not exists if (null !== dirpath && !k2hr3apiutil_1.default.checkMakeDir(dirpath)) { console.warn('Log directory(' + dirpath + ') is not existed, and could not create it.'); dirpath = null; // continue with no log directory } else { // set dir path to log rotation option this.loadedConfig.logrotateopt['path'] = dirpath; } } return dirpath; }; getAccessLogName = () => { return this.loadedConfig.accesslogname; }; getAccessLogFormat = () => { return this.loadedConfig.accesslogform; }; getConsoleLogName = () => { return this.loadedConfig.consolelogname; }; getWatcherLogName = () => { return this.loadedConfig.watcherlogname; }; getWatcherTimeFormat = () => { return this.loadedConfig.watchertimeform; }; getWatcherConsoleLogName = () => { return this.loadedConfig.wconsolelogname; }; getLogRotateOption = () => { return this.loadedConfig.logrotateopt; }; getRotateLogStream = (basedir, filename) => { let logstream = null; const logdir = this.updateLogDir(basedir); if (null == logdir) { return logstream; } if (null === filename || !k2hr3apiutil_1.default.isSafeString(filename)) { return logstream; } try { const rawopt = { ...this.loadedConfig.logrotateopt }; const opt = { ...rawopt, path: rawopt.path ?? undefined, // 'true' -> true(boolean) // 'false' -> false(boolean) // 'gzip' -> 'gzip'(string) // other -> false(boolean) compress: ('true' === rawopt.compress ? true : 'false' === rawopt.compress ? false : 'gzip' === rawopt.compress ? 'gzip' : false), interval: toIntervalNumber(rawopt.interval ?? '1d') }; logstream = rotatefs.createStream(filename, opt); } catch (error) { let msg; if (error instanceof Error) { msg = k2hr3apiutil_1.default.getSafeString(error.message); } else { msg = String(error); } console.warn('Could not create log rotate option by : ' + JSON.stringify(msg)); logstream = null; } return logstream; }; getMorganLoggerOption = (basedir) => { let loggeropt = null; const logstream = this.getRotateLogStream(basedir, this.loadedConfig.accesslogname); if (null !== logstream) { loggeropt = { stream: logstream }; } return loggeropt; }; getWatcherLoggingStream = (basedir) => { let logstream = this.getRotateLogStream(basedir, this.loadedConfig.watcherlogname); if (null == logstream) { // [NOTE] // Default stream is stdout // logstream = process.stdout; } return logstream; }; setConsoleLogging = (basedir, is_watcher) => { let logname = this.loadedConfig.consolelogname; if (k2hr3apiutil_1.default.isBoolean(is_watcher) && is_watcher) { logname = this.loadedConfig.wconsolelogname; } const logstream = this.getRotateLogStream(basedir, logname); if (null !== logstream) { this.consolelog = new console.Console(logstream, logstream); global.console.error = this.consolelog.error; global.console.warn = this.consolelog.warn; global.console.log = this.consolelog.log; global.console.debug = this.consolelog.debug; global.console.info = this.consolelog.info; } return true; }; getUserdataConfig = () => { return this.loadedConfig.userdata; }; getUserdataCryptConfig = () => { const udCryptConfig = { algorithm: this.loadedConfig.userdata.algorithm, passphrase: this.loadedConfig.userdata.passphrase, }; return udCryptConfig; }; getExtdataConfigCount = () => { return Object.keys(this.loadedConfig.extdata).length; }; getAllExtdataConfig = () => { return this.loadedConfig.extdata; }; getK2hr3AdminConfig = () => { return this.loadedConfig.k2hr3admin; }; isLocalTenants = () => { return this.loadedConfig.localtenants; }; isConfirmTenantForService = () => { return this.loadedConfig.confirmtenant; }; getCheckIPConfig = () => { return this.loadedConfig.chkipconfig; }; isAllowedCredentialAccess = () => { return this.loadedConfig.allowcredauth; }; getExpireTimeRoleToken = () => { return this.loadedConfig.expiration.roletoken; }; getExpireTimeRegRoleToken = () => { return this.loadedConfig.expiration.regroletoken; }; getOtherObject = (key) => { if (!k2hr3apiutil_1.default.isSafeString(key)) { return null; } if (!k2hr3apiutil_1.default.isSafeEntity(this.loadedConfig[key])) { return null; } return this.loadedConfig[key]; }; } exports.r3ApiConfig = r3ApiConfig; ; //--------------------------------------------------------- // Exports //--------------------------------------------------------- exports.chkipType = exports.chkipconfigType; /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noexpandtab sw=4 ts=4 fdm=marker * vim<600: noexpandtab sw=4 ts=4 */