feat(audit): API-key writes carry the caller's address to the registry
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 9m11s

Audit entries for API-key writes showed the agent's container address,
because the registry records the peer of its own request. The API host's
proxy now puts the caller's address in X-BP-Client-IP (orca-platform), and
the agent sends it as source_ip for key writes only, when it parses as an
IP address. It is informational; nothing decides on it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-10-14 10:51:08 +02:00
1 parent 07ba0d6665
commit 40b0e3e756
3 files changed
+58 -2

No files matched your search

+45 -1
View File
@@ -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<std::net::IpAddr> {
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 =
+5
View File
@@ -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(),
}
}
+8 -1
View File
@@ -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");
}