UNPKG

k2hr3-api

Version:

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

393 lines (392 loc) 16.9 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: Tue Oct 23 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.basicipcheck = void 0; const dns = __importStar(require("dns")); const childProcess = __importStar(require("child_process")); const k2hr3apiutil_1 = __importDefault(require("./k2hr3apiutil")); const dbglogging_1 = __importDefault(require("./dbglogging")); const k2hr3config_1 = require("./k2hr3config"); //--------------------------------------------------------- // Ping & Set result checking IP Address //--------------------------------------------------------- // ipdata: IP address information // dkcTypeHostRawValueSet = { // ip: ip, -> ip address string // cuk: cuk, -> cuk string (not use) // port: port, -> port number(0 as any) (not use) // extra: string -> 'openstack-auto-v1' or etc (not use) // tag: string -> null or string (not use) // inboundip: ip, -> inbound ip address (not use) // outboundip: ip, -> outbound ip address (not use) // key: string -> this ip address yrn full path(not use) // alive: boolean -> true or false // } // const checkAddressAliveByPing = (ipdata, chkipconfig, callback) => { if (!k2hr3apiutil_1.default.isFunction(callback)) { dbglogging_1.default.elog('callback parameter is wrong'); return; } if (!k2hr3apiutil_1.default.isPlainObject(ipdata)) { callback(new Error('IP data is wrong : ' + JSON.stringify(ipdata)), ipdata); return; } if (!k2hr3apiutil_1.default.isSafeString(ipdata.ip)) { callback(new Error('IP Address is wrong : ' + JSON.stringify(ipdata.ip)), ipdata); return; } if (!k2hr3apiutil_1.default.isPlainObject(chkipconfig)) { // already check this... callback(new Error('chkipconfig value is wrong : ' + JSON.stringify(chkipconfig)), ipdata); return; } let _timeoutms; if (!k2hr3apiutil_1.default.isSafeNumber(chkipconfig.timeoutms)) { dbglogging_1.default.wlog('timeoutms value is wrong : ' + JSON.stringify(chkipconfig.timeoutms)); _timeoutms = 5000; // default } else { _timeoutms = chkipconfig.timeoutms; } // // Ping - Executing ping command with timeout // const _timeoutsec = (_timeoutms / 1000) + 1; let _command; if (ipdata.ip && -1 == ipdata.ip.indexOf(':')) { _command = k2hr3apiutil_1.default.getSafeString(chkipconfig.command4); } else { _command = k2hr3apiutil_1.default.getSafeString(chkipconfig.command6); } _command += ' ' + k2hr3apiutil_1.default.getSafeString(chkipconfig.params); _command += ' ' + k2hr3apiutil_1.default.getSafeString(chkipconfig.timeoutparam); _command += ' ' + _timeoutsec.toString(); _command += ' ' + ipdata.ip; childProcess.exec(_command, (error, _stdout, _stderr) => { if (k2hr3apiutil_1.default.isPlainObject(error)) { dbglogging_1.default.dlog('Something error occurred pinging to ' + ipdata.ip + ' by : ' + JSON.stringify(error)); callback(error, ipdata); } else { callback(null, ipdata); } }); }; //--------------------------------------------------------- // Check DNS & Set result checking IP Address //--------------------------------------------------------- // ipdata: IP address information // dkcTypeHostRawValueSet = { // ip: ip, -> ip address string // cuk: cuk, -> cuk string (not use) // port: port, -> port number(0 as any) (not use) // extra: string -> 'openstack-auto-v1' or etc (not use) // tag: string -> null or string (not use) // inboundip: ip, -> inbound ip address (not use) // outboundip: ip, -> outbound ip address (not use) // key: string -> this ip address yrn full path(not use) // alive: boolean -> true or false // } // const checkAddressAlive = (ipdata, chkipconfig, callback) => { if (!k2hr3apiutil_1.default.isFunction(callback)) { dbglogging_1.default.elog('callback parameter is wrong'); return; } if (!k2hr3apiutil_1.default.isPlainObject(ipdata)) { callback(new Error('IP data is wrong : ' + JSON.stringify(ipdata)), ipdata); return; } if (!k2hr3apiutil_1.default.isSafeString(ipdata.ip)) { callback(new Error('IP Address is wrong : ' + JSON.stringify(ipdata.ip)), ipdata); return; } if (!k2hr3apiutil_1.default.isPlainObject(chkipconfig)) { // already check this... callback(new Error('chkipconfig value is wrong : ' + JSON.stringify(chkipconfig)), ipdata); return; } if (!k2hr3apiutil_1.default.isSafeString(chkipconfig.type) || ('BasicOr' !== chkipconfig.type && 'BasicAnd' !== chkipconfig.type)) { callback(new Error('chkipconfig value is wrong : ' + JSON.stringify(chkipconfig)), ipdata); return; } let _is_and = false; if (k2hr3config_1.chkipType.CHECKER_TYPE_BASIC_OR == chkipconfig.type) { _is_and = false; } else { _is_and = true; } // // checking DNS with timeout by set timer // try { dns.reverse(ipdata.ip, (error, hostnames) => { if (k2hr3apiutil_1.default.isPlainObject(error)) { dbglogging_1.default.dlog('Something error occurred in dns lookup by error: ' + JSON.stringify(error)); if (_is_and) { // [Type = AND] DNS is error, then need to check Ping checkAddressAliveByPing(ipdata, chkipconfig, callback); } else { // [Type = OR] DNS is error, then this IP is not alive callback(error, ipdata); } return; } dbglogging_1.default.dlog('Success dns lookup for ip address(' + ipdata.ip + ') to hostnames(' + JSON.stringify(hostnames) + ').'); if (_is_and) { // [Type = AND] DNS is success, then this IP is alive callback(null, ipdata); } else { // [Type = OR] DNS is error, then need to check Ping checkAddressAliveByPing(ipdata, chkipconfig, callback); } }); } catch (errobj) { dbglogging_1.default.dlog('Something error occurred in dns lookup by error: ' + JSON.stringify(errobj)); if (_is_and) { // [Type = AND] DNS is error, then need to check Ping checkAddressAliveByPing(ipdata, chkipconfig, callback); } else { // [Type = OR] DNS is error, then this IP is not alive if (errobj instanceof Error) { callback(errobj, ipdata); } else { callback(new Error(String(errobj)), ipdata); } } } }; //--------------------------------------------------------- // Check & Set result checking IP Addresses by parallel //--------------------------------------------------------- // ipdatas: IP address information array // dkcTypeHostRawValueSet[] = [{ // ip: <string>, -> ip address string // cuk: <string>, -> cuk string (not use) // port: <number>, -> port number(0 as any) (not use) // extra: <string>, -> 'openstack-auto-v1' or etc (not use) // inboundip: <string>, -> inbound ip address (not use) // outboundip: <string>, -> outbound ip address (not use) // tag: <string>, -> null or string (not use) // key: <string>, -> this ip address yrn full path (not use) // alive: <boolean> -> true or false // }, ...] // start: start position of ipdatas array // chkipconfig: Configuration for IP addresses checking // ipchkTypeConfig = { // type: <string>, -> IP check type (not use) // funcmod: <null or string>, -> IP check special module (not use) // pendingsec: <number>, -> Limit for removing IP which is not alive (not use) // intervalms: <number>, -> Interval ms for checking IP address (not use) // parallelcnt: <number>, -> Parallel processing count // command4: <string>, -> ping command for IPv4 // command6: <string>, -> ping command for IPv6 // params: <string>, -> Common ping command parameters // timeoutparam: <string>, -> Timeout parameter name for ping command // timeoutms: <number> -> Timeout millisecond for each checking // } // // [NOTE] // This function is reentrant after finishing parallel count IP address checking. // If all of ip datas array are finished checking, call callback function. // const checkAddressesAliveParallel = (ipdatas, start, chkipconfig, callback) => { if (!k2hr3apiutil_1.default.isFunction(callback)) { dbglogging_1.default.elog('callback parameter is wrong'); return; } if (!k2hr3apiutil_1.default.isSafeNumber(start)) { callback(new Error('start value is wrong : ' + JSON.stringify(start))); return; } if (!k2hr3apiutil_1.default.isPlainObject(chkipconfig)) { // already check this... callback(new Error('chkipconfig value is wrong : ' + JSON.stringify(chkipconfig))); return; } if (!k2hr3apiutil_1.default.isArray(ipdatas) || !k2hr3apiutil_1.default.isSafeEntity(ipdatas)) { callback(new Error('IP Addresses array is wrong : ' + JSON.stringify(ipdatas))); return; } let _totalcount = chkipconfig.parallelcnt; if (null == _totalcount) { callback(new Error('No rest array for checking')); return; } else if ((ipdatas.length - start) < _totalcount) { _totalcount = ipdatas.length - start; if (_totalcount <= 0) { callback(new Error('No rest array for checking')); return; } } let setcnt = 0; for (let cnt = 0; cnt < _totalcount; ++cnt) { checkAddressAlive(ipdatas[start + cnt], chkipconfig, (error, ipdata) => { if (error) { dbglogging_1.default.dlog('IP Address and CUK (' + JSON.stringify(ipdata) + ') are not alive by ' + JSON.stringify(error)); if (k2hr3apiutil_1.default.isSafeEntity(ipdata)) { ipdata.alive = false; } } else { dbglogging_1.default.dlog('IP Address and CUK (' + JSON.stringify(ipdata) + ') is alive'); if (k2hr3apiutil_1.default.isSafeEntity(ipdata)) { ipdata.alive = true; } } ++setcnt; if (_totalcount <= setcnt) { if (ipdatas.length <= (start + _totalcount)) { callback(null); } else { checkAddressesAliveParallel(ipdatas, (start + _totalcount), chkipconfig, callback); } } }); } }; //--------------------------------------------------------- // Check & Set result checking IP Addresses //--------------------------------------------------------- // ipdatas: IP address information array // dkcTypeHostRawValueSet[] = [{ // ip: <string>, -> ip address string // cuk: <string>, -> cuk string (not use) // port: <number>, -> port number(0 as any) (not use) // extra: <string>, -> 'openstack-auto-v1' or etc (not use) // tag: <string>, -> null or string (not use) // inboundip: <string>, -> inbound ip address (not use) // outboundip: <string>, -> outbound ip address (not use) // key: <string>, -> this ip address yrn full path (not use) // alive: <boolean> -> true or false // }, ...] // chkipconfig: Configuration for IP addresses checking // ipchkTypeConfig = { // type: <string>, -> IP check type (not use) // funcmod: <null or string>, -> IP check special module (not use) // pendingsec: <number>, -> Limit for removing IP which is not alive (not use) // intervalms: <number>, -> Interval ms for checking IP address (not use) // parallelcnt: <number>, -> Parallel processing count // command4: <string>, -> ping command for IPv4 // command6: <string>, -> ping command for IPv6 // params: <string>, -> Common ping command parameters // timeoutparam: <string>, -> Timeout parameter name for ping command // timeoutms: <number> -> Timeout millisecond for each checking // } // // [NOTE] // This function returns Promise object. // resolve: {error: <Error object if error>, data: <reference to input ipdatas>} // reject: <Error object> // const checkAddressesAlivePromise = (ipdatas, chkipconfig) => { const _chkipconfig = chkipconfig; const _ipdatas = ipdatas; // reference return new Promise((resolve, reject) => { const _resolve = resolve; const _reject = reject; if (null == _chkipconfig || !k2hr3apiutil_1.default.isPlainObject(_chkipconfig) || !k2hr3apiutil_1.default.isSafeNumber(_chkipconfig.parallelcnt) || !k2hr3apiutil_1.default.isSafeString(_chkipconfig.command4) || !k2hr3apiutil_1.default.isSafeString(_chkipconfig.command6) || //!apiutil.isSafeString(_chkipconfig.params) || // allow null //!apiutil.isSafeString(_chkipconfig.timeoutparam) || // allow null !k2hr3apiutil_1.default.isSafeNumber(_chkipconfig.timeoutms)) { _reject(new Error('chkipconfig object is wrong : ' + JSON.stringify(_chkipconfig))); return; } if (!k2hr3apiutil_1.default.isArray(_ipdatas)) { _reject(new Error('IP Addresses array is wrong : ' + JSON.stringify(_ipdatas))); return; } checkAddressesAliveParallel(_ipdatas, 0, _chkipconfig, (error) => { if (k2hr3apiutil_1.default.isPlainObject(error)) { dbglogging_1.default.elog('Something error is occurred : ' + JSON.stringify(error)); _resolve({ error: error, data: _ipdatas }); } else { dbglogging_1.default.dlog('Succeed to checking all ip address'); _resolve({ error: null, data: _ipdatas }); } }); }); }; //--------------------------------------------------------- // Exports //--------------------------------------------------------- exports.basicipcheck = { checkAddressesAlive: (ipdatas, chkipconfig) => { return checkAddressesAlivePromise(ipdatas, chkipconfig); } }; exports.default = exports.basicipcheck; /* * 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 */