Skip to content

Commit

Permalink
Added update days threshold parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric authored and Cedric committed Aug 5, 2020
1 parent a2035c2 commit 0742a9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions acme_nginx/Acme.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import tempfile
import time
from datetime import datetime, timedelta

try:
from urllib.request import urlopen, Request # Python 3
Expand All @@ -32,6 +33,7 @@ def __init__(
cert_path='/etc/ssl/private/letsencrypt-domain.pem',
dns_provider=None,
skip_nginx_reload=False,
update_date_threshold_days=None,
debug=False):
"""
Params:
Expand Down Expand Up @@ -60,6 +62,23 @@ def __init__(
self.chain = "https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem"
self.dns_provider = dns_provider
self.skip_nginx_reload = skip_nginx_reload
self.update_date_threshold_days = update_date_threshold_days

self.IsOutOfDate = True
if self.update_date_threshold_days:
try:
certTime = datetime.fromtimestamp(os.path.getmtime(self.cert_path))
certTimeThreshold = certTime + timedelta(days=self.update_date_threshold_days)

self.IsOutOfDate = (certTimeThreshold < datetime.now())
self.log.info('Cert file {1} (expiration time {0})'.format( certTimeThreshold, "is out of date" if self.IsOutOfDate else "is not out of date"))

except OSError as e:
if e.errno == 2:
self.log.info('Cert file {0} not found -> DO UPDATE CERT'.format(self.cert_path))
except:
pass


def _reload_nginx(self):
""" Reload nginx """
Expand Down
11 changes: 9 additions & 2 deletions acme_nginx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def set_arguments():
dest='skip_reload',
action='store_true',
help="don't reload nginx after certificate signing")
parser.add_argument(
'--out-of-date-update-threshold-days',
dest='update_date_threshold_days',
type=int,
help="expiration threshold in days")
return parser.parse_args()


Expand Down Expand Up @@ -107,6 +112,8 @@ def main():
cert_path=args.cert_path,
debug=args.debug,
dns_provider=args.dns_provider,
skip_nginx_reload=args.skip_reload
skip_nginx_reload=args.skip_reload,
update_date_threshold_days = args.update_date_threshold_days
)
acme.get_certificate()
if acme.IsOutOfDate:
acme.get_certificate()

0 comments on commit 0742a9a

Please sign in to comment.