Merge pull request #3517 from dokku/ssh-key-updates

SSH key updates
This commit is contained in:
Jose Diaz-Gonzalez
2019-05-13 00:56:36 -04:00
committed by GitHub
2 changed files with 30 additions and 14 deletions

View File

@@ -14,9 +14,14 @@ verify_ssh_key_file() {
declare desc="Test that public key is valid" declare desc="Test that public key is valid"
[[ -s ${DOKKU_ROOT}/.ssh/authorized_keys ]] || dokku_log_fail "No public keys found." [[ -s ${DOKKU_ROOT}/.ssh/authorized_keys ]] || dokku_log_fail "No public keys found."
local key line=0 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 while read -r key; do
line=$((line + 1)) 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" done <"${DOKKU_ROOT}/.ssh/authorized_keys"
} }

View File

@@ -7,22 +7,33 @@ source "$PLUGIN_AVAILABLE_PATH/ssh-keys/functions"
add_keys() { add_keys() {
declare desc="add a new key via sshcommand" declare desc="add a new key via sshcommand"
local cmd="ssh-keys:add" local cmd="ssh-keys:add"
shift [[ "$1" == "$cmd" ]] && shift 1
local name="$1" key_file="$2" key_contents key_from_pipe declare NAME="$1" KEY_FILE="$2"
[[ -p /dev/stdin ]] && read -r key_from_pipe local TMP_KEY_FILE
if [[ -n "$key_from_pipe" ]]; then TMP_KEY_FILE=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX")
ssh-keygen -lf /dev/stdin <<<"$key_from_pipe" &>/dev/null || dokku_log_fail "Key piped in is not a valid ssh public key" trap 'rm -rf "$TMP_KEY_FILE" >/dev/null' RETURN INT TERM EXIT
key_contents="$key_from_pipe"
elif [[ -n "$key_file" ]]; then if [[ -n "$KEY_FILE" ]]; then
key_contents="$(cat "$key_file")" true
ssh-keygen -lf /dev/stdin <<<"$key_contents" &>/dev/null || dokku_log_fail "Key from file is not a valid ssh public key" 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 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")" if [[ ! -s "$KEY_FILE" ]]; then
[ "$count" -eq 1 ] || dokku_log_fail 'Too many keys provided, set one per invocation of dokku ssh-keys:add <NAME> <KEY_FILE>' 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 <NAME> <KEY_FILE>"
ssh-keygen -lf "$KEY_FILE" &>/dev/null || dokku_log_fail "Key specified in is not a valid ssh public key"
create_ssh_key_file create_ssh_key_file
verify_ssh_key_exists 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 verify_ssh_key_file
} }