summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-19 22:31:31 +0000
committerIgor Soarez <igor@soarez.org>2026-11-19 22:31:31 +0000
commitdcbd341a04bbbf0ac7d720a3a3bf882778983bfe (patch)
treed9abbc2ddfae873f01de96dd0f5ab7501c607b5f
parentf82380fcf0d7901635c27871162fdfc3dff2c555 (diff)
szecret mv --phone: an admin elsewhere rewraps the file key for a secret this machine can't read
The move goes to SZECRET_PHONE_COMMAND with a snapshot of the file; the new file is that snapshot's payload under the header that comes back, written only if the file is unchanged, and opened first where this machine is a new reader.
-rw-r--r--secrets/README.md1
-rwxr-xr-xsecrets/mv.sh82
-rwxr-xr-xsecrets/test/buckets.sh57
3 files changed, 137 insertions, 3 deletions
diff --git a/secrets/README.md b/secrets/README.md
index d4860eb..a6ae932 100644
--- a/secrets/README.md
+++ b/secrets/README.md
@@ -24,6 +24,7 @@ szecret list
| `put <bucket>/<name>` | Encrypt stdin to the bucket's readers and the admins |
| `put <name>` | The same, in `admin-only` (a name still in the old store is rewritten there) |
| `mv <name> <bucket>` | Move a secret to another bucket; never copies |
+| `mv --phone <name> <bucket>` | The same, for a secret this machine can't read: an admin elsewhere (`SZECRET_PHONE_COMMAND`) rewraps its file key |
| `rekey <bucket>` | Re-encrypt a bucket to its readers now; adding a reader needs a yes or `--add-ok` |
| `list` | The secrets this machine reads (`--names`, `--all`) |
| `check [--all]` | Check that the buckets this machine reads (or every bucket) are whole |
diff --git a/secrets/mv.sh b/secrets/mv.sh
index d249ac1..4a7c4f7 100755
--- a/secrets/mv.sh
+++ b/secrets/mv.sh
@@ -11,12 +11,18 @@ the old file. A secret is never copied: when a host outside its bucket needs
it, it moves to a bucket that has every reader who needs it.
Usage:
- mv <name> <bucket>
- mv <bucket>/<name> <bucket>
+ mv [--phone] <name> <bucket>
+ mv [--phone] <bucket>/<name> <bucket>
mv -h|--help
Needs an identity that can read it now. The plaintext goes through a pipe,
never a file.
+
+--phone: for a secret this machine cannot read. An admin key elsewhere,
+reached through \$SZECRET_PHONE_COMMAND, rewraps the file's key for the
+bucket's readers, and this machine puts the file's own encrypted value under
+the new header. Nothing is decrypted here; where this machine is one of the
+new readers, it opens the result before keeping it.
EOF
exit 0
fi
@@ -24,7 +30,12 @@ fi
this_dir="$(cd "$(dirname "$0")" && pwd)"
source "${this_dir}/common.sh"
-[[ "$#" -eq 2 ]] || die "usage: szecret mv <name> <bucket>"
+phone=""
+if [[ "${1:-}" == --phone ]]; then
+ phone=1
+ shift
+fi
+[[ "$#" -eq 2 ]] || die "usage: szecret mv [--phone] <name> <bucket>"
[[ -n "${buckets}" ]] || die "no buckets on this machine"
target="$2"
[[ "${target}" =~ ${re_bucket} ]] || die "not a bucket's name: ${target}"
@@ -37,10 +48,75 @@ to="${dir}/${name}.age"
# A name in two places is a mistake to sort out by hand, not to overwrite.
[[ ! -e "${to}" ]] || die "${target}/${name} already exists; not overwriting it with $(where_is "${from}")"
if [[ -n "${store}" && "$(dirname "${from}")" == "${store}" ]]; then
+ [[ -z "${phone}" ]] || die "mv --phone moves a secret out of a bucket; $(where_is "${from}") is in the old store"
ensure_private_key
fi
set="$(bucket_set "${dir}")" || exit 1
+
+# header_bytes <file>: the length of its age header, up to and with the MAC line.
+header_bytes() {
+ LC_ALL=C awk '{ n += length($0) + 1 } /^--- / { print n; found = 1; exit } END { if (!found) exit 1 }' "$1"
+}
+
+if [[ -n "${phone}" ]]; then
+ [[ -n "${SZECRET_PHONE_COMMAND:-}" ]] || die "mv --phone: SZECRET_PHONE_COMMAND is not set"
+ names="$(key_names "${dir}")"
+ admins="$(admin_keys)" || exit 1
+ header="$(mktemp)" snapshot="$(mktemp)"
+ trap 'rm -f "${header}" "${snapshot}"' EXIT
+ # The phone answers for these bytes, which may sit an hour: a sync can
+ # bring a rekey of the file meanwhile. What is written is built from them,
+ # and only if the file is still them.
+ cp "${from}" "${snapshot}"
+ {
+ echo "name ${name}"
+ echo "from $(bucket_label "$(dirname "${from}")")"
+ echo "to ${target}"
+ echo "file ${snapshot}"
+ echo "dir ${dir}"
+ while read -r k; do
+ echo "reader ${k} $(name_of "${k}" "${names}" "${admins}")"
+ done <<<"${set}"
+ } | bash -c "${SZECRET_PHONE_COMMAND}" > "${header}" \
+ || die "mv --phone: no new header; $(where_is "${from}") is as it was"
+ # The readers can change while the phone answers: then the header is for
+ # the old ones, and a rekey comes first.
+ [[ "$(bucket_set "${dir}")" == "${set}" ]] \
+ || die "the readers of ${target} changed while the phone answered; $(where_is "${from}") is as it was"
+ hlen="$(header_bytes "${header}")" && [[ "${hlen}" -eq "$(wc -c < "${header}" | tr -d ' ')" ]] \
+ || die "mv --phone: what came back is not a whole age header; $(where_is "${from}") is as it was"
+ [[ "$(stanzas_of "${header}" | grep -c .)" -eq "$(grep -c . <<<"${set}")" ]] \
+ || die "mv --phone: the new header does not have one stanza per reader; $(where_is "${from}") is as it was"
+ old_hlen="$(header_bytes "${snapshot}")" || die "$(where_is "${from}") is not an age file"
+ tmp="$(mktemp "${dir}/.put.XXXXXX")" || die "cannot write in ${dir}"
+ # The new header, then this file's own encrypted value, byte for byte.
+ { cat "${header}"; tail -c +"$((old_hlen + 1))" "${snapshot}"; } > "${tmp}"
+ while read -r k; do
+ if grep -qxF "${k}" <<<"${set}"; then
+ load_identities
+ if ! age --decrypt "${ids[@]}" "${tmp}" > /dev/null; then
+ rm -f "${tmp}"
+ die "mv --phone: this machine is a reader of ${target} and cannot open the new file; $(where_is "${from}") is as it was"
+ fi
+ >&2 echo "szecret: opened the new file here, as one of its readers"
+ break
+ fi
+ done < <(my_keys)
+ if ! cmp -s "${snapshot}" "${from}"; then
+ rm -f "${tmp}"
+ die "mv --phone: $(where_is "${from}") changed while the phone answered (a rekey?); nothing written, ask again"
+ fi
+ if [[ -e "${to}" ]]; then
+ rm -f "${tmp}"
+ die "${target}/${name} appeared while the phone answered; not overwriting it"
+ fi
+ chmod 644 "${tmp}"
+ mv "${tmp}" "${to}"
+ rm "${from}"
+ >&2 echo "szecret: moved $(where_is "${to}"), from $(where_is "${from}"), by the phone"
+ exit 0
+fi
age_version_ok
load_identities
if ! age --decrypt "${ids[@]}" "${from}" | encrypt_to "${to}" "${set}"; then
diff --git a/secrets/test/buckets.sh b/secrets/test/buckets.sh
index 2703952..010778a 100755
--- a/secrets/test/buckets.sh
+++ b/secrets/test/buckets.sh
@@ -139,6 +139,62 @@ test_mv() {
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
@@ -273,6 +329,7 @@ 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