Add testcase for PMMapper (Support 28.532 file naming convention)
[integration/csit.git] / tests / oom-platform-cert-service / certservice / libraries / P12ArtifactsValidator.py
1 from OpenSSL import crypto
2 from EnvsReader import EnvsReader
3 from ArtifactParser import ArtifactParser
4
5 class P12ArtifactsValidator:
6
7   def __init__(self, mount_path):
8     self.parser = ArtifactParser(mount_path, "p12")
9
10   def get_and_compare_data_p12(self, path_to_env):
11     data = self.get_data(path_to_env)
12     return data, self.parser.contains_expected_data(data)
13
14   def can_open_keystore_and_truststore_with_pass(self):
15     can_open_keystore = self.can_open_store_file_with_pass_file(self.parser.keystorePassPath, self.parser.keystorePath)
16     can_open_truststore = self.can_open_store_file_with_pass_file(self.parser.truststorePassPath, self.parser.truststorePath)
17
18     return can_open_keystore & can_open_truststore
19
20   def can_open_store_file_with_pass_file(self, pass_file_path, store_file_path):
21     try:
22       self.get_certificate(pass_file_path, store_file_path)
23       return True
24     except:
25       return False
26
27   def get_data(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_certificate(self.parser.keystorePassPath, self.parser.keystorePath)
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_certificate(self, pass_file_path, store_file_path):
35     password = open(pass_file_path, 'rb').read()
36     crypto.load_pkcs12(open(store_file_path, 'rb').read(), password)
37     return crypto.load_pkcs12(open(store_file_path, 'rb').read(), password).get_certificate()