1
1
{ writeShellScriptBin , runtimeShell , age } :
2
2
writeShellScriptBin "agenix" ''
3
- set -euo pipefail
3
+ set -Eeuo pipefail
4
4
5
5
PACKAGE="agenix"
6
6
@@ -84,7 +84,7 @@ trap "cleanup" 0 2 3 15
84
84
85
85
function edit {
86
86
FILE=$1
87
- KEYS=$(nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".public_keys)" | sed 's/"//g' | sed 's/\\n/\n/g')
87
+ KEYS=$(( nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".public_keys)" | sed 's/"//g' | sed 's/\\n/\n/g') || exit 1 )
88
88
89
89
if [ -z "$KEYS" ]
90
90
then
@@ -101,13 +101,21 @@ function edit {
101
101
while IFS= read -r key
102
102
do
103
103
DECRYPT+=(--identity "$key")
104
- done <<<$( find ~/.ssh -maxdepth 1 -type f -not -name "*pub" -not -name "config" -not -name "authorized_keys" -not -name "known_hosts")
104
+ done <<<"$(( find ~/.ssh -maxdepth 1 -type f -not -name "*pub" -not -name "config" -not -name "authorized_keys" -not -name "known_hosts") || exit 1)"
105
105
DECRYPT+=(-o "$CLEARTEXT_FILE" "$FILE")
106
- ${ age } /bin/age "'' ${DECRYPT[@]}"
106
+ ${ age } /bin/age "'' ${DECRYPT[@]}" || exit 1
107
+ cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
107
108
fi
108
109
109
110
$EDITOR "$CLEARTEXT_FILE"
110
111
112
+ if [ ! -f "$CLEARTEXT_FILE" ]
113
+ then
114
+ echo "$FILE wasn't created."
115
+ return
116
+ fi
117
+ [ -f "$FILE" ] && [ "$EDITOR" != ":" ] && diff "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" 1>/dev/null && echo "$FILE wasn't changed, skipping re-encryption." && return
118
+
111
119
ENCRYPT=()
112
120
while IFS= read -r key
113
121
do
@@ -119,21 +127,22 @@ function edit {
119
127
120
128
ENCRYPT+=(-o "$REENCRYPTED_FILE")
121
129
122
- cat "$CLEARTEXT_FILE" | $ {age } /bin/age "'' ${ENCRYPT[@]}"
130
+ $ {age } /bin/age "'' ${ENCRYPT[@]}" <"$CLEARTEXT_FILE" || exit 1
123
131
124
132
mv -f "$REENCRYPTED_FILE" "$1"
125
133
}
126
134
127
135
function rekey {
128
- echo "rekeying..."
129
- FILES=$(nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" (builtins.attrNames rules))" | sed 's/"//g' | sed 's/\\n/\n/g')
136
+ 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)
130
137
131
138
for FILE in $FILES
132
139
do
133
- EDITOR=: edit $FILE
140
+ echo "rekeying $FILE..."
141
+ EDITOR=: edit "$FILE"
142
+ cleanup
134
143
done
135
144
}
136
145
137
146
[ $REKEY -eq 1 ] && rekey && exit 0
138
- edit $FILE && exit 0
147
+ edit " $FILE" && cleanup && exit 0
139
148
''
0 commit comments