check-certificates: use time functionality

No need to calculate that...
This commit is contained in:
Christian Hesse 2019-01-09 11:43:30 +01:00
parent 5539233417
commit 1ee2491e66

View file

@ -10,30 +10,12 @@
:global SendNotification; :global SendNotification;
:local Months { "jan"; "feb"; "mar"; "apr"; "may"; "jun";
"jul"; "aug"; "sep"; "oct"; "nov"; "dec" };
:local CurrentDate [ / system clock get date ];
:local CurrentDay [ :pick $CurrentDate 4 6 ];
:local CurrentYear [ :pick $CurrentDate 7 11 ];
:local CurrentMonth ([ :find $Months [ :pick $CurrentDate 0 3 ] ] + 1);
:local CurrentStamp ($CurrentYear * 365 + $CurrentMonth * 30 + $CurrentDay);
:foreach Cert in=[ / certificate find where !revoked ] do={ :foreach Cert in=[ / certificate find where !revoked ] do={
:local CertName [ / certificate get $Cert name ]; :local CertName [ / certificate get $Cert name ];
:local InvalidBefore [ / certificate get $Cert invalid-before ]; :local ExpiresAfter [ / certificate get $Cert expires-after ];
:local InvalidAfter [ / certificate get $Cert invalid-after ];
:if ([ :len $InvalidAfter ] > 0) do={ :if ([ :typeof $ExpiresAfter ] = "time") do={
:local InvalidDay [ :pick $InvalidAfter 4 6 ]; :if ($ExpiresAfter < 2w) do={
:local InvalidYear [ :pick $InvalidAfter 7 11 ];
:local InvalidMonth ([ :find $Months [ :pick $InvalidAfter 0 3 ] ] + 1);
:local InvalidStamp ($InvalidYear * 365 + $InvalidMonth * 30 + $InvalidDay);
:local Remaining ($InvalidStamp - $CurrentStamp);
:if ($Remaining < 15) do={
:local CommonName [ / certificate get $Cert common-name ]; :local CommonName [ / certificate get $Cert common-name ];
:local FingerPrint [ / certificate get $Cert fingerprint ]; :local FingerPrint [ / certificate get $Cert fingerprint ];
@ -64,8 +46,8 @@
/ certificate remove $Cert; / certificate remove $Cert;
/ certificate set $CertNew name=$CertName; / certificate set $CertNew name=$CertName;
:set InvalidBefore [ / certificate get $CertNew invalid-before ]; :local InvalidBefore [ / certificate get $CertNew invalid-before ];
:set InvalidAfter [ / certificate get $CertNew invalid-after ]; :local InvalidAfter [ / certificate get $CertNew invalid-after ];
:set FingerPrint [ / certificate get $CertNew fingerprint ]; :set FingerPrint [ / certificate get $CertNew fingerprint ];
$SendNotification ("Certificate renewed") \ $SendNotification ("Certificate renewed") \
@ -76,16 +58,19 @@
"Validity: " . $InvalidBefore . " to " . $InvalidAfter); "Validity: " . $InvalidBefore . " to " . $InvalidAfter);
:log info ("The certificate " . $CertName . " has been renewed."); :log info ("The certificate " . $CertName . " has been renewed.");
} on-error={ } on-error={
:local InvalidBefore [ / certificate get $Cert invalid-before ];
:local InvalidAfter [ / certificate get $Cert invalid-after ];
$SendNotification ("Certificate warning!") \ $SendNotification ("Certificate warning!") \
("A certificate on " . $Identity . " is about to expire.\n\n" . \ ("A certificate on " . $Identity . " is about to expire.\n\n" . \
"Certificate Name: " . $CertName . "\n" . \ "Certificate Name: " . $CertName . "\n" . \
"Common Name: " . $CommonName . "\n" . \ "Common Name: " . $CommonName . "\n" . \
"Fingerprint: " . $FingerPrint . "\n" . \ "Fingerprint: " . $FingerPrint . "\n" . \
"Validity: " . $InvalidBefore . " to " . $InvalidAfter); "Validity: " . $InvalidBefore . " to " . $InvalidAfter);
:log warning ("A certificate is about to expire within " . $Remaining . " days: " . $CertName); :log warning ("The certificate " . $CertName . " is about to expire in " . $ExpiresAfter . ".");
} }
} else={ } else={
:log debug ("The certificate " . $CertName . " expires in " . $Remaining . " days."); :log debug ("The certificate " . $CertName . " expires in " . $ExpiresAfter . ".");
} }
} else={ } else={
:log debug ("The certificate " . $CertName . " is just a template."); :log debug ("The certificate " . $CertName . " is just a template.");