blob: 46e0357e44d02f1706f892810099113d0854c47b [file] [log] [blame]
Joanna Jeremiczc21b0082020-06-15 16:21:55 +02001import os
2from OpenSSL import crypto
3from cryptography import x509
4from cryptography.hazmat.backends import default_backend
5from EnvsReader import EnvsReader
6from ArtifactParser import ArtifactParser
7
8class 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