From b2ad30c0148a5c2fcfaa810c1d07869a1d8f66e3 Mon Sep 17 00:00:00 2001 From: Carl Hultay <144816337+crhultay@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:11:07 -0500 Subject: [PATCH 1/5] Create oputil_ssh_key.js --- core/oputil/oputil_ssh_key.js | 158 ++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 core/oputil/oputil_ssh_key.js diff --git a/core/oputil/oputil_ssh_key.js b/core/oputil/oputil_ssh_key.js new file mode 100644 index 000000000..4915939fb --- /dev/null +++ b/core/oputil/oputil_ssh_key.js @@ -0,0 +1,158 @@ +/* jslint node: true */ +/* eslint-disable no-console */ +'use strict'; + +// ENiGMA½ +const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases; + +const { + printUsageAndSetExitCode, + argv, + ExitCodes, + getAnswers, +} = require('./oputil_common.js'); +const getHelpFor = require('./oputil_help.js').getHelpFor; + +// deps +const async = require('async'); +const fs = require('graceful-fs'); +const exec = require('child_process').exec; +const inq = require('inquirer'); +const _ = require('lodash'); + + +exports.handleSSHKeyCommand = handleSSHKeyCommand; + +const ConfigIncludeKeys = [ + 'loginServers.ssh', + 'loginServers.ssh.privateKeyPem', +]; + +const MINIMUM_PASSWORD_LENGTH = 8; +const QUESTIONS = { + Create: [ + { + name: 'createNew', + message: 'Generate New SSH Keys?', + type: 'confirm', + default: false, + }, + { + name: 'password', + message: 'SSL Password:', + default: "", + when: answers => answers.createNew, + }, + ], +}; + +function execute(ui, command) { + ui.log.write("Ping!"); + ui.log.write(command); + exec( + command, + function (error, stdout, stderr) { + ui.log.write(error); + + if (error) { + const reason = error ? error.message : 'OpenSSL Error'; + logDebug( + { + reason: reason, + cmd: util.cmd, + args: args + }, + `openssl command failed` + ); + } + else { + ui.log.write("SSH Keys Generated") + } + } + ); +} + +function createNew(cb) { + const ui = new inq.ui.BottomBar(); + + let sslPassword; + + async.waterfall( + [ + function init(callback) { + return initConfigAndDatabases(callback); + }, + function create(configuration, callback) { + getAnswers(QUESTIONS.Create, answers => { + if (!answers.createNew) { + return callback('exit'); + } + + // Get Answer Value + sslPassword = answers.password; + if (!sslPassword || sslPassword.replaceAll(" ", "") == "") { + ui.log.write('Password must be set.'); + + return callback('exit'); + } + if (sslPassword.length < MINIMUM_PASSWORD_LENGTH) { + ui.log.write(`Password must be at least ${MINIMUM_PASSWORD_LENGTH} characters.`); + + return callback('exit'); + } + + // Check if Keyfiles Exist + const sshKeyPath = "config/security/"; + const sshKeyFilename = "ssh_private_key.pem"; + const targetKeyFile = sshKeyPath + sshKeyFilename; + + // Check if Keyfile Exists + if (fs.existsSync(targetKeyFile)) { + ui.log.write(`${targetKeyFile} already exists.`) + + return callback('exit'); + } + + ui.log.write(`Creating SSH Key: ${targetKeyFile}`); + + // Create Dir + if (!fs.existsSync(sshKeyPath)) { + ui.log.write(`Creating Directory: ${sshKeyPath}`); + exec(`mkdir -p ${sshKeyPath}`); + } + + // Check if OpenSSL binary is installed + const binaryPath = "/usr/bin/openssl"; + if (!fs.existsSync(binaryPath)) { + ui.log.write(`${binaryPath} was not found in your path`); + + return callback('exit'); + } + + // Create SSH Keys + const command = `${binaryPath} genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537 | openssl rsa -out ./${targetKeyFile} -aes128 -traditional -passout pass:`; + execute(ui, `${command}${sslPassword}`); + }); + }, + ], + err => { + return cb(err, configPath, config); + } + ); +} + +function handleSSHKeyCommand() { + if (true === argv.help) { + return printUsageAndSetExitCode(getHelpFor('SSH'), ExitCodes.ERROR); + } + + const action = argv._[1]; + + switch (action) { + case 'create': + return createNew(); + + default: + return printUsageAndSetExitCode(getHelpFor('SSH'), ExitCodes.ERROR); + } +} From 0f7330a377d8108a0ece0c9069c0c2ba087dbb18 Mon Sep 17 00:00:00 2001 From: Carl Hultay <144816337+crhultay@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:13:09 -0500 Subject: [PATCH 2/5] Update oputil_main.js --- core/oputil/oputil_main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/oputil/oputil_main.js b/core/oputil/oputil_main.js index 9dcbc5104..9b79ad3dc 100644 --- a/core/oputil/oputil_main.js +++ b/core/oputil/oputil_main.js @@ -10,6 +10,7 @@ const handleFileBaseCommand = require('./oputil_file_base.js').handleFileBaseCom const handleMessageBaseCommand = require('./oputil_message_base.js').handleMessageBaseCommand; const handleConfigCommand = require('./oputil_config.js').handleConfigCommand; +const handleSSHKeyCommand = require('./oputil_ssh_key.js').handleSSHKeyCommand; const getHelpFor = require('./oputil_help.js').getHelpFor; module.exports = function () { @@ -32,6 +33,8 @@ module.exports = function () { return handleFileBaseCommand(); case 'mb': return handleMessageBaseCommand(); + case 'ssh': + return handleSSHKeyCommand(); default: return printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.BAD_COMMAND); } From c0044dcb04ada4afd861111f15d1975bdcdc5547 Mon Sep 17 00:00:00 2001 From: Carl Hultay <144816337+crhultay@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:13:25 -0500 Subject: [PATCH 3/5] Update oputil_help.js --- core/oputil/oputil_help.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/oputil/oputil_help.js b/core/oputil/oputil_help.js index 7104d827e..14bf9de12 100644 --- a/core/oputil/oputil_help.js +++ b/core/oputil/oputil_help.js @@ -20,6 +20,7 @@ Commands: config Configuration management fb File base management mb Message base management + ssh SSH key management `, User: `usage: oputil.js user [] @@ -219,6 +220,11 @@ qwk-export arguments: TIMESTAMP. --no-qwke Disable QWKE extensions. --no-synchronet Disable Synchronet style extensions. +`, + SSH: `usage: oputil.js ssh + +Actions: + create Create new SSH Keys `, }); From 2c85a9bb51bcd4e457f370c43c4c0ead440282b3 Mon Sep 17 00:00:00 2001 From: Carl Hultay <144816337+crhultay@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:57:32 -0500 Subject: [PATCH 4/5] Update oputil_ssh_key.js per feedback --- core/oputil/oputil_ssh_key.js | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/core/oputil/oputil_ssh_key.js b/core/oputil/oputil_ssh_key.js index 4915939fb..a227ef279 100644 --- a/core/oputil/oputil_ssh_key.js +++ b/core/oputil/oputil_ssh_key.js @@ -15,7 +15,7 @@ const getHelpFor = require('./oputil_help.js').getHelpFor; // deps const async = require('async'); -const fs = require('graceful-fs'); +const fs = require('fs-extra'); const exec = require('child_process').exec; const inq = require('inquirer'); const _ = require('lodash'); @@ -47,8 +47,6 @@ const QUESTIONS = { }; function execute(ui, command) { - ui.log.write("Ping!"); - ui.log.write(command); exec( command, function (error, stdout, stderr) { @@ -75,8 +73,6 @@ function execute(ui, command) { function createNew(cb) { const ui = new inq.ui.BottomBar(); - let sslPassword; - async.waterfall( [ function init(callback) { @@ -89,8 +85,8 @@ function createNew(cb) { } // Get Answer Value - sslPassword = answers.password; - if (!sslPassword || sslPassword.replaceAll(" ", "") == "") { + const sslPassword = answers.password.trim(); + if (!sslPassword || sslPassword == "") { ui.log.write('Password must be set.'); return callback('exit'); @@ -106,31 +102,16 @@ function createNew(cb) { const sshKeyFilename = "ssh_private_key.pem"; const targetKeyFile = sshKeyPath + sshKeyFilename; - // Check if Keyfile Exists - if (fs.existsSync(targetKeyFile)) { - ui.log.write(`${targetKeyFile} already exists.`) - - return callback('exit'); - } - ui.log.write(`Creating SSH Key: ${targetKeyFile}`); // Create Dir - if (!fs.existsSync(sshKeyPath)) { + if (!fs.pathExists(sshKeyPath)) { ui.log.write(`Creating Directory: ${sshKeyPath}`); exec(`mkdir -p ${sshKeyPath}`); } - // Check if OpenSSL binary is installed - const binaryPath = "/usr/bin/openssl"; - if (!fs.existsSync(binaryPath)) { - ui.log.write(`${binaryPath} was not found in your path`); - - return callback('exit'); - } - // Create SSH Keys - const command = `${binaryPath} genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537 | openssl rsa -out ./${targetKeyFile} -aes128 -traditional -passout pass:`; + const command = `openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537 | openssl rsa -out ./${targetKeyFile} -aes128 -traditional -passout pass:`; execute(ui, `${command}${sslPassword}`); }); }, From bc418ff422d62b49196aaf7a26c5df563c688f75 Mon Sep 17 00:00:00 2001 From: Carl Hultay <144816337+crhultay@users.noreply.github.com> Date: Sat, 23 Nov 2024 19:08:05 -0500 Subject: [PATCH 5/5] Update oputil_ssh_key.js per feedback --- core/oputil/oputil_ssh_key.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/oputil/oputil_ssh_key.js b/core/oputil/oputil_ssh_key.js index a227ef279..00ba83677 100644 --- a/core/oputil/oputil_ssh_key.js +++ b/core/oputil/oputil_ssh_key.js @@ -105,10 +105,8 @@ function createNew(cb) { ui.log.write(`Creating SSH Key: ${targetKeyFile}`); // Create Dir - if (!fs.pathExists(sshKeyPath)) { - ui.log.write(`Creating Directory: ${sshKeyPath}`); - exec(`mkdir -p ${sshKeyPath}`); - } + ui.log.write(`Creating Directory: ${sshKeyPath}`); + fs.ensureDirSync(sshKeyPath); // Create SSH Keys const command = `openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537 | openssl rsa -out ./${targetKeyFile} -aes128 -traditional -passout pass:`;