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/README.md | 78 +++++++---- secrets/check.sh | 103 +++++++++++++++ secrets/common.sh | 339 +++++++++++++++++++++++++++++++++++++++++++----- secrets/create-local.sh | 7 + secrets/enrol.sh | 75 +++++++++++ secrets/ensure.sh | 4 + secrets/get-local.sh | 12 +- secrets/get.sh | 40 +++--- secrets/has.sh | 32 +++++ secrets/list.sh | 79 ++++++++--- secrets/mv.sh | 51 ++++++++ secrets/put-local.sh | 12 +- secrets/put.sh | 63 +++++++-- secrets/ready.sh | 13 +- secrets/rekey.sh | 142 ++++++++++++++++++++ secrets/rotate.sh | 119 ----------------- secrets/test/README.md | 1 + secrets/test/buckets.sh | 274 ++++++++++++++++++++++++++++++++++++++ secrets/test/common.sh | 2 + secrets/test/rotate.sh | 69 ---------- 20 files changed, 1216 insertions(+), 299 deletions(-) create mode 100755 secrets/check.sh create mode 100755 secrets/enrol.sh create mode 100755 secrets/has.sh create mode 100755 secrets/mv.sh create mode 100755 secrets/rekey.sh delete mode 100755 secrets/rotate.sh create mode 100755 secrets/test/buckets.sh delete mode 100755 secrets/test/rotate.sh (limited to 'secrets') diff --git a/secrets/README.md b/secrets/README.md index 293b747..d4860eb 100644 --- a/secrets/README.md +++ b/secrets/README.md @@ -19,15 +19,49 @@ szecret list | Command | Description | |---------|-------------| -| `get ` | Retrieve and decrypt a secret | -| `put ` | Store a secret (reads from stdin) | -| `list` | List all secrets in the store | -| `ready` | Check if private key is configured | -| `ensure` | Ensure private key is configured (interactive) | -| `rotate` | Generate a new key and re-encrypt the global store | -| `create-local ` | Create a localized secret store | -| `put-local ` | Encrypt to a local store | -| `get-local ` | Decrypt from a local store | +| `get `, `get /` | Decrypt a secret. A plain name searches every bucket and the old store | +| `has ` | Succeed quietly if this machine can read it; decrypts nothing | +| `put /` | Encrypt stdin to the bucket's readers and the admins | +| `put ` | The same, in `admin-only` (a name still in the old store is rewritten there) | +| `mv ` | Move a secret to another bucket; never copies | +| `rekey ` | 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 | +| `enrol [--tpm\|--se]` | Make this machine's identity and print its public key | +| `ready` | Check there is a store and an identity | +| `ensure` | Ensure the old private key is configured (interactive) | +| `create-local ` | Create a localized secret store (the old kind) | +| `put-local ` | Encrypt to a local store or a repository bucket | +| `get-local ` | Decrypt from a local store or a repository bucket | + +## Buckets + +A bucket is a directory of secrets that all have one set of readers, defined +by its readers, not by a workload: a secret is in exactly one bucket, and when +another host needs it, it moves to a bucket that has every reader who needs it. +The design, and who reads which bucket, are cloud-setup's +(`docs/proposal-secret-roles.md`, `szecret/hosts`); szecret knows no host. + +``` +buckets// + recipients # age public keys, one a line, a comment above each naming its reader + .szecret # kind=bucket, and the full set its files were last encrypted to + .age +``` + +- **The admins**, printed by the command in `SZECRET_ADMINS_COMMAND`, are added + to every encryption. With none, nothing is encrypted. +- **`put` refuses** when the recipients or the admins differ from the set in + `.szecret`, so a forgotten rekey can't leave a removed reader reading new + values. Only `rekey` writes that set. +- **This machine's identity** is `SZECRET_IDENTITY`, or + `~/.local/share/szecret/identity`, made by `enrol` and never copied off; its + public key is in `identity.pub` beside it. Decrypting passes it and the old + key together while both are here. +- **Names** are `[A-Za-z0-9][A-Za-z0-9._-]*`, buckets lowercase, and a name is + unique across every bucket and the old store. +- age 1.3 or later is needed to encrypt (it encrypts to `age1tag1` hardware + keys itself). ## Local Stores @@ -64,16 +98,18 @@ The commands automatically read the store name from the `.szecret` metadata file ### Where the store lives -These scripts carry no secrets. The global store is a directory of `.age` -files plus `id.pub`, the public key they are encrypted to, kept in a checkout -of its own. szecret looks for it in this order: +These scripts carry no secrets. The buckets are found at `$SZECRET_BUCKETS`, +else `~/.local/share/szecret/buckets`. The old global store, which empties as +its secrets move into buckets, is a directory of `.age` files plus `id.pub`, +the public key they are encrypted to, kept in a checkout of its own. szecret +looks for it in this order: 1. `$SZECRET_STORE` and `$SZECRET_PUBLIC_KEY`, if set. 2. `~/.local/share/szecret/store` and `~/.local/share/szecret/id.pub`, beside the private keys. Normally symlinks into the checkout that holds the store. -With neither, every command stops with setup instructions, and `ready` -fails silently. +With no buckets and no old store, every command but `enrol` stops with setup +instructions, and `ready` fails silently. ### Initial Setup @@ -93,17 +129,6 @@ mv private "${HOME}/.local/share/szecret/${pubkey}" szecret ensure ``` -### Rotating the Global Key - -```bash -# Re-encrypt all secrets with a new age key -szecret rotate -``` - -This updates `id.pub` and every file in the store. When complete, commit -those changes in the checkout that holds the store, and update the private key -backup in 1Password. - ## Migration Guide ### Migrating Local Stores (pre-2026) @@ -146,6 +171,9 @@ For testing or custom setups, these environment variables override default paths | `SZECRET_PUBLIC_KEY` | Public key path | `~/.local/share/szecret/id.pub` | | `SZECRET_PRIVATE_KEY` | Single private key file (full override) | (none) | | `SZECRET_PRIVATE_KEY_DIR` | Directory containing private keys (named by their public key) | `~/.local/share/szecret` | +| `SZECRET_BUCKETS` | Buckets directory | `~/.local/share/szecret/buckets` | +| `SZECRET_IDENTITY` | This machine's identity (its public key in `.pub`) | `~/.local/share/szecret/identity` | +| `SZECRET_ADMINS_COMMAND` | Prints the admins' public keys, one a line | (none: nothing is encrypted to a bucket) | * Private keys are stored in `~/.local/share/szecret/` (or `$SZECRET_PRIVATE_KEY_DIR`), with each file named after its public key (e.g. `age1...`). * `id.pub` indicates which public key is current. 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 <...] + check -h|--help + +Options: + --all every bucket, read here or not: an admin's check that the + admins can open everything + 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] [...]" ;; + *) 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" 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}" +} diff --git a/secrets/create-local.sh b/secrets/create-local.sh index ffebd9c..b72600d 100755 --- a/secrets/create-local.sh +++ b/secrets/create-local.sh @@ -56,6 +56,13 @@ if [[ "${metadata_exists}" == "true" ]]; then exit 1 fi +# An old-kind store keeps its key in the old store; with buckets that key +# would land in admin-only, which this machine can't read back. A repository +# bucket is registered in cloud-setup's szecret/repo-buckets instead. +if [[ -n "${buckets}" ]]; then + die "create-local makes an old-kind store, which can't be made on a machine with buckets; make a repository bucket instead (cloud-setup docs/proposal-secret-roles.md)" +fi + # Ensure target directory exists mkdir -p "${target_dir}" diff --git a/secrets/enrol.sh b/secrets/enrol.sh new file mode 100755 index 0000000..2f1d61e --- /dev/null +++ b/secrets/enrol.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +set -euo pipefail + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat <&2 echo "szecret: this machine is already enrolled (${identity})" + keys_in "${identity_pub}" + exit 0 +fi + +mkdir -p "$(dirname "${identity}")" +chmod 700 "$(dirname "${identity}")" +tmp="$(mktemp "${identity}.XXXXXX")" +trap 'rm -f "${tmp}"' EXIT +rm -f "${tmp}" + +case "${kind}" in + file) + (umask 077 && age-keygen -o "${tmp}" 2>/dev/null) + pub="$(age-keygen -y "${tmp}")" + ;; + tpm) + exists age-plugin-tpm || die "--tpm needs age-plugin-tpm, which is not installed; no key made" + [[ -e /dev/tpmrm0 ]] || die "--tpm: no TPM here (/dev/tpmrm0); no key made" + (umask 077 && age-plugin-tpm --generate -o "${tmp}") || die "age-plugin-tpm could not make a key; no key made" + pub="$(age-plugin-tpm -y "${tmp}")" + ;; + se) + exists age-plugin-se || die "--se needs age-plugin-se, which is not installed; no key made" + (umask 077 && age-plugin-se keygen --access-control=none -o "${tmp}") \ + || die "age-plugin-se could not make a key (is there a Secure Enclave, and is the screen unlocked?); no key made" + pub="$(age-plugin-se recipients -i "${tmp}")" + ;; +esac + +[[ "${pub}" =~ ^age1[0-9a-z]+$ ]] || die "the new key's public half doesn't look like one: ${pub:-nothing}; no key made" +chmod 600 "${tmp}" +echo "${pub}" > "${identity_pub}.tmp" +mv "${identity_pub}.tmp" "${identity_pub}" +mv "${tmp}" "${identity}" +>&2 echo "szecret: enrolled; add this machine's public key to cloud-setup's szecret/hosts:" +echo "${pub}" diff --git a/secrets/ensure.sh b/secrets/ensure.sh index e11212f..49ba6a1 100755 --- a/secrets/ensure.sh +++ b/secrets/ensure.sh @@ -29,4 +29,8 @@ root_dir="$(cd "${this_dir}" && cd .. && pwd)" source "${this_dir}/common.sh" +if [[ -r "${identity}" ]]; then + exit 0 +fi +[[ -n "${store}" ]] || die "no identity on this machine (${identity}): run szecret enrol" ensure_private_key diff --git a/secrets/get-local.sh b/secrets/get-local.sh index 719a914..22f0cd9 100755 --- a/secrets/get-local.sh +++ b/secrets/get-local.sh @@ -7,7 +7,9 @@ if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then get-local: Retrieve and decrypt a secret from a localized store Decrypts and outputs a secret value from a localized store. The store name -is read from the .szecret metadata file in the file's directory. +is read from the .szecret metadata file in the file's directory. A directory +whose .szecret says kind=bucket is a repository bucket, read with this +machine's own identity. Usage: get-local @@ -61,6 +63,14 @@ else target_dir="$(dirname "${file_path}")" metadata_file="${target_dir}/.szecret" + # A repository bucket: this machine's identity, if it is a reader. + if is_bucket_dir "${target_dir}"; then + [[ -r "${file_path}" ]] || die "File '${file_path}' not found or not readable" + is_reader "${target_dir}" || die "this machine isn't one of the readers of ${target_dir}" + decrypt "${file_path}" + exit 0 + fi + if ! read_local_metadata "${metadata_file}"; then >&2 echo "ERROR: No .szecret metadata in ${target_dir}" >&2 echo "Run 'szecret create-local ${target_dir}' first" diff --git a/secrets/get.sh b/secrets/get.sh index d88b696..4b5b983 100755 --- a/secrets/get.sh +++ b/secrets/get.sh @@ -4,23 +4,17 @@ set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat < + get searches every bucket and the old store + get / reads that bucket only get -h|--help -Arguments: - secret-name Name of the secret to retrieve - -The command will: -1. Check if the secret exists in the store -2. Ensure the private key is available -3. Decrypt the secret using age and output to stdout - -Note: Exits with error if secret doesn't exist or decryption fails. +Fails, naming both, if is in two places, and says so if this machine +isn't one of the bucket's readers. EOF exit 0 fi @@ -34,13 +28,19 @@ if [[ "$#" -lt 1 ]]; then fi name="$1" -file="$(file_for "${name}")" -if ! [[ -r "${file}" ]]; then - >&2 echo "ERROR: Secret '$name' not found" - exit 1 +file="$(find_secret "${name}")" || exit 1 + +if [[ -n "${store}" && "$(dirname "${file}")" == "${store}" ]]; then + # A machine that has moved to its own identity is not asked for the old key. + if [[ -r "${identity}" && ! -r "${private_key}" ]]; then + die "${name} is in the old store, and the old key is no longer on this machine" + fi + ensure_private_key +else + dir="$(dirname "${file}")" + if ! is_reader "${dir}"; then + die "$(basename "${file}" .age) is in bucket $(bucket_label "${dir}"); this machine isn't one of its readers" + fi fi -ensure_private_key - -age_decrypt "${private_key}" "${file}" - +decrypt "${file}" diff --git a/secrets/has.sh b/secrets/has.sh new file mode 100755 index 0000000..6748754 --- /dev/null +++ b/secrets/has.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +set -euo pipefail + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat < exists and this machine is one of its bucket's readers (in +the old store: if the old key is here). Decrypts nothing and prints nothing. + +Usage: + has + has / + has -h|--help +EOF + exit 0 +fi + +this_dir="$(cd "$(dirname "$0")" && pwd)" +szecret_quiet=1 +source "${this_dir}/common.sh" + +[[ "$#" -ge 1 ]] || exit 1 + +file="$(find_secret "$1" 2>/dev/null)" || exit 1 + +if [[ -n "${store}" && "$(dirname "${file}")" == "${store}" ]]; then + [[ -n "${private_key}" && -r "${private_key}" ]] +else + is_reader "$(dirname "${file}")" +fi diff --git a/secrets/list.sh b/secrets/list.sh index bf087e3..39b17ff 100755 --- a/secrets/list.sh +++ b/secrets/list.sh @@ -4,20 +4,17 @@ set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat </, one a line + list --all every bucket, read here or not list -h|--help -The command will: -1. List all .age files in the store directory -2. Remove the .age extension from filenames -3. Display the secret names in columns - -Note: Only shows secret names, not their values. +Only names, never values. The old store counts as readable while the old key +is here. EOF exit 0 fi @@ -25,12 +22,62 @@ fi this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)" source "${this_dir}/common.sh" -maybe_column() { - if [ -t 1 ]; then - column - else - cat +all="" +names_only="" +while [[ $# -gt 0 ]]; do + case "$1" in + --all) all=1 ;; + --names) names_only=1 ;; + *) die "usage: szecret list [--names] [--all]" ;; + esac + shift +done + +secrets_in() { + local f + for f in "$1"/*.age; do + if [[ -e "${f}" ]]; then + basename "${f}" .age + fi + done | LC_ALL=C sort +} + +# Each bucket shown, then the old store, as "