From 48172e93690f47544343fd0ff9f2fffa5459221c Mon Sep 17 00:00:00 2001 From: yucheng Date: Thu, 24 Sep 2026 07:44:14 +0000 Subject: [PATCH] feat(docker): build cryptography from source against the system OpenSSL in the FIPS image Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- docker/Dockerfile.fips | 9 +++ docker/scan_bundled_crypto.sh | 1 - tests/test_litellm/test_dockerfile_fips.py | 74 +++++++++++++++++++ .../test_litellm/test_scan_bundled_crypto.py | 64 ++++++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 tests/test_litellm/test_dockerfile_fips.py create mode 100644 tests/test_litellm/test_scan_bundled_crypto.py diff --git a/docker/Dockerfile.fips b/docker/Dockerfile.fips index 1aa0f893e1d..9a4d5bcf9ec 100644 --- a/docker/Dockerfile.fips +++ b/docker/Dockerfile.fips @@ -63,6 +63,9 @@ RUN --mount=type=secret,id=cgr-token \ apk add --no-cache \ bash \ gcc \ + rust \ + cargo \ + pkgconf \ python-3.13 \ python-3.13-dev \ openssl \ @@ -73,9 +76,12 @@ RUN --mount=type=secret,id=cgr-token \ # UV_PYTHON_DOWNLOADS=0: uv must never substitute a standalone CPython with # its own bundled OpenSSL for the FIPS-linked system python. +# UV_NO_BINARY_PACKAGE=cryptography: the PyPI wheel statically links its own +# OpenSSL, so it is compiled here against the FIPS-configured system libcrypto. ENV UV_PROJECT_ENVIRONMENT=/app/.venv \ UV_LINK_MODE=copy \ UV_PYTHON_DOWNLOADS=0 \ + UV_NO_BINARY_PACKAGE=cryptography \ PATH="/app/.venv/bin:${PATH}" COPY pyproject.toml uv.lock ./ @@ -107,6 +113,9 @@ RUN uv sync --frozen --no-default-groups --no-editable \ --extra bedrock-realtime \ --python python3.13 +RUN python -c "from cryptography.hazmat.backends.openssl.backend import backend; print(backend.openssl_version_text())" \ + | tee /dev/stderr | grep -q "$(openssl version | cut -d' ' -f1,2)" + RUN HOME=/opt/prisma XDG_CACHE_HOME=/opt/prisma/.cache PRISMA_BINARY_CACHE_DIR=/opt/prisma/binaries \ npm_config_cache=/root/.npm \ prisma generate --schema=./schema.prisma diff --git a/docker/scan_bundled_crypto.sh b/docker/scan_bundled_crypto.sh index fb8bd53b4ce..62028e7d5be 100755 --- a/docker/scan_bundled_crypto.sh +++ b/docker/scan_bundled_crypto.sh @@ -50,7 +50,6 @@ crypto_markers() { is_waived() { case "$1" in - */cryptography/hazmat/bindings/_rust.abi3.so) ;; */grpc/_cython/cygrpc*.so) ;; */hf_xet/hf_xet.abi3.so) ;; */_awscrt.abi3.so) ;; diff --git a/tests/test_litellm/test_dockerfile_fips.py b/tests/test_litellm/test_dockerfile_fips.py new file mode 100644 index 00000000000..4a005e42aa2 --- /dev/null +++ b/tests/test_litellm/test_dockerfile_fips.py @@ -0,0 +1,74 @@ +""" +Static checks on docker/Dockerfile.fips. + +The FIPS image must not ship the PyPI `cryptography` wheel, which statically +links its own OpenSSL. The builder stage compiles it from source against the +FIPS-configured system libcrypto and asserts the loaded backend reports the +system OpenSSL before anything is copied into the runtime image. +""" + +import re +import shlex +from pathlib import Path +from typing import Final + +import pytest + +DOCKERFILE_PATH: Final = Path(__file__).resolve().parents[2] / "docker" / "Dockerfile.fips" + +pytestmark = pytest.mark.skipif(not DOCKERFILE_PATH.exists(), reason="Dockerfile.fips not present in this checkout") + + +def _builder_stage() -> str: + match: Final = re.search(r"^FROM \S+ AS builder\n(.*?)(?=^FROM )", DOCKERFILE_PATH.read_text(), re.MULTILINE | re.DOTALL) + assert match, "Dockerfile.fips has no `FROM ... AS builder` stage" + return match.group(1) + + +def _instructions(stage: str) -> tuple[str, ...]: + return tuple(" ".join(part.strip() for part in block.split("\\\n")) for block in re.split(r"\n(?=[A-Z]+ )", stage)) + + +def _apk_packages(stage: str) -> frozenset[str]: + apk_runs: Final = tuple(i for i in _instructions(stage) if i.startswith("RUN") and "apk add" in i) + assert apk_runs, "builder stage installs nothing with apk" + return frozenset( + word + for run in apk_runs + for word in shlex.split(run.split("apk add", 1)[1]) + if not word.startswith("-") + ) + + +def test_builder_installs_the_toolchain_cryptography_needs_to_compile_against_system_openssl() -> None: + packages: Final = _apk_packages(_builder_stage()) + + assert {"rust", "cargo", "pkgconf", "openssl-dev"} <= packages, sorted(packages) + + +def test_builder_forbids_the_cryptography_wheel_before_the_first_uv_sync() -> None: + instructions: Final = _instructions(_builder_stage()) + first_sync: Final = next(i for i, text in enumerate(instructions) if text.startswith("RUN uv sync")) + no_binary: Final = tuple( + value + for text in instructions[:first_sync] + if text.startswith("ENV") + for key, _, value in (token.partition("=") for token in shlex.split(text.removeprefix("ENV"))) + if key == "UV_NO_BINARY_PACKAGE" + ) + + assert no_binary, "no UV_NO_BINARY_PACKAGE set before the first uv sync" + assert "cryptography" in no_binary[-1].split(","), no_binary + + +def test_builder_asserts_the_cryptography_backend_reports_the_system_openssl_after_the_last_uv_sync() -> None: + instructions: Final = _instructions(_builder_stage()) + last_sync: Final = max(i for i, text in enumerate(instructions) if text.startswith("RUN uv sync")) + assertion: Final = tuple( + text + for text in instructions[last_sync:] + if "cryptography.hazmat.backends.openssl.backend" in text and "openssl_version_text()" in text + ) + + assert len(assertion) == 1, instructions[last_sync:] + assert "openssl version" in assertion[0] and "grep" in assertion[0], assertion[0] diff --git a/tests/test_litellm/test_scan_bundled_crypto.py b/tests/test_litellm/test_scan_bundled_crypto.py new file mode 100644 index 00000000000..3bfbb248e50 --- /dev/null +++ b/tests/test_litellm/test_scan_bundled_crypto.py @@ -0,0 +1,64 @@ +""" +Behavior checks on docker/scan_bundled_crypto.sh, the build-time ELF gate of +the FIPS image. The gate is exercised for real over a synthetic site-packages +tree, so a waiver that lets a bundled OpenSSL through fails here. +""" + +import os +import shutil +import subprocess +from pathlib import Path +from typing import Final + +import pytest + +SCRIPT_PATH: Final = Path(__file__).resolve().parents[2] / "docker" / "scan_bundled_crypto.sh" +ELF_MAGIC: Final = b"\x7fELF" + +pytestmark = pytest.mark.skipif( + not SCRIPT_PATH.exists() or shutil.which("objdump") is None, + reason="scan_bundled_crypto.sh or objdump not present", +) + + +def _write_elf_with_bundled_openssl(path: Path) -> Path: + path.parent.mkdir(parents=True) + path.write_bytes(ELF_MAGIC + b"\0" * 60 + b"OpenSSL 3.0.15 3 Sep 2024\0") + return path + + +def _run_scan(scan_root: Path, tmp_path: Path) -> subprocess.CompletedProcess[str]: + inventory: Final = tmp_path / "inventory.txt" + inventory.write_text("") + return subprocess.run( + ["sh", str(SCRIPT_PATH), str(scan_root)], + env={ + **os.environ, + "SCAN_INVENTORY": str(inventory), + "SCAN_REPORT": str(tmp_path / "report.txt"), + }, + capture_output=True, + text=True, + check=False, + ) + + +def test_cryptography_bundling_its_own_openssl_fails_the_scan(tmp_path: Path) -> None: + binding: Final = _write_elf_with_bundled_openssl( + tmp_path / "venv" / "site-packages" / "cryptography" / "hazmat" / "bindings" / "_rust.abi3.so" + ) + + result: Final = _run_scan(tmp_path / "venv", tmp_path) + + assert result.returncode == 1, result.stdout + result.stderr + assert f"VIOLATION {binding} [bundled-OpenSSL]" in result.stdout, result.stdout + assert "WAIVED" not in result.stdout, result.stdout + + +def test_unrelated_waived_binary_still_passes_the_scan(tmp_path: Path) -> None: + _write_elf_with_bundled_openssl(tmp_path / "venv" / "site-packages" / "grpc" / "_cython" / "cygrpc.abi3.so") + + result: Final = _run_scan(tmp_path / "venv", tmp_path) + + assert result.returncode == 0, result.stdout + result.stderr + assert "WAIVED" in result.stdout and "cygrpc" in result.stdout, result.stdout