diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d0c2025 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +permissions: + contents: read + +jobs: + deploy-secrets: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + - run: sudo apt-get install --quiet --yes --no-install-recommends age + - run: shellcheck bin/deploy-secrets test/deploy-secrets-test.sh + - run: test/deploy-secrets-test.sh diff --git a/README.md b/README.md index c24f04f..4e2b588 100644 --- a/README.md +++ b/README.md @@ -77,3 +77,32 @@ whoever can move a tag here could read them. The environment stays in the calling workflow. A composite action can't declare one, and the keys are the environment's. + +## Secrets + +`bin/deploy-secrets` keeps a repo's secrets in the repo, encrypted with +[age](https://github.com/FiloSottile/age). Each secret is one file in `secrets/`, +encrypted to the keys in `secrets/recipients`: the people who should read them, +from their SSH keys on GitHub, and the host's key, so it can decrypt them on +deploy. + +```sh +deploy-secrets add-github +deploy-secrets add age1... + +deploy-secrets set API_TOKEN # asks for the value +fly ssh console -C env | deploy-secrets import API_TOKEN OTHER_TOKEN + +deploy-secrets list +deploy-secrets get API_TOKEN +deploy-secrets rekey # after changing secrets/recipients +``` + +Setting a secret only takes the public keys. Reading one, or a `rekey`, takes a +private key: `~/.ssh/id_ed25519`, or the file in `DEPLOY_SECRETS_IDENTITY`. age +can read SSH keys from files, but not from an SSH agent, and it can't use security +key (`sk-`) types, so people using those need an age key instead (`age-keygen`, +then `deploy-secrets add`). + +Removing someone from `secrets/recipients` and running `rekey` only protects new +values: they could read the old ones, and git keeps them. Change the secrets too. diff --git a/bin/deploy-secrets b/bin/deploy-secrets new file mode 100755 index 0000000..76e0d7f --- /dev/null +++ b/bin/deploy-secrets @@ -0,0 +1,157 @@ +#!/bin/sh +# Keep a repo's secrets in the repo, encrypted with age. Each secret is one file, +# secrets/.age, so changes show up per secret and setting one only takes +# the public keys in secrets/recipients. A deploy decrypts them with the host's key. +# +# deploy-secrets add-github people, their keys on GitHub +# deploy-secrets add [comment] hosts, or a key from elsewhere +# deploy-secrets set from a prompt, or piped in +# deploy-secrets import ... from NAME=value lines on stdin +# deploy-secrets get +# deploy-secrets list +# deploy-secrets rm +# deploy-secrets rekey after changing secrets/recipients +# +# Needs age. get and rekey read your private key, DEPLOY_SECRETS_IDENTITY or +# ~/.ssh/id_ed25519 by default. age can't use an SSH agent. +set -eu +umask 077 + +dir=${DEPLOY_SECRETS_DIR:-secrets} +recipients=$dir/recipients +github=${DEPLOY_SECRETS_GITHUB:-https://github.com} + +die() { + echo "$*" >&2 + exit 1 +} + +usage() { + sed -n '/^# /s/^# /usage: /p' "$0" >&2 + exit 1 +} + +check_name() { + case $1 in + '' | [0-9]* | *[!A-Za-z0-9_]*) die "Not an environment variable name: $1" ;; + esac +} + +encrypt() { # name, the value on stdin + [ -s "$recipients" ] || die "No recipients yet, see deploy-secrets add-github" + age --armor --recipients-file "$recipients" --output "$dir/.$1.tmp" || { + rm -f "$dir/.$1.tmp" + die "Couldn't encrypt $1" + } + mv "$dir/.$1.tmp" "$dir/$1.age" +} + +identity() { + identity=${DEPLOY_SECRETS_IDENTITY:-$HOME/.ssh/id_ed25519} + [ -r "$identity" ] || die "Can't read $identity, set DEPLOY_SECRETS_IDENTITY" +} + +decrypt() { # name + [ -f "$dir/$1.age" ] || die "No secret named $1" + age --decrypt --identity "$identity" "$dir/$1.age" +} + +add_recipient() { # key, comment + mkdir -p "$dir" + [ -f "$recipients" ] && grep -qxF "$1" "$recipients" && return 0 + printf '# %s\n%s\n' "$2" "$1" >> "$recipients" + added=1 +} + +added= +after_adding() { + [ -z "$added" ] || ! ls "$dir"/*.age > /dev/null 2>&1 || + echo "Run deploy-secrets rekey for the new recipients to read the existing secrets" >&2 +} + +[ $# -gt 0 ] || usage +command=$1 +shift + +case $command in +add-github) + [ $# -eq 1 ] || usage + keys=$(curl -fsS --max-time 30 "$github/$1.keys") || die "Couldn't get $1's keys from GitHub" + usable=$(printf '%s\n' "$keys" | grep -E '^(ssh-ed25519|ssh-rsa) ' || true) + [ -n "$usable" ] || die "$1 has no ssh-ed25519 or ssh-rsa keys on GitHub, age can't use the others" + while IFS= read -r key; do + add_recipient "$key" "github.com/$1" + done << KEYS +$usable +KEYS + after_adding + ;; +add) + [ $# -ge 1 ] && [ $# -le 2 ] || usage + case $1 in + age1* | "ssh-ed25519 "* | "ssh-rsa "*) ;; + *) die "age can't encrypt to: $1" ;; + esac + add_recipient "$1" "${2:-added by hand}" + after_adding + ;; +set) + [ $# -eq 1 ] || usage + check_name "$1" + if [ -t 0 ]; then + printf '%s: ' "$1" >&2 + stty -echo + IFS= read -r value || true + stty echo + echo >&2 + else + value=$(cat) + fi + [ -n "$value" ] || die "No value for $1" + printf '%s\n' "$value" | encrypt "$1" + ;; +import) + [ $# -ge 1 ] || usage + input=$(cat) + for name in "$@"; do + check_name "$name" + line=$(printf '%s\n' "$input" | grep -m 1 "^$name=" || true) + [ -n "$line" ] || die "No $name on stdin" + printf '%s\n' "${line#*=}" | encrypt "$name" + done + ;; +get) + [ $# -eq 1 ] || usage + check_name "$1" + identity + decrypt "$1" + ;; +list) + [ $# -eq 0 ] || usage + for file in "$dir"/*.age; do + [ -e "$file" ] || continue + name=${file##*/} + echo "${name%.age}" + done + ;; +rm) + [ $# -eq 1 ] || usage + check_name "$1" + [ -f "$dir/$1.age" ] || die "No secret named $1" + rm "$dir/$1.age" + ;; +rekey) + [ $# -eq 0 ] || usage + identity + for file in "$dir"/*.age; do + [ -e "$file" ] || continue + name=${file##*/} + name=${name%.age} + value=$(decrypt "$name") || die "Couldn't decrypt $name, run rekey again with a key that can" + printf '%s\n' "$value" | encrypt "$name" + done + ;; +*) + usage + ;; +esac diff --git a/test/deploy-secrets-test.sh b/test/deploy-secrets-test.sh new file mode 100755 index 0000000..118147b --- /dev/null +++ b/test/deploy-secrets-test.sh @@ -0,0 +1,86 @@ +#!/bin/sh +# Runs bin/deploy-secrets against throwaway keys. Needs age and ssh-keygen. +set -eu + +tool=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)/bin/deploy-secrets +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +cd "$tmp" + +failures=0 +check() { # description, expected, actual + if [ "$2" = "$3" ]; then + echo "ok - $1" + else + echo "not ok - $1: expected '$2', got '$3'" + failures=$((failures + 1)) + fi +} +fails() { # description, command... + description=$1 + shift + if "$@" > /dev/null 2>&1; then + check "$description" failure success + else + check "$description" failure failure + fi +} + +ssh-keygen -q -t ed25519 -N '' -C alice -f alice +ssh-keygen -q -t ed25519 -N '' -C bob -f bob +age-keygen -o vm 2> /dev/null +mkdir -p github +{ + cat alice.pub + echo 'sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29t unusable' +} > github/alice.keys +export DEPLOY_SECRETS_GITHUB="file://$tmp/github" +export DEPLOY_SECRETS_IDENTITY="$tmp/alice" + +fails "set needs a recipient" sh -c "echo x | '$tool' set TOKEN" + +"$tool" add-github alice +check "add-github skips keys age can't use" 1 "$(grep -c '^ssh-' secrets/recipients)" +check "add-github notes where keys came from" 1 "$(grep -c '^# github.com/alice$' secrets/recipients)" +"$tool" add-github alice +check "add-github doesn't add a key twice" 1 "$(grep -c '^ssh-' secrets/recipients)" +fails "add-github needs a usable key" "$tool" add-github nobody +"$tool" add "$(age-keygen -y vm)" vm.example.net +fails "add rejects what age can't use" "$tool" add 'sk-ssh-ed25519@openssh.com AAAA' + +printf 'value with spaces\n' | "$tool" set TOKEN +check "get decrypts what set encrypted" "value with spaces" "$("$tool" get TOKEN)" +check "the file doesn't hold the value" 0 "$(grep -c 'value with spaces' secrets/TOKEN.age || true)" +check "the host's key can read it" "value with spaces" "$(age --decrypt --identity vm secrets/TOKEN.age)" + +printf 'A=1\nB=x=y\nC=not imported\n' | "$tool" import A B +check "import keeps = in values" "x=y" "$("$tool" get B)" +check "import only takes the names given" "A +B +TOKEN" "$("$tool" list)" +fails "import needs every name" sh -c "echo A=1 | '$tool' import MISSING" + +for name in 1A A-B 'A B' ../A ''; do + fails "set rejects the name '$name'" sh -c "echo x | '$tool' set '$name'" +done +fails "set rejects an empty value" sh -c "printf '' | '$tool' set EMPTY" +fails "get fails for a missing secret" "$tool" get MISSING + +fails "bob can't read before a rekey" env DEPLOY_SECRETS_IDENTITY="$tmp/bob" "$tool" get TOKEN +message=$("$tool" add "$(cat bob.pub)" bob 2>&1) +check "add says when a rekey is needed" 1 "$(printf '%s\n' "$message" | grep -c rekey)" +"$tool" rekey +check "bob can read after a rekey" "value with spaces" "$(DEPLOY_SECRETS_IDENTITY="$tmp/bob" "$tool" get TOKEN)" +check "a rekey keeps the value for the host" "x=y" "$(age --decrypt --identity vm secrets/B.age)" +fails "a key not on the list can't rekey" env DEPLOY_SECRETS_IDENTITY="$tmp/vm-missing" "$tool" rekey + +"$tool" rm A +check "rm removes the secret" "B +TOKEN" "$("$tool" list)" +check "no temporary files are left" 0 "$(find secrets -name '.*' -type f | wc -l | tr -d ' ')" + +[ "$failures" -eq 0 ] || { + echo "$failures failed" + exit 1 +} +echo "all passed"