Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for password-based SSH authentication using sshpass. #31

Merged
merged 5 commits into from
Jan 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Options: Defaults:
--size SIZE t1.micro
--ssh-key PATH overcast.key
--ssh-pub-key PATH overcast.key.pub
--user NAME root
--user USERNAME root

Examples:
# Specified size:
Expand Down Expand Up @@ -559,7 +559,8 @@ Description:
Expects an Ubuntu server, untested on other distributions.

Options:
--user NAME
--user USERNAME
--password PASSWORD
--whitelist "IP|RANGE"
--whitelist-PORT "IP|RANGE"

Expand All @@ -585,7 +586,8 @@ Description:
Expects an Ubuntu server, untested on other distributions.

Options:
--user NAME
--user USERNAME
--password PASSWORD
--machine-readable, --mr
```

Expand Down Expand Up @@ -691,6 +693,7 @@ Options: Defaults:
--ssh-port PORT 22
--ssh-key PATH overcast.key
--user USERNAME root
--password PASSWORD
```

### overcast info
Expand Down Expand Up @@ -755,6 +758,7 @@ Options: Defaults:
--ssh-port PORT 22
--ssh-key PATH overcast.key
--user USERNAME root
--password PASSWORD

Examples:
$ overcast instance import app.01 127.0.0.1 --cluster app \
Expand Down Expand Up @@ -807,6 +811,7 @@ Options:
--ssh-port PORT
--ssh-key PATH
--user USERNAME
--password PASSWORD

Examples:
$ overcast instance update app.01 --user myuser --ssh-key /path/to/key
Expand Down Expand Up @@ -1092,7 +1097,8 @@ Description:

Options: Defaults:
--rsync false
--user NAME
--user USERNAME
--password PASSWORD

Examples:
Assuming instances "app.01" and "app.02", this will expand to:
Expand All @@ -1116,7 +1122,8 @@ Description:

Options: Defaults:
--rsync false
--user NAME
--user USERNAME
--password PASSWORD

Examples:
Assuming instances "app.01" and "app.02", this will expand to:
Expand Down Expand Up @@ -1164,7 +1171,8 @@ Description:

Options: Defaults:
--env "KEY=VAL KEY='1 2 3'"
--user NAME
--user USERNAME
--password PASSWORD
--ssh-key PATH
--ssh-args ARGS
--continueOnError false
Expand Down Expand Up @@ -1200,7 +1208,8 @@ Description:
Expects a shell variable format, for example MY_VAR_NAME="my_value"

Options: Defaults:
--user NAME
--user USERNAME
--password PASSWORD
--continueOnError false
--machine-readable, --mr false
--parallel, -p false
Expand Down Expand Up @@ -1243,7 +1252,8 @@ Description:
Opens an interactive SSH connection to an instance.

Options:
--user NAME
--user USERNAME
--password PASSWORD
--ssh-key PATH
```

Expand All @@ -1260,7 +1270,8 @@ Description:
Multiple tunnels can be opened over a single connection.

Options:
--user NAME
--user USERNAME
--password PASSWORD
--ssh-key PATH

Examples:
Expand Down
20 changes: 16 additions & 4 deletions bin/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ if [ -z "$OVERCAST_SCRIPT_FILE" ]; then
echo ""
echo "\$ $OVERCAST_ENV$OVERCAST_COMMAND"
fi
ssh -i $OVERCAST_KEY -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $OVERCAST_COMMAND"
if [ -z "$OVERCAST_PASSWORD" ]; then
ssh -i $OVERCAST_KEY -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $OVERCAST_COMMAND"
else
sshpass "-p$OVERCAST_PASSWORD" \
ssh -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no -o PubkeyAuthentication=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $OVERCAST_COMMAND"
fi
else
if [ -z "$SHELL_COMMAND" ]; then
SHELL_COMMAND="/bin/bash -s"
Expand All @@ -15,6 +21,12 @@ else
echo ""
echo "\$ $OVERCAST_ENV$SHELL_COMMAND $OVERCAST_SCRIPT_FILE"
fi
ssh -i $OVERCAST_KEY -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $SHELL_COMMAND" < "$OVERCAST_SCRIPT_FILE"
if [ -z "$OVERCAST_PASSWORD" ]; then
ssh -i $OVERCAST_KEY -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $SHELL_COMMAND" < "$OVERCAST_SCRIPT_FILE"
else
sshpass "-p$OVERCAST_PASSWORD" \
ssh -p $OVERCAST_PORT $OVERCAST_USER@$OVERCAST_IP \
-o StrictHostKeyChecking=no -o PubkeyAuthentication=no $OVERCAST_SSH_ARGS "$OVERCAST_ENV $SHELL_COMMAND" < "$OVERCAST_SCRIPT_FILE"
fi
fi
2 changes: 1 addition & 1 deletion modules/commands/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ commands.create = {
{ usage: '--size SIZE', default: 't1.micro' },
{ usage: '--ssh-key PATH', default: 'overcast.key' },
{ usage: '--ssh-pub-key PATH', default: 'overcast.key.pub' },
{ usage: '--user NAME', default: 'root' }
{ usage: '--user USERNAME', default: 'root' }
],
async: true,
run: function (args, next) {
Expand Down
3 changes: 2 additions & 1 deletion modules/commands/expose.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ commands.expose = {
{ name: 'port...', varName: 'ports', greedy: true }
],
options: [
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--whitelist "IP|RANGE"' },
{ usage: '--whitelist-PORT "IP|RANGE"' }
],
Expand Down
3 changes: 2 additions & 1 deletion modules/commands/exposed.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ commands.exposed = {
'Expects an Ubuntu server, untested on other distributions.'
],
options: [
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--machine-readable, --mr' }
],
required: [{ name: 'instance|cluster|all', varName: 'name' }],
Expand Down
20 changes: 14 additions & 6 deletions modules/commands/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ commands.import = {
{ usage: '--ssh-port PORT', default: '22' },
{ usage: '--ssh-key PATH', default: 'overcast.key' },
{ usage: '--user USERNAME', default: 'root' },
{ usage: '--password PASSWORD' },
],
run: function (args) {
var instance = {
ip: args.ip,
name: args.name,
ssh_port: args['ssh-port'] || '22',
ssh_key: args['ssh-key'] || 'overcast.key',
user: args.user || 'root'
user: args.user || 'root',
password: args.password || ''
};

utils.saveInstanceToCluster(args.cluster, instance, function () {
Expand Down Expand Up @@ -147,7 +149,8 @@ commands.update = {
{ usage: '--ip IP' },
{ usage: '--ssh-port PORT' },
{ usage: '--ssh-key PATH' },
{ usage: '--user USERNAME' }
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' }
],
run: function (args) {
var clusters = utils.getClusters();
Expand Down Expand Up @@ -187,10 +190,15 @@ commands.update = {
messages.push('Instance "' + args.oldName + '" has been renamed to "' + args.name + '".');
}

_.each(['ip', 'ssh-key', 'ssh-port', 'user'], function (prop) {
if (args[prop]) {
clusters[parentClusterName].instances[instance.name][prop.replace('-', '_')] = args[prop];
messages.push('Instance "' + prop + '" has been updated to "' + args[prop] + '".');
_.each(['ip', 'ssh-key', 'ssh-port', 'user', 'password'], function (prop) {
if (prop in args) {
if (args[prop]) {
clusters[parentClusterName].instances[instance.name][prop.replace('-', '_')] = args[prop];
messages.push('Instance property "' + prop + '" has been updated to "' + args[prop] + '".');
} else {
delete clusters[parentClusterName].instances[instance.name][prop.replace('-', '_')];
messages.push('Instance property "' + prop + '" has been unset.');
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion modules/commands/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ commands.pull = {
],
options: [
{ usage: '--rsync', default: 'false' },
{ usage: '--user NAME' }
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' }
],
run: function (args) {
args.direction = 'pull';
Expand Down
3 changes: 2 additions & 1 deletion modules/commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ commands.push = {
],
options: [
{ usage: '--rsync', default: 'false' },
{ usage: '--user NAME' }
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' }
],
run: function (args) {
args.direction = 'push';
Expand Down
3 changes: 2 additions & 1 deletion modules/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ commands.run = {
],
options: [
{ usage: '--env "KEY=VAL KEY=\'1 2 3\'"' },
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--ssh-key PATH' },
{ usage: '--ssh-args ARGS' },
{ usage: '--continueOnError', default: 'false' },
Expand Down
3 changes: 2 additions & 1 deletion modules/commands/scriptvar.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ commands.scriptvar = {
{ name: 'value', varName: 'var_value', raw: true }
],
options: [
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--continueOnError', default: 'false' },
{ usage: '--machine-readable, --mr', default: 'false' },
{ usage: '--parallel, -p', default: 'false' }
Expand Down
40 changes: 27 additions & 13 deletions modules/commands/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ commands.ssh = {
{ name: 'instance', varName: 'name', filters: filters.findFirstMatchingInstance }
],
options: [
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--ssh-key PATH' }
],
run: function (args) {
Expand All @@ -32,19 +33,32 @@ function connect(instance, args) {
var privateKeyFile = utils.normalizeKeyPath(args['ssh-key'] || instance.ssh_key || utils.CONFIG_DIR + '/keys/overcast.key');
var sshPort = instance.ssh_port || '22';
var host = (args.user || instance.user || 'root') + '@' + instance.ip;
var password = (args.password || instance.password || '');

var command = [];
if (password) {
command.push('sshpass');
command.push('-p' + password);
}
command.push('ssh');
command.push('-tt');
if (!password) {
command.push('-i');
command.push(privateKeyFile);
}
command.push('-p');
command.push(sshPort);
command.push('-o');
command.push('StrictHostKeyChecking=no');
if (password) {
command.push('-o');
command.push('PubkeyAuthentication=no');
}
command.push(host);

console.log(command.join(' '));

console.log('ssh -tt -i ' + privateKeyFile + ' -p ' + sshPort + ' ' + host);

var ssh = cp.spawn('ssh', [
'-tt',
'-i',
privateKeyFile,
'-p',
sshPort,
'-o',
'StrictHostKeyChecking=no',
host
], {
var ssh = cp.spawn(command.shift(), command, {
stdio: 'inherit'
});

Expand Down
31 changes: 22 additions & 9 deletions modules/commands/tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ commands.tunnel = {
{ name: 'local-port((:hostname):remote-port)...', varName: 'firstPort', raw: true }
],
options: [
{ usage: '--user NAME' },
{ usage: '--user USERNAME' },
{ usage: '--password PASSWORD' },
{ usage: '--ssh-key PATH' }
],
run: function (args) {
Expand All @@ -41,14 +42,26 @@ commands.tunnel = {
};

function connect(instance, args) {
var sshArgs = [
'ssh',
'-i',
utils.normalizeKeyPath(args['ssh-key'] || instance.ssh_key || 'overcast.key'),
'-p',
(instance.ssh_port || '22'),
'-o StrictHostKeyChecking=no'
];
var password = (args.password || instance.password || '');

var sshArgs = [];
if (password) {
sshArgs.push('sshpass');
sshArgs.push('-p' + password);
}
sshArgs.push('ssh');
if (!password) {
sshArgs.push('-i');
sshArgs.push(utils.normalizeKeyPath(args['ssh-key'] || instance.ssh_key || 'overcast.key'));
}
sshArgs.push('-p');
sshArgs.push((instance.ssh_port || '22'));
sshArgs.push('-o');
sshArgs.push('StrictHostKeyChecking=no');
if (password) {
sshArgs.push('-o');
sshArgs.push('PubkeyAuthentication=no');
}

var ports = exports.normalizePorts(args._);
_.each(ports, function (port) {
Expand Down
19 changes: 18 additions & 1 deletion modules/rsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function runOnInstance(instance, args, next) {
rsync({
ip: instance.ip,
user: args.user || instance.user,
password: args.password || instance.password,
name: instance.name,
ssh_key: args['ssh-key'] || instance.ssh_key,
ssh_port: instance.ssh_port,
Expand All @@ -55,9 +56,25 @@ function rsync(options, next) {
options.user = options.user || 'root';
options.name = options.name || 'Unknown';

var ssh = [];
if (options.password) {
ssh.push('sshpass');
ssh.push('-p' + options.password);
}
ssh.push('ssh');
ssh.push('-p');
ssh.push(options.ssh_port);
if (options.password) {
ssh.push('-o');
ssh.push('PubkeyAuthentication=no');
} else {
ssh.push('-i');
ssh.push(options.ssh_key);
}

var args = [
'rsync',
'-e "ssh -p ' + options.ssh_port + ' -i ' + options.ssh_key + '"',
'-e "' + ssh.join(' ') + '"',
'-varuzP',
'--delete',
'--ignore-errors'
Expand Down
Loading