diff options
Diffstat (limited to 'secrets/list.sh')
| -rwxr-xr-x | secrets/list.sh | 459 |
1 files changed, 443 insertions, 16 deletions
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 |
