-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from crhultay/patch-1
Issue #519 - Implement SSH Key Generation via oputil
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* 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('fs-extra'); | ||
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) { | ||
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(); | ||
|
||
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 | ||
const sslPassword = answers.password.trim(); | ||
if (!sslPassword || sslPassword == "") { | ||
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; | ||
|
||
ui.log.write(`Creating SSH Key: ${targetKeyFile}`); | ||
|
||
// Create Dir | ||
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:`; | ||
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); | ||
} | ||
} |