diff options
| -rw-r--r-- | secrets/README.md | 2 | ||||
| -rwxr-xr-x | secrets/list.sh | 48 | ||||
| -rwxr-xr-x | secrets/test/list.sh | 39 |
3 files changed, 77 insertions, 12 deletions
diff --git a/secrets/README.md b/secrets/README.md index 69fd53c..35aaaec 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, by bucket, with who reads each and anything wrong (`--all`, `--keys`, `--json`, `--names`) | +| `list` | The secrets this machine reads, by bucket, with who reads each and anything wrong; a bucket's store keys (`local-key-<store>`) as one "stores:" line (`--all`, `--keys`, `--json`, `--names`, which name every file) | | `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 | diff --git a/secrets/list.sh b/secrets/list.sh index 3f5f56a..52c683e 100755 --- a/secrets/list.sh +++ b/secrets/list.sh @@ -15,6 +15,10 @@ Usage: list --names <bucket>/<name>, one a line list -h|--help +A bucket's store keys (local-key-<store>, the keys of szecret local stores) +show at a terminal as one "stores:" line naming the stores; --names, --json +and piped list name them as the files they are. + 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 @@ -194,6 +198,15 @@ 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) } +# A store's key (local-key-<store>) is how a bucket's readers open that store, +# not a secret anyone uses: shown as the store's name, apart from the secrets. +function is_store(s) { return index(s, "local-key-") == 1 && length(s) > 10 } +function count_stores(b, i, n) { n = 0; for (i = 1; i <= NS[b]; i++) if (is_store(SEC[b, i])) n++; return n } +# "3 secrets, 1 store", either part left out at 0; "empty" with neither. +function contents(nsec, nst) { + if (nsec == 0 && nst == 0) return "empty" + return (nsec > 0 ? plural(nsec, "secret", "secrets") : "") (nsec > 0 && nst > 0 ? ", " : "") (nst > 0 ? plural(nst, "store", "stores") : "") +} function isort(arr, n, i, j, t) { for (i = 2; i <= n; i++) { t = arr[i] @@ -341,8 +354,9 @@ function wrap_list(prefix, n, arr, indent, i, line, out, item, sep) { # 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] + n = 0 + for (i = 1; i <= NS[b]; i++) if (!is_store(SEC[b, i])) G[++n] = SEC[b, i] + if (n == 0) return isort(G, n) best = 1 for (cols = n; cols > 1; cols--) { @@ -374,8 +388,9 @@ function grid(b, n, i, cols, rows, w, col, total, best, r, line, s, pad) { } } -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")) +function show_bucket(b, i, n, k, R, nadm, hosts, rl, readers, j, nst, ST) { + nst = count_stores(b) + print bold(LABEL[b]) " " dim(contents(NS[b] - nst, nst)) if (OLD[b]) { print dim(" read with the old key") } else if (keys) { @@ -400,7 +415,13 @@ function show_bucket(b, i, n, k, R, nadm, hosts, rl, readers, j) { print warn(" ! " PMSG[b, i]) if (PFIX[b, i] != "") print " " dim("fix:") " " PFIX[b, i] } - if (NS[b] > 0) grid(b) + if (nst > 0) { + n = 0 + for (i = 1; i <= NS[b]; i++) if (is_store(SEC[b, i])) ST[++n] = substr(SEC[b, i], 11) + isort(ST, n) + print wrap_list(" stores:", n, ST, " ") + } + if (NS[b] > nst) grid(b) } # machine(): this machine's name, as the buckets' readers name its key. @@ -411,11 +432,12 @@ function machine( k, b, me) { 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 = "" +function human( b, nread, nsec, nall, nsecall, nother, probs, pl, me, i, A, n, an, nst, nstall, ns, line, tail) { + nread = 0; nsec = 0; nall = 0; nsecall = 0; probs = 0; pl = ""; nst = 0; nstall = 0 for (b = 1; b <= nb; b++) { - nall++; nsecall += NS[b] - if (READABLE[b]) { nread++; nsec += NS[b] } + ns = count_stores(b) + nall++; nsecall += NS[b] - ns; nstall += ns + if (READABLE[b]) { nread++; nsec += NS[b] - ns; nst += ns } if ((READABLE[b] || all) && NP[b] > 0) { probs += NP[b]; pl = pl (pl == "" ? "" : ", ") LABEL[b] } } me = machine() @@ -455,8 +477,12 @@ function human( b, nread, nsec, nall, nsecall, nother, probs, pl, me, i, A, n 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 (all) { line = me " reads " nread " of " plural(nall, "bucket", "buckets") ", " nsec " of " plural(nsecall, "secret", "secrets"); tail = nstall > 0 ? nst " of " plural(nstall, "store", "stores") : "" } + else { line = me " reads " plural(nread, "bucket", "buckets") ", " plural(nsec, "secret", "secrets"); tail = nst > 0 ? plural(nst, "store", "stores") : "" } + # The stores on a line of their own when the whole would not fit. + if (tail == "") print line + else if (length(line ", " tail) <= W) print line ", " tail + else { print line ","; print " " tail } if (admins_known) { an = 0 for (k in ADMIN) A[++an] = (k in ADMIN_NAME) ? ADMIN_NAME[k] : "unnamed admin" diff --git a/secrets/test/list.sh b/secrets/test/list.sh index cf07dd0..bd72f9a 100755 --- a/secrets/test/list.sh +++ b/secrets/test/list.sh @@ -179,6 +179,43 @@ test_list_reads_nothing() { pass "list: says what to do when this machine reads nothing" } +# A store's key shows as its store, apart from the secrets and their count; +# what scripts read still names the file. +test_list_stores() { + local out + echo v | szecret put mine/local-key-proj + echo v | szecret put mine/local-key-alpha + out="$(SZECRET_FORCE_TTY=80 szecret list)" + assert_contains "${out}" "mine 2 secrets, 2 stores" + assert_contains "${out}" " stores: alpha, proj" + assert_contains "${out}" " mine-a mine-b" + [[ "${out}" != *local-key-* ]] || fail "list shows a store's key as a secret: ${out}" + assert_contains "${out}" "me reads 2 buckets, 8 secrets, 2 stores" + assert_contains "$(SZECRET_FORCE_TTY=80 szecret list --all)" "me reads 2 of 4 buckets, 8 of 10 secrets, 2 of 2 stores" + out="$(SZECRET_FORCE_TTY=36 szecret list)" + assert_contains "${out}" "me reads 2 buckets, 8 secrets,"$'\n'" 2 stores" + assert_contains "$(szecret list --names)" "mine/local-key-proj" + assert_contains "$(szecret list)" "local-key-alpha" + assert_contains "$(szecret list --json)" '"secrets":["local-key-alpha","local-key-proj","mine-a","mine-b"]' + rm "${SZECRET_BUCKETS}/mine/local-key-alpha.age" "${SZECRET_BUCKETS}/mine/local-key-proj.age" + pass "list: store keys as one stores line, scripts still see the files" +} + +# A bucket holding only a store's key: no empty grid, no "0 secrets". +test_list_store_only() { + local out + mkdir -p "${SZECRET_BUCKETS}/storeonly" + printf '# me\n%s\n' "${me}" > "${SZECRET_BUCKETS}/storeonly/recipients" + szecret rekey --add-ok storeonly >/dev/null + echo v | szecret put storeonly/local-key-solo + out="$(SZECRET_FORCE_TTY=80 szecret list)" + assert_contains "${out}" "storeonly 1 store" + assert_contains "${out}" " stores: solo" + [[ "${out}" != *"0 secrets"* ]] || fail "list says 0 secrets: ${out}" + rm -r "${SZECRET_BUCKETS}/storeonly" + pass "list: a bucket holding only a store's key" +} + run_tests() { test_list_empty_store test_list_shows_secrets @@ -189,6 +226,8 @@ run_tests() { test_list_terminal_all_and_keys test_list_narrow test_list_json + test_list_stores + test_list_store_only test_list_problems test_list_reads_nothing } |
