Files
dokku/plugins/ssh-keys/subcommands/add
Flavio Poletti a2bacd8096 Patch GH #2446 "Adding a file with two ssh-key..."
This is a proposed change for addressing GH #2446 "Adding a file with
two ssh-keys can result in an error" (see
https://github.com/dokku/dokku/issues/2446).

The following checks are performed:

- validity of key contents when read from a file
- key contents only contains one single line/key
- authorized_keys file is ultimately valid

The last test is an extension over the previous implementation in that
all lines in the file are checked.

The commit also includes extensions to the relevant test file to
excercise the new checks. It also takes care to save and then restore
the authorized_keys file.
2016-10-16 16:32:16 +02:00

28 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/ssh-keys/functions"
add_keys() {
declare desc="add a new key via sshcommand"
local cmd="ssh-keys:add"
shift
local name="$1" key_file="$2" key_contents key_from_pipe
[[ -p /dev/stdin ]] && read -r key_from_pipe
if [[ -n "$key_from_pipe" ]]; then
ssh-keygen -lf /dev/stdin <<< "$key_from_pipe" &> /dev/null || dokku_log_fail "Key piped in is not a valid ssh public key"
key_contents="$key_from_pipe"
elif [[ -n "$key_file" ]]; then
key_contents="$(cat "$key_file")"
ssh-keygen -lf /dev/stdin <<< "$key_contents" &> /dev/null || dokku_log_fail "Key from file is not a valid ssh public key"
fi
[[ -n "$name" && -n "$key_contents" ]] || dokku_log_fail "Two arguments are required if not piping, ie: dokku ssh-keys:add <NAME> <KEY_FILE>"
local count="$(wc -l <<< "$key_contents")"
[ "$count" -eq 1 ] || dokku_log_fail 'Too many keys provided, set one per invocation of dokku ssh-keys:add <NAME> <KEY_FILE>'
verify_ssh_key_exists
echo "$key_contents" | sshcommand acl-add dokku "$name" || dokku_log_fail "sshcommand returned an error: $?"
verify_ssh_key_file
}
add_keys "$@"