all repos — dot @ e01393235ef52fc05f2a4a3768f22e73eb12cd73

Shell, editor and terminal configuration, and the tools that go with them

secrets/get.sh (view raw)

 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 from the store

Decrypts and outputs a secret value from the global secret store.

Usage:
  get <secret-name>
  get -h|--help

Arguments:
  secret-name    Name of the secret to retrieve

The command will:
1. Check if the secret exists in the store
2. Ensure the private key is available
3. Decrypt the secret using age and output to stdout

Note: Exits with error if secret doesn't exist or decryption fails.
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="$(file_for "${name}")"
if ! [[ -r "${file}" ]]; then
    >&2 echo "ERROR: Secret '$name' not found"
    exit 1
fi

ensure_private_key

age_decrypt "${private_key}" "${file}"