2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00

Parse openssl-related vars in pytest

The openssl config needs to be parsed for some tests that use SoftHSM2.
Rewrite the parsing to python and ensure the required variables are
properly set test-wide.
This commit is contained in:
Tom Krizek 2024-02-26 13:52:55 +01:00 committed by Nicki Křížek
parent e531bfc3b3
commit b100ce4c88
No known key found for this signature in database
GPG Key ID: 01623B9B652A20A7
8 changed files with 51 additions and 30 deletions

View File

@ -572,28 +572,4 @@ copy_setports() {
$1 >$2 $1 >$2
} }
# parse_openssl_config - Parse OpenSSL configuration for HSM settings
#
# Will set SOFTHSM2_MODULE, OPENSSL_ENGINE and ENGINE_ARG based on openssl configuration.
parse_openssl_config() {
ENGINE_ARG=""
[ -f "$OPENSSL_CONF" ] || return 0
while IFS="=" read key val; do
# trim variables
key="${key## }"
key="${key%% }"
val="${val## }"
val="${val%% }"
case "$key" in
"engine_id")
OPENSSL_ENGINE="$val"
ENGINE_ARG="-E $OPENSSL_ENGINE"
;;
"MODULE_PATH" | "pkcs11-module-path")
SOFTHSM2_MODULE="$val"
;;
esac
done <"$OPENSSL_CONF"
}
grep_v() { grep -v "$@" || test $? = 1; } grep_v() { grep -v "$@" || test $? = 1; }

View File

@ -23,7 +23,6 @@
exit 255 exit 255
} }
parse_openssl_config
[ -f "$SOFTHSM2_MODULE" ] || { [ -f "$SOFTHSM2_MODULE" ] || {
echo_i "skip: softhsm2 module not available" echo_i "skip: softhsm2 module not available"
exit 1 exit 1

View File

@ -20,7 +20,6 @@ $SHELL clean.sh
OPENSSL_CONF= softhsm2-util --init-token --free --pin 1234 --so-pin 1234 --label "softhsm2-enginepkcs11" | awk '/^The token has been initialized and is reassigned to slot/ { print $NF }' OPENSSL_CONF= softhsm2-util --init-token --free --pin 1234 --so-pin 1234 --label "softhsm2-enginepkcs11" | awk '/^The token has been initialized and is reassigned to slot/ { print $NF }'
parse_openssl_config
printf '%s' "${HSMPIN:-1234}" >ns1/pin printf '%s' "${HSMPIN:-1234}" >ns1/pin
PWD=$(pwd) PWD=$(pwd)

View File

@ -16,7 +16,6 @@ set -e
# shellcheck source=conf.sh # shellcheck source=conf.sh
. ../conf.sh . ../conf.sh
parse_openssl_config
PWD=$(pwd) PWD=$(pwd)
status=0 status=0

View File

@ -16,6 +16,7 @@ from .autoconf import AC_VARS # type: ignore
# pylint: enable=import-error # pylint: enable=import-error
from .basic import BASIC_VARS from .basic import BASIC_VARS
from .openssl import OPENSSL_VARS
class VarLookup(ChainMap): class VarLookup(ChainMap):
@ -48,4 +49,4 @@ class VarLookup(ChainMap):
return iter(self.keys()) return iter(self.keys())
ALL = VarLookup(AC_VARS, BASIC_VARS) ALL = VarLookup(AC_VARS, BASIC_VARS, OPENSSL_VARS)

View File

@ -0,0 +1,49 @@
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
import os
import re
from .. import log
OPENSSL_VARS = {
"OPENSSL_CONF": os.getenv("OPENSSL_CONF", ""),
"SOFTHSM2_CONF": os.getenv("SOFTHSM2_CONF", ""),
"SOFTHSM2_MODULE": "",
"ENGINE_ARG": "",
}
def parse_openssl_config(path: str):
if not os.path.isfile(path):
return
regex = re.compile(r"([^=]+)=(.*)")
log.debug(f"parsing openssl config: {path}")
with open(path, "r", encoding="utf-8") as conf:
for line in conf:
res = regex.match(line)
if res:
key = res.group(1).strip()
val = res.group(2).strip()
if key == "engine_id":
OPENSSL_VARS["ENGINE_ARG"] = f"-E {val}"
os.environ["ENGINE_ARG"] = f"-E {val}"
log.debug("ENGINE_ARG set to {OPENSSL_VARS['ENGINE_ARG']}")
elif key in ["MODULE_PATH", "pkcs11-module-path"]:
OPENSSL_VARS["SOFTHSM2_MODULE"] = val
os.environ["SOFTHSM2_MODULE"] = val
log.debug(
"SOFTHSM2_MODULE set to {OPENSSL_VARS['SOFTHSM2_MODULE']}"
)
parse_openssl_config(OPENSSL_VARS["OPENSSL_CONF"])

View File

@ -18,7 +18,6 @@
exit 255 exit 255
} }
parse_openssl_config
[ -f "$SOFTHSM2_MODULE" ] || { [ -f "$SOFTHSM2_MODULE" ] || {
echo_i "skip: softhsm2 module not available" echo_i "skip: softhsm2 module not available"
exit 1 exit 1

View File

@ -16,7 +16,6 @@ set -e
# shellcheck source=conf.sh # shellcheck source=conf.sh
. ../conf.sh . ../conf.sh
parse_openssl_config
PWD=$(pwd) PWD=$(pwd)
keygen() { keygen() {