improving renew script

This commit is contained in:
John Lancaster
2026-04-04 09:46:55 -05:00
parent 710f13ace4
commit 6c26e898b2
+25 -4
View File
@@ -140,6 +140,8 @@ let
chownCmd = lib.getExe' pkgs.coreutils "chown"; chownCmd = lib.getExe' pkgs.coreutils "chown";
chmodCmd = lib.getExe' pkgs.coreutils "chmod"; chmodCmd = lib.getExe' pkgs.coreutils "chmod";
stepCmd = lib.getExe pkgs.step-cli; stepCmd = lib.getExe pkgs.step-cli;
hasReloadUnits = reloadUnits != [ ];
hasPostCommands = postCommands != [ ];
systemctlCmd = "${lib.getExe' pkgs.systemd "systemctl"} ${lib.escapeShellArgs systemctlArgs}"; systemctlCmd = "${lib.getExe' pkgs.systemd "systemctl"} ${lib.escapeShellArgs systemctlArgs}";
renewReloadScript = lib.concatMapStringsSep "\n" (unit: '' renewReloadScript = lib.concatMapStringsSep "\n" (unit: ''
if ${systemctlCmd} --quiet is-active "${unit}"; then if ${systemctlCmd} --quiet is-active "${unit}"; then
@@ -151,23 +153,42 @@ let
pkgs.writeShellScriptBin "mtls-renew" '' pkgs.writeShellScriptBin "mtls-renew" ''
set -euo pipefail set -euo pipefail
if ${stepCmd} certificate needs-renewal "${certFile}"; then YELLOW_BANG="\e[33m!\e[0m"
${echoCmd} "Renewing mTLS certificate"
else force=0
while [[ $# -gt 0 ]]; do
case $1 in
--force)
force=1
shift
;;
*)
${echoCmd} -e "$YELLOW_BANG Warning: ignoring unrecognized argument '$1'"
exit 1
;;
esac
done
if [[ $force -eq 0 ]] && ! ${stepCmd} certificate needs-renewal "${certFile}"; then
${echoCmd} "Skipping renew" ${echoCmd} "Skipping renew"
exit "$?" exit 0
fi fi
${echoCmd} "Renewing mTLS certificate"
${stepCmd} ca renew --force "${certFile}" "${keyFile}" ${stepCmd} ca renew --force "${certFile}" "${keyFile}"
(umask 077; ${catCmd} "${certFile}" "${keyFile}" > "${bundleFile}") (umask 077; ${catCmd} "${certFile}" "${keyFile}" > "${bundleFile}")
${chownCmd} ${user}:${group} ${certFile} ${keyFile} ${bundleFile} ${chownCmd} ${user}:${group} ${certFile} ${keyFile} ${bundleFile}
${chmodCmd} 640 ${certFile} ${keyFile} ${bundleFile} ${chmodCmd} 640 ${certFile} ${keyFile} ${bundleFile}
${lib.optionalString hasReloadUnits ''
${echoCmd} "Reloading units:" ${echoCmd} "Reloading units:"
${renewReloadScript} ${renewReloadScript}
''}
${lib.optionalString hasPostCommands ''
${echoCmd} "Post commands:" ${echoCmd} "Post commands:"
${renewPostCommands} ${renewPostCommands}
''}
''; '';
mkNixosMtlsRenewService = { mkNixosMtlsRenewService = {