Debian Codegraph @

crt/aws-c-io/.builder/actions/pkcs11_test_setup.py

raw symbols SCIP
1 """
2 Prepare for PKCS#11 tests by configuring SoftHSM2, if it is installed.
3 """
4
5 import Builder
6
7 import os
8 import re
9
10
11 class Pkcs11TestSetup(Builder.Action):
12 """
13 Set up this machine for running the PKCS#11 tests.
14 If SoftHSM2 cannot be installed, the tests are skipped.
15
16 This action should be run in the 'pre_build_steps' or 'build_steps' stage.
17 """
18
19 def run(self, env):
20 if not env.project.needs_tests(env):
21 print("Skipping PKCS#11 setup because tests disabled for project")
22 return
23
24 self.env = env
25
26 # total hack: don't run PKCS#11 tests when building all C libs with -DBUILD_SHARED_LIBS=ON.
27 # here's what happens: libsofthsm2.so loads the system libcrypto.so and
28 # s2n loads the aws-lc's libcrypto.so and really strange things start happening.
29 # this wouldn't happen in the real world, just in our tests, so just bail out
30 if hasattr(env.args, "cmake_extra"):
31 if any('BUILD_SHARED_LIBS=ON' in arg for arg in env.args.cmake_extra):
32 print(
33 "WARNING: PKCS#11 tests disabled when BUILD_SHARED_LIBS=ON due to weird libcrypto.so behavior")
34 return
35
36 # try to install softhsm
37 try:
38 softhsm_install_acion = Builder.InstallPackages(['softhsm'])
39 softhsm_install_acion.run(env)
40 except:
41 print("WARNING: softhsm could not be installed. PKCS#11 tests are disabled")
42 return
43
44 softhsm_lib = self._find_softhsm_lib()
45 if softhsm_lib is None:
46 print("WARNING: libsofthsm2.so not found. PKCS#11 tests are disabled")
47 return
48
49 # print SoftHSM version
50 self._exec_softhsm2_util('--version')
51
52 # bail out if softhsm is too old
53 # 2.1.0 is a known offender that crashes on exit if C_Finalize() isn't called
54 if self._get_softhsm2_version() < (2, 2, 0):
55 print("WARNING: SoftHSM2 installation is too old. PKCS#11 tests are disabled")
56 return
57
58 # set cmake flag so PKCS#11 tests are enabled
59 env.project.config['cmake_args'].append('-DENABLE_PKCS11_TESTS=ON')
60
61 # put SoftHSM config file and token directory under the build dir.
62 softhsm2_dir = os.path.join(env.build_dir, 'softhsm2')
63 conf_path = os.path.join(softhsm2_dir, 'softhsm2.conf')
64 token_dir = os.path.join(softhsm2_dir, 'tokens')
65 env.shell.mkdir(token_dir)
66 self._setenv('SOFTHSM2_CONF', conf_path)
67 with open(conf_path, 'w') as conf_file:
68 conf_file.write(f"directories.tokendir = {token_dir}\n")
69
70 # sanity check SoftHSM is working
71 self._exec_softhsm2_util('--show-slots')
72
73 # set env vars for tests
74 self._setenv('TEST_PKCS11_LIB', softhsm_lib)
75 self._setenv('TEST_PKCS11_TOKEN_DIR', token_dir)
76
77 def _find_softhsm_lib(self):
78 """Return path to SoftHSM2 shared lib, or None if not found"""
79
80 # note: not using `ldconfig --print-cache` to find it because
81 # some installers put it in weird places where ldconfig doesn't look
82 # (like in a subfolder under lib/)
83
84 for lib_dir in ['lib64', 'lib']: # search lib64 before lib
85 for base_dir in ['/usr/local', '/usr', '/', ]:
86 search_dir = os.path.join(base_dir, lib_dir)
87 for root, dirs, files in os.walk(search_dir):
88 for file_name in files:
89 if 'libsofthsm2.so' in file_name:
90 return os.path.join(root, file_name)
91 return None
92
93 def _exec_softhsm2_util(self, *args, **kwargs):
94 if not 'check' in kwargs:
95 kwargs['check'] = True
96
97 result = self.env.shell.exec('softhsm2-util', *args, **kwargs)
98
99 # older versions of softhsm2-util (2.1.0 is a known offender)
100 # return error code 0 and print the help if invalid args are passed.
101 # This should be an error.
102 #
103 # invalid args can happen because newer versions of softhsm2-util
104 # support more args than older versions, so what works on your
105 # machine might not work on some ancient docker image.
106 if 'Usage: softhsm2-util' in result.output:
107 raise Exception('softhsm2-util failed')
108
109 return result
110
111 def _setenv(self, var, value):
112 """
113 Set environment variable now,
114 and ensure the environment variable is set again when tests run
115 """
116 self.env.shell.setenv(var, value)
117 self.env.project.config['test_env'][var] = value
118
119 def _get_softhsm2_version(self):
120 output = self._exec_softhsm2_util('--version').output
121 match = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)', output)
122 return (int(match.group(1)), int(match.group(2)), int(match.group(3)))