From 767b198ed741648ccda33f4be25cd9dd9a04c0c5 Mon Sep 17 00:00:00 2001 From: ari Date: Mon, 19 Oct 2026 20:20:32 -0400 Subject: [PATCH] C: C4 REST error mapping through peer_error_to_app with per-row tests --- docs/api.md | 2 + src/api/behaviour_tests.rs | 99 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/docs/api.md b/docs/api.md index 2b5ec20..de66ebb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -49,6 +49,8 @@ A request that already has `x-slinuxm-hop` is `400` `peer : chained reques Every proxied response, including the consolidator's own errors for that peer (`400`, `404`, `500`, `503`), carries `x-slinuxm-peer: `. +A peer failure never becomes `401`: a peer refusing the consolidator's token is `503` `peer : peer rejected credentials`, so the caller does not re-authenticate against the wrong host. + ```sh curl -H "Authorization: Bearer $TOKEN" 'http://127.0.0.1:1984/api/v1/peers/beta/systemd/units?state=failed' ``` diff --git a/src/api/behaviour_tests.rs b/src/api/behaviour_tests.rs index 115dd42..0e30ed3 100644 --- a/src/api/behaviour_tests.rs +++ b/src/api/behaviour_tests.rs @@ -527,6 +527,105 @@ async fn proxy_unknown_peer_and_smuggled_content_type() { assert_eq!(status, StatusCode::NOT_FOUND); } +/// Peer failures reach the caller through the same table REST and MCP share; +/// none of them tells the caller to re-authenticate with the consolidator. +#[tokio::test] +async fn proxy_maps_every_peer_error_without_unauthorized() { + let rows = [ + ( + PeerError::NotFound, + 404, + "not_found", + "peer beta: not found on peer", + ), + ( + PeerError::BadRequest("bad limit".into()), + 400, + "bad_request", + "peer beta: bad limit", + ), + ( + PeerError::Rejected, + 503, + "unavailable", + "peer beta: peer rejected credentials", + ), + (PeerError::Timeout, 503, "unavailable", "peer beta: timeout"), + ( + PeerError::Unreachable, + 503, + "unavailable", + "peer beta: unreachable", + ), + (PeerError::Dns, 503, "unavailable", "peer beta: dns"), + (PeerError::Tls, 503, "unavailable", "peer beta: tls_error"), + ( + PeerError::Http(502), + 503, + "unavailable", + "peer beta: http_502", + ), + ( + PeerError::Oversize, + 503, + "unavailable", + "peer beta: oversize", + ), + ( + PeerError::Saturated, + 503, + "unavailable", + "peer beta: saturated", + ), + ( + PeerError::TokenFile("/run/credentials/peer".into()), + 503, + "unavailable", + "peer beta: token_file", + ), + ( + PeerError::Malformed("not json".into()), + 500, + "internal", + "peer beta: malformed response", + ), + ]; + for (err, expected_status, code, message) in rows { + let fake = FakeTransport::new(); + fake.peer("beta").respond_err("/systemd/units", err.clone()); + let app = proxy_app(fake); + let (status, headers, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await; + let label = format!("{err:?}"); + assert_eq!(status.as_u16(), expected_status, "{label}"); + assert_ne!(status, StatusCode::UNAUTHORIZED, "{label}"); + let v = json(&body); + assert_eq!(v["error"]["code"], code, "{label}"); + assert_eq!(v["error"]["message"], message, "{label}"); + assert_eq!(peer_header(&headers), Some("beta"), "{label}"); + assert!(!body.contains("/run/credentials"), "{label}"); + assert!(!body.contains("http://"), "{label}"); + } +} + +#[tokio::test] +async fn proxy_peer_token_rejection_is_503_not_401() { + // A peer refusing our token must not read as "your token to us is bad". + let fake = FakeTransport::new(); + fake.peer("beta") + .respond_err("/systemd/units", PeerError::Rejected); + let app = proxy_app(fake); + let (status, headers, body) = call( + &app, + "GET", + "/peers/beta/systemd/units", + &[("authorization", "Bearer caller-token")], + ) + .await; + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); + assert_eq!(peer_header(&headers), Some("beta")); + assert!(body.contains("beta"), "{body}"); +} + /// First chunk is immediate; the next waits long enough that shutdown or the /// follow deadline ends the pass-through. `FakeTransport` only has finite /// scripts, so it cannot hold a stream open.