Jafner.net/homelab/.sops/encrypt-filter.sh
Joey Hafner c02b810237
#3 Update encrypt and decrypt filter scripts
- Decrypt: use realpath of file to decrypt as FILE_PATH
- Decrypt: Switch from in-place to stdout
- Encrypt: Switch from operating on $1 to $FILE_PATH
2024-08-15 16:28:28 -07:00

17 lines
705 B
Bash
Executable File

#!/bin/bash
# Takes file path from stdin
# Outputs to stdout
# Set age directory and default recipients
AGE_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
SOPS_AGE_RECIPIENTS="$(<$AGE_DIR/.age-author-pubkeys)"
FILE_PATH=$(realpath $1)
# Check for host pubkey, add as recipient if present
if [[ -f "$AGE_DIR/../$(realpath -m --relative-to=$AGE_DIR $FILE_PATH | cut -d'/' -f2)/.age-pubkey" ]]; then
HOST_AGE_PUBKEY=$AGE_DIR/../$(realpath -m --relative-to=$AGE_DIR $FILE_PATH | cut -d'/' -f2)/.age-pubkey
HOST_AGE_PUBKEY=$(realpath $HOST_AGE_PUBKEY)
SOPS_AGE_RECIPIENTS="$SOPS_AGE_RECIPIENTS,$(<$HOST_AGE_PUBKEY)"
fi
sops --encrypt --age ${SOPS_AGE_RECIPIENTS} $FILE_PATH