feat: add json format support to ssh-keys:list

This commit is contained in:
Jose Diaz-Gonzalez
2020-12-19 17:20:54 -05:00
parent 13dd8aa224
commit 18492ac83c
4 changed files with 221 additions and 3 deletions

View File

@@ -4,7 +4,7 @@
```
ssh-keys:add <name> [/path/to/key] # Add a new public key by pipe or path
ssh-keys:list [<name>] # List of all authorized Dokku public ssh keys
ssh-keys:list [--format text|json] [<name>] # List of all authorized Dokku public ssh keys
ssh-keys:remove [--fingerprint fingerprint|<name>] # 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:

View File

@@ -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 "$@"

View File

@@ -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"
}

View File

@@ -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() {