summaryrefslogtreecommitdiff
path: root/secrets/mv.sh
blob: d249ac19673367e2b5671e6aa16cdde933238dcc (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
#!/bin/bash

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
mv: Move a secret to another bucket

Decrypts it, encrypts it to the bucket's readers and the admins, and deletes
the old file. A secret is never copied: when a host outside its bucket needs
it, it moves to a bucket that has every reader who needs it.

Usage:
  mv <name> <bucket>
  mv <bucket>/<name> <bucket>
  mv -h|--help

Needs an identity that can read it now. The plaintext goes through a pipe,
never a file.
EOF
    exit 0
fi

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

[[ "$#" -eq 2 ]] || die "usage: szecret mv <name> <bucket>"
[[ -n "${buckets}" ]] || die "no buckets on this machine"
target="$2"
[[ "${target}" =~ ${re_bucket} ]] || die "not a bucket's name: ${target}"

from="$(find_secret "$1")" || exit 1
name="$(basename "${from}" .age)"
dir="${buckets}/${target}"
to="${dir}/${name}.age"
[[ "${from}" != "${to}" ]] || die "${name} is already in ${target}"
# A name in two places is a mistake to sort out by hand, not to overwrite.
[[ ! -e "${to}" ]] || die "${target}/${name} already exists; not overwriting it with $(where_is "${from}")"
if [[ -n "${store}" && "$(dirname "${from}")" == "${store}" ]]; then
    ensure_private_key
fi

set="$(bucket_set "${dir}")" || exit 1
age_version_ok
load_identities
if ! age --decrypt "${ids[@]}" "${from}" | encrypt_to "${to}" "${set}"; then
    rm -f "${to}"
    die "could not move ${name}; $(where_is "${from}") is as it was"
fi
rm "${from}"
>&2 echo "szecret: moved $(where_is "${to}"), from $(where_is "${from}")"