summaryrefslogtreecommitdiff
path: root/secrets
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-20 10:51:00 +0000
committerIgor Soarez <igor@soarez.org>2026-11-20 10:57:22 +0000
commit8440c6816f833e9848bbfd976902af46f49d6ff3 (patch)
tree2f44413e84251bde824b7c6c31ea46b1f9def446 /secrets
parentfcf5c24b66a013672a6d9dcac7ab09b813b4f5c7 (diff)
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 "# <name>" 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.
Diffstat (limited to 'secrets')
-rw-r--r--secrets/README.md15
-rw-r--r--secrets/common.sh27
-rwxr-xr-xsecrets/list.sh459
-rwxr-xr-xsecrets/test/list.sh141
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 <name> <bucket>` | Move a secret to another bucket; never copies |
| `mv --phone <name> <bucket>` | The same, for a secret this machine can't read: an admin elsewhere (`SZECRET_PHONE_COMMAND`) rewraps its file key |
| `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`) |
+| `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/<bucket>/
`~/.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 `# <name>` 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 `<bucket>/<name>`: 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 `<path>.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 `# <name>` 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}" </dev/null)"; then
+ if ! 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
+}
+
+# admin_names <admins output>: "<key> <name>" for each admin key a "# <name>"
+# 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 [<admins output>]: 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 <bucket>/<name>, 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 <bucket>/<name>, 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 =<columns>, 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 "# <name>" 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 <label> <dir> <old>: its marker, then each file awk reads.
+add_bucket() {
+ local label="$1" d="$2" old="$3" f n names="" unread="" files=()
+ for f in "${d}"/*.age; do
+ [[ -e "${f}" ]] || continue
+ n="$(basename "${f}" .age)"
+ names="${names} ${n}"
+ if [[ -r "${f}" && -f "${f}" ]]; then
+ files+=("${f}")
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})"
+ unread="${unread} ${n}"
fi
- secrets_in "${d}" | column | sed 's/^/ /'
- echo
+ done
+ args+=("label=${label}" "old=${old}" "names=${names}" "unread=${unread}" "${marker}")
+ if [[ "${old}" == 0 ]]; then
+ [[ ! -r "${d}/recipients" ]] || args+=("${d}/recipients")
+ [[ ! -r "${d}/.szecret" ]] || args+=("${d}/.szecret")
+ fi
+ args+=(${files[@]+"${files[@]}"})
+}
+
+total=0
+if [[ -n "${buckets}" ]]; then
+ for d in "${buckets}"/*/; do
+ d="${d%/}"
+ [[ -d "${d}" ]] || continue
+ add_bucket "$(basename "${d}")" "${d}" 0
+ total=$((total + 1))
done
fi
+old_readable=0
+if [[ -n "${store}" ]]; then
+ if [[ -n "${private_key}" && -r "${private_key}" ]]; then
+ old_readable=1
+ fi
+ add_bucket "(old store)" "${store}" 1
+fi
+
+width="${SZECRET_FORCE_TTY:-}"
+if ! [[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]]; then
+ width=""
+ if [[ -t 1 ]]; then
+ width="$(tput cols 2>/dev/null || true)"
+ fi
+ if ! [[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]]; then
+ width="${COLUMNS:-80}"
+ fi
+fi
+[[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]] || width=80
+
+color=""
+if [[ -t 1 && -z "${NO_COLOR:-}" && "${TERM:-dumb}" != dumb ]]; then
+ color=1
+fi
+
+identity_key=""
+if [[ -r "${identity_pub}" ]]; then
+ identity_key="$(keys_in "${identity_pub}" | head -1)"
+fi
+has_identity=""
+if [[ -n "${identity_key}" || ${old_readable} == 1 ]]; then
+ has_identity=1
+fi
+
+# read, not $(cat <<...): bash 3.2 misreads quotes in a heredoc inside $().
+IFS= read -r -d '' prog <<'AWK' || true
+function short(k) { return substr(k, 1, 12) "..." }
+function jstr(s) { gsub(/\\/, "\\\\", s); gsub(/"/, "\\\"", s); gsub(/\t/, "\\t", s); return "\"" s "\"" }
+function c(code, s) { return color ? "\033[" code "m" s "\033[0m" : s }
+function bold(s) { return c("1", s) }
+function dim(s) { return c("2", s) }
+function warn(s) { return c("33", s) }
+function plural(n, one, many) { return n " " (n == 1 ? one : many) }
+function isort(arr, n, i, j, t) {
+ for (i = 2; i <= n; i++) {
+ t = arr[i]
+ for (j = i - 1; j > 0 && (tolower(arr[j]) > tolower(t) || (tolower(arr[j]) == tolower(t) && arr[j] > t)); j--) arr[j + 1] = arr[j]
+ arr[j + 1] = t
+ }
+}
+
+# Who key k is in bucket b: "name", plus admin-ness.
+function is_admin(b, k) {
+ if (k in ADMIN) return 1
+ # With no admins command, rekey's record still says who was one.
+ return !admins_known && index(NAME[b, k], "admin age1") == 1
+}
+function who(b, k, n) {
+ if (k in ADMIN_NAME) return ADMIN_NAME[k]
+ n = NAME[b, k]
+ if (is_admin(b, k)) return "unnamed admin"
+ if (n == "" || n == "?" || index(n, "admin age1") == 1) return "unnamed reader"
+ return n
+}
+function who_key(b, k) { return keys ? who(b, k) " (" k ")" : who(b, k) }
+
+function problem(b, msg, fix) {
+ NP[b]++
+ PMSG[b, NP[b]] = msg
+ PFIX[b, NP[b]] = fix
+}
+
+function start_bucket( n, i, a) {
+ nb++
+ LABEL[nb] = label
+ OLD[nb] = old
+ NS[nb] = split(names, a, " ")
+ for (i = 1; i <= NS[nb]; i++) { SEC[nb, i] = a[i]; HAVE[nb, a[i]] = 1 }
+ n = split(unread, a, " ")
+ for (i = 1; i <= n; i++) UNREAD[nb, a[i]] = 1
+ kind = ""
+}
+
+# finish(b): readability, readers, and every problem names and headers show.
+function finish(b, i, k, n, a, l, plus, minus, s) {
+ l = LABEL[b]
+ if (OLD[b]) {
+ READABLE[b] = old_readable
+ } else {
+ n = split(TO_LINE[b], a, " ")
+ NTO[b] = 0
+ for (i = 1; i <= n; i++) if (a[i] != "") { TO[b, a[i]] = 1; NTO[b]++; TOK[b, NTO[b]] = a[i] }
+ READABLE[b] = 0
+ for (k in MINE) if ((b, k) in TO) READABLE[b] = 1
+ # The keys shown as its readers: what its files are encrypted to; a
+ # bucket never keyed shows those it will be.
+ NR_[b] = 0
+ if (KEYED[b]) {
+ for (i = 1; i <= NTO[b]; i++) RD[b, ++NR_[b]] = TOK[b, i]
+ } else {
+ for (i = 1; i <= NRK[b]; i++) RD[b, ++NR_[b]] = RK[b, i]
+ for (k in ADMIN) RD[b, ++NR_[b]] = k
+ }
+ if (!KEYED[b]) {
+ problem(b, "never keyed", "szecret rekey " l)
+ } else if (admins_known) {
+ plus = ""; minus = ""
+ for (i = 1; i <= NRK[b]; i++) WANT[b, RK[b, i]] = 1
+ for (k in ADMIN) WANT[b, k] = 1
+ for (i = 1; i <= NRK[b]; i++) if (!((b, RK[b, i]) in TO) && !SEEN[b, "+" RK[b, i]]++) plus = plus " +" who_key(b, RK[b, i])
+ for (k in ADMIN) if (!((b, k) in TO) && !SEEN[b, "+" k]++) plus = plus " +" who_key(b, k)
+ for (i = 1; i <= NTO[b]; i++) if (!((b, TOK[b, i]) in WANT)) minus = minus " -" who_key(b, TOK[b, i])
+ if (plus minus != "") problem(b, "readers changed since its last rekey:" plus minus, "szecret rekey " l)
+ }
+ }
+ for (i = 1; i <= NS[b]; i++) {
+ s = SEC[b, i]
+ if ((b, s) in UNREAD) problem(b, s ": can't read its file", "")
+ else if (!((b, s) in STANZAS)) problem(b, s ": not an age file", "")
+ else if (BADTYPE[b, s] != "") problem(b, s ": unexpected stanza type" BADTYPE[b, s], "szecret check")
+ else if (!OLD[b] && KEYED[b] && STANZAS[b, s] != NTO[b]) problem(b, s ": encrypted to " STANZAS[b, s] " keys, the bucket to " NTO[b], "szecret check")
+ }
+}
+
+BEGIN {
+ n = split(admin_list, a, " "); for (i = 1; i <= n; i++) ADMIN[a[i]] = 1
+ NADMIN = n
+ n = split(ENVIRON["SZECRET_LIST_ADMIN_NAMES"], a, "\n")
+ for (i = 1; i <= n; i++) { k = a[i]; sub(/ .*/, "", k); nm = a[i]; sub(/^[^ ]* /, "", nm); ADMIN_NAME[k] = nm }
+ n = split(mine, a, " "); for (i = 1; i <= n; i++) MINE[a[i]] = 1
+ OKTYPE["X25519"] = 1; OKTYPE["p256tag"] = 1; OKTYPE["piv-p256"] = 1
+ nb = 0
+}
+
+FNR == 1 {
+ if (FILENAME == marker) { start_bucket(); next }
+ if (FILENAME ~ /\/recipients$/) { kind = "rec"; pending = "" }
+ else if (FILENAME ~ /\/\.szecret$/) kind = "meta"
+ else {
+ kind = "age"
+ cur = FILENAME; sub(/.*\//, "", cur); sub(/\.age$/, "", cur)
+ if ($0 != "age-encryption.org/v1") nextfile
+ STANZAS[nb, cur] = 0
+ next
+ }
+}
+kind == "rec" {
+ if ($0 ~ /^[ \t]*#/) { pending = $0; sub(/^[ \t]*#[ \t]*/, "", pending); next }
+ line = $0; sub(/#.*/, "", line); gsub(/[ \t]/, "", line)
+ if (line == "") next
+ NRK[nb]++; RK[nb, NRK[nb]] = line
+ if (!((nb, line) in NAME)) NAME[nb, line] = pending
+ pending = ""
+ next
+}
+kind == "meta" {
+ if ($0 == "kind=bucket") KEYED[nb] = 1
+ else if (index($0, "encrypted-to=") == 1) TO_LINE[nb] = substr($0, 14)
+ else if (index($0, "name=") == 1) {
+ k = substr($0, 6); nm = k; sub(/ .*/, "", k); sub(/^[^ ]* ?/, "", nm)
+ if (!((nb, k) in NAME) || NAME[nb, k] == "") NAME[nb, k] = nm
+ }
+ next
+}
+kind == "age" {
+ if (index($0, "-> ") == 1) {
+ STANZAS[nb, cur]++
+ if (!($2 in OKTYPE)) BADTYPE[nb, cur] = BADTYPE[nb, cur] " " $2
+ next
+ }
+ if (index($0, "---") == 1) nextfile
+}
+
+# --- Output ---
+
+function wrap_list(prefix, n, arr, indent, i, line, out, item, sep) {
+ line = prefix; out = ""
+ for (i = 1; i <= n; i++) {
+ item = arr[i] (i < n ? "," : "")
+ sep = (line == indent && i > 1 ? "" : " ")
+ if (line != indent && length(line sep item) > W) { out = out line "\n"; line = indent; sep = "" }
+ line = line sep item
+ }
+ return out line
+}
+
+# grid(b): the bucket's secrets in columns, filled downwards as ls does, as
+# many as fit in W.
+function grid(b, n, i, cols, rows, w, col, total, best, r, line, s, pad) {
+ n = NS[b]
+ for (i = 1; i <= n; i++) G[i] = SEC[b, i]
+ isort(G, n)
+ best = 1
+ for (cols = n; cols > 1; cols--) {
+ rows = int((n + cols - 1) / cols)
+ if (int((n + rows - 1) / rows) != cols) continue
+ total = 2
+ for (col = 0; col < cols; col++) {
+ w = 0
+ for (r = 1; r <= rows; r++) { i = col * rows + r; if (i <= n && length(G[i]) > w) w = length(G[i]) }
+ total += w + (col < cols - 1 ? 2 : 0)
+ }
+ if (total <= W) { best = cols; break }
+ }
+ cols = best; rows = int((n + cols - 1) / cols)
+ for (col = 0; col < cols; col++) {
+ CW[col] = 0
+ for (r = 1; r <= rows; r++) { i = col * rows + r; if (i <= n && length(G[i]) > CW[col]) CW[col] = length(G[i]) }
+ }
+ for (r = 1; r <= rows; r++) {
+ line = " "
+ for (col = 0; col < cols; col++) {
+ i = col * rows + r
+ if (i > n) break
+ s = G[i]
+ if (col < cols - 1 && col * rows + r + rows <= n) { pad = CW[col] - length(s) + 2; while (pad-- > 0) s = s " " }
+ line = line s
+ }
+ print line
+ }
+}
+
+function show_bucket(b, i, n, k, R, nadm, hosts, rl, readers, j) {
+ print bold(LABEL[b]) " " dim(NS[b] == 0 ? "empty" : plural(NS[b], "secret", "secrets"))
+ if (OLD[b]) {
+ print dim(" read with the old key")
+ } else if (keys) {
+ print dim(" read by")
+ for (i = 1; i <= NR_[b]; i++) R[i] = who(b, RD[b, i]) "\t" RD[b, i]
+ isort(R, NR_[b])
+ w = 0
+ for (i = 1; i <= NR_[b]; i++) { split(R[i], p, "\t"); if (length(p[1]) > w) w = length(p[1]) }
+ for (i = 1; i <= NR_[b]; i++) { split(R[i], p, "\t"); s = p[1]; while (length(s) < w) s = s " "; print dim(" " s " " p[2]) }
+ } else {
+ n = 0; nadm = 0
+ for (i = 1; i <= NR_[b]; i++) {
+ k = RD[b, i]
+ if (is_admin(b, k)) { ADM[++nadm] = who(b, k) } else { R[++n] = who(b, k) }
+ }
+ isort(R, n)
+ if (n == 0) R[++n] = "the admins only"
+ else if (nadm > 0) R[++n] = "the admins"
+ print dim(wrap_list(" read by", n, R, " "))
+ }
+ for (i = 1; i <= NP[b]; i++) {
+ print warn(" ! " PMSG[b, i])
+ if (PFIX[b, i] != "") print " " dim("fix:") " " PFIX[b, i]
+ }
+ if (NS[b] > 0) grid(b)
+}
+
+# machine(): this machine's name, as the buckets' readers name its key.
+function machine( k, b, me) {
+ me = ""
+ for (k in MINE) for (b = 1; b <= nb; b++) if ((b, k) in NAME && NAME[b, k] != "" && NAME[b, k] != "?" && !is_admin(b, k)) me = NAME[b, k]
+ for (k in MINE) if (k in ADMIN_NAME) me = ADMIN_NAME[k]
+ return me
+}
+
+function human( b, nread, nsec, nall, nsecall, nother, probs, pl, me, i, A, n, an) {
+ nread = 0; nsec = 0; nall = 0; nsecall = 0; probs = 0; pl = ""
+ for (b = 1; b <= nb; b++) {
+ nall++; nsecall += NS[b]
+ if (READABLE[b]) { nread++; nsec += NS[b] }
+ if ((READABLE[b] || all) && NP[b] > 0) { probs += NP[b]; pl = pl (pl == "" ? "" : ", ") LABEL[b] }
+ }
+ me = machine()
+
+ if (nread == 0 && !all) {
+ if (nb == 0) {
+ print "There are no buckets yet."
+ return
+ }
+ print "This machine reads none of the " plural(nb, "bucket", "buckets") " here."
+ if (!has_identity) {
+ print ""
+ print "It has no identity yet. Make one with"
+ print " szecret enrol"
+ print "then add its key to cloud-setup's szecret/hosts and rekey."
+ } else {
+ print ""
+ print "Its key is in no bucket's readers: add it to cloud-setup's"
+ print "szecret/hosts, then rekey the buckets it should read."
+ if (identity_key != "") print dim(" " identity_key)
+ }
+ print ""
+ print dim("All the buckets, with what is in them: szecret list --all")
+ return
+ }
+
+ first = 1
+ for (b = 1; b <= nb; b++) if (READABLE[b]) { if (!first) print ""; first = 0; show_bucket(b) }
+ if (all) {
+ nother = nall - nread
+ if (nother > 0) {
+ if (!first) print ""
+ print bold("Not read here") " " dim(plural(nother, "bucket", "buckets"))
+ for (b = 1; b <= nb; b++) if (!READABLE[b]) { print ""; show_bucket(b) }
+ }
+ }
+
+ print ""
+ if (me == "") me = "This machine"
+ if (all) print me " reads " nread " of " plural(nall, "bucket", "buckets") ", " nsec " of " plural(nsecall, "secret", "secrets")
+ else print me " reads " plural(nread, "bucket", "buckets") ", " plural(nsec, "secret", "secrets")
+ if (admins_known) {
+ an = 0
+ for (k in ADMIN) A[++an] = (k in ADMIN_NAME) ? ADMIN_NAME[k] : "unnamed admin"
+ isort(A, an)
+ # A name twice (several unnamed admins) is said once, with a count.
+ n = 0
+ for (i = 1; i <= an; i++) U[A[i]]++
+ for (i = 1; i <= an; i++) if (!(A[i] in DONE)) { DONE[A[i]] = 1; AL[++n] = U[A[i]] > 1 ? U[A[i]] " " A[i] "s" : A[i] }
+ if (n > 0) print dim(wrap_list("The admins read every bucket:", n, AL, " "))
+ } else {
+ print warn("! the admins are unknown (SZECRET_ADMINS_COMMAND unset or failing), so pending rekeys aren't checked")
+ }
+ if (!all && nall > nread) print dim(plural(nall - nread, "bucket", "buckets") " not read here: szecret list --all")
+ if (probs > 0) print warn("! " plural(probs, "problem", "problems") " in " pl)
+}
+
+function json( b, i, k, first, firstr, s) {
+ printf "{\"buckets\":["
+ first = 1
+ for (b = 1; b <= nb; b++) {
+ if (!READABLE[b] && !all) continue
+ printf "%s{\"name\":%s,\"old_store\":%s,\"readable\":%s,\"keyed\":%s,", (first ? "" : ","), jstr(LABEL[b]), (OLD[b] ? "true" : "false"), (READABLE[b] ? "true" : "false"), (OLD[b] || KEYED[b] ? "true" : "false")
+ first = 0
+ printf "\"readers\":["
+ for (i = 1; i <= NR_[b]; i++) JR[i] = who(b, RD[b, i]) "\t" RD[b, i]
+ isort(JR, NR_[b])
+ for (i = 1; i <= NR_[b]; i++) {
+ k = JR[i]; sub(/.*\t/, "", k)
+ printf "%s{\"name\":%s,\"key\":%s,\"admin\":%s,\"this_machine\":%s}", (i > 1 ? "," : ""), jstr(who(b, k)), jstr(k), (is_admin(b, k) ? "true" : "false"), ((k in MINE) ? "true" : "false")
+ }
+ printf "],\"secrets\":["
+ for (i = 1; i <= NS[b]; i++) G[i] = SEC[b, i]
+ isort(G, NS[b])
+ for (i = 1; i <= NS[b]; i++) printf "%s%s", (i > 1 ? "," : ""), jstr(G[i])
+ printf "],\"problems\":["
+ for (i = 1; i <= NP[b]; i++) {
+ printf "%s{\"problem\":%s", (i > 1 ? "," : ""), jstr(PMSG[b, i])
+ if (PFIX[b, i] != "") printf ",\"fix\":%s", jstr(PFIX[b, i])
+ printf "}"
+ }
+ printf "]}"
+ }
+ printf "],\"machine\":%s,\"admins_known\":%s}\n", (machine() == "" ? "null" : jstr(machine())), (admins_known ? "true" : "false")
+}
+
+END {
+ for (b = 1; b <= nb; b++) finish(b)
+ if (mode == "json") json(); else human()
+}
+AWK
+
+mine="$(my_keys | paste -sd ' ' -)"
+SZECRET_LIST_ADMIN_NAMES="${admin_named}" awk -v marker="${marker}" -v mode="$([[ -n "${json}" ]] && echo json || echo human)" \
+ -v all="${all:-0}" -v keys="${keys:-0}" -v color="${color:-0}" -v W="${width}" \
+ -v admin_list="$(echo "${admins}" | paste -sd ' ' -)" \
+ -v admins_known="${admins_known:-0}" -v mine="${mine}" -v old_readable="${old_readable}" \
+ -v has_identity="${has_identity:-0}" -v identity_key="${identity_key}" \
+ "${prog}" ${args[@]+"${args[@]}"} </dev/null
diff --git a/secrets/test/list.sh b/secrets/test/list.sh
index 08671c8..cf07dd0 100755
--- a/secrets/test/list.sh
+++ b/secrets/test/list.sh
@@ -46,10 +46,151 @@ test_list_sorted() {
fi
}
+# --- Buckets, at a terminal and in JSON --------------------------------------
+
+# buckets_setup: this machine ("me"), another host and two admins, one named
+# by a "# <name>" line in the admins command's output and one not; buckets
+# mine (me, other), theirs (other) and admin-only, keyed, a secret in each.
+buckets_setup() {
+ export SZECRET_BUCKETS="${TEST_DIR}/buckets"
+ export SZECRET_IDENTITY="${TEST_DIR}/me/identity"
+ mkdir -p "${SZECRET_BUCKETS}"
+ 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")"
+ age-keygen -o "${TEST_DIR}/admin2" 2>/dev/null
+ admin2="$(age-keygen -y "${TEST_DIR}/admin2")"
+ # admin2 has no "# <name>" line directly above it: the blank line ends it.
+ printf '# The admins\n# alice phone\n%s\n\n%s\n' "${admin}" "${admin2}" > "${TEST_DIR}/admins"
+ export SZECRET_ADMINS_COMMAND="cat '${TEST_DIR}/admins'"
+ local b row
+ for b in "mine:me ${me}:other ${other}" "theirs:other ${other}" "admin-only"; do
+ mkdir -p "${SZECRET_BUCKETS}/${b%%:*}"
+ {
+ echo "# Written by szecret-buckets"
+ IFS=: read -r -a rows <<<"${b#*:}"
+ if [[ "${b}" == *:* ]]; then
+ for row in "${rows[@]}"; do
+ echo "# ${row%% *}"
+ echo "${row#* }"
+ done
+ fi
+ } > "${SZECRET_BUCKETS}/${b%%:*}/recipients"
+ szecret rekey --add-ok "${b%%:*}" >/dev/null
+ done
+ echo v | szecret put mine/mine-a
+ echo v | szecret put mine/mine-b
+ echo v | szecret put theirs/their-t
+ echo v | szecret put admin-only/admin-a
+}
+
+# Plain list piped, and --names, are what scripts read: unchanged, byte for byte.
+test_list_scripts_unchanged() {
+ assert_equals "$(printf '%s\n' apple mango mine-a mine-b secret-a secret-b secret-c zebra)" \
+ "$(szecret list)" "piped list"
+ assert_equals "$(printf '%s\n' mine/mine-a mine/mine-b '(old store)/apple' '(old store)/mango' \
+ '(old store)/secret-a' '(old store)/secret-b' '(old store)/secret-c' '(old store)/zebra')" \
+ "$(szecret list --names)" "list --names"
+ assert_contains "$(szecret list --names --all)" "theirs/their-t"
+ assert_contains "$(szecret list --names --all)" "admin-only/admin-a"
+ pass "list: piped and --names output unchanged"
+}
+
+test_list_terminal() {
+ local out
+ out="$(SZECRET_FORCE_TTY=80 szecret list)"
+ assert_contains "${out}" "mine 2 secrets"
+ assert_contains "${out}" " read by me, other, the admins"
+ assert_contains "${out}" " mine-a mine-b"
+ assert_contains "${out}" "(old store) 6 secrets"
+ assert_contains "${out}" "me reads 2 buckets, 8 secrets"
+ assert_contains "${out}" "The admins read every bucket: alice phone, unnamed admin"
+ assert_contains "${out}" "2 buckets not read here: szecret list --all"
+ [[ "${out}" != *theirs* ]] || fail "list shows a bucket not read here: ${out}"
+ [[ "${out}" != *age1* ]] || fail "list shows a key without --keys: ${out}"
+ [[ "${out}" != *$'\033'* ]] || fail "colour without a terminal: ${out}"
+ pass "list: at a terminal, buckets with readers by name, and a summary"
+}
+
+test_list_terminal_all_and_keys() {
+ local out
+ out="$(SZECRET_FORCE_TTY=80 szecret list --all)"
+ assert_contains "${out}" "Not read here 2 buckets"
+ assert_contains "${out}" "theirs 1 secret"
+ assert_contains "${out}" " read by the admins only"
+ assert_contains "${out}" "me reads 2 of 4 buckets, 8 of 10 secrets"
+ out="$(SZECRET_FORCE_TTY=80 szecret list --keys)"
+ assert_contains "${out}" "me ${me}"
+ assert_contains "${out}" "unnamed admin ${admin2}"
+ pass "list: --all adds the buckets not read here, --keys the keys"
+}
+
+test_list_narrow() {
+ local out long
+ out="$(SZECRET_FORCE_TTY=44 szecret list --all)"
+ long="$(awk 'length > 44' <<<"${out}")"
+ assert_equals "" "${long}" "lines over 44 columns"
+ assert_contains "${out}" "The admins read every bucket: alice phone,"
+ assert_contains "${out}" " unnamed admin"
+ pass "list: fits a narrow terminal"
+}
+
+test_list_json() {
+ local out
+ out="$(szecret list --json)"
+ assert_contains "${out}" '{"buckets":[{"name":"mine","old_store":false,"readable":true,"keyed":true,"readers":['
+ assert_contains "${out}" "{\"name\":\"me\",\"key\":\"${me}\",\"admin\":false,\"this_machine\":true}"
+ assert_contains "${out}" "{\"name\":\"alice phone\",\"key\":\"${admin}\",\"admin\":true,\"this_machine\":false}"
+ assert_contains "${out}" '"secrets":["mine-a","mine-b"],"problems":[]}'
+ assert_contains "${out}" '"machine":"me","admins_known":true}'
+ [[ "${out}" != *theirs* ]] || fail "--json shows a bucket not read here"
+ assert_contains "$(szecret list --json --all)" '"name":"theirs","old_store":false,"readable":false'
+ if command -v python3 >/dev/null; then
+ python3 -c 'import json,sys; json.load(sys.stdin)' <<<"${out}" || fail "--json is not JSON: ${out}"
+ fi
+ assert_failure szecret list --json --names
+ pass "list: --json, keys included"
+}
+
+test_list_problems() {
+ local out
+ age-keygen -o "${TEST_DIR}/newhost" 2>/dev/null
+ printf '# newhost\n%s\n' "$(age-keygen -y "${TEST_DIR}/newhost")" >> "${SZECRET_BUCKETS}/mine/recipients"
+ echo "not age" > "${SZECRET_BUCKETS}/mine/junk.age"
+ out="$(SZECRET_FORCE_TTY=80 szecret list)"
+ assert_contains "${out}" " ! readers changed since its last rekey: +newhost"
+ assert_contains "${out}" " fix: szecret rekey mine"
+ assert_contains "${out}" " ! junk: not an age file"
+ assert_contains "${out}" "! 2 problems in mine"
+ assert_contains "$(szecret list --json)" '{"problem":"readers changed since its last rekey: +newhost","fix":"szecret rekey mine"}'
+ out="$(SZECRET_FORCE_TTY=80 SZECRET_ADMINS_COMMAND=false szecret list)"
+ assert_contains "${out}" "! the admins are unknown"
+ pass "list: says what is wrong, and how to fix it"
+}
+
+test_list_reads_nothing() {
+ local out
+ out="$(SZECRET_FORCE_TTY=80 SZECRET_STORE= SZECRET_IDENTITY="${TEST_DIR}/stranger/identity" szecret list)"
+ assert_contains "${out}" "This machine reads none of the 3 buckets here."
+ assert_contains "${out}" "szecret enrol"
+ pass "list: says what to do when this machine reads nothing"
+}
+
run_tests() {
test_list_empty_store
test_list_shows_secrets
test_list_sorted
+ buckets_setup
+ test_list_scripts_unchanged
+ test_list_terminal
+ test_list_terminal_all_and_keys
+ test_list_narrow
+ test_list_json
+ test_list_problems
+ test_list_reads_nothing
}
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests