summaryrefslogtreecommitdiff
path: root/secrets/test
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 14:02:09 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 14:02:09 +0000
commit8f818a64f2f0f3a833fc842d52a675358ec64c9c (patch)
treeeac13a48cada4595c9073d62060c1145189c6344 /secrets/test
parent6444d6a508cdfdcf52cc6c83bab1e29a7e2a2e10 (diff)
szecret: buckets beside the old store -- get/has/put/mv/rekey/list/check/enrol, admins from SZECRET_ADMINS_COMMAND; rotate retired (fleet hardening 12)
A bucket is a directory of secrets with one set of readers: its recipients file plus the admins, the full set recorded in .szecret by rekey. put refuses when the readers or admins drift from that set, or with no admins; a plain put of a new name goes to admin-only, one still in the old store is rewritten there. The old store keeps working throughout. link/claude.sh leaves out an MCP server whose secret is in a bucket this host doesn't read. Design: cloud-setup docs/proposal-secret-roles.md.
Diffstat (limited to 'secrets/test')
-rw-r--r--secrets/test/README.md1
-rwxr-xr-xsecrets/test/buckets.sh274
-rwxr-xr-xsecrets/test/common.sh2
-rwxr-xr-xsecrets/test/rotate.sh69
4 files changed, 277 insertions, 69 deletions
diff --git a/secrets/test/README.md b/secrets/test/README.md
index 49d70b2..7317d50 100644
--- a/secrets/test/README.md
+++ b/secrets/test/README.md
@@ -21,6 +21,7 @@ Unit tests for the szecret secret management system.
test/
├── common.sh # Shared utilities, setup/teardown, assertions
├── run.sh # Test runner entrypoint
+├── buckets.sh # Tests for buckets: get, has, put, mv, rekey, list, check, enrol
├── create-local.sh # Tests for create-local command
├── edge-cases.sh # Edge case tests (special chars, unicode, etc.)
├── ensure.sh # Tests for ensure command
diff --git a/secrets/test/buckets.sh b/secrets/test/buckets.sh
new file mode 100755
index 0000000..b021c3c
--- /dev/null
+++ b/secrets/test/buckets.sh
@@ -0,0 +1,274 @@
+#!/bin/bash
+# Tests for buckets: get, has, put, mv, rekey, list, check and enrol, beside
+# the old store that common.sh sets up.
+
+this_dir="$(cd "$(dirname "$0")" && pwd)"
+source "${this_dir}/common.sh"
+
+export SZECRET_BUCKETS="${TEST_DIR}/buckets"
+export SZECRET_IDENTITY="${TEST_DIR}/me/identity"
+mkdir -p "${SZECRET_BUCKETS}"
+
+# This machine, another host, and the admin, each a key of its own.
+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")"
+echo "${admin}" > "${TEST_DIR}/admins"
+export SZECRET_ADMINS_COMMAND="cat '${TEST_DIR}/admins'"
+
+# bucket <name> <"name key" lines...>: a recipients file as szecret-buckets writes it.
+bucket() {
+ local b="$1" row
+ shift
+ mkdir -p "${SZECRET_BUCKETS}/${b}"
+ {
+ echo "# Written by szecret-buckets"
+ for row in "$@"; do
+ echo "# ${row%% *}"
+ echo "${row#* }"
+ done
+ } > "${SZECRET_BUCKETS}/${b}/recipients"
+}
+
+bucket mine "me ${me}" "other ${other}"
+bucket theirs "other ${other}"
+bucket admin-only
+
+test_enrol_twice_same_key() {
+ local again
+ again="$(szecret enrol 2>/dev/null)"
+ assert_equals "${me}" "${again}" "second enrol"
+ assert_success test "$(stat -c %a "${SZECRET_IDENTITY}" 2>/dev/null || stat -f %Lp "${SZECRET_IDENTITY}")" = 600
+ pass "enrol: makes one identity, mode 600, and prints the same key again"
+}
+
+test_enrol_missing_hardware() {
+ local err
+ err="$(SZECRET_IDENTITY="${TEST_DIR}/hw/identity" PATH="/usr/bin:/bin:$(dirname "$(command -v age)")" szecret enrol --tpm 2>&1)" \
+ && fail "enrol --tpm should fail without the plugin"
+ assert_contains "${err}" "no key made"
+ [[ ! -e "${TEST_DIR}/hw/identity" ]] || fail "enrol --tpm left a key"
+ pass "enrol: refuses missing hardware and makes no file key instead"
+}
+
+test_put_refuses_before_rekey() {
+ local err
+ err="$(echo v | szecret put mine/x 2>&1)" && fail "put before the first rekey should fail"
+ assert_contains "${err}" "never been keyed"
+ pass "put: refuses a bucket never keyed"
+}
+
+test_rekey_first_time() {
+ local out
+ out="$(szecret rekey mine)"
+ assert_contains "${out}" "+ me"
+ assert_contains "${out}" "+ admin"
+ assert_contains "$(cat "${SZECRET_BUCKETS}/mine/.szecret")" "kind=bucket"
+ szecret rekey theirs >/dev/null
+ szecret rekey admin-only >/dev/null
+ assert_equals "${admin}" "$(sed -n 's/^encrypted-to=//p' "${SZECRET_BUCKETS}/admin-only/.szecret")" "admin-only's set"
+ pass "rekey: keys empty buckets without asking, the admins always in"
+}
+
+test_put_get_bucket() {
+ echo "v1" | szecret put mine/x
+ assert_equals "v1" "$(szecret get mine/x)" "get mine/x"
+ assert_equals "v1" "$(szecret get x)" "get x"
+ assert_equals "v1" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/x.age")" "the admin reads it"
+ assert_equals "v1" "$(age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age")" "the other reader reads it"
+ echo "v2" | szecret put x 2>/dev/null && fail "plain put of a name in a bucket should fail"
+ pass "put/get: a bucket's readers and the admins read it"
+}
+
+test_not_a_reader() {
+ echo "t" | szecret put theirs/t
+ local err
+ err="$(szecret get t 2>&1)" && fail "get from a bucket not read here should fail"
+ assert_contains "${err}" "t is in bucket theirs; this machine isn't one of its readers"
+ szecret has t && fail "has t should fail"
+ szecret has x || fail "has x should succeed"
+ szecret has nope && fail "has nope should fail"
+ pass "get/has: a bucket this machine doesn't read"
+}
+
+test_plain_put_admin_only() {
+ local err
+ err="$(echo "a" | szecret put a 2>&1)"
+ assert_contains "${err}" "admin-only"
+ assert_file_exists "${SZECRET_BUCKETS}/admin-only/a.age"
+ assert_equals "a" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/admin-only/a.age")" "the admin reads it"
+ szecret get a 2>/dev/null && fail "this machine should not read admin-only"
+ pass "put: a plain name goes to admin-only"
+}
+
+test_old_store_side_by_side() {
+ echo "old" | SZECRET_BUCKETS="" szecret put oldone
+ assert_file_exists "${SZECRET_STORE}/oldone.age"
+ assert_equals "old" "$(szecret get oldone)" "get from the old store"
+ echo "old2" | szecret put oldone 2>/dev/null
+ assert_equals "old2" "$(szecret get oldone)" "rewritten in the old store"
+ szecret has oldone || fail "has oldone"
+ pass "the old store: read and rewritten in place beside the buckets"
+}
+
+test_two_places() {
+ cp "${SZECRET_BUCKETS}/mine/x.age" "${SZECRET_BUCKETS}/theirs/x.age"
+ local err
+ err="$(szecret get x 2>&1)" && fail "a name in two places should fail"
+ assert_contains "${err}" "mine/x"
+ assert_contains "${err}" "theirs/x"
+ assert_equals "v1" "$(szecret get mine/x)" "bucket/name still works"
+ rm "${SZECRET_BUCKETS}/theirs/x.age"
+ pass "get: a name in two places names both"
+}
+
+test_mv() {
+ szecret mv oldone mine 2>/dev/null
+ [[ ! -e "${SZECRET_STORE}/oldone.age" ]] || fail "the old file should be gone"
+ assert_equals "old2" "$(szecret get mine/oldone)" "moved"
+ assert_equals "old2" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/oldone.age")" "the admin reads it"
+ szecret mv a mine 2>/dev/null && fail "mv of a secret this machine can't read should fail"
+ assert_file_exists "${SZECRET_BUCKETS}/admin-only/a.age"
+ cp "${SZECRET_BUCKETS}/mine/oldone.age" "${SZECRET_BUCKETS}/theirs/oldone.age"
+ szecret mv theirs/oldone mine 2>/dev/null && fail "mv onto an existing name should fail"
+ assert_equals "old2" "$(szecret get mine/oldone)" "the existing file is untouched"
+ rm "${SZECRET_BUCKETS}/theirs/oldone.age"
+ pass "mv: moves out of the old store; one it can't read stays put; never overwrites"
+}
+
+test_put_refuses_stale_readers() {
+ bucket mine "me ${me}"
+ local err
+ err="$(echo v | szecret put mine/x 2>&1)" && fail "put after a reader change should fail"
+ assert_contains "${err}" "rekey"
+ out="$(szecret check 2>&1)" && fail "check should report the change"
+ assert_contains "${out}" "readers changed since its last rekey (-other)"
+ pass "put and check: refuse until a rekey after the readers change"
+}
+
+test_rekey_removal() {
+ local out
+ out="$(szecret rekey mine < /dev/null)"
+ assert_contains "${out}" "- other"
+ age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age" >/dev/null 2>&1 && fail "the removed reader still reads x"
+ assert_equals "v1" "$(szecret get mine/x)" "still readable here"
+ pass "rekey: a removed reader no longer reads the files"
+}
+
+test_rekey_addition_needs_yes() {
+ bucket mine "me ${me}" "other ${other}"
+ local err
+ err="$(szecret rekey mine < /dev/null 2>&1)" && fail "an addition without a terminal should fail"
+ assert_contains "${err}" "--add-ok"
+ age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age" >/dev/null 2>&1 && fail "nothing should have changed"
+ szecret rekey --add-ok mine >/dev/null
+ assert_equals "v1" "$(age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age")" "added"
+ pass "rekey: an addition needs a terminal or --add-ok"
+}
+
+test_rekey_unreadable_changes_nothing() {
+ # theirs holds t, which this machine can't open: a rekey here must stop whole.
+ bucket theirs "other ${other}" "me ${me}"
+ local before err
+ before="$(cat "${SZECRET_BUCKETS}/theirs/.szecret")"
+ err="$(szecret rekey --add-ok theirs 2>&1)" && fail "rekey of a file it can't read should fail"
+ assert_contains "${err}" "nothing changed"
+ assert_equals "${before}" "$(cat "${SZECRET_BUCKETS}/theirs/.szecret")" ".szecret unchanged"
+ [[ -z "$(ls -a "${SZECRET_BUCKETS}" | grep rekey)" ]] || fail "work directory left behind"
+ bucket theirs "other ${other}"
+ pass "rekey: a file it can't decrypt stops it, nothing changed"
+}
+
+test_no_admins() {
+ local err
+ err="$(echo v | SZECRET_ADMINS_COMMAND="true" szecret put mine/y 2>&1)" && fail "put without admins should fail"
+ assert_contains "${err}" "no admins"
+ err="$(echo v | env -u SZECRET_ADMINS_COMMAND "${SECRETS_DIR}/main.sh" put mine/y 2>&1)" && fail "put without the command should fail"
+ assert_contains "${err}" "no admins"
+ [[ ! -e "${SZECRET_BUCKETS}/mine/y.age" ]] || fail "y written without admins"
+ pass "put: refuses with no admins"
+}
+
+test_admin_change_needs_rekey() {
+ age-keygen -o "${TEST_DIR}/admin2" 2>/dev/null
+ age-keygen -y "${TEST_DIR}/admin2" >> "${TEST_DIR}/admins"
+ echo v | szecret put mine/y 2>/dev/null && fail "put after the admins changed should fail"
+ szecret rekey --add-ok mine >/dev/null
+ echo v | szecret put mine/y
+ assert_equals "v" "$(age -d -i "${TEST_DIR}/admin2" "${SZECRET_BUCKETS}/mine/y.age")" "the new admin reads it"
+ pass "put: refuses after the admins change, until a rekey"
+}
+
+test_list() {
+ local out
+ out="$(szecret list)"
+ assert_contains "${out}" "oldone"
+ assert_contains "${out}" "x"
+ [[ "$(echo "${out}" | grep -cx t)" -eq 0 ]] || fail "list shows a secret not read here: ${out}"
+ out="$(szecret list --names)"
+ assert_contains "${out}" "mine/x"
+ out="$(szecret list --names --all)"
+ assert_contains "${out}" "theirs/t"
+ assert_contains "${out}" "admin-only/a"
+ pass "list: bare names this machine reads; --names and --all"
+}
+
+test_check_ok() {
+ local out
+ out="$(szecret check)" || fail "check should pass: ${out}"
+ assert_contains "${out}" "no problems"
+ out="$(szecret check --all 2>&1)" && fail "check --all here should fail: this machine can't open theirs or admin-only"
+ assert_contains "${out}" "theirs/t: does not decrypt here"
+ pass "check: passes on what this machine reads; --all finds what it can't open"
+}
+
+test_check_stanza_types() {
+ local d="${SZECRET_BUCKETS}/mine"
+ # A passphrase file's header (age -p wants a terminal).
+ printf 'age-encryption.org/v1\n-> scrypt abc 18\nxyz\n--- mac\n' > "${d}/pw.age"
+ local out
+ out="$(szecret check 2>&1)" && fail "check should flag scrypt"
+ assert_contains "${out}" "mine/pw: unexpected stanza type scrypt"
+ rm "${d}/pw.age"
+ pass "check: flags stanza types it doesn't expect"
+}
+
+test_check_missing_plugin() {
+ bucket mine "me ${me}" "other ${other}" "castle age1se1qgg9a5ep2jkuz2v9d0ek0dvqr3ml7mjpz7nqwfvymk0d4nqx9uxs5vqz0xr"
+ local out
+ out="$(szecret check 2>&1)" && fail "check should flag a key it can't encrypt to"
+ assert_contains "${out}" "can't encrypt to castle here: age-plugin-se is not installed"
+ bucket mine "me ${me}" "other ${other}"
+ pass "check: flags a key type this machine can't encrypt to"
+}
+
+test_ready() {
+ szecret ready || fail "ready with an identity"
+ SZECRET_STORE="" SZECRET_PRIVATE_KEY="${TEST_DIR}/none" szecret ready || fail "ready with only buckets and an identity"
+ pass "ready: an identity and a store"
+}
+
+test_enrol_twice_same_key
+test_enrol_missing_hardware
+test_put_refuses_before_rekey
+test_rekey_first_time
+test_put_get_bucket
+test_not_a_reader
+test_plain_put_admin_only
+test_old_store_side_by_side
+test_two_places
+test_mv
+test_put_refuses_stale_readers
+test_rekey_removal
+test_rekey_addition_needs_yes
+test_rekey_unreadable_changes_nothing
+test_no_admins
+test_admin_change_needs_rekey
+test_list
+test_check_ok
+test_check_stanza_types
+test_check_missing_plugin
+test_ready
diff --git a/secrets/test/common.sh b/secrets/test/common.sh
index 33d7d78..a751f65 100755
--- a/secrets/test/common.sh
+++ b/secrets/test/common.sh
@@ -18,6 +18,8 @@ skip() { echo -e "${YELLOW}SKIP${NC}: $1"; }
# Create temporary test environment
setup() {
export TEST_DIR="$(mktemp -d)"
+ # Never the real data dir: the buckets and identity there are a host's own.
+ export XDG_DATA_HOME="${TEST_DIR}/xdg"
export SZECRET_STORE="${TEST_DIR}/store"
export SZECRET_PUBLIC_KEY="${TEST_DIR}/.id.pub"
export SZECRET_PRIVATE_KEY="${TEST_DIR}/.id"
diff --git a/secrets/test/rotate.sh b/secrets/test/rotate.sh
deleted file mode 100755
index 02abe39..0000000
--- a/secrets/test/rotate.sh
+++ /dev/null
@@ -1,69 +0,0 @@
-#!/bin/bash
-# Tests for rotate command
-
-this_dir="$(cd "$(dirname "$0")" && pwd)"
-source "${this_dir}/common.sh"
-
-setup_key_dir_layout() {
- local keys_dir="${TEST_DIR}/keys"
- mkdir -p "${keys_dir}" "${SZECRET_STORE}"
- rm -f "${SZECRET_STORE}"/*.age
- unset SZECRET_PRIVATE_KEY
- export SZECRET_PRIVATE_KEY_DIR="${keys_dir}"
-
- local temp_key="${TEST_DIR}/initial-key"
- age-keygen -o "${temp_key}" 2>/dev/null
- local pub
- pub="$(age-keygen -y "${temp_key}")"
- echo "${pub}" > "${SZECRET_PUBLIC_KEY}"
- mv "${temp_key}" "${keys_dir}/${pub}"
- chmod 600 "${keys_dir}/${pub}"
- SETUP_PUB="${pub}"
-}
-
-test_rotate_reencrypts_secrets() {
- setup_key_dir_layout
- local old_pub="${SETUP_PUB}"
-
- echo "rotate-me" | szecret put "rotate-test"
- echo "other" | szecret put "rotate-other"
-
- szecret rotate --yes
-
- local new_pub
- new_pub="$(cat "${SZECRET_PUBLIC_KEY}")"
- if [[ "${new_pub}" == "${old_pub}" ]]; then
- fail "public key should change after rotate"
- fi
-
- assert_equals "rotate-me" "$(szecret get "rotate-test")" "rotated secret"
- assert_equals "other" "$(szecret get "rotate-other")" "other rotated secret"
- assert_file_exists "${SZECRET_PRIVATE_KEY_DIR}/${new_pub}" "new private key file"
- pass "rotate re-encrypts secrets and updates keys"
-}
-
-test_rotate_empty_store() {
- setup_key_dir_layout >/dev/null
-
- szecret rotate --yes
-
- local new_pub
- new_pub="$(cat "${SZECRET_PUBLIC_KEY}")"
- assert_file_exists "${SZECRET_PRIVATE_KEY_DIR}/${new_pub}" "new private key with empty store"
- pass "rotate succeeds with empty store"
-}
-
-test_rotate_requires_confirmation() {
- setup_key_dir_layout >/dev/null
-
- assert_failure szecret rotate
- pass "rotate fails without confirmation"
-}
-
-run_tests() {
- test_rotate_reencrypts_secrets
- test_rotate_empty_store
- test_rotate_requires_confirmation
-}
-
-[[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests