diff options
Diffstat (limited to 'secrets')
| -rw-r--r-- | secrets/README.md | 78 | ||||
| -rwxr-xr-x | secrets/check.sh | 103 | ||||
| -rw-r--r-- | secrets/common.sh | 339 | ||||
| -rwxr-xr-x | secrets/create-local.sh | 7 | ||||
| -rwxr-xr-x | secrets/enrol.sh | 75 | ||||
| -rwxr-xr-x | secrets/ensure.sh | 4 | ||||
| -rwxr-xr-x | secrets/get-local.sh | 12 | ||||
| -rwxr-xr-x | secrets/get.sh | 40 | ||||
| -rwxr-xr-x | secrets/has.sh | 32 | ||||
| -rwxr-xr-x | secrets/list.sh | 79 | ||||
| -rwxr-xr-x | secrets/mv.sh | 51 | ||||
| -rwxr-xr-x | secrets/put-local.sh | 12 | ||||
| -rwxr-xr-x | secrets/put.sh | 63 | ||||
| -rwxr-xr-x | secrets/ready.sh | 13 | ||||
| -rwxr-xr-x | secrets/rekey.sh | 142 | ||||
| -rwxr-xr-x | secrets/rotate.sh | 119 | ||||
| -rw-r--r-- | secrets/test/README.md | 1 | ||||
| -rwxr-xr-x | secrets/test/buckets.sh | 274 | ||||
| -rwxr-xr-x | secrets/test/common.sh | 2 | ||||
| -rwxr-xr-x | secrets/test/rotate.sh | 69 |
20 files changed, 1216 insertions, 299 deletions
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 <name>` | Retrieve and decrypt a secret | -| `put <name>` | 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 <name> <dir>` | Create a localized secret store | -| `put-local <file>` | Encrypt to a local store | -| `get-local <file>` | Decrypt from a local store | +| `get <name>`, `get <bucket>/<name>` | Decrypt a secret. A plain name searches every bucket and the old store | +| `has <name>` | Succeed quietly if this machine can read it; decrypts nothing | +| `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 | +| `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 | +| `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 <name> <dir>` | Create a localized secret store (the old kind) | +| `put-local <file>` | Encrypt to a local store or a repository bucket | +| `get-local <file>` | 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/<bucket>/ + 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 + <name>.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 `<path>.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 <<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" 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 <<EOF + >&2 cat <<MSG ERROR: no szecret store on this machine. -Looked for ${szecret_data_dir}/store and ${szecret_data_dir}/id.pub. -Link them to an existing store, point SZECRET_STORE and SZECRET_PUBLIC_KEY at -one, or start a new one: - - age-keygen -o private - pubkey="\$(age-keygen -y private)" - mkdir -p ${szecret_data_dir}/store - echo "\${pubkey}" > ${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: <name>.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=<every key its files were last encrypted to, sorted> +# name=<key> <who> 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 <file>: 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 <dir>: 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 <dir>: 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}" </dev/null)"; then + >&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 <dir> <admins>: what the bucket's files should be encrypted to. +wanted_set() { + { keys_in "$1/recipients"; echo "$2"; } | grep -v '^$' | sorted +} + +# key_names <dir>: "<key> <name>" 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 <key> <key names> <admins>: 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 <dir>: 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 <key>: 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 <file>: 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 <out> <keys>: stdin, encrypted to each key (one a line), in +# place at <out> 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 <dir>: 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 <name>: 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 <file>: "<bucket>/<name>", or "<name> 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 <name or bucket/name>: 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 <<EOF +enrol: Make this machine's identity, and print its public key + +The private key is made here and never leaves: in the TPM (--tpm), in the +Secure Enclave (--se), or in a file readable only by you. Its public key goes +in identity.pub beside it, and is printed for cloud-setup's szecret/hosts. + +Usage: + enrol [--tpm|--se] + enrol -h|--help + +Run again, it prints the same key and makes no second one. The old key, if +it is here, stays beside it until it is deleted. +EOF + exit 0 +fi + +this_dir="$(cd "$(dirname "$0")" && pwd)" +szecret_no_store_ok=1 +source "${this_dir}/common.sh" + +kind=file +case "${1:-}" in + "") ;; + --tpm) kind=tpm ;; + --se) kind=se ;; + *) die "usage: szecret enrol [--tpm|--se]" ;; +esac + +if [[ -e "${identity}" ]]; then + if [[ ! -s "${identity_pub}" ]]; then + die "${identity} exists, but ${identity_pub} is missing or empty: write its public key there" + fi + >&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 <file-path> @@ -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 <store-name> ${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 <<EOF -get: Retrieve and decrypt a secret from the store +get: Retrieve and decrypt a secret -Decrypts and outputs a secret value from the global secret store. +Decrypts a secret and writes it to stdout. Usage: - get <secret-name> + get <name> searches every bucket and the old store + get <bucket>/<name> 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 <name> 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 <<EOF +has: Check, quietly, whether this machine can read a secret + +Succeeds if <name> 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 <name> + has <bucket>/<name> + 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 <<EOF -list: List all available secrets in the store - -Displays all secret names in the global store in a columnar format. +list: List the secrets this machine can read Usage: - list + list at a terminal: each bucket this machine reads, with its + readers and names; otherwise bare names, one a line + list --names <bucket>/<name>, 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 "<label>\t<dir>". +shown() { + local d + if [[ -n "${buckets}" ]]; then + for d in "${buckets}"/*/; do + d="${d%/}" + [[ -d "${d}" ]] || continue + if [[ -n "${all}" ]] || is_reader "${d}"; then + printf '%s\t%s\n' "$(basename "${d}")" "${d}" + fi + done + fi + if [[ -n "${store}" ]] && { [[ -n "${all}" ]] || [[ -n "${private_key}" && -r "${private_key}" ]]; }; then + printf '%s\t%s\n' "(old store)" "${store}" fi } -ls "${store}" | sed 's/\.age$//' | sort | maybe_column +if [[ -n "${names_only}" ]]; then + shown | while IFS=$'\t' read -r label d; do + secrets_in "${d}" | sed "s|^|${label}/|" + done +elif [[ ! -t 1 ]]; then + shown | while IFS=$'\t' read -r label d; do + secrets_in "${d}" + done | LC_ALL=C sort +else + admins="$(admin_keys 2>/dev/null || true)" + shown | while IFS=$'\t' read -r label d; do + if [[ "${label}" == "(old store)" ]]; then + echo "the old store (the old key)" + else + readers="$(encrypted_to "${d}" | while read -r k; do name_of "${k}" "$(key_names "${d}")" "${admins}"; done | sort -u | paste -sd ',' - | sed 's/,/, /g')" + [[ -n "${readers}" ]] || readers="never keyed" + echo "${label} (${readers})" + fi + secrets_in "${d}" | column | sed 's/^/ /' + echo + done +fi diff --git a/secrets/mv.sh b/secrets/mv.sh new file mode 100755 index 0000000..d249ac1 --- /dev/null +++ b/secrets/mv.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +set -euo pipefail + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat <<EOF +mv: Move a secret to another bucket + +Decrypts it, encrypts it to the bucket's readers and the admins, and deletes +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 -h|--help + +Needs an identity that can read it now. The plaintext goes through a pipe, +never a file. +EOF + exit 0 +fi + +this_dir="$(cd "$(dirname "$0")" && pwd)" +source "${this_dir}/common.sh" + +[[ "$#" -eq 2 ]] || die "usage: szecret mv <name> <bucket>" +[[ -n "${buckets}" ]] || die "no buckets on this machine" +target="$2" +[[ "${target}" =~ ${re_bucket} ]] || die "not a bucket's name: ${target}" + +from="$(find_secret "$1")" || exit 1 +name="$(basename "${from}" .age)" +dir="${buckets}/${target}" +to="${dir}/${name}.age" +[[ "${from}" != "${to}" ]] || die "${name} is already in ${target}" +# 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 + ensure_private_key +fi + +set="$(bucket_set "${dir}")" || exit 1 +age_version_ok +load_identities +if ! age --decrypt "${ids[@]}" "${from}" | encrypt_to "${to}" "${set}"; then + rm -f "${to}" + die "could not move ${name}; $(where_is "${from}") is as it was" +fi +rm "${from}" +>&2 echo "szecret: moved $(where_is "${to}"), from $(where_is "${from}")" diff --git a/secrets/put-local.sh b/secrets/put-local.sh index cba71c6..5510df6 100755 --- a/secrets/put-local.sh +++ b/secrets/put-local.sh @@ -7,7 +7,9 @@ if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then put-local: Store a secret in a localized store Encrypts data from stdin and stores it in 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. In a +repository bucket (kind=bucket), it is encrypted to the bucket's readers and +the admins. Usage: put-local <file-path> @@ -42,6 +44,14 @@ file_path="$1" target_dir="$(dirname "${file_path}")" metadata_file="${target_dir}/.szecret" +# A repository bucket: its readers and the admins, as put does. +if is_bucket_dir "${target_dir}"; then + set="$(bucket_set "${target_dir}")" || exit 1 + age_version_ok + encrypt_to "${file_path}" "${set}" + exit 0 +fi + # Read store name and pubkey from metadata if ! read_local_metadata "${metadata_file}"; then >&2 echo "ERROR: No .szecret metadata in ${target_dir}" diff --git a/secrets/put.sh b/secrets/put.sh index 2675066..b060255 100755 --- a/secrets/put.sh +++ b/secrets/put.sh @@ -4,24 +4,20 @@ set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat <<EOF -put: Store a secret in the encrypted store +put: Store a secret, read from stdin -Encrypts data from stdin and stores it as a named secret in the global store. +Encrypts stdin to the bucket's readers and the admins. Needs only public keys. Usage: - put <secret-name> - echo "secret-value" | put <secret-name> + echo "value" | put <bucket>/<name> + echo "value" | put <name> the admin-only bucket, the narrowest set put -h|--help -Arguments: - secret-name Name to store the secret under - -The command will: -1. Read data from stdin -2. Encrypt the data using age with the public key -3. Store the encrypted data in the store as <secret-name>.age - -Note: Will overwrite existing secrets with the same name. +Overwrites <name> where it already is. Refuses if <name> is in another bucket +(it moves with mv, never copies), if the bucket's readers or the admins changed +since its last rekey, or if there are no admins. A <name> still in the old +store is rewritten there until it moves. On a machine with no buckets, <name> +goes to the old store, as before buckets. EOF exit 0 fi @@ -36,5 +32,44 @@ fi name="$1" ensure_age -age_encrypt "${public_key}" "$(file_for "${name}")" +# Before buckets: the old store, as it always was. +if [[ -z "${buckets}" ]]; then + [[ "${name}" != */* ]] || die "no buckets on this machine: ${name}" + age_encrypt "${public_key}" "$(file_for "${name}")" + exit 0 +fi + +if [[ "${name}" == */* ]]; then + bucket="${name%%/*}" + name="${name#*/}" + [[ "${bucket}" =~ ${re_bucket} ]] || die "not a bucket's name: ${bucket}" +else + bucket="" +fi +[[ "${name}" =~ ${re_secret} ]] || die "not a secret's name: ${name}" + +found="$(locate "${name}")" +if [[ -z "${bucket}" ]]; then + if [[ -n "${store}" && "${found}" == "${store}/${name}.age" ]]; then + >&2 echo "szecret: ${name} is still in the old store; rewriting it there" + age_encrypt "${public_key}" "${found}" + exit 0 + fi + if [[ -z "${found}" ]]; then + bucket="admin-only" + >&2 echo "szecret: storing ${name} in admin-only (put <bucket>/${name} for another bucket)" + fi +fi +while read -r f; do + [[ -n "${f}" && "${f}" != "${buckets}/${bucket}/${name}.age" ]] || continue + if [[ -n "${store}" && "$(dirname "${f}")" == "${store}" ]]; then + die "${name} is in the old store; szecret mv ${name} ${bucket:-<bucket>} moves it" + fi + die "${name} is in $(bucket_label "$(dirname "${f}")"); use $(where_is "${f}")" +done <<<"${found}" + +dir="${buckets}/${bucket}" +set="$(bucket_set "${dir}")" || exit 1 +age_version_ok +encrypt_to "${dir}/${name}.age" "${set}" diff --git a/secrets/ready.sh b/secrets/ready.sh index dee2f86..2760ec4 100755 --- a/secrets/ready.sh +++ b/secrets/ready.sh @@ -6,15 +6,16 @@ if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat <<EOF ready: Check if the secret management system is ready for use -Tests whether the age private key is properly configured and available -for secret operations. Exits with status 0 if ready, non-zero if not. +Ready means a store (buckets, or the old store) and an identity to read it +with: this machine's own (szecret enrol), or the old key. Exits with status 0 +if ready, non-zero if not. Usage: ready ready -h|--help The command will: -1. Check if the private key exists and is readable +1. Check for this machine's identity, or the old private key 2. Perform any necessary legacy key migration 3. Exit with appropriate status code @@ -27,5 +28,7 @@ this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)" szecret_quiet=1 source "${this_dir}/common.sh" -private_key_is_setup - +if [[ -r "${identity}" ]]; then + exit 0 +fi +[[ -n "${store}" ]] && private_key_is_setup diff --git a/secrets/rekey.sh b/secrets/rekey.sh new file mode 100755 index 0000000..58256bd --- /dev/null +++ b/secrets/rekey.sh @@ -0,0 +1,142 @@ +#!/bin/bash + +set -euo pipefail + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat <<EOF +rekey: Re-encrypt a bucket to its readers now + +Shows the bucket's new readers, marking who is added (+) and removed (-) +since its last rekey, then encrypts every file again to its recipients plus +the admins, and records that set in its .szecret. + +Usage: + rekey [--add-ok] [-n] <bucket>|<directory> (a directory: a path, with a /) + rekey -h|--help + +A repository bucket is named by its path, which must contain a / (./keys). + +Options: + --add-ok add readers without asking (needed with no terminal) + -n show the change, and change nothing + +Every file is built and checked in a directory beside the bucket before any +is swapped in: a file it can't decrypt stops it with nothing changed. Adding a +reader lets it read every secret in the bucket, so with files in the bucket +that needs a yes at a terminal, or --add-ok. +EOF + exit 0 +fi + +this_dir="$(cd "$(dirname "$0")" && pwd)" +source "${this_dir}/common.sh" + +add_ok="" +dry="" +arg="" +while [[ $# -gt 0 ]]; do + case "$1" in + --add-ok) add_ok=1 ;; + -n) dry=1 ;; + -*) die "usage: szecret rekey [--add-ok] [-n] <bucket>|<directory>" ;; + *) [[ -z "${arg}" ]] || die "one bucket at a time"; arg="$1" ;; + esac + shift +done +[[ -n "${arg}" ]] || die "usage: szecret rekey [--add-ok] [-n] <bucket>|<directory>" + +if [[ "${arg}" == */* ]]; then + dir="${arg%/}" +else + [[ "${arg}" =~ ${re_bucket} ]] || die "not a bucket's name: ${arg}" + [[ -n "${buckets}" ]] || die "no buckets on this machine" + dir="${buckets}/${arg}" +fi +label="$(bucket_label "${dir}")" +[[ -d "${dir}" ]] || die "no bucket ${label}" +[[ -r "${dir}/recipients" ]] || die "bucket ${label} has no recipients file (cloud-setup's bin/szecret-buckets writes it)" +if [[ -e "${dir}/.szecret" ]] && ! is_bucket_dir "${dir}"; then + die "${dir}/.szecret is not a bucket's: an old-style repository store moves by hand" +fi + +admins="$(admin_keys)" || exit 1 +want="$(wanted_set "${dir}" "${admins}")" +have="$(encrypted_to "${dir}")" +names="$(key_names "${dir}")" + +files=() +for f in "${dir}"/*.age; do + if [[ -e "${f}" ]]; then + files+=("${f}") + fi +done + +added="" +echo "${label}: ${#files[@]} secret(s), readers:" +while read -r k; do + [[ -n "${k}" ]] || continue + if grep -qxF "${k}" <<<"${want}"; then + if grep -qxF "${k}" <<<"${have}"; then + mark=" " + else + mark="+" + added=1 + fi + else + mark="-" + fi + printf ' %s %s\n' "${mark}" "$(name_of "${k}" "${names}" "${admins}")" +done < <({ echo "${want}"; echo "${have}"; } | grep -v '^$' | sorted) + +if [[ "${want}" == "${have}" ]]; then + echo "${label}: already encrypted to these readers" + exit 0 +fi +if [[ -n "${dry}" ]]; then + echo "${label}: needs a rekey (-n: nothing changed)" + exit 0 +fi + +if [[ -n "${added}" && ${#files[@]} -gt 0 && -z "${add_ok}" ]]; then + if [[ ! -t 0 || ! -t 1 ]]; then + die "rekey adds readers to ${label}, who could then read all of it: run it at a terminal, or pass --add-ok" + fi + yes="" + read -r -p "The readers marked + can then read every secret in ${label}. Go ahead? [y/N] " yes || true + [[ "${yes}" == y || "${yes}" == Y ]] || die "not rekeyed" +fi + +age_version_ok +load_identities +work="$(mktemp -d "$(dirname "${dir}")/.rekey-$(basename "${dir}").XXXXXX")" +trap 'rm -rf "${work}"' EXIT + +can_check="" +while read -r k; do + if [[ -n "${k}" ]] && grep -qxF "${k}" <<<"${want}"; then + can_check=1 + fi +done <<<"$(my_keys)" + +for f in ${files[@]+"${files[@]}"}; do + n="$(basename "${f}")" + if ! age --decrypt "${ids[@]}" "${f}" | encrypt_to "${work}/${n}" "${want}"; then + die "cannot re-encrypt ${label}/${n%.age}; nothing changed" + fi + # Readable by this machine afterwards: the new file must say what the old one did. + if [[ -n "${can_check}" ]] && ! cmp -s <(age --decrypt "${ids[@]}" "${f}") <(age --decrypt "${ids[@]}" "${work}/${n}"); then + die "${label}/${n%.age} did not survive re-encryption; nothing changed" + fi +done + +for f in ${files[@]+"${files[@]}"}; do + mv "${work}/$(basename "${f}")" "${f}" +done +{ + printf 'kind=bucket\nencrypted-to=%s\n' "$(echo "${want}" | paste -sd ' ' -)" + while read -r k; do + echo "name=${k} $(name_of "${k}" "${names}" "${admins}")" + done <<<"${want}" +} > "${work}/.szecret" +mv "${work}/.szecret" "${dir}/.szecret" +echo "${label}: rekeyed" diff --git a/secrets/rotate.sh b/secrets/rotate.sh deleted file mode 100755 index 4286f4c..0000000 --- a/secrets/rotate.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then - cat <<EOF -rotate: Generate a new age key and re-encrypt the global secret store - -Creates a new age keypair, re-encrypts every secret in the global store with -the new public key, updates the public key file, and stores the new -private key in the key directory. - -Usage: - rotate - rotate --yes - rotate -h|--help - -Options: - --yes Skip interactive confirmation - -The command will: -1. Confirm the operation (unless --yes) -2. Decrypt each secret in the store using the current private key -3. Generate a new age keypair -4. Re-encrypt all secrets with the new public key -5. Update the public key file and install the new private key - -Note: Interactive command. Commit the updated store files and public key - wherever the store is tracked, - and update the private key backup in 1Password when complete. -EOF - exit 0 -fi - -this_dir="$(cd "$(dirname "$0")" && pwd)" -source "${this_dir}/common.sh" - -assume_yes=false -if [[ "${1:-}" == "--yes" ]]; then - assume_yes=true -elif [[ -n "${1:-}" ]]; then - >&2 echo "ERROR: Unknown argument: $1" - exit 1 -fi - -ensure_age -ensure_private_key - -old_private_key="${private_key}" -old_public_key="${key_id}" - -shopt -s nullglob -store_files=("${store}"/*.age) -shopt -u nullglob -secret_count="${#store_files[@]}" - -if [[ "${assume_yes}" != "true" ]]; then - if [[ ! -t 1 ]]; then - >&2 echo "ERROR: rotate requires interactive confirmation (use --yes to skip)" - exit 1 - fi - >&2 echo "This will generate a new age key and re-encrypt ${secret_count} secret(s)." - >&2 echo "Current public key: ${old_public_key}" - >&2 printf "Continue? [y/N] " - local_confirm="" - read -r local_confirm </dev/tty - if [[ "${local_confirm}" != [yY] ]]; then - >&2 echo "Aborted." - exit 1 - fi -fi - -staging="$(mktemp -d "${TMPDIR:-/tmp}/szecret-rotate.XXXXXX")" -temp_private_key="$(mktemp -u "${TMPDIR:-/tmp}/szecret-rotate-key.XXXXXX")" -temp_public_key="$(mktemp "${TMPDIR:-/tmp}/szecret-rotate-pub.XXXXXX")" - -cleanup() { - rm -rf "${staging}" - rm -f "${temp_private_key}" "${temp_public_key}" -} -trap cleanup EXIT - -age-keygen -o "${temp_private_key}" 2>/dev/null -new_public_key="$(age-keygen -y "${temp_private_key}")" -echo "${new_public_key}" > "${temp_public_key}" - -for secret_file in "${store_files[@]}"; do - secret_name="$(basename "${secret_file}" .age)" - age_decrypt "${old_private_key}" "${secret_file}" \ - | age_encrypt "${temp_public_key}" "${staging}/${secret_name}.age" -done - -for secret_file in "${store_files[@]}"; do - secret_name="$(basename "${secret_file}" .age)" - mv "${staging}/${secret_name}.age" "${secret_file}" -done - -echo "${new_public_key}" > "${public_key}" - -installed_private_key="${private_key_dir}/${new_public_key}" -mv "${temp_private_key}" "${installed_private_key}" -temp_private_key="" -chmod 600 "${installed_private_key}" 2>/dev/null || true - -cat <<EOF - -Rotation complete. - - Old public key: ${old_public_key} - New public key: ${new_public_key} - Secrets re-encrypted: ${secret_count} - New private key: ${installed_private_key} - -Next steps: - 1. Commit the updated store and $(basename "${public_key}") - 2. Update the szecret private key in 1Password - -The old private key remains at ${old_private_key} until you remove it. -EOF diff --git a/secrets/test/README.md b/secrets/test/README.md index 49d70b2..7317d50 100644 --- a/secrets/test/README.md +++ b/secrets/test/README.md @@ -21,6 +21,7 @@ Unit tests for the szecret secret management system. test/ ├── common.sh # Shared utilities, setup/teardown, assertions ├── run.sh # Test runner entrypoint +├── buckets.sh # Tests for buckets: get, has, put, mv, rekey, list, check, enrol ├── create-local.sh # Tests for create-local command ├── edge-cases.sh # Edge case tests (special chars, unicode, etc.) ├── ensure.sh # Tests for ensure command diff --git a/secrets/test/buckets.sh b/secrets/test/buckets.sh new file mode 100755 index 0000000..b021c3c --- /dev/null +++ b/secrets/test/buckets.sh @@ -0,0 +1,274 @@ +#!/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> <"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" +} + +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_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_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_check_stanza_types +test_check_missing_plugin +test_ready diff --git a/secrets/test/common.sh b/secrets/test/common.sh index 33d7d78..a751f65 100755 --- a/secrets/test/common.sh +++ b/secrets/test/common.sh @@ -18,6 +18,8 @@ skip() { echo -e "${YELLOW}SKIP${NC}: $1"; } # Create temporary test environment setup() { export TEST_DIR="$(mktemp -d)" + # Never the real data dir: the buckets and identity there are a host's own. + export XDG_DATA_HOME="${TEST_DIR}/xdg" export SZECRET_STORE="${TEST_DIR}/store" export SZECRET_PUBLIC_KEY="${TEST_DIR}/.id.pub" export SZECRET_PRIVATE_KEY="${TEST_DIR}/.id" diff --git a/secrets/test/rotate.sh b/secrets/test/rotate.sh deleted file mode 100755 index 02abe39..0000000 --- a/secrets/test/rotate.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash -# Tests for rotate command - -this_dir="$(cd "$(dirname "$0")" && pwd)" -source "${this_dir}/common.sh" - -setup_key_dir_layout() { - local keys_dir="${TEST_DIR}/keys" - mkdir -p "${keys_dir}" "${SZECRET_STORE}" - rm -f "${SZECRET_STORE}"/*.age - unset SZECRET_PRIVATE_KEY - export SZECRET_PRIVATE_KEY_DIR="${keys_dir}" - - local temp_key="${TEST_DIR}/initial-key" - age-keygen -o "${temp_key}" 2>/dev/null - local pub - pub="$(age-keygen -y "${temp_key}")" - echo "${pub}" > "${SZECRET_PUBLIC_KEY}" - mv "${temp_key}" "${keys_dir}/${pub}" - chmod 600 "${keys_dir}/${pub}" - SETUP_PUB="${pub}" -} - -test_rotate_reencrypts_secrets() { - setup_key_dir_layout - local old_pub="${SETUP_PUB}" - - echo "rotate-me" | szecret put "rotate-test" - echo "other" | szecret put "rotate-other" - - szecret rotate --yes - - local new_pub - new_pub="$(cat "${SZECRET_PUBLIC_KEY}")" - if [[ "${new_pub}" == "${old_pub}" ]]; then - fail "public key should change after rotate" - fi - - assert_equals "rotate-me" "$(szecret get "rotate-test")" "rotated secret" - assert_equals "other" "$(szecret get "rotate-other")" "other rotated secret" - assert_file_exists "${SZECRET_PRIVATE_KEY_DIR}/${new_pub}" "new private key file" - pass "rotate re-encrypts secrets and updates keys" -} - -test_rotate_empty_store() { - setup_key_dir_layout >/dev/null - - szecret rotate --yes - - local new_pub - new_pub="$(cat "${SZECRET_PUBLIC_KEY}")" - assert_file_exists "${SZECRET_PRIVATE_KEY_DIR}/${new_pub}" "new private key with empty store" - pass "rotate succeeds with empty store" -} - -test_rotate_requires_confirmation() { - setup_key_dir_layout >/dev/null - - assert_failure szecret rotate - pass "rotate fails without confirmation" -} - -run_tests() { - test_rotate_reencrypts_secrets - test_rotate_empty_store - test_rotate_requires_confirmation -} - -[[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests |
