Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 1 | import docker |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 2 | import os |
| 3 | import shutil |
| 4 | import tarfile |
| 5 | import re |
| 6 | from OpenSSL import crypto |
| 7 | |
| 8 | ARCHIVES_PATH = os.getenv("WORKSPACE") + "/archives/" |
| 9 | TMP_PATH = os.getenv("WORKSPACE") + "/tests/aaf/certservice/tmp" |
| 10 | |
| 11 | ERROR_API_REGEX = 'Error on API response.*[0-9]{3}' |
| 12 | RESPONSE_CODE_REGEX = '[0-9]{3}' |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class CertClientManager: |
| 16 | |
| 17 | def run_client_container(self, client_image, container_name, path_to_env, request_url, network): |
| 18 | client = docker.from_env() |
| 19 | environment = self.read_list_env_from_file(path_to_env) |
| 20 | environment.append("REQUEST_URL=" + request_url) |
| 21 | container = client.containers.run(image=client_image, name=container_name, detach=True, environment=environment, |
| 22 | network=network) |
| 23 | exitcode = container.wait() |
| 24 | return exitcode |
| 25 | |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 26 | def read_list_env_from_file(self, path): |
| 27 | f = open(path, "r") |
| 28 | r_list = [] |
| 29 | for line in f: |
| 30 | line = line.strip() |
| 31 | if line[0] != "#": |
| 32 | r_list.append(line) |
| 33 | return r_list |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 34 | |
| 35 | def remove_client_container_and_save_logs(self, container_name, log_file_name): |
| 36 | client = docker.from_env() |
| 37 | container = client.containers.get(container_name) |
| 38 | text_file = open(ARCHIVES_PATH + "container_" + log_file_name + ".log", "w") |
| 39 | text_file.write(container.logs()) |
| 40 | text_file.close() |
| 41 | container.remove() |
| 42 | |
| 43 | def can_open_keystore_and_truststore_with_pass(self, container_name): |
| 44 | self.copy_jks_file_to_tmp_dir(container_name) |
| 45 | |
| 46 | keystore_pass_path = TMP_PATH + '/logs/log/keystore.pass' |
| 47 | keystore_jks_path = TMP_PATH + '/logs/log/keystore.jks' |
| 48 | can_open_keystore = self.can_open_jks_file_by_pass_file(keystore_pass_path, keystore_jks_path) |
| 49 | |
| 50 | truststore_pass_path = TMP_PATH + '/logs/log/truststore.pass' |
| 51 | truststore_jks_path = TMP_PATH + '/logs/log/truststore.jks' |
| 52 | can_open_truststore = self.can_open_jks_file_by_pass_file(truststore_pass_path, truststore_jks_path) |
| 53 | |
| 54 | self.remove_tmp_dir(TMP_PATH) |
| 55 | return can_open_keystore & can_open_truststore |
| 56 | |
| 57 | def copy_jks_file_to_tmp_dir(self, container_name): |
| 58 | os.mkdir(TMP_PATH) |
| 59 | self.copy_jks_file_from_container_to_tmp_dir(container_name) |
| 60 | self.extract_tar_file() |
| 61 | |
| 62 | def copy_jks_file_from_container_to_tmp_dir(self, container_name): |
| 63 | client = docker.from_env() |
| 64 | container = client.containers.get(container_name) |
| 65 | f = open(TMP_PATH + '/var_log.tar', 'wb') |
| 66 | bits, stat = container.get_archive('/var/log/') |
| 67 | for chunk in bits: |
| 68 | f.write(chunk) |
| 69 | f.close() |
| 70 | |
| 71 | def extract_tar_file(self): |
| 72 | my_tar = tarfile.open(TMP_PATH + '/var_log.tar') |
| 73 | my_tar.extractall(TMP_PATH + '/logs') |
| 74 | my_tar.close() |
| 75 | |
| 76 | def can_open_jks_file_by_pass_file(self, pass_file_path, jks_file_path): |
| 77 | try: |
| 78 | password = open(pass_file_path, 'rb').read() |
| 79 | crypto.load_pkcs12(open(jks_file_path, 'rb').read(), password) |
| 80 | return True |
| 81 | except: |
| 82 | return False |
| 83 | |
| 84 | def remove_tmp_dir(self, tmp_path): |
| 85 | shutil.rmtree(tmp_path) |
| 86 | |
| 87 | def can_find_api_response_in_logs(self, container_name): |
| 88 | logs = self.get_container_logs(container_name) |
| 89 | api_logs = re.findall(ERROR_API_REGEX, logs) |
| 90 | if api_logs: |
| 91 | return True |
| 92 | else: |
| 93 | return False |
| 94 | |
| 95 | def get_api_response_from_logs(self, container_name): |
| 96 | logs = self.get_container_logs(container_name) |
| 97 | error_api_message = re.findall(ERROR_API_REGEX, logs) |
| 98 | code = re.findall(RESPONSE_CODE_REGEX, error_api_message[0]) |
| 99 | return code[0] |
| 100 | |
| 101 | def get_container_logs(self, container_name): |
| 102 | client = docker.from_env() |
| 103 | container = client.containers.get(container_name) |
| 104 | logs = container.logs() |
| 105 | return logs |