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/list.sh | 459 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 443 insertions(+), 16 deletions(-) (limited to 'secrets/list.sh') 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