diff options
Diffstat (limited to 'secrets/list.sh')
| -rwxr-xr-x | secrets/list.sh | 79 |
1 files changed, 63 insertions, 16 deletions
diff --git a/secrets/list.sh b/secrets/list.sh index bf087e3..39b17ff 100755 --- a/secrets/list.sh +++ b/secrets/list.sh @@ -4,20 +4,17 @@ set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat <<EOF -list: List all available secrets in the store - -Displays all secret names in the global store in a columnar format. +list: List the secrets this machine can read Usage: - list + 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 -The command will: -1. List all .age files in the store directory -2. Remove the .age extension from filenames -3. Display the secret names in columns - -Note: Only shows secret names, not their values. +Only names, never values. The old store counts as readable while the old key +is here. EOF exit 0 fi @@ -25,12 +22,62 @@ fi this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)" source "${this_dir}/common.sh" -maybe_column() { - if [ -t 1 ]; then - column - else - cat +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 } -ls "${store}" | sed 's/\.age$//' | sort | maybe_column +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 |
