diff --git a/plugins/ssh-keys/functions b/plugins/ssh-keys/functions index f18200830..8e4560fa7 100755 --- a/plugins/ssh-keys/functions +++ b/plugins/ssh-keys/functions @@ -14,9 +14,14 @@ verify_ssh_key_file() { declare desc="Test that public key is valid" [[ -s ${DOKKU_ROOT}/.ssh/authorized_keys ]] || dokku_log_fail "No public keys found." local key line=0 + local TMP_KEY_FILE + TMP_KEY_FILE=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX") + trap 'rm -rf "$TMP_KEY_FILE" >/dev/null' RETURN INT TERM EXIT + while read -r key; do line=$((line + 1)) - ssh-keygen -l -f /dev/stdin <<<"$key" &>/dev/null || dokku_log_fail "${DOKKU_ROOT}/.ssh/authorized_keys line $line failed ssh-keygen check." + echo "$key" >"$TMP_KEY_FILE" + ssh-keygen -lf "$TMP_KEY_FILE" &>/dev/null || dokku_log_fail "${DOKKU_ROOT}/.ssh/authorized_keys line $line failed ssh-keygen check." done <"${DOKKU_ROOT}/.ssh/authorized_keys" } diff --git a/plugins/ssh-keys/subcommands/add b/plugins/ssh-keys/subcommands/add index ecd6c9746..89242b8c3 100755 --- a/plugins/ssh-keys/subcommands/add +++ b/plugins/ssh-keys/subcommands/add @@ -7,22 +7,33 @@ 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" + [[ "$1" == "$cmd" ]] && shift 1 + declare NAME="$1" KEY_FILE="$2" + local TMP_KEY_FILE + TMP_KEY_FILE=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX") + trap 'rm -rf "$TMP_KEY_FILE" >/dev/null' RETURN INT TERM EXIT + + if [[ -n "$KEY_FILE" ]]; then + true + elif [[ -p /dev/stdin ]]; then + local key_contents + read -r key_contents + echo "$key_contents" >"$TMP_KEY_FILE" + KEY_FILE="$TMP_KEY_FILE" + else + dokku_log_fail "No key specified via file or pipe" fi - [[ -n "$name" && -n "$key_contents" ]] || dokku_log_fail "Two arguments are required if not piping, ie: dokku ssh-keys:add " - 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 ' + + if [[ ! -s "$KEY_FILE" ]]; then + dokku_log_fail "Empty key was specified" + fi + + local count="$(wc -l "$KEY_FILE" | awk '{ print $1; }')" + [[ "$count" -eq 1 ]] || dokku_log_fail "Too many keys provided, set one per invocation of dokku ssh-keys:add " + ssh-keygen -lf "$KEY_FILE" &>/dev/null || dokku_log_fail "Key specified in is not a valid ssh public key" create_ssh_key_file verify_ssh_key_exists - echo "$key_contents" | sshcommand acl-add dokku "$name" || dokku_log_fail "sshcommand returned an error: $?" + sshcommand acl-add dokku "$NAME" "$KEY_FILE" || dokku_log_fail "sshcommand returned an error: $?" verify_ssh_key_file }