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"); }