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

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
get: Retrieve and decrypt a secret

Decrypts a secret and writes it to stdout.

Usage:
  get <name>            searches every bucket and the old store
  get <bucket>/<name>   reads that bucket only
  get -h|--help

Fails, naming both, if <name> is in two places, and says so if this machine
isn't one of the bucket's readers.
EOF
    exit 0
fi

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

if [[ "$#" -lt 1 ]]; then
    >&2 echo "ERROR: Need secret name"
    exit 1
fi
name="$1"

file="$(find_secret "${name}")" || exit 1

if [[ -n "${store}" && "$(dirname "${file}")" == "${store}" ]]; then
    # A machine that has moved to its own identity is not asked for the old key.
    if [[ -r "${identity}" && ! -r "${private_key}" ]]; then
        die "${name} is in the old store, and the old key is no longer on this machine"
    fi
    ensure_private_key
else
    dir="$(dirname "${file}")"
    if ! is_reader "${dir}"; then
        die "$(basename "${file}" .age) is in bucket $(bucket_label "${dir}"); this machine isn't one of its readers"
    fi
fi

decrypt "${file}"