diff options
Diffstat (limited to 'secrets/put.sh')
| -rwxr-xr-x | secrets/put.sh | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/secrets/put.sh b/secrets/put.sh index 2675066..b060255 100755 --- a/secrets/put.sh +++ b/secrets/put.sh @@ -4,24 +4,20 @@ set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat <<EOF -put: Store a secret in the encrypted store +put: Store a secret, read from stdin -Encrypts data from stdin and stores it as a named secret in the global store. +Encrypts stdin to the bucket's readers and the admins. Needs only public keys. Usage: - put <secret-name> - echo "secret-value" | put <secret-name> + echo "value" | put <bucket>/<name> + echo "value" | put <name> the admin-only bucket, the narrowest set put -h|--help -Arguments: - secret-name Name to store the secret under - -The command will: -1. Read data from stdin -2. Encrypt the data using age with the public key -3. Store the encrypted data in the store as <secret-name>.age - -Note: Will overwrite existing secrets with the same name. +Overwrites <name> where it already is. Refuses if <name> is in another bucket +(it moves with mv, never copies), if the bucket's readers or the admins changed +since its last rekey, or if there are no admins. A <name> still in the old +store is rewritten there until it moves. On a machine with no buckets, <name> +goes to the old store, as before buckets. EOF exit 0 fi @@ -36,5 +32,44 @@ fi name="$1" ensure_age -age_encrypt "${public_key}" "$(file_for "${name}")" +# Before buckets: the old store, as it always was. +if [[ -z "${buckets}" ]]; then + [[ "${name}" != */* ]] || die "no buckets on this machine: ${name}" + age_encrypt "${public_key}" "$(file_for "${name}")" + exit 0 +fi + +if [[ "${name}" == */* ]]; then + bucket="${name%%/*}" + name="${name#*/}" + [[ "${bucket}" =~ ${re_bucket} ]] || die "not a bucket's name: ${bucket}" +else + bucket="" +fi +[[ "${name}" =~ ${re_secret} ]] || die "not a secret's name: ${name}" + +found="$(locate "${name}")" +if [[ -z "${bucket}" ]]; then + if [[ -n "${store}" && "${found}" == "${store}/${name}.age" ]]; then + >&2 echo "szecret: ${name} is still in the old store; rewriting it there" + age_encrypt "${public_key}" "${found}" + exit 0 + fi + if [[ -z "${found}" ]]; then + bucket="admin-only" + >&2 echo "szecret: storing ${name} in admin-only (put <bucket>/${name} for another bucket)" + fi +fi +while read -r f; do + [[ -n "${f}" && "${f}" != "${buckets}/${bucket}/${name}.age" ]] || continue + if [[ -n "${store}" && "$(dirname "${f}")" == "${store}" ]]; then + die "${name} is in the old store; szecret mv ${name} ${bucket:-<bucket>} moves it" + fi + die "${name} is in $(bucket_label "$(dirname "${f}")"); use $(where_is "${f}")" +done <<<"${found}" + +dir="${buckets}/${bucket}" +set="$(bucket_set "${dir}")" || exit 1 +age_version_ok +encrypt_to "${dir}/${name}.age" "${set}" |
