blob: 58256bd4a35993dd05f379d20f87f18741b30e34 (
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#!/bin/bash
set -euo pipefail
if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
cat <<EOF
rekey: Re-encrypt a bucket to its readers now
Shows the bucket's new readers, marking who is added (+) and removed (-)
since its last rekey, then encrypts every file again to its recipients plus
the admins, and records that set in its .szecret.
Usage:
rekey [--add-ok] [-n] <bucket>|<directory> (a directory: a path, with a /)
rekey -h|--help
A repository bucket is named by its path, which must contain a / (./keys).
Options:
--add-ok add readers without asking (needed with no terminal)
-n show the change, and change nothing
Every file is built and checked in a directory beside the bucket before any
is swapped in: a file it can't decrypt stops it with nothing changed. Adding a
reader lets it read every secret in the bucket, so with files in the bucket
that needs a yes at a terminal, or --add-ok.
EOF
exit 0
fi
this_dir="$(cd "$(dirname "$0")" && pwd)"
source "${this_dir}/common.sh"
add_ok=""
dry=""
arg=""
while [[ $# -gt 0 ]]; do
case "$1" in
--add-ok) add_ok=1 ;;
-n) dry=1 ;;
-*) die "usage: szecret rekey [--add-ok] [-n] <bucket>|<directory>" ;;
*) [[ -z "${arg}" ]] || die "one bucket at a time"; arg="$1" ;;
esac
shift
done
[[ -n "${arg}" ]] || die "usage: szecret rekey [--add-ok] [-n] <bucket>|<directory>"
if [[ "${arg}" == */* ]]; then
dir="${arg%/}"
else
[[ "${arg}" =~ ${re_bucket} ]] || die "not a bucket's name: ${arg}"
[[ -n "${buckets}" ]] || die "no buckets on this machine"
dir="${buckets}/${arg}"
fi
label="$(bucket_label "${dir}")"
[[ -d "${dir}" ]] || die "no bucket ${label}"
[[ -r "${dir}/recipients" ]] || die "bucket ${label} has no recipients file (cloud-setup's bin/szecret-buckets writes it)"
if [[ -e "${dir}/.szecret" ]] && ! is_bucket_dir "${dir}"; then
die "${dir}/.szecret is not a bucket's: an old-style repository store moves by hand"
fi
admins="$(admin_keys)" || exit 1
want="$(wanted_set "${dir}" "${admins}")"
have="$(encrypted_to "${dir}")"
names="$(key_names "${dir}")"
files=()
for f in "${dir}"/*.age; do
if [[ -e "${f}" ]]; then
files+=("${f}")
fi
done
added=""
echo "${label}: ${#files[@]} secret(s), readers:"
while read -r k; do
[[ -n "${k}" ]] || continue
if grep -qxF "${k}" <<<"${want}"; then
if grep -qxF "${k}" <<<"${have}"; then
mark=" "
else
mark="+"
added=1
fi
else
mark="-"
fi
printf ' %s %s\n' "${mark}" "$(name_of "${k}" "${names}" "${admins}")"
done < <({ echo "${want}"; echo "${have}"; } | grep -v '^$' | sorted)
if [[ "${want}" == "${have}" ]]; then
echo "${label}: already encrypted to these readers"
exit 0
fi
if [[ -n "${dry}" ]]; then
echo "${label}: needs a rekey (-n: nothing changed)"
exit 0
fi
if [[ -n "${added}" && ${#files[@]} -gt 0 && -z "${add_ok}" ]]; then
if [[ ! -t 0 || ! -t 1 ]]; then
die "rekey adds readers to ${label}, who could then read all of it: run it at a terminal, or pass --add-ok"
fi
yes=""
read -r -p "The readers marked + can then read every secret in ${label}. Go ahead? [y/N] " yes || true
[[ "${yes}" == y || "${yes}" == Y ]] || die "not rekeyed"
fi
age_version_ok
load_identities
work="$(mktemp -d "$(dirname "${dir}")/.rekey-$(basename "${dir}").XXXXXX")"
trap 'rm -rf "${work}"' EXIT
can_check=""
while read -r k; do
if [[ -n "${k}" ]] && grep -qxF "${k}" <<<"${want}"; then
can_check=1
fi
done <<<"$(my_keys)"
for f in ${files[@]+"${files[@]}"}; do
n="$(basename "${f}")"
if ! age --decrypt "${ids[@]}" "${f}" | encrypt_to "${work}/${n}" "${want}"; then
die "cannot re-encrypt ${label}/${n%.age}; nothing changed"
fi
# Readable by this machine afterwards: the new file must say what the old one did.
if [[ -n "${can_check}" ]] && ! cmp -s <(age --decrypt "${ids[@]}" "${f}") <(age --decrypt "${ids[@]}" "${work}/${n}"); then
die "${label}/${n%.age} did not survive re-encryption; nothing changed"
fi
done
for f in ${files[@]+"${files[@]}"}; do
mv "${work}/$(basename "${f}")" "${f}"
done
{
printf 'kind=bucket\nencrypted-to=%s\n' "$(echo "${want}" | paste -sd ' ' -)"
while read -r k; do
echo "name=${k} $(name_of "${k}" "${names}" "${admins}")"
done <<<"${want}"
} > "${work}/.szecret"
mv "${work}/.szecret" "${dir}/.szecret"
echo "${label}: rekeyed"
|