Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- from datetime import datetime,timedelta
- import pytz
- import uuid
- try:
- from OpenSSL import crypto
- except:
- print ("Do you have pyOpenSSL? Try 'pip install pyOpenSSL'.")
- exit(1)
- try:
- from icalendar import Calendar,Event,Alarm
- except:
- print ("Do you have Python icalendar installed? Try 'pip install icalendar'.")
- exit(1)
- class InvalidCertificateException(Exception):
- pass
- class CertificateExpiration(Calendar):
- PRODID='-//Certificate expiration tracker'
- VERSION="2.0"
- def __init__(self,certfile,local_tz_str,email_addr):
- super(self.__class__, self).__init__()
- try:
- cert=crypto.load_certificate(crypto.FILETYPE_PEM, file(certfile).read())
- except crypto.Error:
- print("Error loading certificate file: %s" % certfile)
- raise InvalidCertificateException()
- commonName=str(cert.get_subject().commonName)
- expires_datetime=pytz.utc.localize(datetime.strptime(cert.get_notAfter(),"%Y%m%d%H%M%SZ"))
- local_tz=pytz.timezone(local_tz_str)
- local_expires=expires_datetime.astimezone(local_tz)
- self.expires=local_expires.date()
- nextday=self.expires+timedelta(days=1)
- self.add('prodid',self.PRODID)
- self.add('version',self.VERSION)
- event=Event()
- event.add('summary', ("Certificate Expiration: %s" % commonName))
- event.add('dtstart',self.expires)
- event.add('dtend',nextday)
- event.add('uid',uuid.uuid4())
- event.add('dtstamp',datetime.now())
- #Human readable time string with time zone (reflecting daylight savings time)
- #to add to calendar event description
- local_expires_str=local_expires.strftime("%Y-%m-%d, %-I:%M %p %Z")
- description="TLS Certificate CN: %s\r\n" % commonName
- description+="expires on: %s\r\n" % local_expires_str
- event.add('Description',description)
- #Email alarms are considered unsafe and are removed by Calendar.app
- # emailAlarm=Alarm()
- # emailAlarm.add('trigger',timedelta(days=-30))
- # emailAlarm.add('action','EMAIL')
- # emailAlarm.add('attendee',email_addr)
- # emailAlarm.add('summary',"Certificate expires in 30 days: %s" % commonName)
- # emailAlarm.add('description',"This is a reminder that the TLS certificate for %s expires on: %s" % (commonName,local_expires_str))
- # emailAlarm.add('uid',uuid.uuid4())
- # event.add_component(emailAlarm)
- displayAlarm=Alarm()
- #this ends up being 7 days, since the "all day" event starts at T-1day
- displayAlarm.add('trigger',timedelta(days=-6))
- displayAlarm.add('description','Certificate expires in 7 days: %s' % commonName)
- displayAlarm.add('action','DISPLAY')
- displayAlarm.add('uid',uuid.uuid4())
- event.add_component(displayAlarm)
- self.add_component(event)
- print self
- def display(self):
- return self.to_ical() #.replace('\r\n','\n').strip()
- def write(self,filename):
- open(filename,"wb").write(self.to_ical() )
- def main(cert_filename,local_tz_str,ics_filename,email_addr):
- try:
- cert_expiration=CertificateExpiration(cert_filename,local_tz_str,email_addr)
- except InvalidCertificateException:
- exit(1)
- cert_expiration.write(ics_filename)
- if __name__ == '__main__':
- tz_str="America/Los_Angeles"
- certfile=sys.argv[1]
- email_addr=sys.argv[2]
- main(certfile,tz_str,"./Cert Expiration.ics",email_addr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement