certificate-renew-issued: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:00 +01:00
parent 4cbf9fab74
commit 2d112c0b33

View file

@ -8,17 +8,19 @@
# renew locally issued certificates # renew locally issued certificates
# https://git.eworm.de/cgit/routeros-scripts/about/doc/certificate-renew-issued.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/certificate-renew-issued.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local Main do={
:local ScriptName [ :tostr $1 ];
:global CertIssuedExportPass; :global CertIssuedExportPass;
:global LogPrintExit2; :global LogPrintExit2;
:global MkDir; :global MkDir;
:global ScriptLock; :global ScriptLock;
$ScriptLock $0; $ScriptLock $ScriptName;
:foreach Cert in=[ /certificate/find where issued expires-after<3w ] do={ :foreach Cert in=[ /certificate/find where issued expires-after<3w ] do={
:local CertVal [ /certificate/get $Cert ]; :local CertVal [ /certificate/get $Cert ];
@ -32,12 +34,15 @@ $ScriptLock $0;
/certificate/export-certificate ($CertVal->"name") type=pkcs12 \ /certificate/export-certificate ($CertVal->"name") type=pkcs12 \
file-name=("cert-issued/" . $CertVal->"common-name") \ file-name=("cert-issued/" . $CertVal->"common-name") \
export-passphrase=($CertIssuedExportPass->($CertVal->"common-name")); export-passphrase=($CertIssuedExportPass->($CertVal->"common-name"));
$LogPrintExit2 info $0 ("Issued a new certificate for \"" . $CertVal->"common-name" . \ $LogPrintExit2 info $ScriptName ("Issued a new certificate for \"" . $CertVal->"common-name" . \
"\", exported to \"cert-issued/" . $CertVal->"common-name" . ".p12\".") false; "\", exported to \"cert-issued/" . $CertVal->"common-name" . ".p12\".") false;
} else={ } else={
$LogPrintExit2 warning $0 ("Failed creating directory, not exporting certificate.") false; $LogPrintExit2 warning $ScriptName ("Failed creating directory, not exporting certificate.") false;
} }
} else={ } else={
$LogPrintExit2 info $0 ("Issued a new certificate for \"" . $CertVal->"common-name" . "\".") false; $LogPrintExit2 info $ScriptName ("Issued a new certificate for \"" . $CertVal->"common-name" . "\".") false;
} }
} }
}
$Main [ :jobname ];