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

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
create-local: Create a new localized secret store

Creates a new age private key for a localized secret store and initializes
a directory with metadata for automatic store discovery.

If the store key already exists but the directory lacks metadata, this command
will add the metadata file (useful for migrating existing stores).

Usage:
  create-local <bucket>/<store-name> <directory>
  create-local <store-name> <directory>
  create-local -h|--help

Arguments:
  bucket        The bucket the store's key goes in; its readers read the store.
                Needed for a new store on a machine with buckets
  store-name    Name of the local store to create
  directory     Directory to initialize with store metadata

The command will:
1. Check if the directory already has a .szecret metadata file
2. If the store key (local-key-<store-name>) doesn't exist, generate a new age
   private key and put it in <bucket>, or in the old store on a machine with
   no buckets
3. Write the store name and public key to <directory>/.szecret

Note: This command will refuse to overwrite existing metadata.
EOF
    exit 0
fi

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

if [[ "$#" -lt 2 ]]; then
    >&2 echo "ERROR: Need local store name and directory"
    exit 1
fi

bucket=""
local_store_name="$1"
if [[ "${local_store_name}" == */* ]]; then
    bucket="${local_store_name%%/*}"
    local_store_name="${local_store_name#*/}"
    [[ -n "${buckets}" ]] || die "no buckets on this machine: $1"
    [[ "${bucket}" =~ ${re_bucket} ]] || die "not a bucket's name: ${bucket}"
fi
[[ "${local_store_name}" =~ ${re_secret} ]] || die "not a store's name: ${local_store_name}"
target_dir="$2"
metadata_file="${target_dir}/.szecret"
local_key_name="$(local_key_name "${local_store_name}")"

# Check if metadata already exists
if [[ -e "${metadata_file}" ]]; then
    >&2 echo "ERROR: Directory already has .szecret metadata"
    exit 1
fi

found="$(locate "${local_key_name}")"
if [[ "${found}" == *$'\n'* ]]; then
    die "${local_key_name} is in more than one place: $(while read -r f; do where_is "${f}"; done <<<"${found}" | paste -sd ',' - | sed 's/,/, /g')"
fi
if [[ -n "${found}" && -n "${bucket}" && "${found}" != "${buckets}/${bucket}/${local_key_name}.age" ]]; then
    if [[ -n "${store}" && "${found}" == "${store}/${local_key_name}.age" ]]; then
        die "${local_key_name} is in the old store, not in ${bucket}; szecret mv ${local_key_name} ${bucket} moves it"
    fi
    die "${local_key_name} is in $(where_is "${found}"), not in ${bucket}"
fi
if [[ -z "${found}" && -n "${buckets}" ]]; then
    if [[ -z "${bucket}" ]]; then
        die "name the bucket for the new store's key, whose readers read the store: szecret create-local <bucket>/${local_store_name} ${target_dir}"
    fi
    # The store is used where its key is read: put-local and get-local both
    # decrypt the key.
    [[ -d "${buckets}/${bucket}" ]] || die "no bucket ${bucket}"
    [[ -n "$(encrypted_to "${buckets}/${bucket}")" ]] || die "${bucket} has never been keyed: szecret rekey ${bucket} first"
    if ! is_reader "${buckets}/${bucket}"; then
        while read -r k; do
            if [[ -n "${k}" ]] && keys_in "${buckets}/${bucket}/recipients" | grep -qxF "${k}"; then
                die "this machine was added to ${bucket}'s readers since its last rekey: szecret rekey ${bucket} first"
            fi
        done <<<"$(my_keys)"
        die "this machine doesn't read ${bucket}, so it couldn't use the store; make it where ${bucket} is read"
    fi
fi
if [[ -e "${target_dir}" && ! -d "${target_dir}" ]]; then
    die "${target_dir} is not a directory"
fi

ensure_age

if [[ -n "${found}" ]]; then
    local_pubkey="$(get_local_public_key "${local_store_name}")"
else
    # New store: the key goes in first, so a refused put leaves no metadata.
    local_private_key="$(age-keygen 2>/dev/null)"
    local_pubkey="$(printf '%s\n' "${local_private_key}" | age-keygen -y)"
    printf '%s\n' "${local_private_key}" | "${this_dir}/put.sh" "${bucket:+${bucket}/}${local_key_name}"
    unset local_private_key
fi

mkdir -p "${target_dir}"
write_local_metadata "${metadata_file}" "${local_store_name}" "${local_pubkey}"

if [[ -n "${found}" ]]; then
    >&2 echo "Added metadata for existing store '${local_store_name}'"
fi