Created
August 30, 2017 17:10
-
-
Save maretekent/70c13c64a9898a68b9b37e5e02a2114d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ssl_valid_time_remaining(hostname): | |
"""Get the number of days left in a cert's lifetime.""" | |
expires = ssl_expiry_datetime(hostname) | |
logger.debug( | |
"SSL cert for %s expires at %s", | |
hostname, expires.isoformat() | |
) | |
return expires - datetime.datetime.utcnow() | |
def ssl_expires_in(hostname, buffer_days=14): | |
"""Check if `hostname` SSL cert expires is within `buffer_days`. | |
Raises `AlreadyExpired` if the cert is past due | |
""" | |
remaining = ssl_valid_time_remaining(hostname) | |
# if the cert expires in less than two weeks, we should reissue it | |
if remaining < datetime.timedelta(days=0): | |
# cert has already expired - uhoh! | |
raise AlreadyExpired("Cert expired %s days ago" % remaining.days) | |
elif remaining < datetime.timedelta(days=buffer_days): | |
# expires sooner than the buffer | |
return True | |
else: | |
# everything is fine | |
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"cloudfront:ListDistributions", | |
"sns:Publish" | |
], | |
"Resource": [ | |
"*" | |
] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import ssl | |
def ssl_expiry_datetime(hostname): | |
ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z' | |
context = ssl.create_default_context() | |
conn = context.wrap_socket( | |
socket.socket(socket.AF_INET), | |
server_hostname=hostname, | |
) | |
# 3 second timeout because Lambda has runtime limitations | |
conn.settimeout(3.0) | |
conn.connect((hostname, 443)) | |
ssl_info = conn.getpeercert() | |
# parse the string from the certificate into a Python datetime object | |
return datetime.datetime.strptime(ssl_info['notAfter'], ssl_date_fmt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment