blob: 92f1cb66d21042235933f9f3c8ebd8fd9e4f2e32 [file] [log] [blame]
Dzmitry Stremkouskib0887c92018-07-03 13:05:35 +02001#!/usr/bin/env python
2
3import os
4import sys
5from cryptography import x509
6from cryptography.hazmat.backends import default_backend
7from cryptography.x509.oid import ExtensionOID
8from datetime import datetime
9
10salt_master_ca_path = '/etc/pki/ca/salt_master_ca/certs/'
11certs = []
12elem_in_list = False
13
14_files = os.listdir(salt_master_ca_path)
15for _file in _files:
16 _file_obj = open(salt_master_ca_path + _file, 'r')
17 pem_data = _file_obj.read()
18 _file_obj.close()
19
20 cert_serial = _file.split('.')[0].lower()
21 cert = x509.load_pem_x509_certificate(pem_data, default_backend())
22 cert_date = datetime.strptime(str(cert.not_valid_before), '%Y-%m-%d %H:%M:%S').strftime('%s')
23 cert_exts = cert.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_ALTERNATIVE_NAME)
24 cert_exts_list = cert_exts.value.get_values_for_type(x509.DNSName)
25
26 for _name in cert.subject:
27 if _name.oid.dotted_string == '2.5.4.3':
28 cert_cn = _name.value
29
30 for elem in certs:
31 if (elem[0] == cert_cn) and (elem[1] == cert_exts_list):
32 elem_in_list = True
33 if elem[2] < cert_date:
34 elem[2] = cert_date
35
36 if not elem_in_list:
37 certs.append([ cert_cn, cert_exts_list, cert_date, cert_serial ])
38
39 elem_in_list = False
40
41for elem in certs:
42 print salt_master_ca_path + elem[3].upper() + '.crt (' + str(elem[0]) + ', ' + ', '.join(map(str, elem[1])) + ')'