Skip to content

Commit 57949d8

Browse files
committed
chore: compactify bash scripts
1 parent cb4baef commit 57949d8

20 files changed

+279
-497
lines changed

.github/scripts/cargo_build.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function BuildProject
1010
[string]$BuildType
1111
)
1212

13-
$env:RUST_LOG = "cosmian_kms_cli=error,cosmian_kms_server=error,cosmian_kmip=error,test_kms_server=error"
1413
# Add target
1514
rustup target add x86_64-pc-windows-msvc
1615

@@ -24,26 +23,25 @@ function BuildProject
2423
if ($BuildType -eq "release")
2524
{
2625
cargo build --release --features "non-fips"
27-
cargo test --release --features "non-fips" -- --nocapture
2826
cargo packager --verbose --formats nsis --release
2927
}
3028
else
3129
{
3230
cargo build --features "non-fips"
33-
cargo test --features "non-fips" -- --nocapture
3431
cargo packager --verbose --formats nsis
3532
}
3633
Get-ChildItem ..\..
3734

3835
# Check dynamic links
36+
$previousErrorActionPreference = $ErrorActionPreference
3937
$ErrorActionPreference = "SilentlyContinue"
4038
$output = & "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\dumpbin.exe" /dependents target\$BuildType\cosmian_kms.exe | Select-String "libcrypto"
39+
$ErrorActionPreference = $previousErrorActionPreference
4140
if ($output)
4241
{
43-
throw "OpenSSL (libcrypto) found in dynamic dependencies. Error: $output"
42+
Write-Error "OpenSSL (libcrypto) found in dynamic dependencies. Error: $output"
43+
exit 1
4444
}
45-
46-
exit 0
4745
}
4846

4947

.github/scripts/cargo_test.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
$ErrorActionPreference = "Stop"
2+
Set-StrictMode -Version Latest
3+
$PSNativeCommandUseErrorActionPreference = $true # might be true by default
4+
5+
function TestProject
6+
{
7+
param (
8+
[Parameter(Mandatory = $true)]
9+
[ValidateSet("debug", "release")]
10+
[string]$BuildType
11+
)
12+
13+
$env:RUST_LOG = "cosmian_kms_cli=error,cosmian_kms_server=error,cosmian_kmip=error,test_kms_server=error"
14+
# Add target
15+
rustup target add x86_64-pc-windows-msvc
16+
17+
$env:OPENSSL_DIR = "$env:VCPKG_INSTALLATION_ROOT\packages\openssl_x64-windows-static"
18+
Get-ChildItem -Recurse $env:OPENSSL_DIR
19+
20+
if ($BuildType -eq "release")
21+
{
22+
cargo test -p cosmian_kms_server --release --features "non-fips" -- --nocapture
23+
# cargo test --lib --workspace --release --features "non-fips" -- --nocapture
24+
if ($LASTEXITCODE -ne 0)
25+
{
26+
Write-Error "Release tests failed with exit code $LASTEXITCODE"
27+
exit $LASTEXITCODE
28+
}
29+
}
30+
else
31+
{
32+
cargo test -p cosmian_kms_server --features "non-fips" -- --nocapture
33+
# cargo test --lib --workspace --features "non-fips" -- --nocapture
34+
if ($LASTEXITCODE -ne 0)
35+
{
36+
Write-Error "Debug tests failed with exit code $LASTEXITCODE"
37+
exit $LASTEXITCODE
38+
}
39+
}
40+
}
41+
42+
43+
# Example usage:
44+
# TestProject -BuildType debug
45+
# TestProject -BuildType release

.github/scripts/check_build.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/scripts/find_empty_files.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.
-8.5 MB
Binary file not shown.

artifacts/openssl-3.1.2-linux-x86_64-glibc2.27.tar.gz.origin_prefix.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

artifacts/openssl-3.1.2-linux-x86_64-glibc2.27.tar.gz.origin_prefix.txt.sha256

Lines changed: 0 additions & 1 deletion
This file was deleted.

artifacts/openssl-3.1.2-linux-x86_64-glibc2.27.tar.gz.sha256

Lines changed: 0 additions & 1 deletion
This file was deleted.

nix/scripts/build.sh

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,29 @@
22
set -eo pipefail
33
set -x
44

5-
# Resolve inputs with defaults inside the nix environment
6-
: "${DEBUG_OR_RELEASE:=debug}"
7-
: "${FEATURES:=}"
5+
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
6+
source "$SCRIPT_DIR/common.sh"
87

9-
RELEASE_FLAG=""
10-
if [ "$DEBUG_OR_RELEASE" = "release" ]; then
11-
RELEASE_FLAG="--release"
12-
fi
13-
14-
# Construct features flag
15-
FEATURES_FLAG=()
16-
if [ -n "$FEATURES" ]; then
17-
FEATURES_FLAG=(--features "$FEATURES")
18-
fi
8+
init_build_env
199

20-
cargo build -p cosmian_kms_server $RELEASE_FLAG "${FEATURES_FLAG[@]}"
10+
cargo build -p cosmian_kms_server "$RELEASE_FLAG" "${FEATURES_FLAG[@]}"
2111

2212
COSMIAN_KMS_EXE="target/$DEBUG_OR_RELEASE/cosmian_kms"
2313

24-
# For verification during build, we temporarily use the nix store OpenSSL config
25-
# At runtime, the binary will use /usr/local/lib/openssl (its compiled-in OPENSSLDIR)
26-
# after running the setup_openssl_runtime.sh script
14+
# Verify binary works (temporarily use nix store OpenSSL config)
2715
export OPENSSL_CONF="${NIX_OPENSSL_OUT:-}/ssl/openssl.cnf"
2816
INFO_OUTPUT=$("$COSMIAN_KMS_EXE" --version 2>&1 || true)
2917
echo "$INFO_OUTPUT"
3018
echo "$INFO_OUTPUT" | grep -q "cosmian_kms_server" || {
3119
echo "Error: Binary does not appear to be working" >&2
3220
exit 1
3321
}
34-
35-
# Unset OPENSSL_CONF so runtime will use the compiled-in OPENSSLDIR
3622
unset OPENSSL_CONF
3723

3824
echo "Note: Binary built with OPENSSLDIR=/usr/local/lib/openssl"
3925
echo "Run 'nix-shell --keep NIX_OPENSSL_OUT shell.nix --run \"bash nix/scripts/setup_openssl_runtime.sh\"' to install runtime files"
4026

27+
# Platform-specific checks
4128
UNAME=$(uname)
4229
if [ "$UNAME" = "Linux" ]; then
4330
LDD_OUTPUT=$(ldd "$COSMIAN_KMS_EXE")
@@ -47,17 +34,15 @@ if [ "$UNAME" = "Linux" ]; then
4734
exit 1
4835
}
4936

50-
# Verify GLIBC symbol versions are <= 2.28 (Linux only)
37+
# Verify GLIBC symbol versions are <= 2.28
5138
GLIBC_SYMS=$(readelf -sW "$COSMIAN_KMS_EXE" | grep -o 'GLIBC_[0-9][0-9.]*' | sort -Vu)
5239
echo "$GLIBC_SYMS"
5340
MAX_GLIBC_VER=""
54-
if [ -n "$GLIBC_SYMS" ]; then
55-
MAX_GLIBC_VER=$(echo "$GLIBC_SYMS" | sed 's/^GLIBC_//' | sort -V | tail -n1)
56-
fi
57-
if [ -n "$MAX_GLIBC_VER" ] && [ "$(printf '%s\n' "$MAX_GLIBC_VER" "2.28" | sort -V | tail -n1)" != "2.28" ]; then
41+
[ -n "$GLIBC_SYMS" ] && MAX_GLIBC_VER=$(echo "$GLIBC_SYMS" | sed 's/^GLIBC_//' | sort -V | tail -n1)
42+
[ -n "$MAX_GLIBC_VER" ] && [ "$(printf '%s\n' "$MAX_GLIBC_VER" "2.28" | sort -V | tail -n1)" != "2.28" ] && {
5843
echo "Error: GLIBC symbols exceed 2.28 (max found: $MAX_GLIBC_VER)." >&2
5944
exit 1
60-
fi
45+
}
6146
else
6247
# macOS: check with otool
6348
if command -v otool >/dev/null 2>&1; then

nix/scripts/common.sh

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,153 @@
22
# Common build utilities for Cosmian KMS build and packaging scripts
33
# Source this file to use the functions
44

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+
586
# Prepare OpenSSL staging directory for packaging
6-
# Usage: prepare_openssl_staging
787
prepare_openssl_staging() {
888
local repo_root="${1:-$(pwd)}"
989
: "${FEATURES:=}"
1090

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"
2093

2194
echo "Preparing OpenSSL artifacts for ${variant_name} packaging..."
2295

2396
local openssl_staging="$repo_root/target/openssl-staging"
24-
25-
# Clean staging directory first
2697
rm -rf "$openssl_staging"
2798
mkdir -p "$openssl_staging/lib64/ossl-modules"
2899

29-
# Find OpenSSL in Nix store
30100
local openssl_path openssl_dir
31101
openssl_path=$(type -p openssl || command -v openssl)
32-
if [ -z "$openssl_path" ]; then
102+
[ -z "$openssl_path" ] && {
33103
echo "Error: openssl not found in PATH" >&2
34104
return 1
35-
fi
105+
}
36106

37107
openssl_dir=$(dirname "$(dirname "$openssl_path")")
38108
echo "Using OpenSSL from: $openssl_dir"
39109
echo "Staging OpenSSL artifacts to: $openssl_staging"
40110

111+
# Determine module extension (.so for Linux, .dylib for macOS)
112+
local module_ext="so"
113+
[ "$(uname)" = "Darwin" ] && module_ext="dylib"
114+
41115
# 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
50128
return 1
51-
fi
129+
}
52130

53131
# Copy SSL configuration files for FIPS variant
54132
if [ -z "$FEATURES" ]; then
55133
mkdir -p "$openssl_staging/ssl"
56134

57135
if [ -f "$openssl_dir/ssl/openssl.cnf" ]; then
58136
cp "$openssl_dir/ssl/openssl.cnf" "$openssl_staging/ssl/"
59-
# Replace nix store path with /usr/local/lib/openssl
60137
sed -i "s|$openssl_dir/ssl|/usr/local/lib/openssl|g" "$openssl_staging/ssl/openssl.cnf"
61138
echo "Copied and updated openssl.cnf"
62139
fi
63140

64141
if [ -f "$openssl_dir/ssl/fipsmodule.cnf" ]; then
65-
# Regenerate fipsmodule.cnf with correct module path for packaging
66142
"$openssl_path" fipsinstall \
67143
-module "$openssl_staging/lib64/ossl-modules/fips.so" \
68144
-out "$openssl_staging/ssl/fipsmodule.cnf"
69-
70-
# Add explicit module path pointing to install location
71145
sed -i '/^\[fips_sect\]/a module-filename = /usr/local/lib/openssl/lib64/ossl-modules/fips.so' \
72146
"$openssl_staging/ssl/fipsmodule.cnf"
73-
74147
echo "Regenerated fipsmodule.cnf with correct MAC and paths"
75148
fi
76149
fi
77150

78151
echo "OpenSSL ${variant_name} artifacts prepared at: $openssl_staging"
79152
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/"
83154
}

0 commit comments

Comments
 (0)