From 8f818a64f2f0f3a833fc842d52a675358ec64c9c Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Wed, 18 Nov 2026 14:02:09 +0000 Subject: 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. --- secrets/common.sh | 339 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 310 insertions(+), 29 deletions(-) (limited to 'secrets/common.sh') diff --git a/secrets/common.sh b/secrets/common.sh index 1274741..5eee8b6 100644 --- a/secrets/common.sh +++ b/secrets/common.sh @@ -70,15 +70,19 @@ XDG_DATA_HOME=${XDG_DATA_HOME:-"$HOME/.local/share"} # SZECRET_PRIVATE_KEY can still be used as a full single-file override (mainly for tests). private_key_dir="${SZECRET_PRIVATE_KEY_DIR:-${XDG_DATA_HOME}/szecret}" -# The global store and the public key its secrets are encrypted to. This code -# carries neither: a machine with only these scripts holds no secrets. Looked -# for in order: +# Two kinds of store, side by side while secrets move from one to the other +# (cloud-setup docs/proposal-secret-roles.md). This code carries neither: a +# machine with only these scripts holds no secrets. # -# 1. SZECRET_STORE / SZECRET_PUBLIC_KEY, if set (tests, or a separate store). -# 2. store/ and id.pub in ~/.local/share/szecret, beside the private keys. -# Normally links into whichever checkout holds the store. +# The old store: one directory of .age files, every one encrypted to the key +# in id.pub. SZECRET_STORE / SZECRET_PUBLIC_KEY if set, else store/ and +# id.pub in ~/.local/share/szecret, normally links into the checkout that +# holds them. # -# With none of these, every command fails with the instructions below. +# Buckets: a directory of directories, each a set of secrets with one set of +# readers. SZECRET_BUCKETS if set, else buckets/ in ~/.local/share/szecret. +# +# With neither, every command but enrol fails with the instructions below. szecret_data_dir="${XDG_DATA_HOME}/szecret" if [[ -n "${SZECRET_STORE:-}" ]]; then @@ -92,43 +96,61 @@ fi if [[ -n "${SZECRET_PUBLIC_KEY:-}" ]]; then public_key="${SZECRET_PUBLIC_KEY}" -elif [[ -r "${szecret_data_dir}/id.pub" ]]; then - public_key="${szecret_data_dir}/id.pub" else public_key="${szecret_data_dir}/id.pub" fi -if [[ -z "${store}" || ! -r "${public_key}" ]]; then +if [[ -n "${SZECRET_BUCKETS:-}" ]]; then + buckets="${SZECRET_BUCKETS}" + mkdir -p "${buckets}" +elif [[ -d "${szecret_data_dir}/buckets" ]]; then + buckets="${szecret_data_dir}/buckets" +else + buckets="" +fi + +# This machine's identity: made here by `szecret enrol`, never copied off. Its +# public key is beside it, as a hardware key can't give it without its plugin. +# A path of its own, not the profile's: timers, systemd and launchd read no +# shell profile. +identity="${SZECRET_IDENTITY:-${szecret_data_dir}/identity}" +identity_pub="${identity}.pub" + +# The old store counts only with the public key it is encrypted to. +if [[ -n "${store}" && ! -r "${public_key}" && -n "${buckets}" ]]; then + store="" +fi + +if [[ -z "${szecret_no_store_ok:-}" ]] && [[ -z "${buckets}" ]] && [[ -z "${store}" || ! -r "${public_key}" ]]; then # `ready` is asked by every interactive shell; a machine with no store is # simply not ready, which needs no explanation each time. if [[ -n "${szecret_quiet:-}" ]]; then exit 1 fi - >&2 cat <&2 cat < ${szecret_data_dir}/id.pub - mv private "${private_key_dir}/\${pubkey}" +Looked for ${szecret_data_dir}/buckets, and for ${szecret_data_dir}/store +with ${szecret_data_dir}/id.pub. Link them to an existing store, or point +SZECRET_BUCKETS (or SZECRET_STORE and SZECRET_PUBLIC_KEY) at one. -EOF +MSG exit 1 fi -key_id="$(cat "${public_key}" 2>/dev/null || true)" -if [[ -z "$key_id" ]]; then - >&2 echo "WARN: Invalid public key: ${public_key}" - exit 1 +key_id="" +private_key="" +if [[ -n "${store}" && -r "${public_key}" ]]; then + key_id="$(cat "${public_key}" 2>/dev/null || true)" + if [[ -z "$key_id" ]]; then + >&2 echo "WARN: Invalid public key: ${public_key}" + exit 1 + fi + private_key="${SZECRET_PRIVATE_KEY:-${private_key_dir}/${key_id}}" +else + store="" fi -private_key="${SZECRET_PRIVATE_KEY:-${private_key_dir}/${key_id}}" - file_for() { local name="$1" echo "${store}/${name}.age" @@ -173,8 +195,7 @@ ensure_age() { fi local os local arch - os="$(uname)" - os=${os,,} + os="$(uname | tr "[:upper:]" "[:lower:]")" arch="$(uname -m)" if [[ "$arch" == "aarch64" ]]; then arch="arm64" @@ -306,3 +327,263 @@ verify_local_pubkey() { fi } + +# --- Buckets ------------------------------------------------------------------- +# +# A bucket is a directory: .age files, `recipients` (age public keys, one +# a line, a comment above each naming its reader, written by cloud-setup's +# bin/szecret-buckets) and `.szecret`, which only `rekey` writes: +# +# kind=bucket +# encrypted-to= +# name= one for each of those keys +# +# The admins, printed by SZECRET_ADMINS_COMMAND, are added to every +# encryption, and nothing is encrypted without them. + +re_secret='^[A-Za-z0-9][A-Za-z0-9._-]*$' +re_bucket='^[a-z0-9][a-z0-9._-]*$' + +# The stanza types a bucket's files may carry: age's own X25519 and p256tag +# (age1tag1 keys: age-plugin-tpm, and age-plugin-se with tag recipients), and +# age-plugin-se's piv-p256 (age1se1 keys). +stanza_types="X25519 p256tag piv-p256" + +die() { >&2 echo "ERROR: $*"; exit 1; } + +sorted() { LC_ALL=C sort -u; } + +# keys_in : the keys in a recipients file, comments and blank lines dropped. +keys_in() { + sed -e 's/#.*//' -e 's/[[:space:]]//g' "$1" 2>/dev/null | grep -v '^$' || true +} + +is_bucket_dir() { grep -qx 'kind=bucket' "$1/.szecret" 2>/dev/null; } + +# encrypted_to : the keys the bucket's files were last encrypted to. +encrypted_to() { + sed -n 's/^encrypted-to=//p' "$1/.szecret" 2>/dev/null | tr ' ' '\n' | grep -v '^$' | sorted || true +} + +# bucket_label : its name for messages: the bucket's name, or a +# repository bucket's path. +bucket_label() { + if [[ -n "${buckets}" && "$(dirname "$1")" == "${buckets}" ]]; then + basename "$1" + else + echo "$1" + fi +} + +# admin_keys: the admins, one a line. With none it fails and says why. +admin_keys() { + local out + if [[ -z "${SZECRET_ADMINS_COMMAND:-}" ]]; then + >&2 echo "ERROR: no admins: SZECRET_ADMINS_COMMAND is not set, and nothing is encrypted without them" + return 1 + fi + if ! out="$(bash -c "${SZECRET_ADMINS_COMMAND}" &2 echo "ERROR: no admins: '${SZECRET_ADMINS_COMMAND}' failed, and nothing is encrypted without them" + return 1 + fi + out="$(grep -E '^age1[0-9a-z]+$' <<<"${out}" | sorted || true)" + if [[ -z "${out}" ]]; then + >&2 echo "ERROR: no admins: '${SZECRET_ADMINS_COMMAND}' printed none, and nothing is encrypted without them" + return 1 + fi + echo "${out}" +} + +# wanted_set : what the bucket's files should be encrypted to. +wanted_set() { + { keys_in "$1/recipients"; echo "$2"; } | grep -v '^$' | sorted +} + +# key_names : " " for each key in its recipients, named by +# the comment above it, then the names its last rekey recorded, so a reader +# since removed still has one. +key_names() { + awk '/^[[:space:]]*#/ { sub(/^[[:space:]]*#[[:space:]]*/, ""); name = $0; next } + /^[[:space:]]*$/ { next } + { print $1, (name == "" ? "?" : name); name = "" }' "$1/recipients" 2>/dev/null || true + sed -n 's/^name=//p' "$1/.szecret" 2>/dev/null || true +} + +# name_of : who a key is. +name_of() { + local n + if grep -qxF "$1" <<<"$3"; then + echo "admin ${1:0:16}..." + return + fi + n="$(awk -v k="$1" '$1 == k { $1 = ""; sub(/^ /, ""); print; exit }' <<<"$2")" + echo "${n:-$1}" +} + +# my_keys: this machine's public keys: its identity's, and the old key's while +# it is here. +my_keys() { + if [[ -r "${identity_pub}" ]]; then + keys_in "${identity_pub}" + fi + if [[ -n "${private_key}" && -r "${private_key}" ]]; then + echo "${key_id}" + fi + return 0 +} + +# is_reader : one of this machine's keys is among those the bucket was +# last encrypted to. +is_reader() { + local to mine k + to="$(encrypted_to "$1")" + mine="$(my_keys)" + [[ -n "${to}" && -n "${mine}" ]] || return 1 + while read -r k; do + if grep -qxF "${k}" <<<"${to}"; then + return 0 + fi + done <<<"${mine}" + return 1 +} + +# load_identities: age's -i for each identity this machine has, in ids. Both +# can be given at once: age passes over one whose plugin is missing when +# another opens the file (tested 2026-11-18, age 1.3.2). +load_identities() { + ids=() + if [[ -r "${identity}" ]]; then + ids+=(-i "${identity}") + fi + if [[ -n "${private_key}" && -r "${private_key}" ]]; then + ids+=(-i "${private_key}") + fi + if [[ ${#ids[@]} -eq 0 ]]; then + die "no identity on this machine (${identity}): run szecret enrol" + fi +} + +decrypt() { + load_identities + age --decrypt "${ids[@]}" "$1" +} + +# age_version_ok: age 1.3 or later, which encrypts to age1tag1 keys itself. +age_version_ok() { + local v major minor + v="$(age --version 2>/dev/null || true)" + v="${v#v}" + major="${v%%.*}" + minor="${v#*.}" + minor="${minor%%.*}" + if [[ "${major}" =~ ^[0-9]+$ && "${minor}" =~ ^[0-9]+$ ]] && (( major > 1 || (major == 1 && minor >= 3) )); then + return 0 + fi + die "age ${v:-?} is older than 1.3, which buckets need: upgrade it (brew upgrade age, or cloud-setup's setup/age.sh)" +} + +# plugin_for : the plugin age needs to encrypt to it; nothing for age's +# own kinds. +plugin_for() { + local hrp="${1%1*}" + hrp="${hrp#age1}" + case "${hrp}" in + age|tag|pq|tagpq) ;; + *) echo "age-plugin-${hrp}" ;; + esac +} + +# stanzas_of : the type of each recipient stanza in its header, which +# needs no key. +stanzas_of() { + LC_ALL=C awk 'NR == 1 && $0 != "age-encryption.org/v1" { print "(not an age file)"; exit } + /^---/ { exit } + /^-> / { print $2 }' "$1" +} + +# encrypt_to : stdin, encrypted to each key (one a line), in +# place at only once whole. +encrypt_to() { + local out="$1" k tmp args=() + while read -r k; do + if [[ -n "${k}" ]]; then + args+=(-r "${k}") + fi + done <<<"$2" + [[ ${#args[@]} -gt 0 ]] || die "nothing to encrypt $(basename "${out}") to" + tmp="$(mktemp "$(dirname "${out}")/.put.XXXXXX")" || die "cannot write in $(dirname "${out}")" + if ! age --encrypt "${args[@]}" -o "${tmp}"; then + rm -f "${tmp}" + die "age could not encrypt $(basename "${out}")" + fi + chmod 644 "${tmp}" + mv "${tmp}" "${out}" +} + +# bucket_set : the keys a new file in that bucket is encrypted to. Refuses +# with no admins, before the bucket's first rekey, and when its readers or the +# admins have changed since its last one, so a forgotten rekey can't leave a +# removed reader reading new values. +bucket_set() { + local dir="$1" admins want + [[ -d "${dir}" ]] || die "no bucket $(bucket_label "${dir}")" + [[ -r "${dir}/recipients" ]] \ + || die "bucket $(bucket_label "${dir}") has no recipients file (cloud-setup's bin/szecret-buckets writes it)" + admins="$(admin_keys)" || exit 1 + want="$(wanted_set "${dir}" "${admins}")" + is_bucket_dir "${dir}" \ + || die "bucket $(bucket_label "${dir}") has never been keyed: szecret rekey $(bucket_label "${dir}") first" + if [[ "${want}" != "$(encrypted_to "${dir}")" ]]; then + die "the readers of $(bucket_label "${dir}") or the admins changed since its last rekey: szecret rekey $(bucket_label "${dir}") first" + fi + echo "${want}" +} + +# locate : every file holding that name: buckets first, then the old store. +locate() { + local f + if [[ -n "${buckets}" ]]; then + for f in "${buckets}"/*/"$1.age"; do + if [[ -e "${f}" ]]; then + echo "${f}" + fi + done + fi + if [[ -n "${store}" && -e "${store}/$1.age" ]]; then + echo "${store}/$1.age" + fi + return 0 +} + +# where_is : "/", or " in the old store". +where_is() { + local n + n="$(basename "$1" .age)" + if [[ -n "${store}" && "$(dirname "$1")" == "${store}" ]]; then + echo "${n} in the old store" + else + echo "$(bucket_label "$(dirname "$1")")/${n}" + fi +} + +# find_secret : the one file it names, or an error. +find_secret() { + local arg="$1" b n found count + if [[ "${arg}" == */* ]]; then + b="${arg%%/*}" + n="${arg#*/}" + [[ "${b}" =~ ${re_bucket} && "${n}" =~ ${re_secret} ]] || die "not a secret's name: ${arg}" + [[ -n "${buckets}" && -r "${buckets}/${b}/${n}.age" ]] || die "Secret '${arg}' not found" + echo "${buckets}/${b}/${n}.age" + return + fi + [[ "${arg}" =~ ${re_secret} ]] || die "not a secret's name: ${arg}" + found="$(locate "${arg}")" + count="$(grep -c . <<<"${found}" || true)" + if [[ "${count}" -eq 0 ]]; then + die "Secret '${arg}' not found" + elif [[ "${count}" -gt 1 ]]; then + die "'${arg}' is in more than one place: $(while read -r f; do where_is "${f}"; done <<<"${found}" | paste -sd ',' - | sed 's/,/, /g')" + fi + echo "${found}" +} -- cgit v1.2.3