diff --git a/sshcommand b/sshcommand index 29ad1a2..72e5653 100755 --- a/sshcommand +++ b/sshcommand @@ -214,7 +214,11 @@ sshcommand-list() { 's/^command="FINGERPRINT=(\S+) NAME=(\\"|)(.*)\2 `.*",(\S+).*/{ "fingerprint": "\1", "name": "\3", "SSHCOMMAND_ALLOWED_KEYS": "\4" }/p' \ "$userhome/.ssh/authorized_keys" | tr '\n' ',' | sed '$s/,$/\n/') - echo "[${data}]" + if [[ -n "$NAME" ]]; then + echo "[${data}]" | jq -cM --arg NAME "$NAME" 'map( select (.name == $NAME) )' + else + echo "[${data}]" + fi else OUTPUT="$(sed --silent --regexp-extended \ 's/^command="FINGERPRINT=(\S+) NAME=(\\"|)(.*)\2 `.*",(\S+).*/\1 NAME="\3" SSHCOMMAND_ALLOWED_KEYS="\4"/p' \ diff --git a/tests/unit/core.bats b/tests/unit/core.bats index eb68020..8c9cdb0 100644 --- a/tests/unit/core.bats +++ b/tests/unit/core.bats @@ -205,12 +205,12 @@ check_custom_allowed_keys() { assert_failure cp tests/unit/fixtures/authorized_keys/input_variants "/home/${TEST_USER}/.ssh/authorized_keys" - run bash -c "sshcommand list $TEST_USER md5" + run bash -c "sshcommand list $TEST_USER '' json" echo "output: $output" echo "status: $status" assert_equal \ "$(head -n1 tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output)" \ - "$(sshcommand list "$TEST_USER" md5 json)" + "$(sshcommand list "$TEST_USER" "" json)" } @test "(core) sshcommand list (authorized_keys format variants)" { @@ -226,13 +226,13 @@ check_custom_allowed_keys() { @test "(core) sshcommand list (json output)" { cp tests/unit/fixtures/authorized_keys/input_variants "/home/${TEST_USER}/.ssh/authorized_keys" - run bash -c "sshcommand list $TEST_USER json" + run bash -c "sshcommand list $TEST_USER md5 json" echo "output: $output" echo "status: $status" assert_equal \ - "$(cat tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output)" \ - "$(sshcommand list "$TEST_USER" json)" + "$(cat tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered)" \ + "$(sshcommand list "$TEST_USER" "md5" json)" rm "/home/${TEST_USER}/.ssh/authorized_keys" } diff --git a/tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered b/tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered new file mode 100644 index 0000000..7052be5 --- /dev/null +++ b/tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered @@ -0,0 +1 @@ +[{"fingerprint":"2a:f7:39:1c:63:80:c4:9e:a8:92:ec:e6:94:91:fa:c0","name":"md5","SSHCOMMAND_ALLOWED_KEYS":"no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding"}]