2020-09-18 21:59:01 +03:00
|
|
|
{writeShellScriptBin, runtimeShell, pkgs} :
|
|
|
|
let
|
|
|
|
rage = pkgs.callPackage ./rage.nix {};
|
|
|
|
ageBin = "${rage}/bin/rage";
|
|
|
|
in
|
2020-09-03 21:24:33 +03:00
|
|
|
writeShellScriptBin "agenix" ''
|
2020-09-04 02:51:23 +03:00
|
|
|
set -Eeuo pipefail
|
2020-09-04 01:18:20 +03:00
|
|
|
|
2020-09-03 21:24:33 +03:00
|
|
|
PACKAGE="agenix"
|
|
|
|
|
|
|
|
function show_help () {
|
|
|
|
echo "$PACKAGE - edit and rekey age secret files"
|
|
|
|
echo " "
|
2020-09-03 23:16:44 +03:00
|
|
|
echo "$PACKAGE -e FILE [-i PRIVATE_KEY]"
|
|
|
|
echo "$PACKAGE -r [-i PRIVATE_KEY]"
|
2020-09-03 21:24:33 +03: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 23:16:44 +03:00
|
|
|
echo '-i, --identity identity to use when decrypting'
|
2020-09-03 21:24:33 +03:00
|
|
|
echo ' '
|
|
|
|
echo 'FILE an age-encrypted file'
|
|
|
|
echo ' '
|
2020-09-03 23:16:44 +03:00
|
|
|
echo 'PRIVATE_KEY a path to a private SSH key used to decrypt file'
|
|
|
|
echo ' '
|
2020-09-03 21:24:33 +03:00
|
|
|
echo 'EDITOR environment variable of editor to use when editing FILE'
|
|
|
|
echo ' '
|
2020-09-04 01:18:20 +03:00
|
|
|
echo 'RULES environment variable with path to Nix file specifying recipient public keys.'
|
2020-09-04 02:07:43 +03:00
|
|
|
echo "Defaults to './secrets.nix'"
|
2020-09-18 23:13:54 +03:00
|
|
|
echo ' '
|
|
|
|
echo "age binary path: ${ageBin}"
|
|
|
|
echo "age version: $(${ageBin} --version)"
|
2020-09-03 21:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
test $# -eq 0 && (show_help && exit 1)
|
|
|
|
|
|
|
|
REKEY=0
|
2020-09-04 01:18:20 +03:00
|
|
|
DEFAULT_DECRYPT=(--decrypt)
|
2020-09-03 21:24:33 +03: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 23:16:44 +03:00
|
|
|
echo "no FILE specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-i|--identity)
|
|
|
|
shift
|
|
|
|
if test $# -gt 0; then
|
2020-09-04 01:18:20 +03:00
|
|
|
DEFAULT_DECRYPT+=(--identity "$1")
|
2020-09-03 23:16:44 +03:00
|
|
|
else
|
|
|
|
echo "no PRIVATE_KEY specified"
|
2020-09-03 21:24:33 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-r|--rekey)
|
|
|
|
shift
|
|
|
|
REKEY=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-09-04 02:07:43 +03:00
|
|
|
RULES=''${RULES:-./secrets.nix}
|
2020-09-03 21:24:33 +03: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
|
2020-09-04 07:13:10 +03:00
|
|
|
KEYS=$((nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".publicKeys)" | sed 's/"//g' | sed 's/\\n/\n/g') || exit 1)
|
2020-09-04 02:07:43 +03:00
|
|
|
|
2020-09-03 21:24:33 +03:00
|
|
|
if [ -z "$KEYS" ]
|
|
|
|
then
|
|
|
|
>&2 echo "There is no rule for $FILE in $RULES."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
CLEARTEXT_DIR=$(mktemp -d)
|
|
|
|
CLEARTEXT_FILE="$CLEARTEXT_DIR/$(basename "$FILE")"
|
|
|
|
|
|
|
|
if [ -f "$FILE" ]
|
|
|
|
then
|
2020-09-04 01:18:20 +03:00
|
|
|
DECRYPT=("''${DEFAULT_DECRYPT[@]}")
|
2020-09-03 21:24:33 +03:00
|
|
|
while IFS= read -r key
|
|
|
|
do
|
|
|
|
DECRYPT+=(--identity "$key")
|
2020-09-04 02:51:23 +03:00
|
|
|
done <<<"$((find ~/.ssh -maxdepth 1 -type f -not -name "*pub" -not -name "config" -not -name "authorized_keys" -not -name "known_hosts") || exit 1)"
|
2020-09-03 21:24:33 +03:00
|
|
|
DECRYPT+=(-o "$CLEARTEXT_FILE" "$FILE")
|
2020-09-18 21:59:01 +03:00
|
|
|
${ageBin} "''${DECRYPT[@]}" || exit 1
|
2020-09-04 02:51:23 +03:00
|
|
|
cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
|
2020-09-03 21:24:33 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
$EDITOR "$CLEARTEXT_FILE"
|
|
|
|
|
2020-09-04 02:51:23 +03:00
|
|
|
if [ ! -f "$CLEARTEXT_FILE" ]
|
|
|
|
then
|
|
|
|
echo "$FILE wasn't created."
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
[ -f "$FILE" ] && [ "$EDITOR" != ":" ] && diff "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" 1>/dev/null && echo "$FILE wasn't changed, skipping re-encryption." && return
|
|
|
|
|
2020-09-03 21:24:33 +03:00
|
|
|
ENCRYPT=()
|
|
|
|
while IFS= read -r key
|
|
|
|
do
|
|
|
|
ENCRYPT+=(--recipient "$key")
|
|
|
|
done <<< "$KEYS"
|
|
|
|
|
|
|
|
REENCRYPTED_DIR=$(mktemp -d)
|
|
|
|
REENCRYPTED_FILE="$REENCRYPTED_DIR/$(basename "$FILE")"
|
|
|
|
|
|
|
|
ENCRYPT+=(-o "$REENCRYPTED_FILE")
|
|
|
|
|
2020-09-18 21:59:01 +03:00
|
|
|
${ageBin} "''${ENCRYPT[@]}" <"$CLEARTEXT_FILE" || exit 1
|
2020-09-03 21:24:33 +03:00
|
|
|
|
|
|
|
mv -f "$REENCRYPTED_FILE" "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function rekey {
|
2020-09-04 02:51:23 +03:00
|
|
|
FILES=$((nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" (builtins.attrNames rules))" | sed 's/"//g' | sed 's/\\n/\n/g') || exit 1)
|
2020-09-04 02:07:43 +03:00
|
|
|
|
2020-09-03 21:24:33 +03:00
|
|
|
for FILE in $FILES
|
|
|
|
do
|
2020-09-04 02:51:23 +03:00
|
|
|
echo "rekeying $FILE..."
|
|
|
|
EDITOR=: edit "$FILE"
|
|
|
|
cleanup
|
2020-09-03 21:24:33 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
[ $REKEY -eq 1 ] && rekey && exit 0
|
2020-09-04 02:51:23 +03:00
|
|
|
edit "$FILE" && cleanup && exit 0
|
2020-09-03 21:24:33 +03:00
|
|
|
''
|