UNPKG

k2hr3-api

Version:

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

218 lines (217 loc) 7.24 kB
#!/usr/bin/env node "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 Jun 8 2017 * 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 }); // // Module dependencies. // const app_1 = __importDefault(require("../app")); const debug_1 = __importDefault(require("debug")); const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const cluster_1 = __importDefault(require("cluster")); const https = __importStar(require("https")); const http = __importStar(require("http")); const k2hr3apiutil_1 = __importDefault(require("../lib/k2hr3apiutil")); const k2hr3config_1 = require("../lib/k2hr3config"); const dbg = (0, debug_1.default)('k2hr3-api:server'); const numCPUs = os_1.default.cpus().length; const apiConf = new k2hr3config_1.r3ApiConfig(); const key = apiConf.getPrivateKey(); // allow empty const cert = apiConf.getCert(); // allow empty const ca = apiConf.getCA(); // allow empty const user = apiConf.getRunUser(); // allow empty const port = apiConf.getPort(); let server; // // Event listener for HTTP server "error" event. // const onError = (error) => { if ('listen' !== error.syscall) { throw error; } const bind = k2hr3apiutil_1.default.isString(port) ? ('Pipe ' + port) : ('Port ' + String(port)); // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } }; // // Event listener for HTTP server "listening" event. // const onListening = () => { const addr = server.address(); if (!k2hr3apiutil_1.default.isSafeEntity(addr)) { dbg('Server address is null'); } else { if (k2hr3apiutil_1.default.isString(addr)) { const bind = 'pipe ' + addr; dbg('Listening on ' + bind); } else if (k2hr3apiutil_1.default.isSafeNumeric(addr.port)) { const bind = 'port ' + String(addr.port); dbg('Listening on ' + bind); } else { dbg('Server address is unknown'); } } }; // // Setup console logging // apiConf.setConsoleLogging(__dirname + '/..', false); // replace output from stdout/stderr to file if set in config if (cluster_1.default.isPrimary && (!k2hr3apiutil_1.default.isSafeEntity(apiConf.isMultiProc()) || false !== apiConf.isMultiProc())) { console.log(`Master ${process.pid} is running`); // Fork workers. for (let cnt = 0; cnt < numCPUs; ++cnt) { cluster_1.default.fork(); } cluster_1.default.on('exit', (worker, code, signal) => { if (k2hr3apiutil_1.default.isString(signal)) { console.log(`worker was killed by signal: ${signal}`); } else if (0 !== code) { console.log(`worker exited with error code: ${code}`); } else { console.log(`worker ${worker.process.pid} died`); } }); } else { // // Get port from environment and store in Express. // let options = {}; let secure = false; // // scheme // if ('https' == apiConf.getScheme() || 'HTTPS' == apiConf.getScheme()) { secure = true; options = { key: fs_1.default.readFileSync(key), cert: fs_1.default.readFileSync(cert), ca: fs_1.default.readFileSync(ca) }; } else if ('http' == apiConf.getScheme() || 'HTTP' == apiConf.getScheme()) { secure = false; } else { console.log('scheme value(' + apiConf.getScheme() + ') in config is wrong'); process.exit(1); } // // Others // const hostname = os_1.default.hostname() || '127.0.0.1'; // // Store in Express. // app_1.default.set('port', port); // // Create HTTP server. // if (secure) { server = https.createServer(options, app_1.default); } else { server = http.createServer(app_1.default); } // // Listen on provided port, on all network interfaces. // server.listen(port, () => { if (k2hr3apiutil_1.default.isSafeString(user)) { console.log('Attempting setuid to user "' + user + '"...'); if (k2hr3apiutil_1.default.isFunction(process.setuid)) { try { process.setuid(user); console.log('Succeeded to setuid'); } catch (err) { console.log('Failed to setuid', JSON.stringify(err)); process.exit(1); } } } }); server.on('error', onError); server.on('listening', onListening); console.log('Server running at ' + apiConf.getScheme() + '://' + hostname + ':' + port + '/'); console.log(`Worker ${process.pid} started`); } /* * 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 */