/
certificates.py
84 lines (65 loc) · 2.8 KB
/
certificates.py
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
from datetime import datetime
from middlewared.alert.base import AlertClass, OneShotAlertClass, AlertCategory, AlertLevel, Alert, AlertSource
class CertificateIsExpiringAlertClass(AlertClass):
category = AlertCategory.CERTIFICATES
level = AlertLevel.NOTICE
title = "Certificate Is Expiring"
text = "Certificate %(name)r is expiring within %(days)d days."
class CertificateIsExpiringSoonAlertClass(AlertClass):
category = AlertCategory.CERTIFICATES
level = AlertLevel.WARNING
title = "Certificate Is Expiring Soon"
text = "Certificate %(name)r is expiring within %(days)d days."
class CertificateIsExpiringAlertSource(AlertSource):
async def check(self):
alerts = []
for cert in await self.middleware.call(
'certificate.query',
[['certificate', '!=', None]]
) + await self.middleware.call('certificateauthority.query'):
if cert['parsed']:
diff = (datetime.strptime(cert['until'], '%a %b %d %H:%M:%S %Y') - datetime.utcnow()).days
if diff < 10:
alerts.append(
Alert(
CertificateIsExpiringSoonAlertClass if diff <= 2 else CertificateIsExpiringAlertClass,
{
"name": cert["name"],
"days": diff,
},
key=[cert["name"]],
)
)
return alerts
class CertificateParsingFailedAlertClass(AlertClass):
category = AlertCategory.CERTIFICATES
level = AlertLevel.WARNING
title = "Certificate Parsing Failed"
text = "Failed to parse %(type)s %(name)r."
class CertificateParsingFailedAlertSource(AlertSource):
async def check(self):
alerts = []
for cert in await self.middleware.call(
'certificate.query',
[['certificate', '!=', None]]
) + await self.middleware.call('certificateauthority.query'):
if not cert['parsed']:
alerts.append(
Alert(
CertificateParsingFailedAlertClass,
{
"type": cert["cert_type"].capitalize(),
"name": cert["name"],
},
)
)
return alerts
class WebUiCertificateSetupFailedAlertClass(AlertClass, OneShotAlertClass):
category = AlertCategory.CERTIFICATES
level = AlertLevel.CRITICAL
title = "Web UI HTTPS Certificate Setup Failed"
text = "Web UI HTTPS certificate setup failed."
async def create(self, args):
return Alert(WebUiCertificateSetupFailedAlertClass)
async def delete(self, alerts, query):
return []