Skip to content

Commit

Permalink
Introduce installSecretFn script function to make script shorter.
Browse files Browse the repository at this point in the history
  • Loading branch information
catwith1hat committed Dec 21, 2023
1 parent 13ac9ac commit b3ee74a
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions modules/age.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,49 @@ with lib; let
}
'';

installSecret = secretType: ''
${setTruePath secretType}
echo "decrypting '${secretType.file}' to '$_truePath'..."
TMP_FILE="$_truePath.tmp"
installSecretFn = ''
installSecret() {
SYMLINK=$1
NAME=$2
PATH=$3
FILE=$4
MODE=$5
if "$SYMLINK"; then
_truePath="${cfg.secretsMountPoint}/$_agenix_generation/$NAME"
else
_truePath="$PATH"
fi
echo "decrypting $FILE to '$_truePath'..."
TMP_FILE="$_truePath.tmp"
IDENTITIES=()
for identity in ${toString cfg.identityPaths}; do
test -r "$identity" || continue
IDENTITIES+=(-i)
IDENTITIES+=("$identity")
done
IDENTITIES=()
for identity in ${toString cfg.identityPaths}; do
test -r "$identity" || continue
IDENTITIES+=(-i)
IDENTITIES+=("$identity")
done
test "''${#IDENTITIES[@]}" -eq 0 && echo "[agenix] WARNING: no readable identities found!"
test "''${#IDENTITIES[@]}" -eq 0 && \
echo "[agenix] WARNING: no readable identities found!"
mkdir -p "$(dirname "$_truePath")"
[ "${secretType.path}" != "${cfg.secretsDir}/${secretType.name}" ] && mkdir -p "$(dirname "${secretType.path}")"
(
umask u=r,g=,o=
test -f "${secretType.file}" || echo '[agenix] WARNING: encrypted file ${secretType.file} does not exist!'
test -d "$(dirname "$TMP_FILE")" || echo "[agenix] WARNING: $(dirname "$TMP_FILE") does not exist!"
LANG=${config.i18n.defaultLocale or "C"} ${ageBin} --decrypt "''${IDENTITIES[@]}" -o "$TMP_FILE" "${secretType.file}"
)
chmod ${secretType.mode} "$TMP_FILE"
mv -f "$TMP_FILE" "$_truePath"
mkdir -p "$(dirname "$_truePath")"
[ "$PATH" != "${cfg.secretsDir}/$NAME" ] && mkdir -p "$(dirname "$PATH")"
(
umask u=r,g=,o=
test -f "$FILE" || \
echo '[agenix] WARNING: encrypted file '$FILE' does not exist!'
test -d "$(dirname "$TMP_FILE")" || \
echo "[agenix] WARNING: $(dirname "$TMP_FILE") does not exist!"
LANG=${config.i18n.defaultLocale or "C"} \
${ageBin} --decrypt "''${IDENTITIES[@]}" -o "$TMP_FILE" "$FILE"
)
chmod "$MODE" "$TMP_FILE"
mv -f "$TMP_FILE" "$_truePath"
${optionalString secretType.symlink ''
[ "${secretType.path}" != "${cfg.secretsDir}/${secretType.name}" ] && ln -sfn "${cfg.secretsDir}/${secretType.name}" "${secretType.path}"
''}
"$SYMLINK" && ([ "$PATH" != "${cfg.secretsDir}/$NAME" ] && \
ln -sfn "${cfg.secretsDir}/$NAME" "$PATH")
true
}
'';

testIdentities =
Expand All @@ -110,12 +125,22 @@ with lib; let
}
'';

installSecrets = builtins.concatStringsSep "\n" (
["echo '[agenix] decrypting secrets...'"]
++ testIdentities
++ (map installSecret (builtins.attrValues cfg.secrets))
++ [cleanupAndLink]
);
installSecrets = let
mkLine = secretType: ''
installSecret "${
if secretType.symlink
then "true"
else "false"
}" "${secretType.name}" "${secretType.path}" "${secretType.file}" "${secretType.mode}";
'';
in
builtins.concatStringsSep "\n" (
["echo '[agenix] decrypting secrets...'"]
++ testIdentities
++ [installSecretFn]
++ (map mkLine (builtins.attrValues cfg.secrets))
++ [cleanupAndLink]
);

chownSecret = secretType: ''
${setTruePath secretType}
Expand Down

0 comments on commit b3ee74a

Please sign in to comment.