2021-05-04 15:24:31 +02:00
|
|
|
{
|
2021-07-20 17:50:08 +02:00
|
|
|
lib,
|
2021-05-04 15:24:31 +02:00
|
|
|
writeShellScriptBin,
|
|
|
|
runtimeShell,
|
|
|
|
callPackage,
|
|
|
|
rage,
|
|
|
|
gnused,
|
|
|
|
nix,
|
|
|
|
mktemp,
|
2021-07-22 13:58:29 +02:00
|
|
|
diffutils,
|
2021-12-06 00:08:18 +01:00
|
|
|
ageBin ? "${
|
|
|
|
# we need at least rage 0.5.0 to support ssh keys
|
|
|
|
if rage.version < "0.5.0"
|
|
|
|
then callPackage ./rage.nix {}
|
|
|
|
else rage
|
|
|
|
}/bin/rage"
|
2021-05-04 15:24:31 +02:00
|
|
|
} :
|
2020-09-18 20:59:01 +02:00
|
|
|
let
|
2021-05-04 15:24:31 +02:00
|
|
|
sedBin = "${gnused}/bin/sed";
|
|
|
|
nixInstantiate = "${nix}/bin/nix-instantiate";
|
|
|
|
mktempBin = "${mktemp}/bin/mktemp";
|
2021-07-22 13:58:29 +02:00
|
|
|
diffBin = "${diffutils}/bin/diff";
|
2020-09-18 20:59:01 +02:00
|
|
|
in
|
2021-07-20 17:50:08 +02:00
|
|
|
lib.recursiveUpdate (writeShellScriptBin "agenix" ''
|
2020-09-04 01:51:23 +02:00
|
|
|
set -Eeuo pipefail
|
2020-09-04 00:18:20 +02:00
|
|
|
|
2020-09-03 20:24:33 +02:00
|
|
|
PACKAGE="agenix"
|
|
|
|
|
|
|
|
function show_help () {
|
|
|
|
echo "$PACKAGE - edit and rekey age secret files"
|
|
|
|
echo " "
|
2020-09-03 22:16:44 +02:00
|
|
|
echo "$PACKAGE -e FILE [-i PRIVATE_KEY]"
|
|
|
|
echo "$PACKAGE -r [-i PRIVATE_KEY]"
|
2020-09-03 20:24:33 +02:00
|
|
|
echo ' '
|
|
|
|
echo 'options:'
|
|
|
|
echo '-h, --help show help'
|
|
|
|
echo '-e, --edit FILE edits FILE using $EDITOR'
|
|
|
|
echo '-r, --rekey re-encrypts all secrets with specified recipients'
|
2020-09-03 22:16:44 +02:00
|
|
|
echo '-i, --identity identity to use when decrypting'
|
2020-12-19 17:52:54 +01:00
|
|
|
echo '-v, --verbose verbose output'
|
2020-09-03 20:24:33 +02:00
|
|
|
echo ' '
|
|
|
|
echo 'FILE an age-encrypted file'
|
|
|
|
echo ' '
|
2020-09-03 22:16:44 +02:00
|
|
|
echo 'PRIVATE_KEY a path to a private SSH key used to decrypt file'
|
|
|
|
echo ' '
|
2020-09-03 20:24:33 +02:00
|
|
|
echo 'EDITOR environment variable of editor to use when editing FILE'
|
|
|
|
echo ' '
|
2020-09-04 00:18:20 +02:00
|
|
|
echo 'RULES environment variable with path to Nix file specifying recipient public keys.'
|
2020-09-04 01:07:43 +02:00
|
|
|
echo "Defaults to './secrets.nix'"
|
2020-09-18 22:13:54 +02:00
|
|
|
echo ' '
|
2022-09-25 23:14:27 +02:00
|
|
|
echo "agenix version: 0.12.0"
|
2020-09-18 22:13:54 +02:00
|
|
|
echo "age binary path: ${ageBin}"
|
|
|
|
echo "age version: $(${ageBin} --version)"
|
2020-09-03 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
test $# -eq 0 && (show_help && exit 1)
|
|
|
|
|
|
|
|
REKEY=0
|
2020-09-04 00:18:20 +02:00
|
|
|
DEFAULT_DECRYPT=(--decrypt)
|
2020-09-03 20:24:33 +02:00
|
|
|
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-e|--edit)
|
|
|
|
shift
|
|
|
|
if test $# -gt 0; then
|
|
|
|
export FILE=$1
|
|
|
|
else
|
2020-09-03 22:16:44 +02:00
|
|
|
echo "no FILE specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-i|--identity)
|
|
|
|
shift
|
|
|
|
if test $# -gt 0; then
|
2020-09-04 00:18:20 +02:00
|
|
|
DEFAULT_DECRYPT+=(--identity "$1")
|
2020-09-03 22:16:44 +02:00
|
|
|
else
|
|
|
|
echo "no PRIVATE_KEY specified"
|
2020-09-03 20:24:33 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-r|--rekey)
|
|
|
|
shift
|
|
|
|
REKEY=1
|
|
|
|
;;
|
2020-12-19 17:52:54 +01:00
|
|
|
-v|--verbose)
|
|
|
|
shift
|
|
|
|
set -x
|
|
|
|
;;
|
2020-09-03 20:24:33 +02:00
|
|
|
*)
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-09-04 01:07:43 +02:00
|
|
|
RULES=''${RULES:-./secrets.nix}
|
2020-09-03 20:24:33 +02:00
|
|
|
|
|
|
|
function cleanup {
|
|
|
|
if [ ! -z ''${CLEARTEXT_DIR+x} ]
|
|
|
|
then
|
|
|
|
rm -rf "$CLEARTEXT_DIR"
|
|
|
|
fi
|
|
|
|
if [ ! -z ''${REENCRYPTED_DIR+x} ]
|
|
|
|
then
|
|
|
|
rm -rf "$REENCRYPTED_DIR"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap "cleanup" 0 2 3 15
|
|
|
|
|
|
|
|
function edit {
|
|
|
|
FILE=$1
|
2022-05-15 20:03:52 +02:00
|
|
|
KEYS=$((${nixInstantiate} --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".publicKeys)" | ${sedBin} 's/"//g' | ${sedBin} 's/\\n/\n/g') | ${sedBin} '/^$/d' || exit 1)
|
2020-09-04 01:07:43 +02:00
|
|
|
|
2020-09-03 20:24:33 +02:00
|
|
|
if [ -z "$KEYS" ]
|
|
|
|
then
|
|
|
|
>&2 echo "There is no rule for $FILE in $RULES."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-04 15:24:31 +02:00
|
|
|
CLEARTEXT_DIR=$(${mktempBin} -d)
|
2020-09-03 20:24:33 +02:00
|
|
|
CLEARTEXT_FILE="$CLEARTEXT_DIR/$(basename "$FILE")"
|
|
|
|
|
|
|
|
if [ -f "$FILE" ]
|
|
|
|
then
|
2020-09-04 00:18:20 +02:00
|
|
|
DECRYPT=("''${DEFAULT_DECRYPT[@]}")
|
2020-12-19 04:23:47 +01:00
|
|
|
if [ -f "$HOME/.ssh/id_rsa" ]; then
|
|
|
|
DECRYPT+=(--identity "$HOME/.ssh/id_rsa")
|
|
|
|
fi
|
|
|
|
if [ -f "$HOME/.ssh/id_ed25519" ]; then
|
|
|
|
DECRYPT+=(--identity "$HOME/.ssh/id_ed25519")
|
|
|
|
fi
|
2020-12-19 04:57:48 +01:00
|
|
|
if [[ "''${DECRYPT[*]}" != *"--identity"* ]]; then
|
|
|
|
echo "No identity found to decrypt $FILE. Try adding an SSH key at $HOME/.ssh/id_rsa or $HOME/.ssh/id_ed25519 or using the --identity flag to specify a file."
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-09-03 20:24:33 +02:00
|
|
|
DECRYPT+=(-o "$CLEARTEXT_FILE" "$FILE")
|
2020-09-18 20:59:01 +02:00
|
|
|
${ageBin} "''${DECRYPT[@]}" || exit 1
|
2020-09-04 01:51:23 +02:00
|
|
|
cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
|
2020-09-03 20:24:33 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
$EDITOR "$CLEARTEXT_FILE"
|
|
|
|
|
2020-09-04 01:51:23 +02:00
|
|
|
if [ ! -f "$CLEARTEXT_FILE" ]
|
|
|
|
then
|
|
|
|
echo "$FILE wasn't created."
|
|
|
|
return
|
|
|
|
fi
|
2021-07-22 13:58:29 +02:00
|
|
|
[ -f "$FILE" ] && [ "$EDITOR" != ":" ] && ${diffBin} "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" 1>/dev/null && echo "$FILE wasn't changed, skipping re-encryption." && return
|
2020-09-04 01:51:23 +02:00
|
|
|
|
2020-09-03 20:24:33 +02:00
|
|
|
ENCRYPT=()
|
|
|
|
while IFS= read -r key
|
|
|
|
do
|
|
|
|
ENCRYPT+=(--recipient "$key")
|
|
|
|
done <<< "$KEYS"
|
|
|
|
|
2021-05-04 15:24:31 +02:00
|
|
|
REENCRYPTED_DIR=$(${mktempBin} -d)
|
2020-09-03 20:24:33 +02:00
|
|
|
REENCRYPTED_FILE="$REENCRYPTED_DIR/$(basename "$FILE")"
|
|
|
|
|
|
|
|
ENCRYPT+=(-o "$REENCRYPTED_FILE")
|
|
|
|
|
2020-09-18 20:59:01 +02:00
|
|
|
${ageBin} "''${ENCRYPT[@]}" <"$CLEARTEXT_FILE" || exit 1
|
2020-09-03 20:24:33 +02:00
|
|
|
|
|
|
|
mv -f "$REENCRYPTED_FILE" "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function rekey {
|
2021-05-05 05:28:24 +02:00
|
|
|
FILES=$((${nixInstantiate} --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" (builtins.attrNames rules))" | ${sedBin} 's/"//g' | ${sedBin} 's/\\n/\n/g') || exit 1)
|
2020-09-04 01:07:43 +02:00
|
|
|
|
2020-09-03 20:24:33 +02:00
|
|
|
for FILE in $FILES
|
|
|
|
do
|
2020-09-04 01:51:23 +02:00
|
|
|
echo "rekeying $FILE..."
|
|
|
|
EDITOR=: edit "$FILE"
|
|
|
|
cleanup
|
2020-09-03 20:24:33 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
[ $REKEY -eq 1 ] && rekey && exit 0
|
2020-09-04 01:51:23 +02:00
|
|
|
edit "$FILE" && cleanup && exit 0
|
2021-07-20 17:50:08 +02:00
|
|
|
'')
|
|
|
|
|
|
|
|
{
|
|
|
|
meta.description = "age-encrypted secrets for NixOS";
|
|
|
|
}
|