From 937c75e0978561ac248a03aa75b30a39e5123a4a Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Tue, 12 Aug 2025 10:59:02 +0200 Subject: [PATCH] ocserv: notify users about certificates about to expire --- roles/ocserv/files/notify-expiring-certs | 46 ++++++++++++++++++++++++ roles/ocserv/meta/main.yml | 3 +- roles/ocserv/tasks/main.yml | 22 ++++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 roles/ocserv/files/notify-expiring-certs diff --git a/roles/ocserv/files/notify-expiring-certs b/roles/ocserv/files/notify-expiring-certs new file mode 100644 index 0000000..2b8f4f3 --- /dev/null +++ b/roles/ocserv/files/notify-expiring-certs @@ -0,0 +1,46 @@ +#!/bin/sh + +set -e + +for cert in /var/lib/ocserv/certs/*.crt ; do + # get email + email="$(openssl x509 -noout -email -in "${cert}")" + if [ -z "${email}" ] ; then + # if emailAddress is not specified in certificate, assume CN is the email + email="$(openssl x509 -noout -subject -in "${cert}" | sed 's/^.* CN = \([^,]*\).*$/\1/')" + fi + if [ -z "${email}" ] ; then + # bail if we still don’t have an email to send to + continue + fi + + # get number of days the certificate will remain valid for + end="$(openssl x509 -noout -dateopt iso_8601 -enddate -in "${cert}" | cut -d '=' -f 2)" + validity="$(( ($(date -d "${end}" +%s) - $(date +%s)) / 86400 ))" + + # send notice 14 and 7 days before expiry + if [ "${validity}" -eq 14 ] || [ "${validity}" -eq 7 ] ; then + /usr/sbin/sendmail -t <