Joanna Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 1 | from cryptography.x509.oid import ExtensionOID |
Piotr Marcinkiewicz | a94201c | 2020-11-25 12:46:26 +0100 | [diff] [blame^] | 2 | |
| 3 | SANS_DELIMITER = ',' |
| 4 | |
Joanna Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 5 | |
| 6 | class 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 Marcinkiewicz | a94201c | 2020-11-25 12:46:26 +0100 | [diff] [blame^] | 24 | 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 Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 27 | |
| 28 | def get_envs_as_dict(self, list): |
| 29 | envs = self.get_list_of_pairs_by_mappings(list) |
Piotr Marcinkiewicz | a94201c | 2020-11-25 12:46:26 +0100 | [diff] [blame^] | 30 | 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 Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 33 | return self.remove_nones_from_dict(envs) |
| 34 | |
Piotr Marcinkiewicz | a94201c | 2020-11-25 12:46:26 +0100 | [diff] [blame^] | 35 | def get_sorted_sans(self, sans_strings): |
| 36 | sans_strings.sort() |
| 37 | return SANS_DELIMITER.join(sans_strings) |
| 38 | |
Joanna Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 39 | 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 | |