Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

build: ship Rust license notices and cache AI gateway dependency builds (#18889)

Tobias Wochingera9a58dfb1568
diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml
index fda0f6306c..a0f46820a4 100644
--- a/.github/workflows/pipeline.yml
+++ b/.github/workflows/pipeline.yml
@@ -212,8 +212,14 @@ jobs:
run: cargo deny --locked check bans licenses sources
- name: Run Rust tests
run: bash "$GITHUB_WORKSPACE/scripts/ci/retry-on-merge-queue.sh" cargo test --locked
+ # Persistent layer cache, so the toolchain and compiled-dependency layers
+ # are reused until the Dockerfile, Cargo.toml or Cargo.lock change.
+ - name: Setup Blacksmith Builder
+ uses: useblacksmith/setup-docker-builder@1e75355f81cbb3f93cba1eb5dd5e5b05110ca8f9 # v2.0.1
+ with:
+ cache-key: langfuse/test-ai-gateway
- name: Build runtime image
- run: docker build --pull --target runtime -t langfuse-ai-gateway:test .
+ run: docker buildx build --load --pull --target runtime -t langfuse-ai-gateway:test .
- name: Smoke test runtime image
run: bash scripts/smoke-image.sh langfuse-ai-gateway:test
diff --git a/.semgrepignore b/.semgrepignore
index 201e381613..f91738c127 100644
--- a/.semgrepignore
+++ b/.semgrepignore
@@ -11,3 +11,10 @@ scripts/code-eval-runners/
# path-only, so the whole file is skipped; list further traced *.lock.yml files
# here individually rather than widening to a glob.
.github/workflows/dependabot-security-maintainer.lock.yml
+
+# cargo-about templates rendering the plain-text license notices shipped in the
+# Rust images at build time. Triple braces keep crate names and license texts
+# from being HTML-escaped; the output is never served as HTML, so the mustache
+# explicit-unescape XSS rule does not apply.
+ai-gateway/about.hbs
+packages/native/about.hbs
diff --git a/ai-gateway/.dockerignore b/ai-gateway/.dockerignore
index 27387a05fe..06ca183730 100644
--- a/ai-gateway/.dockerignore
+++ b/ai-gateway/.dockerignore
@@ -2,5 +2,7 @@
!Cargo.toml
!Cargo.lock
!rust-toolchain.toml
+!about.toml
+!about.hbs
!src
!src/**
diff --git a/ai-gateway/Dockerfile b/ai-gateway/Dockerfile
index 4e72ff3a99..c0caaa9f04 100644
--- a/ai-gateway/Dockerfile
+++ b/ai-gateway/Dockerfile
@@ -16,7 +16,7 @@
ARG BASE_IMAGE=registry.access.redhat.com/ubi9/ubi:9.8
ARG MICRO_BASE_IMAGE=registry.access.redhat.com/ubi9/ubi-micro:9.8
-FROM --platform=${TARGETPLATFORM:-linux/amd64} ${BASE_IMAGE} AS builder
+FROM --platform=${TARGETPLATFORM:-linux/amd64} ${BASE_IMAGE} AS toolchain
ARG TARGETARCH
@@ -26,8 +26,11 @@ ARG TARGETARCH
# the official rust:1.98.0-bookworm image links against Debian's glibc 2.36,
# so binaries built there refuse to run on UBI9 (glibc 2.34). Building on UBI9
# keeps the toolchain and the binary on the FIPS-validated platform.
-RUN dnf -y upgrade && \
- dnf -y install --setopt=install_weak_deps=0 gcc tar gzip && \
+# No `dnf upgrade`: nothing from this stage but the binary and the license
+# notices reaches the runtime image, whose packages are installed fresh by the
+# rpm-installer stage. Linking against the base image's glibc also keeps it at
+# or below the runtime's version.
+RUN dnf -y install --setopt=install_weak_deps=0 gcc tar gzip && \
dnf clean all
# Pinned standalone Rust 1.98.0 installer, the exact channel from
@@ -49,15 +52,55 @@ RUN set -eux; \
--components=rustc,cargo,rust-std-${triple}; \
rm -rf /tmp/rust.tar.gz /tmp/rust-1.98.0-${triple}
+# cargo-about renders the third-party license notices shipped in the runtime
+# image. Pinned, checksum-verified static release binary (sha256 from the
+# release's `.tar.gz.sha256` assets), so the builder does not compile it.
+RUN set -eux; \
+ case "${TARGETARCH:-amd64}" in \
+ amd64) about_arch=x86_64; about_sha256=9099a59e820c38a68b9d65f300662a567d56562f9a10f6aa4c7e86c17c2566af ;; \
+ arm64) about_arch=aarch64; about_sha256=af5169282fb6f84e13471493f405437e43ac517744c9ae12fbe2cdf0a6f0e5a8 ;; \
+ *) echo "unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
+ esac; \
+ about_dir="cargo-about-0.9.2-${about_arch}-unknown-linux-musl"; \
+ curl -fsSL -o /tmp/cargo-about.tar.gz "https://github.com/EmbarkStudios/cargo-about/releases/download/0.9.2/${about_dir}.tar.gz"; \
+ echo "${about_sha256} /tmp/cargo-about.tar.gz" | sha256sum -c -; \
+ tar -xzf /tmp/cargo-about.tar.gz -C /tmp; \
+ install -m 0755 "/tmp/${about_dir}/cargo-about" /usr/local/bin/cargo-about; \
+ rm -rf /tmp/cargo-about.tar.gz "/tmp/${about_dir}"
+
+FROM --platform=${TARGETPLATFORM:-linux/amd64} toolchain AS deps
+
WORKDIR /app
+# Fetch the locked dependency sources against empty targets, so this layer and
+# everything built on it stay cached until Cargo.toml or Cargo.lock change.
COPY Cargo.toml Cargo.lock rust-toolchain.toml ./
-COPY src ./src
+RUN mkdir src && echo 'fn main() {}' > src/main.rs && touch src/lib.rs && \
+ cargo fetch --locked
+
+FROM --platform=${TARGETPLATFORM:-linux/amd64} deps AS builder
# rust-toolchain.toml is honored by rustup only; the standalone installer's
-# cargo ignores it, so --locked plus the pinned installer above guarantee the
-# 1.98.0 toolchain and the checked-in lockfile.
-RUN cargo build --release --locked
+# cargo ignores it, so --frozen (the checked-in lockfile, sources from the
+# fetch above) plus the pinned installer guarantee the 1.98.0 toolchain.
+# Dependencies compile once against the stub targets; source-only changes
+# rebuild just the crate. `cargo clean -p` drops the stub's own artifacts so
+# the real sources always rebuild regardless of their mtimes.
+RUN cargo build --release --frozen && \
+ cargo clean --release --frozen -p ai-gateway && \
+ rm -rf src
+
+COPY src ./src
+
+RUN cargo build --release --frozen
+
+# Runs in parallel with the compile. `--frozen` reads licenses only from the
+# fetched crate sources, and `generate` fails on any dependency whose license
+# about.toml does not accept.
+FROM --platform=${TARGETPLATFORM:-linux/amd64} deps AS licenses
+
+COPY about.toml about.hbs ./
+RUN cargo about generate --frozen --fail about.hbs -o THIRD_PARTY_LICENSES.txt
# Runtime rootfs assembled with dnf --installroot and layered onto ubi9-micro:
# the final image carries no package manager, python, or curl. It holds only
@@ -91,6 +134,7 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} ${MICRO_BASE_IMAGE} AS runtime
COPY --from=rpm-installer /mnt/rootfs /
COPY --from=builder /app/target/release/ai-gateway /usr/local/bin/ai-gateway
+COPY --from=licenses /app/THIRD_PARTY_LICENSES.txt /usr/share/licenses/ai-gateway/THIRD_PARTY_LICENSES.txt
ARG BUILD_ID
ENV BUILD_ID=$BUILD_ID
diff --git a/ai-gateway/README.md b/ai-gateway/README.md
index 524226c1b6..e083b797a3 100644
--- a/ai-gateway/README.md
+++ b/ai-gateway/README.md
@@ -802,6 +802,13 @@ grace period longer than the gateway shutdown timeout. The smoke test uses an
isolated container and an ephemeral host port, checks read-only/non-root startup,
probes, rejection of unauthenticated inference and a clean SIGTERM exit, then removes it.
+The image ships the license notices of every third-party crate compiled into the
+binary at `/usr/share/licenses/ai-gateway/THIRD_PARTY_LICENSES.txt`, rendered by
+[cargo-about](https://github.com/EmbarkStudios/cargo-about) from `about.hbs`.
+`about.toml` lists the accepted licenses; the image build fails when a dependency
+uses any other license, so a new license needs an explicit entry there. To render
+the file locally: `cargo about generate --locked --fail about.hbs`.
+
## Tests and module boundaries
### Web resolution client
diff --git a/ai-gateway/about.hbs b/ai-gateway/about.hbs
new file mode 100644
index 0000000000..6b264f0f8c
--- /dev/null
+++ b/ai-gateway/about.hbs
@@ -0,0 +1,22 @@
+THIRD-PARTY SOFTWARE NOTICES
+
+The Langfuse AI gateway binary includes the third-party Rust crates listed
+below. Each license text is reproduced as distributed with the crates that
+use it.
+
+Licenses:
+{{#each overview}}
+ {{{name}}} ({{{count}}})
+{{/each}}
+{{#each licenses}}
+
+================================================================================
+{{{name}}}
+
+Used by:
+{{#each used_by}}
+ {{{crate.name}}} {{{crate.version}}}{{#if crate.repository}} <{{{crate.repository}}}>{{/if}}
+{{/each}}
+
+{{{text}}}
+{{/each}}
diff --git a/ai-gateway/about.toml b/ai-gateway/about.toml
new file mode 100644
index 0000000000..0facf49ea6
--- /dev/null
+++ b/ai-gateway/about.toml
@@ -0,0 +1,14 @@
+# cargo-about configuration for the third-party notices shipped in the image
+# (see Dockerfile). `cargo about generate` fails on any dependency whose
+# license is not listed here, so adding a license is a deliberate review step.
+# Order is preference: dual-licensed crates are attributed to the first match.
+accepted = [
+ "MIT",
+ "Apache-2.0",
+ "BSD-3-Clause",
+ "ISC",
+ "Unicode-3.0",
+]
+targets = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
+ignore-dev-dependencies = true
+private = { ignore = true }
diff --git a/ai-gateway/scripts/smoke-image.sh b/ai-gateway/scripts/smoke-image.sh
index 15baa74f05..3eb51ae0d0 100644
--- a/ai-gateway/scripts/smoke-image.sh
+++ b/ai-gateway/scripts/smoke-image.sh
@@ -13,7 +13,9 @@ cleanup() {
trap cleanup EXIT
port="$(docker port "$container" 8080/tcp)"
for route in health ready; do
- body="$(curl --fail --silent --show-error --retry 20 --retry-connrefused \
+ # Docker's port proxy can accept and then reset connections until the
+ # gateway listens, which --retry-connrefused alone does not retry.
+ body="$(curl --fail --silent --show-error --retry 20 --retry-all-errors \
--retry-delay 1 --max-time 2 "http://$port/$route")"
case "$route:$body" in
'health:{"status":"ok"}'|'ready:{"status":"ready"}') ;;
@@ -22,10 +24,11 @@ for route in health ready; do
done
docker exec "$container" sh -c 'wget -qO /dev/null http://localhost:8080/health || exit 1'
[[ "$(docker exec "$container" id -u)" != 0 ]]
+docker exec "$container" test -s /usr/share/licenses/ai-gateway/THIRD_PARTY_LICENSES.txt
[[ "$(curl --silent --output /dev/null --write-out '%{http_code}' \
--max-time 2 -X POST "http://$port/openai/v1/responses")" == 401 ]]
docker stop --time 5 "$container" >/dev/null
[[ "$(docker inspect --format '{{.State.ExitCode}}' "$container")" == 0 ]]
docker logs "$container" 2>&1 | grep -q 'gateway draining'
docker logs "$container" 2>&1 | grep -q 'gateway stopped'
-printf '%s\n' 'Image smoke: non-root, probes, ECS health command, unauthenticated inference, SIGTERM passed'
+printf '%s\n' 'Image smoke: non-root, license notices, probes, ECS health command, unauthenticated inference, SIGTERM passed'
diff --git a/packages/native/README.md b/packages/native/README.md
index 2b0e28c1ef..c7080980e7 100644
--- a/packages/native/README.md
+++ b/packages/native/README.md
@@ -84,7 +84,12 @@ the checksum-verified standalone Rust installer into its UBI9 builder stage;
`turbo run build --filter=worker...` compiles the addon for glibc, and
`pnpm deploy` copies the `.node` file into the runtime image next to the
loader. Each architecture builds on a native runner, so no cross
-compilation is involved. The runtime image gains only the compiled library.
+compilation is involved. The runtime image gains only the compiled library and
+its third-party license notices at
+`/usr/share/licenses/langfuse-native/THIRD_PARTY_LICENSES.txt`, rendered by
+[cargo-about](https://github.com/EmbarkStudios/cargo-about) from `about.hbs`.
+`about.toml` lists the accepted licenses; the worker image build fails when a
+dependency uses any other license.
CI jobs that build the worker need no extra setup: rustup on the runner
installs the pinned toolchain the first time `cargo` runs in this directory.
The Rust lint runs as its own step in the lint job, outside the ESLint turbo
diff --git a/packages/native/about.hbs b/packages/native/about.hbs
new file mode 100644
index 0000000000..278101b391
--- /dev/null
+++ b/packages/native/about.hbs
@@ -0,0 +1,22 @@
+THIRD-PARTY SOFTWARE NOTICES
+
+The Langfuse worker's native addon (@langfuse/native) includes the
+third-party Rust crates listed below. Each license text is reproduced as
+distributed with the crates that use it.
+
+Licenses:
+{{#each overview}}
+ {{{name}}} ({{{count}}})
+{{/each}}
+{{#each licenses}}
+
+================================================================================
+{{{name}}}
+
+Used by:
+{{#each used_by}}
+ {{{crate.name}}} {{{crate.version}}}{{#if crate.repository}} <{{{crate.repository}}}>{{/if}}
+{{/each}}
+
+{{{text}}}
+{{/each}}
diff --git a/packages/native/about.toml b/packages/native/about.toml
new file mode 100644
index 0000000000..103382a4cf
--- /dev/null
+++ b/packages/native/about.toml
@@ -0,0 +1,15 @@
+# cargo-about configuration for the third-party notices shipped in the worker
+# image (see worker/Dockerfile). `cargo about generate` fails on any dependency
+# whose license is not listed here, so adding a license is a deliberate review
+# step. Order is preference: dual-licensed crates are attributed to the first
+# match.
+accepted = [
+ "MIT",
+ "Apache-2.0",
+ "ISC",
+ "Unicode-3.0",
+ "Zlib",
+]
+targets = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
+ignore-dev-dependencies = true
+private = { ignore = true }
diff --git a/worker/Dockerfile b/worker/Dockerfile
index 795fe49847..35974d3a47 100644
--- a/worker/Dockerfile
+++ b/worker/Dockerfile
@@ -136,6 +136,23 @@ RUN set -eux; \
--components=rustc,cargo,rust-std-${triple}; \
rm -rf /tmp/rust.tar.gz /tmp/rust-1.98.0-${triple}
+# cargo-about renders the third-party license notices for the native addon's
+# Rust dependencies. Pinned, checksum-verified static release binary (sha256
+# from the release's `.tar.gz.sha256` assets); keep the version in sync with
+# ai-gateway/Dockerfile.
+RUN set -eux; \
+ case "${TARGETARCH:-amd64}" in \
+ amd64) about_arch=x86_64; about_sha256=9099a59e820c38a68b9d65f300662a567d56562f9a10f6aa4c7e86c17c2566af ;; \
+ arm64) about_arch=aarch64; about_sha256=af5169282fb6f84e13471493f405437e43ac517744c9ae12fbe2cdf0a6f0e5a8 ;; \
+ *) echo "unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
+ esac; \
+ about_dir="cargo-about-0.9.2-${about_arch}-unknown-linux-musl"; \
+ curl -fsSL -o /tmp/cargo-about.tar.gz "https://github.com/EmbarkStudios/cargo-about/releases/download/0.9.2/${about_dir}.tar.gz"; \
+ echo "${about_sha256} /tmp/cargo-about.tar.gz" | sha256sum -c -; \
+ tar -xzf /tmp/cargo-about.tar.gz -C /tmp; \
+ install -m 0755 "/tmp/${about_dir}/cargo-about" /usr/local/bin/cargo-about; \
+ rm -rf /tmp/cargo-about.tar.gz "/tmp/${about_dir}"
+
FROM --platform=${TARGETPLATFORM:-linux/amd64} toolchain AS builder
WORKDIR /app
@@ -166,6 +183,18 @@ COPY --from=pruner /app/out/full/ .
RUN turbo run build --filter=worker...
+# Runs after the build so the generated clickhouse patch overlay
+# (packages/native/.cargo) resolves the same dependency graph that was
+# compiled. `cargo fetch` adds the sources the build skipped (dev and
+# other-target dependencies) that cargo metadata still reads; `generate` then
+# reads licenses only from local crate sources and fails on any dependency
+# whose license packages/native/about.toml does not accept.
+RUN cd packages/native && \
+ mkdir -p /usr/share/licenses/langfuse-native && \
+ cargo fetch --locked && \
+ cargo about generate --frozen --fail about.hbs \
+ -o /usr/share/licenses/langfuse-native/THIRD_PARTY_LICENSES.txt
+
FROM --platform=${TARGETPLATFORM:-linux/amd64} builder AS prod-deps
# previously we copied the --from=builder /app . (includes full node_modules etc)
@@ -200,6 +229,7 @@ ENV DOCKER_BUILD 0
# Copy only production worker payload instead of full builder workspace (just /prod/worker not entire /app)
COPY --from=prod-deps --chown=expressjs:expressjs /prod/worker ./worker
+COPY --from=builder /usr/share/licenses/langfuse-native /usr/share/licenses/langfuse-native
RUN chmod +x ./worker/entrypoint.sh
# Numeric USER (uid of expressjs) so runAsNonRoot checks and Iron Bank
# hardening validation can verify the user without resolving names.