Joanna Jeremicz | c21b008 | 2020-06-15 16:21:55 +0200 | [diff] [blame] | 1 | import os |
| 2 | from OpenSSL import crypto |
| 3 | from cryptography import x509 |
| 4 | from cryptography.hazmat.backends import default_backend |
| 5 | from EnvsReader import EnvsReader |
| 6 | from ArtifactParser import ArtifactParser |
| 7 | |
| 8 | class PemArtifactsValidator: |
| 9 | |
| 10 | def __init__(self, mount_path): |
| 11 | self.parser = ArtifactParser(mount_path, "pem") |
| 12 | self.key = mount_path + '/key.pem' |
| 13 | |
| 14 | def get_and_compare_data_pem(self, path_to_env): |
| 15 | data = self.get_data_pem(path_to_env) |
| 16 | return data, self.parser.contains_expected_data(data) |
| 17 | |
| 18 | def artifacts_exist_and_are_not_empty(self): |
| 19 | keystoreExists = self.file_exists_and_is_not_empty(self.parser.keystorePath) |
| 20 | truststoreExists = self.file_exists_and_is_not_empty(self.parser.truststorePath) |
| 21 | keyExists = self.file_exists_and_is_not_empty(self.key) |
| 22 | return keystoreExists and truststoreExists and keyExists |
| 23 | |
| 24 | def file_exists_and_is_not_empty(self, pathToFile): |
| 25 | return os.path.isfile(pathToFile) and os.path.getsize(pathToFile) > 0 |
| 26 | |
| 27 | def get_data_pem(self, path_to_env): |
| 28 | envs = self.parser.get_envs_as_dict(EnvsReader().read_env_list_from_file(path_to_env)) |
| 29 | certificate = self.get_keystore_certificate() |
| 30 | data = self.parser.get_owner_data_from_certificate(certificate) |
| 31 | data['SANS'] = self.parser.get_sans(certificate) |
| 32 | return type('', (object,), {"expectedData": envs, "actualData": data}) |
| 33 | |
| 34 | def get_keystore_certificate(self): |
| 35 | return crypto.X509.from_cryptography(self.load_x509_certificate()) |
| 36 | |
| 37 | def load_x509_certificate(self): |
| 38 | cert = x509.load_pem_x509_certificate(open(self.parser.keystorePath, 'rb').read(), default_backend()) |
| 39 | return cert |