summaryrefslogtreecommitdiff
path: root/secrets
diff options
context:
space:
mode:
Diffstat (limited to 'secrets')
-rw-r--r--secrets/README.md9
-rw-r--r--secrets/common.sh4
-rwxr-xr-xsecrets/create-local.sh79
-rwxr-xr-xsecrets/get-local.sh2
-rwxr-xr-xsecrets/put-local.sh4
-rwxr-xr-xsecrets/test/buckets.sh69
6 files changed, 135 insertions, 32 deletions
diff --git a/secrets/README.md b/secrets/README.md
index 5e98f3f..69fd53c 100644
--- a/secrets/README.md
+++ b/secrets/README.md
@@ -31,7 +31,8 @@ szecret list
| `enrol [--tpm\|--se]` | Make this machine's identity and print its public key |
| `ready` | Check there is a store and an identity |
| `ensure` | Ensure the old private key is configured (interactive) |
-| `create-local <name> <dir>` | Create a localized secret store (the old kind) |
+| `create-local <bucket>/<name> <dir>` | Create a localized secret store, its key in `<bucket>`, which this machine must read |
+| `create-local <name> <dir>` | The same for an existing store, or on a machine with no buckets |
| `put-local <file>` | Encrypt to a local store or a repository bucket |
| `get-local <file>` | Decrypt from a local store or a repository bucket |
@@ -82,10 +83,12 @@ Local stores allow you to keep encrypted secrets outside the global store, usefu
```bash
# Initialize a local store in a directory
-szecret create-local myproject /path/to/project/secrets/
+szecret create-local web-hosts/myproject /path/to/project/secrets/
# This creates:
-# - A key in the global store (local-key-myproject)
+# - A key, local-key-myproject, in the bucket web-hosts: that bucket's
+# readers read the store (on a machine with no buckets,
+# `create-local myproject <dir>` puts it in the global store)
# - A metadata file at /path/to/project/secrets/.szecret containing:
# store=myproject
# pubkey=age1...
diff --git a/secrets/common.sh b/secrets/common.sh
index adf5488..f11d902 100644
--- a/secrets/common.sh
+++ b/secrets/common.sh
@@ -307,7 +307,7 @@ read_local_metadata() {
if [[ -z "${_metadata_store}" || -z "${_metadata_pubkey}" ]]; then
>&2 echo "ERROR: Invalid metadata format in ${metadata_file}"
- >&2 echo "Run 'szecret create-local <store-name> <directory>' to recreate"
+ >&2 echo "Run 'szecret create-local [<bucket>/]<store-name> <directory>' to recreate"
return 1
fi
}
@@ -316,7 +316,7 @@ get_local_public_key() {
local local_store_name="$1"
local local_private_key
local_private_key="$(get_local_private_key "${local_store_name}")"
- age-keygen -y <<<"${local_private_key}"
+ printf '%s\n' "${local_private_key}" | age-keygen -y
}
verify_local_pubkey() {
diff --git a/secrets/create-local.sh b/secrets/create-local.sh
index b72600d..b07d6f0 100755
--- a/secrets/create-local.sh
+++ b/secrets/create-local.sh
@@ -13,17 +13,22 @@ 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 doesn't exist, generate a new age private key
-3. Write the store name to <directory>/.szecret
+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
@@ -38,45 +43,71 @@ if [[ "$#" -lt 2 ]]; then
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}")"
-local_key_file="$(file_for "${local_key_name}")"
-
-key_exists=false
-metadata_exists=false
-
-[[ -r "${local_key_file}" ]] && key_exists=true
-[[ -e "${metadata_file}" ]] && metadata_exists=true
# Check if metadata already exists
-if [[ "${metadata_exists}" == "true" ]]; then
+if [[ -e "${metadata_file}" ]]; then
>&2 echo "ERROR: Directory already has .szecret metadata"
exit 1
fi
-# An old-kind store keeps its key in the old store; with buckets that key
-# would land in admin-only, which this machine can't read back. A repository
-# bucket is registered in cloud-setup's szecret/repo-buckets instead.
-if [[ -n "${buckets}" ]]; then
- die "create-local makes an old-kind store, which can't be made on a machine with buckets; make a repository bucket instead (cloud-setup docs/proposal-secret-roles.md)"
+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 target directory exists
-mkdir -p "${target_dir}"
ensure_age
-if [[ "${key_exists}" == "false" ]]; then
- # New store: create key first
- age-keygen 2>/dev/null | "${this_dir}/put.sh" "${local_key_name}"
+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
-# Get public key and write metadata
-local_pubkey="$(get_local_public_key "${local_store_name}")"
+mkdir -p "${target_dir}"
write_local_metadata "${metadata_file}" "${local_store_name}" "${local_pubkey}"
-if [[ "${key_exists}" == "true" ]]; then
+if [[ -n "${found}" ]]; then
>&2 echo "Added metadata for existing store '${local_store_name}'"
fi
diff --git a/secrets/get-local.sh b/secrets/get-local.sh
index 22f0cd9..fb0ae1b 100755
--- a/secrets/get-local.sh
+++ b/secrets/get-local.sh
@@ -73,7 +73,7 @@ else
if ! read_local_metadata "${metadata_file}"; then
>&2 echo "ERROR: No .szecret metadata in ${target_dir}"
- >&2 echo "Run 'szecret create-local <store-name> ${target_dir}' first"
+ >&2 echo "Run 'szecret create-local [<bucket>/]<store-name> ${target_dir}' first"
exit 1
fi
diff --git a/secrets/put-local.sh b/secrets/put-local.sh
index 5510df6..42b9564 100755
--- a/secrets/put-local.sh
+++ b/secrets/put-local.sh
@@ -55,7 +55,7 @@ fi
# Read store name and pubkey from metadata
if ! read_local_metadata "${metadata_file}"; then
>&2 echo "ERROR: No .szecret metadata in ${target_dir}"
- >&2 echo "Run 'szecret create-local <store-name> ${target_dir}' first"
+ >&2 echo "Run 'szecret create-local [<bucket>/]<store-name> ${target_dir}' first"
exit 1
fi
@@ -75,7 +75,7 @@ local_private_key="$(get_local_private_key "${local_store_name}")"
# Create a temporary file for the public key (needed because age_encrypt uses pipes)
temp_public_key="$(mktemp)"
trap 'rm -f "${temp_public_key}"' EXIT
-age-keygen -y <<<"${local_private_key}" > "${temp_public_key}"
+printf '%s\n' "${local_private_key}" | age-keygen -y > "${temp_public_key}"
# Encrypt data from stdin to the specified file path using the local store's public key
age_encrypt "${temp_public_key}" "${file_path}"
diff --git a/secrets/test/buckets.sh b/secrets/test/buckets.sh
index 010778a..782e7ad 100755
--- a/secrets/test/buckets.sh
+++ b/secrets/test/buckets.sh
@@ -313,6 +313,72 @@ test_local_store_key_in_bucket() {
pass "get-local/put-local: an old-style repository store whose key moved to a bucket"
}
+test_create_local_in_bucket() {
+ local d="${TEST_DIR}/new-store" err pub
+ err="$(szecret create-local newstore "${d}" 2>&1)" && fail "create-local with no bucket should fail here"
+ assert_contains "${err}" "create-local <bucket>/newstore"
+ [[ ! -e "${d}/.szecret" ]] || fail "a refused create-local left metadata"
+ [[ -z "$(find "${SZECRET_BUCKETS}" -name local-key-newstore.age)" ]] || fail "a refused create-local left a key"
+
+ szecret create-local mine/newstore "${d}" 2>/dev/null || fail "create-local mine/newstore"
+ assert_file_exists "${SZECRET_BUCKETS}/mine/local-key-newstore.age"
+ pub="$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/local-key-newstore.age" | age-keygen -y)"
+ assert_contains "$(cat "${d}/.szecret")" "pubkey=${pub}"
+ assert_success age -d -i "${TEST_DIR}/other" -o /dev/null "${SZECRET_BUCKETS}/mine/local-key-newstore.age"
+ echo "n1" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local in the new store"
+ assert_equals "n1" "$(szecret get-local "${d}/v.age")" "get-local in the new store"
+ pass "create-local: a new store's key goes in the bucket named, read by that bucket's readers"
+}
+
+test_create_local_existing_key() {
+ local d="${TEST_DIR}/new-store-2" err
+ err="$(szecret create-local theirs/newstore "${d}" 2>&1)" && fail "create-local naming another bucket should fail"
+ assert_contains "${err}" "is in mine/local-key-newstore, not in theirs"
+ [[ ! -e "${d}/.szecret" ]] || fail "a refused create-local left metadata"
+ szecret create-local newstore "${d}" 2>/dev/null || fail "create-local of an existing store"
+ assert_equals "n1" "$(cp "${TEST_DIR}/new-store/v.age" "${d}/v.age" && szecret get-local "${d}/v.age")" "the same store in a second directory"
+ szecret create-local mine/newstore "${TEST_DIR}/new-store-3" 2>/dev/null || fail "create-local naming the bucket it's in"
+ assert_equals "$(sed -n 's/^pubkey=//p' "${TEST_DIR}/new-store/.szecret")" "$(sed -n 's/^pubkey=//p' "${TEST_DIR}/new-store-3/.szecret")" "the existing store's key"
+ SZECRET_BUCKETS="" szecret create-local oldk "${TEST_DIR}/oldk" 2>/dev/null || fail "create-local in the old store"
+ err="$(szecret create-local mine/oldk "${TEST_DIR}/oldk-2" 2>&1)" && fail "create-local naming a bucket for an old-store key should fail"
+ assert_contains "${err}" "szecret mv local-key-oldk mine"
+ pass "create-local: an existing store gets metadata, and refuses a bucket it isn't in"
+}
+
+test_create_local_refusals() {
+ local err
+ err="$(SZECRET_BUCKETS="" szecret create-local mine/x "${TEST_DIR}/r1" 2>&1)" && fail "a bucket with no buckets should fail"
+ assert_contains "${err}" "no buckets on this machine"
+ err="$(szecret create-local Mine/x "${TEST_DIR}/r2" 2>&1)" && fail "a bad bucket name should fail"
+ assert_contains "${err}" "not a bucket's name"
+ err="$(szecret create-local theirs/tx "${TEST_DIR}/r4" 2>&1)" && fail "a bucket this machine doesn't read should fail"
+ assert_contains "${err}" "doesn't read theirs"
+ [[ ! -e "${TEST_DIR}/r4" && ! -e "${SZECRET_BUCKETS}/theirs/local-key-tx.age" ]] || fail "a refused create-local left something"
+ bucket theirs "other ${other}" "me ${me}"
+ err="$(szecret create-local theirs/added "${TEST_DIR}/r7" 2>&1)" && fail "a reader not yet rekeyed in should fail"
+ assert_contains "${err}" "szecret rekey theirs first"
+ bucket theirs "other ${other}"
+ touch "${TEST_DIR}/afile"
+ err="$(szecret create-local mine/ff "${TEST_DIR}/afile" 2>&1)" && fail "a file as the directory should fail"
+ assert_contains "${err}" "is not a directory"
+ [[ ! -e "${SZECRET_BUCKETS}/mine/local-key-ff.age" ]] || fail "a refused create-local left a key"
+ err="$(szecret create-local nobucket/x "${TEST_DIR}/r5" 2>&1)" && fail "a missing bucket should fail"
+ assert_contains "${err}" "no bucket nobucket"
+ [[ ! -e "${TEST_DIR}/r5" ]] || fail "a refused create-local left its directory"
+ age-keygen 2>/dev/null | szecret put mine/local-key-dup 2>/dev/null
+ cp "${SZECRET_BUCKETS}/mine/local-key-dup.age" "${SZECRET_BUCKETS}/theirs/"
+ err="$(szecret create-local mine/dup "${TEST_DIR}/r6" 2>&1)" && fail "a key in two places should fail"
+ assert_contains "${err}" "more than one place"
+ rm "${SZECRET_BUCKETS}/mine/local-key-dup.age" "${SZECRET_BUCKETS}/theirs/local-key-dup.age"
+ bucket mine "me ${me}"
+ err="$(szecret create-local mine/stale "${TEST_DIR}/r3" 2>&1)" && fail "create-local into a stale bucket should fail"
+ assert_contains "${err}" "rekey"
+ [[ ! -e "${TEST_DIR}/r3" ]] || fail "a refused put left its directory"
+ [[ ! -e "${SZECRET_BUCKETS}/mine/local-key-stale.age" ]] || fail "a refused put left a key"
+ bucket mine "me ${me}" "other ${other}"
+ pass "create-local: refuses a bucket with no buckets, a bad name, one not read here, a missing one, a key in two places, and a bucket needing a rekey"
+}
+
test_ready() {
szecret ready || fail "ready with an identity"
SZECRET_STORE="" SZECRET_PRIVATE_KEY="${TEST_DIR}/none" szecret ready || fail "ready with only buckets and an identity"
@@ -339,6 +405,9 @@ test_admin_change_needs_rekey
test_list
test_check_ok
test_local_store_key_in_bucket
+test_create_local_in_bucket
+test_create_local_existing_key
+test_create_local_refusals
test_check_stanza_types
test_check_missing_plugin
test_ready