From 990a23be281ee13e33c165f446d2ac3c0c88299c Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Sat, 21 Nov 2026 13:24:26 +0000 Subject: szecret create-local /: a new store's key goes in a bucket this machine reads; plain for an existing store or no buckets --- secrets/create-local.sh | 79 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 24 deletions(-) (limited to 'secrets/create-local.sh') 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 / create-local 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 /.szecret +2. If the store key (local-key-) doesn't exist, generate a new age + private key and put it in , or in the old store on a machine with + no buckets +3. Write the store name and public key to /.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 /${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 -- cgit v1.2.3