-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_ssl.py
More file actions
146 lines (130 loc) · 4.66 KB
/
Copy pathcheck_ssl.py
File metadata and controls
146 lines (130 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
import os
import ssl
import json
import yaml
from time import sleep, time
import socket
from datetime import datetime, timedelta
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
try:
filesource = os.environ['LIST']
except KeyError:
print('Missing LIST environment variable, exiting.')
exit(1)
def open_file():
with open(filesource, 'r') as stream:
try:
file = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return file
def date_converter(o):
if isinstance(o, datetime):
return o.__str__()
def json_format(url, expiration, dns, delta, tags):
data = {}
data['url'] = url
data['expiration'] = expiration
data['dns'] = dns
data['delta'] = delta.days
if tags != []:
data['tags'] = tags
data = json.dumps(data, default=date_converter)
print(data)
def format_url(base_url):
new_url = base_url.replace('https://', '')
if new_url.find('/') != -1:
index = new_url.find('/')
new_url = new_url[0:index]
return new_url
def connect_to_influxdb():
try:
influxdb_host = os.environ['INFLUXDB-HOST']
except:
print('INFLUXDB-HOST environnement variable is not set, running without InfluxDB')
return None
else:
if (influxdb_host != None and influxdb_host != ''):
try:
os.environ['INFLUXDB-TOKEN']
os.environ['INFLUXDB-ORG']
os.environ['INFLUXDB-BUCKET']
except:
print('INFLUXDB-HOST is defined please define INFLUX-TOKEN, INFLUX-ORG and INFLUX-BUCKET environnement variables.')
exit(1)
else:
print('INFLUXDB-HOST environnement variable is empty, running without InfluxDB')
return None
if (influxdb_host != None and influxdb_host != ''):
client = InfluxDBClient(url=influxdb_host, token=os.environ['INFLUXDB-TOKEN'])
return client.write_api(write_options=SYNCHRONOUS)
def insert_to_influxdb(write_api, influxdb_connected, hostname, expiration, dns, delta, tags):
new_delta = delta.days
sequence = [
f'ssl_check,host={hostname} expiration="{expiration}",dns="{dns}",delta={new_delta}'
]
if tags != []:
sequence.append(f'tags,host={hostname} tags="{tags}"')
while True:
try:
write_api.write(os.environ['INFLUXDB-BUCKET'], os.environ['INFLUXDB-ORG'], sequence)
except Exception as e:
print('Waiting 10 seconds for InfluxDB to start...')
print('Script is unable to connect :\n', str(e))
sleep(10)
else:
if not influxdb_connected:
print('Connected to InfluxDB !')
break
return True
def main():
influxdb_connected = False
while 42:
data = open_file()
today = datetime.today()
write_api = connect_to_influxdb()
for url in data['website']:
try:
tmp = url['ssl']
except KeyError:
tmp = False
if tmp:
base_url = url['url']
port = '443'
hostname = format_url(base_url)
context = ssl.create_default_context()
try:
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
data = ssock.getpeercert()
except ssl.SSLCertVerificationError:
expiration = datetime.now() - timedelta(days=1)
dns = ""
else:
expiration = datetime.utcfromtimestamp(ssl.cert_time_to_seconds(data['notAfter']))
dns = str(data['subjectAltName'])
dns = dns.replace('(', '').replace(')', '').replace("'", '').replace('DNS', '').replace(' ', '')
dns = dns[1:]
dns = dns.split(',,')
delta = expiration - today
try:
tags = url['tags']
except:
tags = []
if write_api != None:
influxdb_connected = insert_to_influxdb(write_api, influxdb_connected, hostname, expiration, dns, delta, tags)
else:
json_format(hostname, expiration, dns, delta, tags)
try:
data['ssl_delay']
except:
sleep(3600)
else:
sleep(int(data['ssl_delay']))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit(1)