blob: bc796ed663ac3767bfd43bdc70027f48fe4f92b2 [file] [log] [blame]
Joanna Jeremiczc21b0082020-06-15 16:21:55 +02001from cryptography.x509.oid import ExtensionOID
Piotr Marcinkiewicza94201c2020-11-25 12:46:26 +01002
3SANS_DELIMITER = ','
4
Joanna Jeremiczc21b0082020-06-15 16:21:55 +02005
6class ArtifactParser:
7
8 def __init__(self, mount_path, ext):
9 self.keystorePassPath = mount_path + '/keystore.pass'
10 self.keystorePath = mount_path + '/keystore.' + ext
11 self.truststorePassPath = mount_path + '/truststore.pass'
12 self.truststorePath = mount_path + '/truststore.' + ext
13
14 def contains_expected_data(self, data):
15 expectedData = data.expectedData
16 actualData = data.actualData
17 return cmp(expectedData, actualData) == 0
18
19 def get_owner_data_from_certificate(self, certificate):
20 list = certificate.get_subject().get_components()
21 return dict((k, v) for k, v in list)
22
23 def get_sans(self, cert):
Piotr Marcinkiewicza94201c2020-11-25 12:46:26 +010024 sans = cert.to_cryptography().extensions.get_extension_for_oid(ExtensionOID.SUBJECT_ALTERNATIVE_NAME).value
25 sans_strings = [str(alt_name.value) for alt_name in sans]
26 return self.get_sorted_sans(sans_strings)
Joanna Jeremiczc21b0082020-06-15 16:21:55 +020027
28 def get_envs_as_dict(self, list):
29 envs = self.get_list_of_pairs_by_mappings(list)
Piotr Marcinkiewicza94201c2020-11-25 12:46:26 +010030 SANS = 'SANS'
31 sans_env_strings = SANS in envs and envs[SANS].split(SANS_DELIMITER) or []
32 envs[SANS] = self.get_sorted_sans(sans_env_strings)
Joanna Jeremiczc21b0082020-06-15 16:21:55 +020033 return self.remove_nones_from_dict(envs)
34
Piotr Marcinkiewicza94201c2020-11-25 12:46:26 +010035 def get_sorted_sans(self, sans_strings):
36 sans_strings.sort()
37 return SANS_DELIMITER.join(sans_strings)
38
Joanna Jeremiczc21b0082020-06-15 16:21:55 +020039 def remove_nones_from_dict(self, dictionary):
40 return dict((k, v) for k, v in dictionary.iteritems() if k is not None)
41
42 def get_list_of_pairs_by_mappings(self, list):
43 mappings = self.get_mappings()
44 listOfEnvs = map(lambda k: k.split('='), list)
45 return dict((mappings.get(a[0]), a[1]) for a in listOfEnvs)
46
47 def get_mappings(self):
48 return {'COMMON_NAME':'CN', 'ORGANIZATION':'O', 'ORGANIZATION_UNIT':'OU', 'LOCATION':'L', 'STATE':'ST', 'COUNTRY':'C', 'SANS':'SANS'}
49