summaryrefslogtreecommitdiff
path: root/secrets/list.sh
blob: 39b17ff532f5a17415fefb5272aca16bfebaa9ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
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 --all      every bucket, read here or not
  list -h|--help

Only names, never values. The old store counts as readable while the old key
is here.
EOF
    exit 0
fi

this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
source "${this_dir}/common.sh"

all=""
names_only=""
while [[ $# -gt 0 ]]; do
    case "$1" in
        --all) all=1 ;;
        --names) names_only=1 ;;
        *) die "usage: szecret list [--names] [--all]" ;;
    esac
    shift
done

secrets_in() {
    local f
    for f in "$1"/*.age; do
        if [[ -e "${f}" ]]; then
            basename "${f}" .age
        fi
    done | LC_ALL=C sort
}

# Each bucket shown, then the old store, as "<label>\t<dir>".
shown() {
    local d
    if [[ -n "${buckets}" ]]; then
        for d in "${buckets}"/*/; do
            d="${d%/}"
            [[ -d "${d}" ]] || continue
            if [[ -n "${all}" ]] || is_reader "${d}"; then
                printf '%s\t%s\n' "$(basename "${d}")" "${d}"
            fi
        done
    fi
    if [[ -n "${store}" ]] && { [[ -n "${all}" ]] || [[ -n "${private_key}" && -r "${private_key}" ]]; }; then
        printf '%s\t%s\n' "(old store)" "${store}"
    fi
}

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
    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)"
        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})"
        fi
        secrets_in "${d}" | column | sed 's/^/  /'
        echo
    done
fi