Catch error when cert_expiration plugin is not able to connect to server

This commit is contained in:
Justus Grunow 2025-05-08 20:56:36 -04:00
parent 3c0e669596
commit f10ae438e0

View File

@ -1,6 +1,7 @@
import ssl import ssl
import OpenSSL import OpenSSL
from datetime import datetime,timedelta from datetime import datetime,timedelta
from socket import gaierror
def cert_expiration(args): def cert_expiration(args):
hostname = args['endpoint'].split(":")[0] hostname = args['endpoint'].split(":")[0]
@ -10,15 +11,18 @@ def cert_expiration(args):
except IndexError: except IndexError:
port = 443 port = 443
try:
cert = ssl.get_server_certificate((hostname, port)) cert = ssl.get_server_certificate((hostname, port))
except gaierror:
return [False, "Failed to connect to server."]
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
expiration_date = datetime.strptime(f"{x509.get_notAfter().decode("UTF-8")[0:-1]}UTC", "%Y%m%d%H%M%S%Z") expiration_date = datetime.strptime(f"{x509.get_notAfter().decode("UTF-8")[0:-1]}UTC", "%Y%m%d%H%M%S%Z")
status = "Success"
if (expiration_date - timedelta(days = args['expiration_warning_days'])) < datetime.now(): if (expiration_date - timedelta(days = args['expiration_warning_days'])) < datetime.now():
status = "Warning" status = False
else: else:
status = "Success" status = True
return [status, f"Expiration: {expiration_date}"] return [status, f"Expiration: {expiration_date}"]