|
1 |
| -import datetime |
| 1 | +import os |
| 2 | +import ssl |
| 3 | +import subprocess |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from ssl import SSLError, SSLZeroReturnError, SSLEOFError, CertificateError |
2 | 6 |
|
| 7 | +import OpenSSL |
| 8 | +import dateutil.parser |
3 | 9 | from celery import task
|
| 10 | + |
| 11 | + |
| 12 | +@task() |
| 13 | +def check_cert_expiration_date(): |
| 14 | + # WARNING: The user from which this task is launched should be added to the sudoers. |
| 15 | + # For example: |
| 16 | + # <username> ALL = (ALL:ALL) NOPASSWD: /bin/systemctl stop nginx.service |
| 17 | + # <username> ALL = (ALL:ALL) NOPASSWD: /bin/systemctl start nginx.service |
| 18 | + # <username> ALL = (ALL:ALL) NOPASSWD: /usr/bin/certbot renew |
| 19 | + |
| 20 | + django_certbot_cert = os.getenv('DJANGO_CERTBOT_CERT') |
| 21 | + if django_certbot_cert == 'False' or django_certbot_cert is None: |
| 22 | + print("CertBot certificate not used!") |
| 23 | + return |
| 24 | + elif django_certbot_cert == 'True': |
| 25 | + def get_days_left(): |
| 26 | + try: |
| 27 | + cert = ssl.get_server_certificate((os.getenv('DJANGO_DOMAIN'), 443)) |
| 28 | + x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) |
| 29 | + days_left = dateutil.parser.parse(x509.get_notAfter()).date() - datetime.now().date() |
| 30 | + return days_left |
| 31 | + except (SSLError, SSLEOFError, |
| 32 | + SSLZeroReturnError, CertificateError, |
| 33 | + ConnectionAbortedError, ConnectionError, |
| 34 | + ConnectionRefusedError, ConnectionResetError) as e: |
| 35 | + print('Got the following error during to get server certificate: {error}'.format(error=e)) |
| 36 | + return timedelta(days=100) |
| 37 | + |
| 38 | + if get_days_left() < timedelta(days=7): |
| 39 | + print("Certificate will expire soon! Performing certificate renewal.") |
| 40 | + |
| 41 | + nginx_process = subprocess.run(['sudo', 'systemctl', 'stop', 'nginx.service'], |
| 42 | + stdout=subprocess.PIPE) |
| 43 | + if nginx_process.returncode != 0: |
| 44 | + print("Nginx service has not been stopped successfully!") |
| 45 | + print(nginx_process.stdout) |
| 46 | + return |
| 47 | + |
| 48 | + certbot_process = subprocess.run(['sudo', 'certbot', 'renew'], |
| 49 | + stdout=subprocess.PIPE) |
| 50 | + if certbot_process.returncode == 0: |
| 51 | + print("Certificate has been renewed!") |
| 52 | + print("{days} days left when certificate will expire.".format(days=get_days_left().days)) |
| 53 | + else: |
| 54 | + print("Certificate has not been renewed for reason below!") |
| 55 | + print(certbot_process.stdout) |
| 56 | + |
| 57 | + nginx_process = subprocess.run(['sudo', 'systemctl', 'start', 'nginx.service'], |
| 58 | + stdout=subprocess.PIPE) |
| 59 | + |
| 60 | + if nginx_process.returncode != 0: |
| 61 | + print("Nginx service has not been started successfully!") |
| 62 | + print(nginx_process.stdout) |
| 63 | + return |
| 64 | + |
| 65 | + print("The 'check_cert_expiration_date' task has been successfully completed!") |
| 66 | + else: |
| 67 | + print("{days} days left when certificate will expire.".format(days=get_days_left().days)) |
0 commit comments