summaryrefslogtreecommitdiff
path: root/secrets/rekey.sh
diff options
context:
space:
mode:
Diffstat (limited to 'secrets/rekey.sh')
-rwxr-xr-xsecrets/rekey.sh142
1 files changed, 142 insertions, 0 deletions
diff --git a/secrets/rekey.sh b/secrets/rekey.sh
new file mode 100755
index 0000000..58256bd
--- /dev/null
+++ b/secrets/rekey.sh
@@ -0,0 +1,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"