From 8440c6816f833e9848bbfd976902af46f49d6ff3 Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Fri, 20 Nov 2026 10:51:00 +0000 Subject: szecret list: a terminal view worth reading, and --json At a terminal, list shows each bucket this machine reads: its readers by name (hosts from recipients, admins from a "# " line above their key in SZECRET_ADMINS_COMMAND's output, "the admins" once per bucket), its secrets in columns fitted to the width, any problem the file names and headers show with the command that fixes it, and a summary. No key is shown without --keys or --json. --all groups the buckets not read here after the rest. It reads every bucket in one awk pass, never decrypting. Piped output and --names, which scripts read, are unchanged. --- secrets/test/list.sh | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) (limited to 'secrets/test/list.sh') diff --git a/secrets/test/list.sh b/secrets/test/list.sh index 08671c8..cf07dd0 100755 --- a/secrets/test/list.sh +++ b/secrets/test/list.sh @@ -46,10 +46,151 @@ test_list_sorted() { fi } +# --- Buckets, at a terminal and in JSON -------------------------------------- + +# buckets_setup: this machine ("me"), another host and two admins, one named +# by a "# " line in the admins command's output and one not; buckets +# mine (me, other), theirs (other) and admin-only, keyed, a secret in each. +buckets_setup() { + export SZECRET_BUCKETS="${TEST_DIR}/buckets" + export SZECRET_IDENTITY="${TEST_DIR}/me/identity" + mkdir -p "${SZECRET_BUCKETS}" + szecret enrol >/dev/null 2>&1 + me="$(cat "${SZECRET_IDENTITY}.pub")" + age-keygen -o "${TEST_DIR}/other" 2>/dev/null + other="$(age-keygen -y "${TEST_DIR}/other")" + age-keygen -o "${TEST_DIR}/admin" 2>/dev/null + admin="$(age-keygen -y "${TEST_DIR}/admin")" + age-keygen -o "${TEST_DIR}/admin2" 2>/dev/null + admin2="$(age-keygen -y "${TEST_DIR}/admin2")" + # admin2 has no "# " line directly above it: the blank line ends it. + printf '# The admins\n# alice phone\n%s\n\n%s\n' "${admin}" "${admin2}" > "${TEST_DIR}/admins" + export SZECRET_ADMINS_COMMAND="cat '${TEST_DIR}/admins'" + local b row + for b in "mine:me ${me}:other ${other}" "theirs:other ${other}" "admin-only"; do + mkdir -p "${SZECRET_BUCKETS}/${b%%:*}" + { + echo "# Written by szecret-buckets" + IFS=: read -r -a rows <<<"${b#*:}" + if [[ "${b}" == *:* ]]; then + for row in "${rows[@]}"; do + echo "# ${row%% *}" + echo "${row#* }" + done + fi + } > "${SZECRET_BUCKETS}/${b%%:*}/recipients" + szecret rekey --add-ok "${b%%:*}" >/dev/null + done + echo v | szecret put mine/mine-a + echo v | szecret put mine/mine-b + echo v | szecret put theirs/their-t + echo v | szecret put admin-only/admin-a +} + +# Plain list piped, and --names, are what scripts read: unchanged, byte for byte. +test_list_scripts_unchanged() { + assert_equals "$(printf '%s\n' apple mango mine-a mine-b secret-a secret-b secret-c zebra)" \ + "$(szecret list)" "piped list" + assert_equals "$(printf '%s\n' mine/mine-a mine/mine-b '(old store)/apple' '(old store)/mango' \ + '(old store)/secret-a' '(old store)/secret-b' '(old store)/secret-c' '(old store)/zebra')" \ + "$(szecret list --names)" "list --names" + assert_contains "$(szecret list --names --all)" "theirs/their-t" + assert_contains "$(szecret list --names --all)" "admin-only/admin-a" + pass "list: piped and --names output unchanged" +} + +test_list_terminal() { + local out + out="$(SZECRET_FORCE_TTY=80 szecret list)" + assert_contains "${out}" "mine 2 secrets" + assert_contains "${out}" " read by me, other, the admins" + assert_contains "${out}" " mine-a mine-b" + assert_contains "${out}" "(old store) 6 secrets" + assert_contains "${out}" "me reads 2 buckets, 8 secrets" + assert_contains "${out}" "The admins read every bucket: alice phone, unnamed admin" + assert_contains "${out}" "2 buckets not read here: szecret list --all" + [[ "${out}" != *theirs* ]] || fail "list shows a bucket not read here: ${out}" + [[ "${out}" != *age1* ]] || fail "list shows a key without --keys: ${out}" + [[ "${out}" != *$'\033'* ]] || fail "colour without a terminal: ${out}" + pass "list: at a terminal, buckets with readers by name, and a summary" +} + +test_list_terminal_all_and_keys() { + local out + out="$(SZECRET_FORCE_TTY=80 szecret list --all)" + assert_contains "${out}" "Not read here 2 buckets" + assert_contains "${out}" "theirs 1 secret" + assert_contains "${out}" " read by the admins only" + assert_contains "${out}" "me reads 2 of 4 buckets, 8 of 10 secrets" + out="$(SZECRET_FORCE_TTY=80 szecret list --keys)" + assert_contains "${out}" "me ${me}" + assert_contains "${out}" "unnamed admin ${admin2}" + pass "list: --all adds the buckets not read here, --keys the keys" +} + +test_list_narrow() { + local out long + out="$(SZECRET_FORCE_TTY=44 szecret list --all)" + long="$(awk 'length > 44' <<<"${out}")" + assert_equals "" "${long}" "lines over 44 columns" + assert_contains "${out}" "The admins read every bucket: alice phone," + assert_contains "${out}" " unnamed admin" + pass "list: fits a narrow terminal" +} + +test_list_json() { + local out + out="$(szecret list --json)" + assert_contains "${out}" '{"buckets":[{"name":"mine","old_store":false,"readable":true,"keyed":true,"readers":[' + assert_contains "${out}" "{\"name\":\"me\",\"key\":\"${me}\",\"admin\":false,\"this_machine\":true}" + assert_contains "${out}" "{\"name\":\"alice phone\",\"key\":\"${admin}\",\"admin\":true,\"this_machine\":false}" + assert_contains "${out}" '"secrets":["mine-a","mine-b"],"problems":[]}' + assert_contains "${out}" '"machine":"me","admins_known":true}' + [[ "${out}" != *theirs* ]] || fail "--json shows a bucket not read here" + assert_contains "$(szecret list --json --all)" '"name":"theirs","old_store":false,"readable":false' + if command -v python3 >/dev/null; then + python3 -c 'import json,sys; json.load(sys.stdin)' <<<"${out}" || fail "--json is not JSON: ${out}" + fi + assert_failure szecret list --json --names + pass "list: --json, keys included" +} + +test_list_problems() { + local out + age-keygen -o "${TEST_DIR}/newhost" 2>/dev/null + printf '# newhost\n%s\n' "$(age-keygen -y "${TEST_DIR}/newhost")" >> "${SZECRET_BUCKETS}/mine/recipients" + echo "not age" > "${SZECRET_BUCKETS}/mine/junk.age" + out="$(SZECRET_FORCE_TTY=80 szecret list)" + assert_contains "${out}" " ! readers changed since its last rekey: +newhost" + assert_contains "${out}" " fix: szecret rekey mine" + assert_contains "${out}" " ! junk: not an age file" + assert_contains "${out}" "! 2 problems in mine" + assert_contains "$(szecret list --json)" '{"problem":"readers changed since its last rekey: +newhost","fix":"szecret rekey mine"}' + out="$(SZECRET_FORCE_TTY=80 SZECRET_ADMINS_COMMAND=false szecret list)" + assert_contains "${out}" "! the admins are unknown" + pass "list: says what is wrong, and how to fix it" +} + +test_list_reads_nothing() { + local out + out="$(SZECRET_FORCE_TTY=80 SZECRET_STORE= SZECRET_IDENTITY="${TEST_DIR}/stranger/identity" szecret list)" + assert_contains "${out}" "This machine reads none of the 3 buckets here." + assert_contains "${out}" "szecret enrol" + pass "list: says what to do when this machine reads nothing" +} + run_tests() { test_list_empty_store test_list_shows_secrets test_list_sorted + buckets_setup + test_list_scripts_unchanged + test_list_terminal + test_list_terminal_all_and_keys + test_list_narrow + test_list_json + test_list_problems + test_list_reads_nothing } [[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests -- cgit v1.2.3