From 18492ac83c35e83fe3f319bc5ba468d619399324 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 19 Dec 2020 17:20:54 -0500 Subject: [PATCH] feat: add json format support to ssh-keys:list --- docs/deployment/user-management.md | 16 ++- plugins/ssh-keys/subcommands/list | 25 ++++- tests/unit/ssh_keys.bats | 162 +++++++++++++++++++++++++++++ tests/unit/test_helper.bash | 21 ++++ 4 files changed, 221 insertions(+), 3 deletions(-) diff --git a/docs/deployment/user-management.md b/docs/deployment/user-management.md index 767d5f08f..0b0f68109 100644 --- a/docs/deployment/user-management.md +++ b/docs/deployment/user-management.md @@ -4,7 +4,7 @@ ``` ssh-keys:add [/path/to/key] # Add a new public key by pipe or path -ssh-keys:list [] # List of all authorized Dokku public ssh keys +ssh-keys:list [--format text|json] [] # List of all authorized Dokku public ssh keys ssh-keys:remove [--fingerprint fingerprint|] # Remove SSH public key by name ``` @@ -42,6 +42,20 @@ The keys for a specific user may be listed by specifying a second argument to th dokku ssh-keys:list admin ``` +> 0.22.3 + +The output format may be specified via the `--format` flag. Supported values include `json` and `text`. + +```shell +dokku ssh-keys:list --format json +``` + +This can additionally be used to filter to keys for a particular user: + +```shell +dokku ssh-keys:list --format json admin +``` + ### Adding SSH keys You can add your public key to Dokku with the `ssh-keys:add` command. The output will be the fingerprint of the SSH key: diff --git a/plugins/ssh-keys/subcommands/list b/plugins/ssh-keys/subcommands/list index df875ee50..2a7ad3085 100755 --- a/plugins/ssh-keys/subcommands/list +++ b/plugins/ssh-keys/subcommands/list @@ -8,10 +8,31 @@ cmd-ssh-keys-list() { declare desc="List ssh key hashes" declare cmd="ssh-keys:list" [[ "$1" == "$cmd" ]] && shift 1 - declare SSH_NAME="$1" + declare NAME="$1" + local FORMAT=text + if [[ "$NAME" == "--format" ]]; then + FORMAT="$2" + NAME="$3" + fi + + if [[ "$FORMAT" != "text" ]] && [[ "$FORMAT" != "json" ]]; then + dokku_log_fail "Invalid output format specified, supported formats: json, text" + fi + + if [[ "$FORMAT" == "text" ]]; then + FORMAT="" + fi verify_ssh_key_file - sshcommand list dokku "$SSH_NAME" + if [[ "$FORMAT" == "json" ]]; then + if [[ -n "$NAME" ]]; then + sshcommand list dokku "" "$FORMAT" | jq -cM "[.[] | select(.name == \"$NAME\")]" + else + sshcommand list dokku "" "$FORMAT" + fi + else + sshcommand list dokku "$NAME" + fi } cmd-ssh-keys-list "$@" diff --git a/tests/unit/ssh_keys.bats b/tests/unit/ssh_keys.bats index 62b6a9ecd..37f079c1e 100644 --- a/tests/unit/ssh_keys.bats +++ b/tests/unit/ssh_keys.bats @@ -254,4 +254,166 @@ teardown() { echo "output: $output" echo "status: $status" assert_failure + + run /bin/bash -c 'echo "" > "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"' + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:add new-user /tmp/testkey.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" + + run /bin/bash -c "dokku ssh-keys:add another-user /tmp/testkey-no-newline.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "2" + + run /bin/bash -c "dokku ssh-keys:list new-user | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" +} + + +@test "(ssh-keys) ssh-keys:list --format invalid" { + run /bin/bash -c "dokku ssh-keys:list --format invalid" + echo "output: $output" + echo "status: $status" + assert_failure +} + +@test "(ssh-keys) ssh-keys:list --format text" { + run /bin/bash -c "dokku ssh-keys:list --format text" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c 'echo "" >> "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"' + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format text" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format text | grep name1" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c "dokku ssh-keys:list --format text name1" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c 'echo "" > "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"' + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:add new-user /tmp/testkey.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format text | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" + + run /bin/bash -c "dokku ssh-keys:add another-user /tmp/testkey-no-newline.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format text | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "2" + + run /bin/bash -c "dokku ssh-keys:list --format text new-user | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" +} + +@test "(ssh-keys) ssh-keys:list --format json" { + run /bin/bash -c "dokku ssh-keys:list --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c 'echo "" >> "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"' + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format json name1" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "[]" + + run /bin/bash -c 'echo "" > "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"' + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:add new-user /tmp/testkey.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format json | jq length" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" + + run /bin/bash -c "dokku ssh-keys:add another-user /tmp/testkey-no-newline.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ssh-keys:list --format json | jq length" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "2" + + run /bin/bash -c "dokku ssh-keys:list --format json new-user | jq length" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "1" } diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index e3fe5c21a..1cdf008ab 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -78,6 +78,15 @@ assert_equal() { fi } +assert_not_equal() { + if [[ "$1" == "$2" ]]; then + { + echo "unexpected: $1" + echo "actual: $2" + } | flunk + fi +} + # ShellCheck doesn't know about $output from Bats # shellcheck disable=SC2154 assert_output() { @@ -90,6 +99,18 @@ assert_output() { assert_equal "$expected" "$output" } +# ShellCheck doesn't know about $output from Bats +# shellcheck disable=SC2154 +assert_not_output() { + local expected + if [[ $# -eq 0 ]]; then + unexpected="$(cat -)" + else + unexpected="$1" + fi + assert_not_equal "$unexpected" "$output" +} + # ShellCheck doesn't know about $output from Bats # shellcheck disable=SC2154 assert_output_exists() {