-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-cert-sync.py
More file actions
287 lines (249 loc) · 11.2 KB
/
https-cert-sync.py
File metadata and controls
287 lines (249 loc) · 11.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
#
# Author: Wade Wells github/Pack3tL0ss
#
# Version 2020-1.0
import socket
import threading
import time
from datetime import datetime, timezone
from pathlib import Path, PurePath
from common.arubaos_ssh import Cli
import requests
# from OpenSSL import crypto # type: ignore
# from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.serialization import pkcs12
from cryptography.x509.extensions import SubjectAlternativeName
# from cryptography import x509
from common import AosConnect, config, log, parse
LOCK = threading.Lock()
COUNT = 3
controllers = ''
port = ''
class Certificate:
def __init__(self, data: dict):
self.expired = None
self.update_data(data)
def update_data(self, data):
self.name = data.get("cert_cn")
self.ca_issuers_uri = data.get("cert_ca_issuers_uri")
self.expire_date = data.get("cert_exp_date")
if self.expire_date:
self.expired = datetime.now(timezone.utc) > self.expire_date
# try:
# except TypeError:
# self.expired = datetime.now() > self.expire_date
self.san = data.get("cert_san")
def verify_get_new_cert():
if not config.cert.ok:
raise Exception("Configuration data missing, verify contents of config.yaml")
p = Path(PurePath(config.cert.dir, config.cert.p12_name))
if not p.exists():
log.fatal(f"{p.name} Not Found Exiting...")
exit(1)
le_p12 = pkcs12.load_key_and_certificates(p.read_bytes(), config.cert.p12_pass.encode("UTF-8"))
le_key, cert, other_certs_in_chain = le_p12
# le_cert=cert.public_bytes(serialization.Encoding.PEM)
le_exp = cert.not_valid_after_utc
# le_key=le_key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.TraditionalOpenSSL, serialization.NoEncryption())
sub = cert.subject.rfc4514_string()
cn = sub.split("=")[-1]
data = {}
data["cert_cn"] = None if not cn else cn
data["cert_exp_date"] = le_exp
_san = cert.extensions.get_extension_for_class(SubjectAlternativeName)
data["cert-san"] = _san.value
return Certificate(data)
class ManagedDevice():
def __init__(self, data: dict = None, connection=None):
if connection:
self.connection = connection
if data:
self.update_data(data)
def update_data(self, data):
if "cert_cn" in data.keys():
self.portal = Certificate(data)
else:
self.name = data.get('Name')
self.cfg_id = data.get('Config ID')
self.sync_time = data.get('Config Sync Time (sec)')
self.cfg_state = data.get('Configuration State')
self.ip = data.get('IP Address')
self.location = data.get('Location')
self.model = data.get('Model')
self.status = data.get('Status')
self.type = data.get('Type')
self.version = data.get('Version')
def __repr__(self):
ret = f" ---- {self.name} ----\n"
for k, v in self.__dict__.items():
if isinstance(v, (str, int)):
ret += f" {k}: {v}\n"
return ret
def _repr_csv_(self):
"""csv representation
Returns:
tuple: commas seperated values of class attributes: (keys, values)
"""
head = ""
ret = ""
for k, v in self.__dict__.items():
if isinstance(v, (str, int)):
head += f"{k},"
ret += f"{v},"
return head.rstrip(','), ret.rstrip(',')
class Controllers():
def __init__(self, conductors):
self.conductors = conductors
self.new_cert = verify_get_new_cert()
self.data = {}
self.run()
def run(self):
''' Start parallel threads to establish session with Mobility Conductors
updates data attribute
'''
self.start_controller_threads(self.conductors)
start = len(self.data)
if self.data:
self.exec_api()
if len(self.data) > start:
md_list = [dev for dev in self.data if not hasattr(self.data[dev], 'connection')]
self.start_controller_threads(md_list)
self.exec_api(conductor=False)
def push_new_cert(self, md: ManagedDevice) -> None:
new_cert = self.new_cert
diff = new_cert.expire_date - md.portal.expire_date
# if diff.days > 0:
d = datetime.now()
time_stamp = d.strftime("%h%d_%Y") # Dec25_2020 Unique enough to ensure no conflicts with previous cert
new_cert_name = f"LE_{time_stamp}_2"
# TODO would need to determine this md is associated with this conductor in the event they put multiple in the config. For now we just use the first one.
# might already be handled using self vs config
if diff.days != 0:
cfg_dict = {
"ip": self.conductors[0],
"cli_user": config.user,
"cli_pass": f"{config.password}",
"cmd_list": [
f"copy tftp: {config.cert.tftp_svr} {config.cert.p12_name} flash {new_cert.name}.p12", # File copied successfully
"conf t",
f"crypto pki-import pkcs12 ServerCert {new_cert_name} {new_cert.name}.p12 {config.cert.p12_pass}", # Certificate is uploaded. Please execute "crypto-local pki SERVERCERT securelogin.kabrew.com_0412.p12 securelogin.kabrew.com.p12" from a config node
f"cd {config.cert.md_path}",
f"crypto-local pki ServerCert {new_cert_name} {new_cert.name}.p12",
"write mem", # Configuration Saved
"web-server profile",
f"captive-portal-cert {new_cert_name}"
]
}
cli = Cli(**cfg_dict)
print(cli)
log.info("If the Script was done the Cert would be pushed here")
# TODO login to mds and # "process restart httpd", "y"
else:
log.info(f"{md.name}: No Certificate Update necessary Expiration is the same.")
def start_controller_threads(self, devices):
"""Login/establish session for each controller
"""
thread_array = []
t = [None] * len(devices)
for i, dev in enumerate(devices):
_this = (dev, config.user, config.password)
thread_array.append(_this)
t[i] = threading.Thread(target=self.get_session, args=(_this))
t[i].daemon = True
t[i].start()
time.sleep(0.5)
for thread in t:
thread.join()
def get_session(self, dev, username, password):
try:
ip = socket.gethostbyname(dev)
con = AosConnect(ip, user=username, password=password)
r = con.api_login()
if r.ok:
log.info(f"{ip}: Session Estabished")
if ip not in self.data:
self.data[ip] = ManagedDevice(connection=con)
else:
setattr(self.data[ip], 'connection', con)
else:
log.error(f"{dev}: Failure Establishing Session: {r.error}")
except socket.gaierror:
log.critical(f"{dev}: Unable to resolve host.")
except ConnectionRefusedError as e:
log.critical(f"{dev}: Unable to connect to Controller. Login Failed.\n{e}")
except requests.RequestException as e:
log.critical(f"{dev}: Requests Exception {e}")
except Exception as e:
log.critical(f"{dev}: Exception Occured {e}")
def exec_api_md(self):
for dev in self.data:
if hasattr(self.data[dev], "connection"):
con = self.data[dev].connection
res = con.execute_command("show web-server profile")
pretty_name = f"{self.data[dev].name}:({dev})"
if res.ok:
web_svr_data = parse.show_web_server_profile(res)
cert_name = web_svr_data.get("Captive Portal Certificate")
if not cert_name:
log.error(f"{pretty_name}: No Captive Portal Certificate Returned")
self.data[dev].portal = None
elif cert_name == "default":
log.info(f"{pretty_name} is using the default certificate... data retrieval skipped")
self.data[dev].portal = None
else:
cert_data = con.execute_command(f"show crypto pki ServerCert {cert_name}")
cert_data = parse.show_crypto_pki_servercert(cert_data)
if not cert_data:
log.error(
f"{pretty_name}: "
f"No cert data retunred from output of show crypto pki ServerCert {cert_name}"
)
else:
self.data[dev].update_data(cert_data)
yield self.data[dev]
def exec_api(self, conductor=True):
if conductor:
''' get all the MDs connect to the Mobility Conductor'''
for dev in self.data.copy():
if self.data[dev].connection.handle:
con = self.data[dev].connection
res = con.execute_command("show switches")
if res.ok:
switch_dict = parse.show_switches(res.json())
for ip in switch_dict:
if switch_dict[ip]['Type'] in ["MD", "master", "conductor"]:
self.data[ip] = ManagedDevice(data=switch_dict[ip])
# Determine if this is VRRP address for MM
try:
res = con.execute_command("show vrrp")
if res.json().get('_data'):
if dev in '\n'.join(res.json()['_data']):
log.info(
f'Removing MM VRRP addrress ({dev}) from data '
f'- data will include physical addresses'
)
con.handle.close()
log.info(f"{dev}: Session Closed")
del self.data[dev]
except Exception as e:
log.error(f"{dev}: Exception occured 'show vrrp' {e}")
else:
for dev in self.exec_api_md():
self.push_new_cert(dev)
con = dev.connection
pretty_name = f"{dev.name}:({dev.ip})"
# Done with API calls close session with Controller
try:
con.handle.close()
log.info(f"{pretty_name}: Session Closed")
except Exception as e:
log.error(f"{pretty_name}: Error on session close {e}")
if __name__ == "__main__":
log.info(f" {'-' * 10 } Script Startup {'-' * 20 }")
mcds = config.conductors
if mcds:
aruba = Controllers(mcds)
log.info(f" {'-' * 10 } Script Complete {'-' * 20 }")
else:
print('No Data, Check config.yaml')