Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and mxamin committed Apr 5, 2024
1 parent 7ee15f1 commit ce6e875
Showing 1 changed file with 12 additions and 44 deletions.
56 changes: 12 additions & 44 deletions tests/softhsm_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def run_cmd(args, softhsm_conf=None) -> Tuple[bytes, bytes]:
if softhsm_conf is not None:
env['SOFTHSM_CONF'] = softhsm_conf
env['SOFTHSM2_CONF'] = softhsm_conf
proc = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
out, err = proc.communicate()
if err is not None and len(err) > 0:
logging.error(err)
Expand Down Expand Up @@ -88,19 +86,15 @@ def run_cmd(args, softhsm_conf=None) -> Tuple[bytes, bytes]:
}

component_path: Dict[str, str] = {
component_name: find_alts(
component_name, paths_for_component(component_name, default_paths)
)
component_name: find_alts(component_name, paths_for_component(component_name, default_paths))
for component_name, default_paths in component_default_paths.items()
}

softhsm_version = 1
if component_path['SOFTHSM'].endswith('softhsm2-util'):
softhsm_version = 2

openssl_version = subprocess.check_output([component_path['OPENSSL'], 'version'])[
8:11
].decode()
openssl_version = subprocess.check_output([component_path['OPENSSL'], 'version'])[8:11].decode()

p11_test_files: List[str] = []
softhsm_conf: Optional[str] = None
Expand All @@ -119,9 +113,7 @@ def _temp_dir() -> str:
return d


@unittest.skipIf(
component_path['P11_MODULE'] is None, 'SoftHSM PKCS11 module not installed'
)
@unittest.skipIf(component_path['P11_MODULE'] is None, 'SoftHSM PKCS11 module not installed')
def setup() -> None:
logging.debug('Creating test pkcs11 token using softhsm')
try:
Expand All @@ -131,24 +123,18 @@ def setup() -> None:
with open(softhsm_conf, 'w') as f:
if softhsm_version == 2:
softhsm_db = _temp_dir()
f.write(
"""
f.write(f"""
# Generated by test
directories.tokendir = %s
directories.tokendir = {softhsm_db}
objectstore.backend = file
log.level = DEBUG
"""
% softhsm_db
)
""")
else:
softhsm_db = _temp_file()
f.write(
"""
f.write(f"""
# Generated by test
0:%s
"""
% softhsm_db
)
0:{softhsm_db}
""")

logging.debug('Initializing the token')
out, err = run_cmd(
Expand All @@ -167,16 +153,6 @@ def setup() -> None:
softhsm_conf=softhsm_conf,
)

# logging.debug('Generating 1024 bit RSA key in token')
# run_cmd([component_path['PKCS11_TOOL'],
# '--module', component_path['P11_MODULE'],
# '-l',
# '-k',
# '--key-type', 'rsa:1024',
# '--id', 'a1b2',
# '--label', 'test',
# '--pin', 'secret1'], softhsm_conf=softhsm_conf)

hash_priv_key = _temp_file()
logging.debug('Converting test private key to format for softhsm')
run_cmd(
Expand Down Expand Up @@ -228,16 +204,8 @@ def setup() -> None:
)
signer_cert_pem = _temp_file()
openssl_conf = _temp_file()
logging.debug(
'Generating OpenSSL config for version {}'.format(openssl_version)
)
logging.debug('Generating OpenSSL config for version {}'.format(openssl_version))
with open(openssl_conf, 'w') as f:
# Might be needed with some versions of openssl, but in more recent versions dynamic_path breaks it.
# dynamic_path = (
# 'dynamic_path = %s' % component_path['P11_ENGINE']
# if openssl_version.startswith(b'1.')
# else ''
# )
f.write(
'\n'.join(
[
Expand Down Expand Up @@ -342,7 +310,7 @@ def setup() -> None:
print('-' * 64)
traceback.print_exc()
print('-' * 64)
logging.error('PKCS11 tests disabled: unable to initialize test token: %s' % ex)
logging.error(f'PKCS11 tests disabled: unable to initialize test token: {ex}')
raise ex


Expand Down

0 comments on commit ce6e875

Please sign in to comment.