From 2157ae2d42894919f9aefab4d3fda3beb347e436 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Thu, 5 Nov 2026 17:32:27 +0000 Subject: [PATCH] fix(telemetry): service.version reports the release, not the crate version (#883) --- .gitea/workflows/ci.yml | 7 ++++ Dockerfile.dashboard | 3 ++ compliance-agent/src/api/handlers/platform.rs | 33 +--------------- compliance-core/src/build_info.rs | 38 +++++++++++++++++++ compliance-core/src/lib.rs | 1 + compliance-core/src/telemetry.rs | 5 ++- 6 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 compliance-core/src/build_info.rs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index b762865..edd5264 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -374,9 +374,16 @@ jobs: apk add --no-cache git curl openssl git init && git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" git fetch --depth=1 origin "${GITHUB_SHA}" && git checkout FETCH_HEAD + # service.version in telemetry: the latest release tag without its v, + # as for the agent. No tag → empty, and the crate version is used. + git fetch --quiet --unshallow --tags origin || true + TAG=$(git describe --tags --abbrev=0 --match 'v[0-9]*' 2>/dev/null || true) + PRUEFWERK_VERSION="${TAG#v}" + echo "version: ${PRUEFWERK_VERSION:-crate}" IMAGE=repo.breakpilot.com/pruefwerk/compliance-dashboard echo "$REGISTRY_PASSWORD" | docker login repo.breakpilot.com -u "$REGISTRY_USERNAME" --password-stdin DOCKER_BUILDKIT=1 docker build --pull --secret id=git_token,env=PLATFORM_CI_TOKEN \ + --build-arg PRUEFWERK_VERSION="${PRUEFWERK_VERSION}" \ -f Dockerfile.dashboard -t "$IMAGE:latest" -t "$IMAGE:${GITHUB_SHA}" . docker push "$IMAGE:latest" && docker push "$IMAGE:${GITHUB_SHA}" { command -v cosign >/dev/null 2>&1 || curl -sSfLo /usr/local/bin/cosign https://github.com/sigstore/cosign/releases/download/v2.4.3/cosign-linux-amd64 || wget -qO /usr/local/bin/cosign https://github.com/sigstore/cosign/releases/download/v2.4.3/cosign-linux-amd64; } || echo "::warning::cosign fetch failed" diff --git a/Dockerfile.dashboard b/Dockerfile.dashboard index 01c1d87..1cd06db 100644 --- a/Dockerfile.dashboard +++ b/Dockerfile.dashboard @@ -5,6 +5,9 @@ RUN cargo install dioxus-cli --version 0.7.3 --locked # Legal pages linked from the sidebar; the defaults are breakpilot.com's. ARG IMPRINT_URL=https://breakpilot.com/impressum ARG PRIVACY_URL=https://breakpilot.com/datenschutz +# The release telemetry reports as service.version (CI passes the latest +# release tag); empty falls back to the crate version. +ARG PRUEFWERK_VERSION= WORKDIR /app COPY . . diff --git a/compliance-agent/src/api/handlers/platform.rs b/compliance-agent/src/api/handlers/platform.rs index 0a011d8..fceda1f 100644 --- a/compliance-agent/src/api/handlers/platform.rs +++ b/compliance-agent/src/api/handlers/platform.rs @@ -18,6 +18,8 @@ use mongodb::bson::{doc, Document}; use serde::{Deserialize, Serialize}; use compliance_core::auth::{validate_service_token, JwksState}; +// Re-exported: the VEX, evidence and change-detection code import them from here. +pub use compliance_core::build_info::{BUILD_COMMIT, BUILD_VERSION}; use crate::agent::ComplianceAgent; @@ -432,23 +434,6 @@ pub async fn usage( })) } -/// The build version: `PRUEFWERK_VERSION` at compile time (CI: the latest -/// release tag without its `v`), else the crate version. -pub const BUILD_VERSION: &str = - non_empty_or(option_env!("PRUEFWERK_VERSION"), env!("CARGO_PKG_VERSION")); - -/// The commit the build is from: `PRUEFWERK_COMMIT` at compile time (CI: -/// the full SHA), else "unknown". The portal's version line shows 8 characters. -pub const BUILD_COMMIT: &str = non_empty_or(option_env!("PRUEFWERK_COMMIT"), "unknown"); - -/// A Docker build arg left empty is an empty env var, not an unset one. -const fn non_empty_or(value: Option<&'static str>, fallback: &'static str) -> &'static str { - match value { - Some(v) if !v.is_empty() => v, - _ => fallback, - } -} - /// `GET /api/v1/platform/version` — contract handshake (§4.1 /version). pub async fn version(State(st): State) -> Json { Json(serde_json::json!({ @@ -807,20 +792,6 @@ pub async fn delete_git_source( }))) } -#[cfg(test)] -mod build_info_tests { - use super::*; - - #[test] - fn an_unset_or_empty_build_value_falls_back() { - assert_eq!(non_empty_or(None, "0.4.4"), "0.4.4"); - assert_eq!(non_empty_or(Some(""), "unknown"), "unknown"); - assert_eq!(non_empty_or(Some("0.4.5"), "0.4.4"), "0.4.5"); - assert!(!BUILD_VERSION.is_empty()); - assert!(!BUILD_COMMIT.is_empty()); - } -} - #[cfg(test)] mod target_count_tests { use super::*; diff --git a/compliance-core/src/build_info.rs b/compliance-core/src/build_info.rs new file mode 100644 index 0000000..cafc5e0 --- /dev/null +++ b/compliance-core/src/build_info.rs @@ -0,0 +1,38 @@ +//! The release a binary was built as, read at compile time. +//! +//! CI passes the latest release tag (without its `v`) as `PRUEFWERK_VERSION` +//! and the commit as `PRUEFWERK_COMMIT` to the image build. The crate version +//! is only the fallback for a local build: it is not bumped per release, so +//! anything that names the running version (the agent's `/version` handshake, +//! telemetry's `service.version`, evidence tooling) uses these constants. + +/// The build version: `PRUEFWERK_VERSION` at compile time, else the crate +/// version. +pub const BUILD_VERSION: &str = + non_empty_or(option_env!("PRUEFWERK_VERSION"), env!("CARGO_PKG_VERSION")); + +/// The commit the build is from: `PRUEFWERK_COMMIT` at compile time (CI: the +/// full SHA), else "unknown". The portal's version line shows 8 characters. +pub const BUILD_COMMIT: &str = non_empty_or(option_env!("PRUEFWERK_COMMIT"), "unknown"); + +/// A Docker build arg left empty is an empty env var, not an unset one. +const fn non_empty_or(value: Option<&'static str>, fallback: &'static str) -> &'static str { + match value { + Some(v) if !v.is_empty() => v, + _ => fallback, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_unset_or_empty_build_value_falls_back() { + assert_eq!(non_empty_or(None, "0.4.4"), "0.4.4"); + assert_eq!(non_empty_or(Some(""), "unknown"), "unknown"); + assert_eq!(non_empty_or(Some("0.4.5"), "0.4.4"), "0.4.5"); + assert!(!BUILD_VERSION.is_empty()); + assert!(!BUILD_COMMIT.is_empty()); + } +} diff --git a/compliance-core/src/lib.rs b/compliance-core/src/lib.rs index c781990..e85c2e0 100644 --- a/compliance-core/src/lib.rs +++ b/compliance-core/src/lib.rs @@ -1,3 +1,4 @@ +pub mod build_info; pub mod config; pub mod control_check; pub mod csaf_docs; diff --git a/compliance-core/src/telemetry.rs b/compliance-core/src/telemetry.rs index e31d896..0d08ab2 100644 --- a/compliance-core/src/telemetry.rs +++ b/compliance-core/src/telemetry.rs @@ -55,7 +55,10 @@ fn build_resource(service_name: &str) -> Resource { let name = std::env::var("OTEL_SERVICE_NAME").unwrap_or_else(|_| service_name.to_string()); Resource::builder() .with_service_name(name) - .with_attributes([KeyValue::new("service.version", env!("CARGO_PKG_VERSION"))]) + .with_attributes([KeyValue::new( + "service.version", + crate::build_info::BUILD_VERSION, + )]) .build() }