summaryrefslogtreecommitdiff
path: root/secrets/check.sh
blob: 299344bb96020e36d9f4c6666c5c105d8f01d7e3 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
check: Check that the buckets this machine reads are whole

For each bucket this machine reads: its readers and the admins are what its
files were last encrypted to; every key in it can be encrypted to here; each
file's header carries only the stanza types expected; and each file decrypts
here (to nowhere: nothing is printed).

Usage:
  check [--all] [<directory>...]
  check -h|--help

Options:
  --all        every bucket, read here or not: an admin's check that the
               admins can open everything
  <directory>  a repository bucket, checked too (a path, with a /)

Prints one line per problem, then a summary. Exit 1 on any problem.
EOF
    exit 0
fi

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

all=""
dirs=()
while [[ $# -gt 0 ]]; do
    case "$1" in
        --all) all=1 ;;
        -*) die "usage: szecret check [--all] [<directory>...]" ;;
        *) dirs+=("${1%/}") ;;
    esac
    shift
done

if [[ -n "${buckets}" ]]; then
    for d in "${buckets}"/*/; do
        d="${d%/}"
        if [[ -d "${d}" ]] && { [[ -n "${all}" ]] || is_reader "${d}"; }; then
            dirs+=("${d}")
        fi
    done
fi

problems=0
problem() {
    echo "$*"
    problems=$((problems + 1))
}

admins="$(admin_keys)" || exit 1
load_identities
checked=0
secrets=0
for d in ${dirs[@]+"${dirs[@]}"}; do
    label="$(bucket_label "${d}")"
    checked=$((checked + 1))
    if ! is_bucket_dir "${d}"; then
        problem "${label}: never keyed (szecret rekey ${label})"
        continue
    fi
    want="$(wanted_set "${d}" "${admins}")"
    have="$(encrypted_to "${d}")"
    if [[ "${want}" != "${have}" ]]; then
        names="$(key_names "${d}")"
        change="$(
            comm -13 <(echo "${have}") <(echo "${want}") | while read -r k; do [[ -z "${k}" ]] || echo "+$(name_of "${k}" "${names}" "${admins}")"; done
            comm -23 <(echo "${have}") <(echo "${want}") | while read -r k; do [[ -z "${k}" ]] || echo "-$(name_of "${k}" "${names}" "${admins}")"; done
        )"
        problem "${label}: readers changed since its last rekey ($(echo "${change}" | paste -sd ' ' -)): szecret rekey ${label}"
    fi
    while read -r k; do
        p="$(plugin_for "${k}")"
        if [[ -n "${k}" && -n "${p}" ]] && ! exists "${p}"; then
            problem "${label}: can't encrypt to $(name_of "${k}" "$(key_names "${d}")" "${admins}") here: ${p} is not installed"
        fi
    done <<<"${want}"
    for f in "${d}"/*.age; do
        [[ -e "${f}" ]] || continue
        secrets=$((secrets + 1))
        n="${label}/$(basename "${f}" .age)"
        while read -r t; do
            if [[ -n "${t}" && " ${stanza_types} " != *" ${t} "* ]]; then
                problem "${n}: unexpected stanza type ${t}"
            fi
        done <<<"$(stanzas_of "${f}")"
        if ! age --decrypt "${ids[@]}" "${f}" >/dev/null 2>&1; then
            problem "${n}: does not decrypt here"
        fi
    done
done

if [[ "${problems}" -gt 0 ]]; then
    echo "szecret check: ${problems} problem(s) in ${checked} bucket(s), ${secrets} secret(s)"
    exit 1
fi
echo "szecret check: ${checked} bucket(s), ${secrets} secret(s), no problems"