From 1c602b5ddcda5328bb66980a8d44c674033a1fe4 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 28 Apr 2024 06:59:23 -0400 Subject: [PATCH 1/5] fix: add missing ability to filter ssh keys when listing in json format --- sshcommand | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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' \ From 1f745972bf392183b37cba6725a6b2c28f366c0a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 28 Apr 2024 07:11:57 -0400 Subject: [PATCH 2/5] Create sshcommand_list_expected_json_output_md5_filtered --- .../sshcommand_list_expected_json_output_md5_filtered | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered 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..372b88a --- /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" }] From fe07ab4f361877411266883bf21effa6331dc81f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 28 Apr 2024 07:13:43 -0400 Subject: [PATCH 3/5] fix: update test cases --- tests/unit/core.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/core.bats b/tests/unit/core.bats index eb68020..1072e6d 100644 --- a/tests/unit/core.bats +++ b/tests/unit/core.bats @@ -210,7 +210,7 @@ check_custom_allowed_keys() { 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)" { @@ -231,8 +231,8 @@ check_custom_allowed_keys() { 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" } From b1bdf542e32970fb8259653e7abd995e8603303d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 18 May 2024 03:38:22 -0400 Subject: [PATCH 4/5] tests: correct input commands --- tests/unit/core.bats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/core.bats b/tests/unit/core.bats index 1072e6d..8c9cdb0 100644 --- a/tests/unit/core.bats +++ b/tests/unit/core.bats @@ -205,7 +205,7 @@ 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 \ @@ -226,7 +226,7 @@ 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" From 3212f7241df7e29072d9dba279f1b717412faab7 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 18 May 2024 04:57:11 -0400 Subject: [PATCH 5/5] tests: correct fixture --- .../sshcommand_list_expected_json_output_md5_filtered | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 372b88a..7052be5 100644 --- 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 @@ -1 +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" }] +[{"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"}]