diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-18 14:02:09 +0000 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-18 14:02:09 +0000 |
| commit | 8f818a64f2f0f3a833fc842d52a675358ec64c9c (patch) | |
| tree | eac13a48cada4595c9073d62060c1145189c6344 /secrets/check.sh | |
| parent | 6444d6a508cdfdcf52cc6c83bab1e29a7e2a2e10 (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/check.sh')
| -rwxr-xr-x | secrets/check.sh | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/secrets/check.sh b/secrets/check.sh new file mode 100755 index 0000000..299344b --- /dev/null +++ b/secrets/check.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +set -euo pipefail + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat <<EOF +check: Check that the buckets this machine reads are whole + +For each bucket this machine reads: its readers and the admins are what its +files were last encrypted to; every key in it can be encrypted to here; each +file's header carries only the stanza types expected; and each file decrypts +here (to nowhere: nothing is printed). + +Usage: + check [--all] [<directory>...] + check -h|--help + +Options: + --all every bucket, read here or not: an admin's check that the + admins can open everything + <directory> a repository bucket, checked too (a path, with a /) + +Prints one line per problem, then a summary. Exit 1 on any problem. +EOF + exit 0 +fi + +this_dir="$(cd "$(dirname "$0")" && pwd)" +source "${this_dir}/common.sh" + +all="" +dirs=() +while [[ $# -gt 0 ]]; do + case "$1" in + --all) all=1 ;; + -*) die "usage: szecret check [--all] [<directory>...]" ;; + *) dirs+=("${1%/}") ;; + esac + shift +done + +if [[ -n "${buckets}" ]]; then + for d in "${buckets}"/*/; do + d="${d%/}" + if [[ -d "${d}" ]] && { [[ -n "${all}" ]] || is_reader "${d}"; }; then + dirs+=("${d}") + fi + done +fi + +problems=0 +problem() { + echo "$*" + problems=$((problems + 1)) +} + +admins="$(admin_keys)" || exit 1 +load_identities +checked=0 +secrets=0 +for d in ${dirs[@]+"${dirs[@]}"}; do + label="$(bucket_label "${d}")" + checked=$((checked + 1)) + if ! is_bucket_dir "${d}"; then + problem "${label}: never keyed (szecret rekey ${label})" + continue + fi + want="$(wanted_set "${d}" "${admins}")" + have="$(encrypted_to "${d}")" + if [[ "${want}" != "${have}" ]]; then + names="$(key_names "${d}")" + change="$( + comm -13 <(echo "${have}") <(echo "${want}") | while read -r k; do [[ -z "${k}" ]] || echo "+$(name_of "${k}" "${names}" "${admins}")"; done + comm -23 <(echo "${have}") <(echo "${want}") | while read -r k; do [[ -z "${k}" ]] || echo "-$(name_of "${k}" "${names}" "${admins}")"; done + )" + problem "${label}: readers changed since its last rekey ($(echo "${change}" | paste -sd ' ' -)): szecret rekey ${label}" + fi + while read -r k; do + p="$(plugin_for "${k}")" + if [[ -n "${k}" && -n "${p}" ]] && ! exists "${p}"; then + problem "${label}: can't encrypt to $(name_of "${k}" "$(key_names "${d}")" "${admins}") here: ${p} is not installed" + fi + done <<<"${want}" + for f in "${d}"/*.age; do + [[ -e "${f}" ]] || continue + secrets=$((secrets + 1)) + n="${label}/$(basename "${f}" .age)" + while read -r t; do + if [[ -n "${t}" && " ${stanza_types} " != *" ${t} "* ]]; then + problem "${n}: unexpected stanza type ${t}" + fi + done <<<"$(stanzas_of "${f}")" + if ! age --decrypt "${ids[@]}" "${f}" >/dev/null 2>&1; then + problem "${n}: does not decrypt here" + fi + done +done + +if [[ "${problems}" -gt 0 ]]; then + echo "szecret check: ${problems} problem(s) in ${checked} bucket(s), ${secrets} secret(s)" + exit 1 +fi +echo "szecret check: ${checked} bucket(s), ${secrets} secret(s), no problems" |
