UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

97 lines (94 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.openssl = exports.isOpenSslInstalled = exports.editor = exports.createTlsCert = exports.createKeyPair = void 0; var _child_process = require("child_process"); var _path = _interopRequireDefault(require("path")); var _snooplogg = _interopRequireDefault(require("snooplogg")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /* istanbul ignore file */ // note: mocking/stubbing spawn is not a trivial task and causes some problems // if done incorrectly. Need to find a good way to test it. const { log } = (0, _snooplogg.default)('central: bashCommands'); // mask / unmask used in file paths for preventing incorrect params split in "openssl" function const maskSpaces = str => str.replace(/ /g, '<SPACE_REPLACEMENT>'); const unmaskSpaces = str => str.replace(/<SPACE_REPLACEMENT>/g, ' '); const openssl = (params, showStdio = false) => { const parsedParams = params.split(' ').map(unmaskSpaces); const process = (0, _child_process.spawn)('openssl', parsedParams, { stdio: showStdio ? 'inherit' : undefined }); return new Promise(resolve => { process.on('exit', code => { if (code === 0) { log('openssl command successful'); resolve({ isComplete: true, code }); } else { log(`openssl command unsuccessful, code: ${code}`); resolve({ isComplete: false, code }); } }); process.on('error', code => { log(`openssl command error before exit, code: ${code}`); resolve({ isComplete: false, code }); }); }); }; exports.openssl = openssl; const isOpenSslInstalled = async () => await openssl('version').then(res => { if (res && !res.isComplete) { throw Error('OpenSSL is not installed, and must be installed to proceed with TLS certificate creation. Please install OpenSSL and try again.'); } return true; }); exports.isOpenSslInstalled = isOpenSslInstalled; const createKeyPair = async () => { // note: space in file name is not supported let privateKey = _path.default.join(process.cwd(), 'private_key.pem'); let publicKey = _path.default.join(process.cwd(), 'public_key.pem'); const privKeyRes = await openssl(`genpkey -algorithm RSA -out ${maskSpaces(privateKey)} -pkeyopt rsa_keygen_bits:2048`); if (privKeyRes.code === 1) throw new Error('OpenSSL failed to create the private key'); const pubKeyRes = await openssl(`rsa -pubout -in ${maskSpaces(privateKey)} -out ${maskSpaces(publicKey)}`); if (pubKeyRes.code === 1) throw new Error('OpenSSL failed to create the public key'); return { publicKey, privateKey }; }; exports.createKeyPair = createKeyPair; const createTlsCert = async (secretName, domain) => { // note: space in file name is not supported const cert = _path.default.join(process.cwd(), `${secretName}.crt`); const privateKey = _path.default.join(process.cwd(), `${secretName}.key`); const output = await openssl(`req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj /C=US/ST=AZ/L=Phoenix/O=Axway/CN=${domain} -keyout ${maskSpaces(privateKey)} -out ${maskSpaces(cert)}`); if (output.code === 1) throw new Error('OpenSSL failed to create the certificate'); return { cert, privateKey }; }; exports.createTlsCert = createTlsCert; const editor = (editor, filePath) => { log(`editor ${filePath}`); return new Promise(resolve => { (0, _child_process.spawn)(editor, [filePath], { stdio: 'inherit' }).on('exit', code => { log(`editor exit code ${code}`); resolve(code); }); }); }; exports.editor = editor;