Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing ability to filter ssh keys when listing in json format #130

Merged
merged 5 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sshcommand
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/core.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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)" {
Expand All @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"}]