From 8440c6816f833e9848bbfd976902af46f49d6ff3 Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Fri, 20 Nov 2026 10:51:00 +0000 Subject: szecret list: a terminal view worth reading, and --json At a terminal, list shows each bucket this machine reads: its readers by name (hosts from recipients, admins from a "# " line above their key in SZECRET_ADMINS_COMMAND's output, "the admins" once per bucket), its secrets in columns fitted to the width, any problem the file names and headers show with the command that fixes it, and a summary. No key is shown without --keys or --json. --all groups the buckets not read here after the rest. It reads every bucket in one awk pass, never decrypting. Piped output and --names, which scripts read, are unchanged. --- secrets/README.md | 15 +- secrets/common.sh | 27 ++- secrets/list.sh | 459 +++++++++++++++++++++++++++++++++++++++++++++++++-- secrets/test/list.sh | 141 ++++++++++++++++ 4 files changed, 620 insertions(+), 22 deletions(-) diff --git a/secrets/README.md b/secrets/README.md index a6ae932..5e98f3f 100644 --- a/secrets/README.md +++ b/secrets/README.md @@ -26,7 +26,7 @@ szecret list | `mv ` | Move a secret to another bucket; never copies | | `mv --phone ` | The same, for a secret this machine can't read: an admin elsewhere (`SZECRET_PHONE_COMMAND`) rewraps its file key | | `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`) | +| `list` | The secrets this machine reads, by bucket, with who reads each and anything wrong (`--all`, `--keys`, `--json`, `--names`) | | `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 | @@ -59,6 +59,16 @@ buckets// `~/.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. +- **`list`** at a terminal shows each bucket this machine reads: its + readers by name, its secrets in columns, any problem its file names and + headers show (a pending rekey, a file that isn't age, one encrypted to a + different number of keys) with the command that fixes it, and a summary. + Readers are named by the comment above their key in `recipients`; admins by + a `# ` line directly above their key in `SZECRET_ADMINS_COMMAND`'s + output (older szecret ignores such lines). No key is shown without + `--keys` or `--json`. Piped, it prints bare names one a line, and + `--names` prints `/`: both are read by scripts and stay as + they are. - **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 @@ -174,7 +184,8 @@ For testing or custom setups, these environment variables override default paths | `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) | +| `SZECRET_ADMINS_COMMAND` | Prints the admins' public keys, one a line, each optionally named by a `# ` line above it | (none: nothing is encrypted to a bucket) | +| `SZECRET_FORCE_TTY` | `list` prints its terminal view even when piped; a number sets the width | (none) | * 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/common.sh b/secrets/common.sh index 13fe749..adf5488 100644 --- a/secrets/common.sh +++ b/secrets/common.sh @@ -380,17 +380,36 @@ bucket_label() { fi } -# admin_keys: the admins, one a line. With none it fails and says why. -admin_keys() { - local out +# admins_output: what SZECRET_ADMINS_COMMAND prints. With no command, or a +# failed one, it fails and says why. +admins_output() { 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 +} + +# admin_names : " " for each admin key a "# " +# comment line names, the last comment above it as in a recipients file. Only +# list uses the names; encryption reads the key lines alone. +admin_names() { + awk '/^[[:space:]]*#/ { sub(/^[[:space:]]*#[[:space:]]*/, ""); name = $0; next } + /^age1[0-9a-z]+$/ { if (name != "") print $1, name; name = "" }' <<<"$1" +} + +# admin_keys []: the admins, one a line. With none it fails and +# says why. +admin_keys() { + local out + if [[ $# -gt 0 ]]; then + out="$1" + else + out="$(admins_output)" || 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" diff --git a/secrets/list.sh b/secrets/list.sh index 39b17ff..9037071 100755 --- a/secrets/list.sh +++ b/secrets/list.sh @@ -7,14 +7,24 @@ if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then list: List the secrets this machine can read Usage: - list at a terminal: each bucket this machine reads, with its - readers and names; otherwise bare names, one a line - list --names /, one a line + list each bucket this machine reads: its readers, its secrets + and anything wrong with it, then a summary list --all every bucket, read here or not + list --keys each reader's age public key too + list --json the same as one JSON document, keys included, for scripts + list --names /, one a line list -h|--help -Only names, never values. The old store counts as readable while the old key +Only names, never values; nothing is decrypted. Piped, plain "list" prints +bare names, one a line, sorted (SZECRET_FORCE_TTY=1, or =, gives the +terminal view instead). The old store counts as readable while the old key is here. + +Readers are named by the comment above their key in the bucket's recipients, +and admins by a "# " line above theirs in SZECRET_ADMINS_COMMAND's +output; a key with neither is "unnamed". The problems shown come from file +names and headers alone: szecret check decrypts every file to be sure. +Colour follows the terminal, and NO_COLOR turns it off. EOF exit 0 fi @@ -24,14 +34,21 @@ source "${this_dir}/common.sh" all="" names_only="" +json="" +keys="" while [[ $# -gt 0 ]]; do case "$1" in --all) all=1 ;; --names) names_only=1 ;; - *) die "usage: szecret list [--names] [--all]" ;; + --json) json=1 ;; + --keys) keys=1 ;; + *) die "usage: szecret list [--all] [--keys | --json | --names]" ;; esac shift done +if [[ -n "${names_only}" && -n "${json}" ]]; then + die "--names and --json are two different outputs: pick one" +fi secrets_in() { local f @@ -59,25 +76,435 @@ shown() { fi } +# Scripts read these two, so they stay as they were: --names, and plain list +# piped. 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 + exit 0 +fi +if [[ -z "${json}" && ! -t 1 && -z "${SZECRET_FORCE_TTY:-}" ]]; 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)" + exit 0 +fi + +# --- The terminal view, and --json -------------------------------------------- +# +# One awk reads every bucket at once: its recipients, its .szecret and the +# header of each .age file (up to the "---" line, never the payload). Before +# each bucket's files it reads a marker file, with the bucket's label and +# its secrets' names set as awk variables just before it. + +LC_ALL=C +export LC_ALL + +admins_known=1 +admins_out="$(admins_output 2>/dev/null)" || admins_known="" +admins="" +admin_named="" +if [[ -n "${admins_known}" ]]; then + admins="$(admin_keys "${admins_out}" 2>/dev/null)" || admins_known="" + admin_named="$(admin_names "${admins_out}")" +fi + +marker="$(mktemp "${TMPDIR:-/tmp}/szecret-list.XXXXXX")" +trap 'rm -f "${marker}"' EXIT +echo "bucket" > "${marker}" + +args=() +# add_bucket