From 5ce496303d3695bb75e3f14d295fcf634d3d5266 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Wed, 14 Oct 2026 09:33:24 +0000 Subject: [PATCH] feat(audit): API-key writes carry the caller's address to the registry (#567) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audit entries for API-key writes recorded the agent's container address (172.17.0.6 on stage), because the registry stores the peer of its own request. With this change they carry the caller's address. - The API host's proxy puts the caller's address in `X-BP-Client-IP`, overwriting anything the client sent. That proxy change is a separate orca-platform PR on `pruefwerk-stage/api-proxy.conf`. - `audit_delegated_writes` reads the header for **API-key requests only** and keeps it only if it parses as an IP address, because the registry stores it as Postgres `inet` and garbage would sink the whole entry. - `AuditEntry.source_ip` is sent as `source_ip` and skipped when empty. - The registry ignores the field until tenant-registry accepts it (separate PR), so the deploy order doesn't matter. - The address is informational: nothing decides on it. - Support writes via the dashboard still show the dashboard's address. Tests: unit test for the header parse (v4, v6, a list, junk, missing); `tests/api_key_access.rs` checks `source_ip` in the posted audit entry. Not run locally (RAM); CI runs them. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Reviewed-on: https://git.breakpilot.com/platform/pruefwerk/pulls/567 --- compliance-agent/src/api/server.rs | 46 +++++++++++++++++++++++- compliance-agent/src/registry.rs | 5 +++ compliance-agent/tests/api_key_access.rs | 9 ++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/compliance-agent/src/api/server.rs b/compliance-agent/src/api/server.rs index 78f551a..51dbb55 100644 --- a/compliance-agent/src/api/server.rs +++ b/compliance-agent/src/api/server.rs @@ -204,6 +204,12 @@ pub async fn audit_delegated_writes( .cloned(); let method = request.method().clone(); let path = request.uri().path().to_string(); + // Only the API host's proxy sets this header, and it overwrites whatever + // the caller sent; support requests arrive through the dashboard. + let client_ip = staff_ctx + .as_ref() + .filter(|c| c.api_key.is_some()) + .and_then(|_| client_ip(request.headers())); let response = next.run(request).await; let (Some(ctx), Some(registry)) = (staff_ctx, agent.registry.clone()) else { return response; @@ -211,7 +217,8 @@ pub async fn audit_delegated_writes( if matches!(method, Method::GET | Method::HEAD | Method::OPTIONS) { return response; } - let entry = delegated_audit_entry(&ctx, method.as_str(), &path, response.status().as_u16()); + let mut entry = delegated_audit_entry(&ctx, method.as_str(), &path, response.status().as_u16()); + entry.source_ip = client_ip.map(|ip| ip.to_string()).unwrap_or_default(); tokio::spawn(async move { if let Err(e) = registry.append_audit(&entry).await { tracing::warn!(tenant_id = %entry.tenant_id, "write audit not recorded: {e}"); @@ -220,6 +227,23 @@ pub async fn audit_delegated_writes( response } +/// The header the API host's proxy puts the caller's address in +/// (orca-platform `pruefwerk-*/api-proxy.conf`). +const CLIENT_IP_HEADER: &str = "x-bp-client-ip"; + +/// The caller's address from [`CLIENT_IP_HEADER`], or `None` when it is +/// missing or not an IP address (the registry stores it as Postgres `inet`, +/// which would refuse the whole entry). +fn client_ip(headers: &axum::http::HeaderMap) -> Option { + headers + .get(CLIENT_IP_HEADER)? + .to_str() + .ok()? + .trim() + .parse() + .ok() +} + /// The audit entry for a support or API-key request. The path names the /// resource; the query and body are left out, as they may carry customer /// data. @@ -545,6 +569,26 @@ mod support_tests { assert_eq!(ctx.status, TenantStatus::Restricted); } + #[test] + fn the_client_ip_is_taken_only_when_it_is_an_address() { + let headers = |value: &str| { + let mut map = axum::http::HeaderMap::new(); + map.insert(CLIENT_IP_HEADER, HeaderValue::from_str(value).unwrap()); + map + }; + assert_eq!( + client_ip(&headers(" 203.0.113.7 ")).map(|ip| ip.to_string()), + Some("203.0.113.7".to_string()) + ); + assert_eq!( + client_ip(&headers("2001:db8::1")).map(|ip| ip.to_string()), + Some("2001:db8::1".to_string()) + ); + assert_eq!(client_ip(&headers("203.0.113.7, 10.0.0.1")), None); + assert_eq!(client_ip(&headers("evil")), None); + assert_eq!(client_ip(&axum::http::HeaderMap::new()), None); + } + #[test] fn a_support_change_is_audited_without_its_content() { let entry = diff --git a/compliance-agent/src/registry.rs b/compliance-agent/src/registry.rs index c3112e0..c7207cc 100644 --- a/compliance-agent/src/registry.rs +++ b/compliance-agent/src/registry.rs @@ -97,6 +97,10 @@ pub struct AuditEntry { #[serde(skip_serializing_if = "String::is_empty")] pub target_name: String, pub metadata: serde_json::Value, + /// The caller's address, when the agent knows it; the registry otherwise + /// records the address of the agent itself. + #[serde(skip_serializing_if = "String::is_empty")] + pub source_ip: String, } /// The cache key for an API key: its SHA-256, hex. The key itself is never @@ -137,6 +141,7 @@ impl AuditEntry { target_id: String::new(), target_name: String::new(), metadata, + source_ip: String::new(), } } diff --git a/compliance-agent/tests/api_key_access.rs b/compliance-agent/tests/api_key_access.rs index 29b5976..fbb125b 100644 --- a/compliance-agent/tests/api_key_access.rs +++ b/compliance-agent/tests/api_key_access.rs @@ -231,7 +231,13 @@ async fn a_key_write_is_audited_as_the_key() { let Some(agent) = agent(&base).await else { return; }; - let (status, _) = call(&agent, "PATCH", "bp_good", &[("x-bp-actor", "jdoe")]).await; + let (status, _) = call( + &agent, + "PATCH", + "bp_good", + &[("x-bp-actor", "jdoe"), ("x-bp-client-ip", "203.0.113.7")], + ) + .await; assert_eq!(status, StatusCode::OK); // The audit is posted in the background. @@ -250,4 +256,5 @@ async fn a_key_write_is_audited_as_the_key() { assert_eq!(audit["actor_name"], "CI pipeline"); assert_eq!(audit["metadata"]["on_behalf_of"], "jdoe"); assert_eq!(audit["tenant_id"], TENANT); + assert_eq!(audit["source_ip"], "203.0.113.7"); }