from datetime import datetime
import demistomock as demisto
from CommonServerPython import *
def create_html_table(headers, certList, listType):
tableBody = ""
tableBody += "\t\t
\n\t\t\t\n\t\t\t\tEXPIRED CERTIFICATES
\n\t\t\t\n\t\t
\n"
elif listType == "expiring":
tableBody += ">\n\t\t\t\tCertificates expiring in 90 days or less
\n\t\t\t\n\t\t\n"
elif listType == "warning":
tableBody += (
' style="color: #ff9900;">\n\t\t\t\tCertificates expiring between 91 and 180 days'
+ " from today\n\t\t\t\n\t\t\n"
)
elif listType == "good":
tableBody += (
' style="color: #339966;">\n\t\t\t\tCertificates expiring more than 180 days from today'
+ "\n\t\t\t\n\t\t\n"
)
# Setup table
tableBody += '\t\t\n\t\t\t'
# Setup table row headers
tableBody += '\n\t\t\t\t| Site/Domain/IP | '
tableBody += 'Expiration Date | '
if listType != "expired":
tableBody += 'Days to Expiration |
\n'
else:
tableBody += 'Days Expired | \n'
# Parse table rows from certList
for cert in certList:
tableBody += '\t\t\t\t\n\t\t\t\t\t| ' + cert["Domain"]
tableBody += ' | \n\t\t\t\t\t' + cert["ExpirationDate"]
tableBody += (
' | \n\t\t\t\t\t'
+ str(cert["TimeToExpiration"])
+ " days | "
+ "\n\t\t\t\t
\n"
)
tableBody += "\t\t\t\n\t\t
\n"
return tableBody
def main():
try:
emailHTMLBody = ""
headers = ("Site", "Expiration Date", "Days to Expiration")
good = demisto.get(demisto.context(), "SSLReport.Good")
warning = demisto.get(demisto.context(), "SSLReport.Warning")
expiring = demisto.get(demisto.context(), "SSLReport.Expiring")
expired = demisto.get(demisto.context(), "SSLReport.Expired")
# Setup email header
emailHTMLBody += (
"\n\t\n\t\t\n\t\n\t\t\n"
+ "\t\t\tSSL Certificate Report for "
+ datetime.today().strftime("%Y/%m/%d")
+ "\n\t\t
\n"
)
# Setup tables
if expired:
emailHTMLBody += create_html_table(headers, expired, "expired")
if expiring:
emailHTMLBody += create_html_table(headers, expiring, "expiring")
if warning:
emailHTMLBody += create_html_table(headers, warning, "warning")
if good:
emailHTMLBody += create_html_table(headers, good, "good")
emailHTMLBody += "\t\n"
demisto.setContext("SSLReport.HTML", emailHTMLBody)
except Exception as ex:
return_error(f"An Error occured: {ex}", error=ex)
if __name__ in ("__main__", "__builtin__", "builtins"):
main()