|
2 | 2 | # Common build utilities for Cosmian KMS build and packaging scripts |
3 | 3 | # Source this file to use the functions |
4 | 4 |
|
| 5 | +# Initialize common environment variables and flags |
| 6 | +# Sets: DEBUG_OR_RELEASE, FEATURES, RELEASE_FLAG, FEATURES_FLAG array, VARIANT_NAME |
| 7 | +init_build_env() { |
| 8 | + # Set defaults if not already set |
| 9 | + DEBUG_OR_RELEASE="${DEBUG_OR_RELEASE:-debug}" |
| 10 | + FEATURES="${FEATURES:-}" |
| 11 | + |
| 12 | + RELEASE_FLAG="" |
| 13 | + [ "$DEBUG_OR_RELEASE" = "release" ] && RELEASE_FLAG="--release" |
| 14 | + |
| 15 | + FEATURES_FLAG=() |
| 16 | + [ -n "$FEATURES" ] && FEATURES_FLAG=(--features "$FEATURES") |
| 17 | + |
| 18 | + VARIANT_NAME="FIPS" |
| 19 | + [ -n "$FEATURES" ] && VARIANT_NAME="non-FIPS" |
| 20 | + |
| 21 | + # Export variables so they're available in the calling script |
| 22 | + export DEBUG_OR_RELEASE FEATURES VARIANT_NAME |
| 23 | +} |
| 24 | + |
| 25 | +# Get repository root directory |
| 26 | +get_repo_root() { |
| 27 | + local script_dir="${1:-.}" |
| 28 | + cd "$script_dir" || exit |
| 29 | + git rev-parse --show-toplevel 2>/dev/null || (cd "$script_dir/../.." && pwd) |
| 30 | +} |
| 31 | + |
| 32 | +# Setup RUST_LOG for tests |
| 33 | +setup_test_logging() { |
| 34 | + export RUST_LOG="cosmian_kms_cli=error,cosmian_kms_server=error,cosmian_kmip=error,test_kms_server=error" |
| 35 | +} |
| 36 | + |
| 37 | +# Check if a TCP port is open (portable bash implementation) |
| 38 | +check_port() { |
| 39 | + local host="$1" port="$2" |
| 40 | + (exec 3<>/dev/tcp/"$host"/"$port") 2>/dev/null & |
| 41 | + local pid=$! |
| 42 | + local count=0 |
| 43 | + while [ $count -lt 20 ]; do |
| 44 | + kill -0 $pid 2>/dev/null || { |
| 45 | + wait $pid 2>/dev/null |
| 46 | + return $? |
| 47 | + } |
| 48 | + sleep 0.1 |
| 49 | + count=$((count + 1)) |
| 50 | + done |
| 51 | + kill -9 $pid 2>/dev/null |
| 52 | + wait $pid 2>/dev/null |
| 53 | + return 1 |
| 54 | +} |
| 55 | + |
| 56 | +# Run database tests (library and specific) |
| 57 | +# Usage: run_db_tests <db_type> [extra_test_args] |
| 58 | +run_db_tests() { |
| 59 | + local db_type="$1" |
| 60 | + shift |
| 61 | + |
| 62 | + echo "Running $db_type library tests..." |
| 63 | + KMS_TEST_DB="$db_type" cargo test --workspace --lib "$RELEASE_FLAG" "${FEATURES_FLAG[@]}" -- --nocapture "$@" |
| 64 | + |
| 65 | + echo "Running $db_type database-specific tests..." |
| 66 | + local test_name="test_db_${db_type//-/_}" |
| 67 | + KMS_TEST_DB="$db_type" cargo test -p cosmian_kms_server_database --lib "$RELEASE_FLAG" "${FEATURES_FLAG[@]}" -- --nocapture "$test_name" --ignored |
| 68 | +} |
| 69 | + |
| 70 | +# Check database service availability and run tests |
| 71 | +# Usage: check_and_test_db <db_name> <db_type> <host_var> <port_var> |
| 72 | +check_and_test_db() { |
| 73 | + local db_name="$1" db_type="$2" host_var="$3" port_var="$4" |
| 74 | + local host="${!host_var}" port="${!port_var}" |
| 75 | + |
| 76 | + if check_port "$host" "$port"; then |
| 77 | + echo "$db_name is running at $host:$port" |
| 78 | + run_db_tests "$db_type" |
| 79 | + echo "$db_name tests completed successfully." |
| 80 | + else |
| 81 | + echo "Error: $db_name is not running at $host:$port" >&2 |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +} |
| 85 | + |
5 | 86 | # Prepare OpenSSL staging directory for packaging |
6 | | -# Usage: prepare_openssl_staging |
7 | 87 | prepare_openssl_staging() { |
8 | 88 | local repo_root="${1:-$(pwd)}" |
9 | 89 | : "${FEATURES:=}" |
10 | 90 |
|
11 | | - # Determine variant based on FEATURES |
12 | | - local variant_name module_name |
13 | | - if [ -n "$FEATURES" ]; then |
14 | | - variant_name="non-FIPS" |
15 | | - module_name="legacy" |
16 | | - else |
17 | | - variant_name="FIPS" |
18 | | - module_name="fips" |
19 | | - fi |
| 91 | + local variant_name="FIPS" module_name="fips" |
| 92 | + [ -n "$FEATURES" ] && variant_name="non-FIPS" && module_name="legacy" |
20 | 93 |
|
21 | 94 | echo "Preparing OpenSSL artifacts for ${variant_name} packaging..." |
22 | 95 |
|
23 | 96 | local openssl_staging="$repo_root/target/openssl-staging" |
24 | | - |
25 | | - # Clean staging directory first |
26 | 97 | rm -rf "$openssl_staging" |
27 | 98 | mkdir -p "$openssl_staging/lib64/ossl-modules" |
28 | 99 |
|
29 | | - # Find OpenSSL in Nix store |
30 | 100 | local openssl_path openssl_dir |
31 | 101 | openssl_path=$(type -p openssl || command -v openssl) |
32 | | - if [ -z "$openssl_path" ]; then |
| 102 | + [ -z "$openssl_path" ] && { |
33 | 103 | echo "Error: openssl not found in PATH" >&2 |
34 | 104 | return 1 |
35 | | - fi |
| 105 | + } |
36 | 106 |
|
37 | 107 | openssl_dir=$(dirname "$(dirname "$openssl_path")") |
38 | 108 | echo "Using OpenSSL from: $openssl_dir" |
39 | 109 | echo "Staging OpenSSL artifacts to: $openssl_staging" |
40 | 110 |
|
| 111 | + # Determine module extension (.so for Linux, .dylib for macOS) |
| 112 | + local module_ext="so" |
| 113 | + [ "$(uname)" = "Darwin" ] && module_ext="dylib" |
| 114 | + |
41 | 115 | # Copy the appropriate module |
42 | | - if [ -f "$openssl_dir/lib64/ossl-modules/${module_name}.so" ]; then |
43 | | - cp "$openssl_dir/lib64/ossl-modules/${module_name}.so" "$openssl_staging/lib64/ossl-modules/" |
44 | | - echo "Copied ${module_name}.so from lib64" |
45 | | - elif [ -f "$openssl_dir/lib/ossl-modules/${module_name}.so" ]; then |
46 | | - cp "$openssl_dir/lib/ossl-modules/${module_name}.so" "$openssl_staging/lib64/ossl-modules/" |
47 | | - echo "Copied ${module_name}.so from lib" |
48 | | - else |
49 | | - echo "Error: ${module_name}.so not found" >&2 |
| 116 | + local module_found=false |
| 117 | + for libdir in lib64 lib; do |
| 118 | + if [ -f "$openssl_dir/$libdir/ossl-modules/${module_name}.${module_ext}" ]; then |
| 119 | + cp "$openssl_dir/$libdir/ossl-modules/${module_name}.${module_ext}" "$openssl_staging/lib64/ossl-modules/${module_name}.so" |
| 120 | + echo "Copied ${module_name}.${module_ext} from $libdir (saved as ${module_name}.so)" |
| 121 | + module_found=true |
| 122 | + break |
| 123 | + fi |
| 124 | + done |
| 125 | + |
| 126 | + [ "$module_found" = "false" ] && { |
| 127 | + echo "Error: ${module_name}.${module_ext} not found in lib or lib64/ossl-modules" >&2 |
50 | 128 | return 1 |
51 | | - fi |
| 129 | + } |
52 | 130 |
|
53 | 131 | # Copy SSL configuration files for FIPS variant |
54 | 132 | if [ -z "$FEATURES" ]; then |
55 | 133 | mkdir -p "$openssl_staging/ssl" |
56 | 134 |
|
57 | 135 | if [ -f "$openssl_dir/ssl/openssl.cnf" ]; then |
58 | 136 | cp "$openssl_dir/ssl/openssl.cnf" "$openssl_staging/ssl/" |
59 | | - # Replace nix store path with /usr/local/lib/openssl |
60 | 137 | sed -i "s|$openssl_dir/ssl|/usr/local/lib/openssl|g" "$openssl_staging/ssl/openssl.cnf" |
61 | 138 | echo "Copied and updated openssl.cnf" |
62 | 139 | fi |
63 | 140 |
|
64 | 141 | if [ -f "$openssl_dir/ssl/fipsmodule.cnf" ]; then |
65 | | - # Regenerate fipsmodule.cnf with correct module path for packaging |
66 | 142 | "$openssl_path" fipsinstall \ |
67 | 143 | -module "$openssl_staging/lib64/ossl-modules/fips.so" \ |
68 | 144 | -out "$openssl_staging/ssl/fipsmodule.cnf" |
69 | | - |
70 | | - # Add explicit module path pointing to install location |
71 | 145 | sed -i '/^\[fips_sect\]/a module-filename = /usr/local/lib/openssl/lib64/ossl-modules/fips.so' \ |
72 | 146 | "$openssl_staging/ssl/fipsmodule.cnf" |
73 | | - |
74 | 147 | echo "Regenerated fipsmodule.cnf with correct MAC and paths" |
75 | 148 | fi |
76 | 149 | fi |
77 | 150 |
|
78 | 151 | echo "OpenSSL ${variant_name} artifacts prepared at: $openssl_staging" |
79 | 152 | ls -la "$openssl_staging/lib64/ossl-modules/" |
80 | | - if [ -z "$FEATURES" ]; then |
81 | | - ls -la "$openssl_staging/ssl/" |
82 | | - fi |
| 153 | + [ -z "$FEATURES" ] && ls -la "$openssl_staging/ssl/" |
83 | 154 | } |
0 commit comments