Homelab: Regenerating certificates
Previously, we spent huge amount of time to set up an intermediate CA.
Our goal was to be able to deploy it on Barracuda, so it can automatically generate trusted certificates. The idea, borrowed from Meta engineering blog, is to ensure that, even if a certificate is broken, it won't affect for a long time.
Let's start by changing the way we're generating certificated, to work in a non-interactive mode:
#!/usr/bin/env bash
if [;
then
fi
PASS_FILE=
SERVICE=
CRT_DIR=server/generated/services/
CA_DIR=server/generated/ca-intermediate
REQ_DIR=server/requests
COMMON_NAME=" .barracuda.local ( )"
|
# Create a new certificate
# Sign it with our CA
TARGET_DIR=/etc/nixos/certificates/servers/
TARGET_LOGIN=black@192.168.0.4
Then, we have to set up a cron job (well, a systemd timer):
systemd.timers."certs-regen-withing" = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "1m";
OnCalendar = "*-*-* 3:00:00";
Unit = "certs-regen-withing.service";
};
};
systemd.services."certs-regen-withing" = {
script = ''
set -eu
/etc/nixos/generateServerServiceBatch.sh "/etc/nixos/certs-pw" "withing"
pkgs.systemd/bin/systemctl restart nginx.service
'';
serviceConfig = {
Type = "oneshot";
User = "root";
WorkingDirectory=/etc/nixos
};
};
Finally, the certificate will be regenerated at 3 AM every day (when I'm "usually" sleeping).