#!/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 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" } # mv --phone: what szecret itself checks of the header that comes back. The # phone's own work (cloud-setup's phone/szmv.py) is tested there. test_mv_phone_refusals() { local err f="${SZECRET_BUCKETS}/admin-only/a.age" before before="$(shasum "${f}")" untouched() { assert_equals "${before}" "$(shasum "${f}")" "$1: admin-only/a is as it was" [[ ! -e "${SZECRET_BUCKETS}/mine/a.age" ]] || fail "$1: left mine/a" [[ -z "$(ls -A "${SZECRET_BUCKETS}/mine" | grep '^\.put\.')" ]] || fail "$1: left a temporary file" } err="$(SZECRET_PHONE_COMMAND="" szecret mv --phone a mine 2>&1)" && fail "no command should fail" assert_contains "${err}" "SZECRET_PHONE_COMMAND" untouched "no command" err="$(SZECRET_PHONE_COMMAND="cat >/dev/null; exit 1" szecret mv --phone a mine 2>&1)" && fail "a failed command should fail" untouched "a failed command" err="$(SZECRET_PHONE_COMMAND="cat >/dev/null; echo not-a-header" szecret mv --phone a mine 2>&1)" && fail "garbage should fail" assert_contains "${err}" "not a whole age header" untouched "garbage" # The readers it is told of, and a header of the right shape for them that # is not for this file: this machine is one of the readers, and cannot open it. cat > "${TEST_DIR}/fake-phone" <<'STUB' #!/bin/bash desc="$(cat)" args=() while read -r k v _; do [[ "${k}" == reader ]] && args+=(-r "${v}"); done <<<"${desc}" echo "${desc}" > "${TEST_DIR}/fake-phone.desc" echo x | age "${args[@]}" | LC_ALL=C awk '{ print } /^--- / { exit }' STUB chmod +x "${TEST_DIR}/fake-phone" err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone" szecret mv --phone a mine 2>&1)" \ && fail "a header for another file key should fail where this machine reads it" assert_contains "${err}" "cannot open the new file" untouched "a wrong header" assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "name a" assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "from admin-only" assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "reader ${me} me" assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "reader ${admin} admin" # The readers change while the phone answers. local saved saved="$(cat "${SZECRET_BUCKETS}/mine/recipients")" err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone; echo '${other}' >> '${SZECRET_BUCKETS}/mine/recipients'" \ szecret mv --phone a mine 2>&1)" && fail "a reader change while waiting should fail" echo "${saved}" > "${SZECRET_BUCKETS}/mine/recipients" untouched "a reader change" # The file changes while the phone answers (a rekey arriving), into a # bucket this machine does not read, so it cannot open the result. cp "${f}" "${TEST_DIR}/a.saved" err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone; printf x >> '${f}'" \ szecret mv --phone a theirs 2>&1)" && fail "a file changed while waiting should fail" assert_contains "${err}" "changed while the phone answered" [[ ! -e "${SZECRET_BUCKETS}/theirs/a.age" ]] || fail "a changed file: left theirs/a" cp "${TEST_DIR}/a.saved" "${f}" untouched "a changed file" pass "mv --phone: no command, a failure, garbage, a header that is not this file's, readers or the file changing: nothing written" } 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_local_store_key_in_bucket() { local d="${TEST_DIR}/repo-store" mkdir -p "${d}" SZECRET_BUCKETS="" szecret create-local inrepo "${d}" >/dev/null 2>&1 || fail "create-local" echo "local1" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local before the move" szecret mv local-key-inrepo mine 2>/dev/null || fail "mv local-key-inrepo" assert_equals "local1" "$(szecret get-local "${d}/v.age")" "get-local with its key in a bucket" echo "local2" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local after the move" assert_equals "local2" "$(szecret get-local "${d}/v.age")" "put-local with its key in a bucket" pass "get-local/put-local: an old-style repository store whose key moved to a bucket" } test_create_local_in_bucket() { local d="${TEST_DIR}/new-store" err pub err="$(szecret create-local newstore "${d}" 2>&1)" && fail "create-local with no bucket should fail here" assert_contains "${err}" "create-local /newstore" [[ ! -e "${d}/.szecret" ]] || fail "a refused create-local left metadata" [[ -z "$(find "${SZECRET_BUCKETS}" -name local-key-newstore.age)" ]] || fail "a refused create-local left a key" szecret create-local mine/newstore "${d}" 2>/dev/null || fail "create-local mine/newstore" assert_file_exists "${SZECRET_BUCKETS}/mine/local-key-newstore.age" pub="$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/local-key-newstore.age" | age-keygen -y)" assert_contains "$(cat "${d}/.szecret")" "pubkey=${pub}" assert_success age -d -i "${TEST_DIR}/other" -o /dev/null "${SZECRET_BUCKETS}/mine/local-key-newstore.age" echo "n1" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local in the new store" assert_equals "n1" "$(szecret get-local "${d}/v.age")" "get-local in the new store" pass "create-local: a new store's key goes in the bucket named, read by that bucket's readers" } test_create_local_existing_key() { local d="${TEST_DIR}/new-store-2" err err="$(szecret create-local theirs/newstore "${d}" 2>&1)" && fail "create-local naming another bucket should fail" assert_contains "${err}" "is in mine/local-key-newstore, not in theirs" [[ ! -e "${d}/.szecret" ]] || fail "a refused create-local left metadata" szecret create-local newstore "${d}" 2>/dev/null || fail "create-local of an existing store" assert_equals "n1" "$(cp "${TEST_DIR}/new-store/v.age" "${d}/v.age" && szecret get-local "${d}/v.age")" "the same store in a second directory" szecret create-local mine/newstore "${TEST_DIR}/new-store-3" 2>/dev/null || fail "create-local naming the bucket it's in" assert_equals "$(sed -n 's/^pubkey=//p' "${TEST_DIR}/new-store/.szecret")" "$(sed -n 's/^pubkey=//p' "${TEST_DIR}/new-store-3/.szecret")" "the existing store's key" SZECRET_BUCKETS="" szecret create-local oldk "${TEST_DIR}/oldk" 2>/dev/null || fail "create-local in the old store" err="$(szecret create-local mine/oldk "${TEST_DIR}/oldk-2" 2>&1)" && fail "create-local naming a bucket for an old-store key should fail" assert_contains "${err}" "szecret mv local-key-oldk mine" pass "create-local: an existing store gets metadata, and refuses a bucket it isn't in" } test_create_local_refusals() { local err err="$(SZECRET_BUCKETS="" szecret create-local mine/x "${TEST_DIR}/r1" 2>&1)" && fail "a bucket with no buckets should fail" assert_contains "${err}" "no buckets on this machine" err="$(szecret create-local Mine/x "${TEST_DIR}/r2" 2>&1)" && fail "a bad bucket name should fail" assert_contains "${err}" "not a bucket's name" err="$(szecret create-local theirs/tx "${TEST_DIR}/r4" 2>&1)" && fail "a bucket this machine doesn't read should fail" assert_contains "${err}" "doesn't read theirs" [[ ! -e "${TEST_DIR}/r4" && ! -e "${SZECRET_BUCKETS}/theirs/local-key-tx.age" ]] || fail "a refused create-local left something" bucket theirs "other ${other}" "me ${me}" err="$(szecret create-local theirs/added "${TEST_DIR}/r7" 2>&1)" && fail "a reader not yet rekeyed in should fail" assert_contains "${err}" "szecret rekey theirs first" bucket theirs "other ${other}" touch "${TEST_DIR}/afile" err="$(szecret create-local mine/ff "${TEST_DIR}/afile" 2>&1)" && fail "a file as the directory should fail" assert_contains "${err}" "is not a directory" [[ ! -e "${SZECRET_BUCKETS}/mine/local-key-ff.age" ]] || fail "a refused create-local left a key" err="$(szecret create-local nobucket/x "${TEST_DIR}/r5" 2>&1)" && fail "a missing bucket should fail" assert_contains "${err}" "no bucket nobucket" [[ ! -e "${TEST_DIR}/r5" ]] || fail "a refused create-local left its directory" age-keygen 2>/dev/null | szecret put mine/local-key-dup 2>/dev/null cp "${SZECRET_BUCKETS}/mine/local-key-dup.age" "${SZECRET_BUCKETS}/theirs/" err="$(szecret create-local mine/dup "${TEST_DIR}/r6" 2>&1)" && fail "a key in two places should fail" assert_contains "${err}" "more than one place" rm "${SZECRET_BUCKETS}/mine/local-key-dup.age" "${SZECRET_BUCKETS}/theirs/local-key-dup.age" bucket mine "me ${me}" err="$(szecret create-local mine/stale "${TEST_DIR}/r3" 2>&1)" && fail "create-local into a stale bucket should fail" assert_contains "${err}" "rekey" [[ ! -e "${TEST_DIR}/r3" ]] || fail "a refused put left its directory" [[ ! -e "${SZECRET_BUCKETS}/mine/local-key-stale.age" ]] || fail "a refused put left a key" bucket mine "me ${me}" "other ${other}" pass "create-local: refuses a bucket with no buckets, a bad name, one not read here, a missing one, a key in two places, and a bucket needing a rekey" } 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_mv_phone_refusals 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_local_store_key_in_bucket test_create_local_in_bucket test_create_local_existing_key test_create_local_refusals test_check_stanza_types test_check_missing_plugin test_ready