diff --git a/src/api/behaviour_tests.rs b/src/api/behaviour_tests.rs index a520687..f175ff9 100644 --- a/src/api/behaviour_tests.rs +++ b/src/api/behaviour_tests.rs @@ -318,7 +318,7 @@ fn proxy_app(fake: Arc) -> axum::Router { async fn proxy_forwards_allowlisted_get_and_filters_the_query() { let fake = FakeTransport::new(); fake.peer("beta").respond( - "/systemd/units", + "/api/v1/systemd/units", 200, "application/json", r#"[{"name":"nginx.service"}]"#, @@ -343,7 +343,7 @@ async fn proxy_forwards_allowlisted_get_and_filters_the_query() { assert_eq!(peer_header(&headers), Some("beta")); let calls = fake.calls(); assert_eq!(calls.len(), 1); - assert_eq!(calls[0].1.path, "/systemd/units"); + assert_eq!(calls[0].1.path, "/api/v1/systemd/units"); assert_eq!(calls[0].1.query, vec![("state".into(), "failed".into())]); assert!(calls[0] .1 @@ -366,7 +366,7 @@ async fn proxy_forwards_allowlisted_get_and_filters_the_query() { async fn proxy_rejects_non_get_traversal_hop_and_foreign_query() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units", 200, "application/json", "{}"); + .respond("/api/v1/systemd/units", 200, "application/json", "{}"); let app = proxy_app(fake.clone()); for method in ["POST", "DELETE", "PUT"] { let (status, _, body) = call(&app, method, "/peers/beta/systemd/units", &[]).await; @@ -472,9 +472,14 @@ async fn proxy_stream_stops_at_follow_deadline() { async fn proxy_caps_buffered_bodies_and_passes_sse_bytes() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units", 200, "application/json", "0123456789") + .respond( + "/api/v1/systemd/units", + 200, + "application/json", + "0123456789", + ) .stream( - "/journal/follow", + "/api/v1/journal/follow", vec![Bytes::from_static(b"abcd"), Bytes::from_static(b"efgh")], ); let mut peers = fake_peers_config(&["beta"]); @@ -511,7 +516,7 @@ async fn proxy_caps_buffered_bodies_and_passes_sse_bytes() { async fn proxy_forwards_unit_names_the_local_route_accepts() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units/", 200, "application/json", "{}"); + .respond("/api/v1/systemd/units/", 200, "application/json", "{}"); let app = proxy_app(fake.clone()); for uri in [ "/peers/beta/systemd/units/-.mount", @@ -528,11 +533,11 @@ async fn proxy_forwards_unit_names_the_local_route_accepts() { assert_eq!( paths, vec![ - "/systemd/units/-.mount", - "/systemd/units/-.slice", - "/systemd/units/mnt-data%5Cx2dbackup.mount", - "/systemd/units/foo%5Cx2db.mount", - "/systemd/units/foo%5Cx2db.mount", + "/api/v1/systemd/units/-.mount", + "/api/v1/systemd/units/-.slice", + "/api/v1/systemd/units/mnt-data%5Cx2dbackup.mount", + "/api/v1/systemd/units/foo%5Cx2db.mount", + "/api/v1/systemd/units/foo%5Cx2db.mount", ] ); } @@ -544,8 +549,8 @@ async fn proxy_forwards_unit_names_the_local_route_accepts() { async fn proxy_forwards_journal_unit_values_the_local_route_accepts() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/journal/follow", 200, "text/event-stream", "") - .respond("/journal", 200, "application/json", "[]"); + .respond("/api/v1/journal/follow", 200, "text/event-stream", "") + .respond("/api/v1/journal", 200, "application/json", "[]"); let app = proxy_app(fake.clone()); for uri in [ "/peers/beta/journal?unit=mnt-data%5Cx2dbackup.mount", @@ -613,7 +618,7 @@ async fn proxy_maps_non_success_status_instead_of_relaying_it() { for (peer_status, expected, code, message) in rows { let fake = FakeTransport::new(); fake.peer("beta").respond( - "/systemd/units", + "/api/v1/systemd/units", peer_status, "application/json", r#"{"error":{"code":"peer_body"}}"#, @@ -634,7 +639,7 @@ async fn proxy_maps_non_success_status_instead_of_relaying_it() { async fn proxy_unknown_peer_and_smuggled_content_type() { let fake = FakeTransport::new(); fake.peer("beta").respond( - "/systemd/units", + "/api/v1/systemd/units", 200, "application/json\r\nX-Injected: 1", "{}", @@ -731,7 +736,8 @@ async fn proxy_maps_every_peer_error_without_unauthorized() { ]; for (err, expected_status, code, message) in rows { let fake = FakeTransport::new(); - fake.peer("beta").respond_err("/systemd/units", err.clone()); + fake.peer("beta") + .respond_err("/api/v1/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:?}"); @@ -751,7 +757,7 @@ 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); + .respond_err("/api/v1/systemd/units", PeerError::Rejected); let app = proxy_app(fake); let (status, headers, body) = call( &app, @@ -782,7 +788,7 @@ impl PeerTransport for GatedSse { req: PeerRequest, _: Duration, ) -> Result { - assert_eq!(req.path, "/journal/follow"); + assert_eq!(req.path, "/api/v1/journal/follow"); assert_eq!(req.accept.as_deref(), Some("text/event-stream")); let stream = futures::stream::unfold(0u32, |n| async move { if n > 0 { diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs index 833d507..d59e327 100644 --- a/src/mcp/mod.rs +++ b/src/mcp/mod.rs @@ -1312,43 +1312,43 @@ mod tests { let fake = FakeTransport::new(); fake.peer("beta") .respond( - "/systemd/units/", + "/api/v1/systemd/units/", 200, "application/json", r#"{"name":"peer-teapot.service","n_restarts":3}"#, ) .respond( - "/systemd/units", + "/api/v1/systemd/units", 200, "application/json", r#"[{"name":"peer-teapot.service"}]"#, ) .respond( - "/systemd/timers", + "/api/v1/systemd/timers", 200, "application/json", r#"[{"name":"peer.timer"}]"#, ) .respond( - "/journal", + "/api/v1/journal", 200, "application/json", r#"[{"message":"peer journal line"}]"#, ) .respond( - "/docker/containers/peer-ctr/logs", + "/api/v1/docker/containers/peer-ctr/logs", 200, "application/json", r#"[{"name":"peer-ctr"}]"#, ) .respond( - "/docker/containers/", + "/api/v1/docker/containers/", 200, "application/json", r#"{"name":"peer-ctr"}"#, ) .respond( - "/docker/ps", + "/api/v1/docker/ps", 200, "application/json", r#"[{"name":"peer-ctr"}]"#, @@ -1433,14 +1433,14 @@ mod tests { assert_eq!( paths, [ - "/systemd/units", - "/systemd/units/peer-teapot.service", - "/systemd/timers", - "/journal", - "/docker/ps", - "/docker/containers/peer-ctr", + "/api/v1/systemd/units", + "/api/v1/systemd/units/peer-teapot.service", + "/api/v1/systemd/timers", + "/api/v1/journal", + "/api/v1/docker/ps", + "/api/v1/docker/containers/peer-ctr", // Docker logs share the container prefix; the script answers both. - "/docker/containers/peer-ctr/logs", + "/api/v1/docker/containers/peer-ctr/logs", ] ); assert_eq!( @@ -1498,7 +1498,7 @@ mod tests { async fn peer_tail_journal_consumes_the_follow_stream() { let fake = FakeTransport::new(); fake.peer("beta").stream( - "/journal/follow", + "/api/v1/journal/follow", vec![ bytes::Bytes::from_static( b"event: journal\ndata: {\"message\":\"one\"}\n\n", @@ -1526,7 +1526,7 @@ mod tests { assert_eq!(v["entries"].as_array().unwrap().len(), 2); assert_eq!(v["entries"][1]["message"], "two"); let calls = fake.calls(); - assert_eq!(calls[0].1.path, "/journal/follow"); + assert_eq!(calls[0].1.path, "/api/v1/journal/follow"); assert_eq!(calls[0].1.accept.as_deref(), Some("text/event-stream")); assert!(v["note"].as_str().unwrap().contains("closed the stream")); } @@ -1539,7 +1539,7 @@ mod tests { let fake = FakeTransport::new(); let chunk = bytes::Bytes::from(vec![b'x'; 512]); fake.peer("beta") - .stream("/journal/follow", vec![chunk; 100]); + .stream("/api/v1/journal/follow", vec![chunk; 100]); let s = peer_svc_limits(Config::default(), peer_config(4096), &fake); let start = tokio::time::Instant::now(); let r = s.tail_journal(follow_params("beta", 30)).await.unwrap(); @@ -1564,7 +1564,7 @@ mod tests { let frames: Vec = (0..20) .map(|i| bytes::Bytes::from(format!("data: {{\"n\":{i}}}\n\n"))) .collect(); - fake.peer("beta").stream("/journal/follow", frames); + fake.peer("beta").stream("/api/v1/journal/follow", frames); let mut cfg = Config::default(); cfg.limits.max_lines = 3; let s = peer_svc_with(cfg, &fake); @@ -1577,7 +1577,7 @@ mod tests { async fn peer_tail_journal_accepts_a_buffered_body() { let fake = FakeTransport::new(); fake.peer("beta").respond( - "/journal/follow", + "/api/v1/journal/follow", 200, "text/event-stream", "data: {\"message\":\"a\"}\n\ndata: {\"message\":\"b\"}\n\n", @@ -1593,7 +1593,7 @@ mod tests { async fn peer_tail_journal_maps_a_follow_error_like_rest() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/journal/follow", 404, "application/json", "{}"); + .respond("/api/v1/journal/follow", 404, "application/json", "{}"); let s = peer_svc_with(Config::default(), &fake); let r = s.tail_journal(follow_params("beta", 30)).await.unwrap(); assert_eq!(r.is_error, Some(true)); @@ -1604,7 +1604,7 @@ mod tests { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/journal/follow", 500, "application/json", "{}"); + .respond("/api/v1/journal/follow", 500, "application/json", "{}"); let s = peer_svc_with(Config::default(), &fake); let r = s.tail_journal(follow_params("beta", 30)).await.unwrap(); assert!( @@ -1640,9 +1640,9 @@ mod tests { async fn peer_failures_are_tool_errors() { let fake = FakeTransport::new(); fake.peer("beta") - .respond_err("/systemd/units", PeerError::Rejected) - .respond("/systemd/timers", 503, "application/json", "{}") - .respond("/journal", 200, "application/json", "not json"); + .respond_err("/api/v1/systemd/units", PeerError::Rejected) + .respond("/api/v1/systemd/timers", 503, "application/json", "{}") + .respond("/api/v1/journal", 200, "application/json", "not json"); let s = peer_svc_with(Config::default(), &fake); let r = s @@ -1731,8 +1731,8 @@ mod tests { async fn peer_paths_encode_and_clamp_like_the_rest_routes() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units/", 200, "application/json", "{}") - .respond("/docker/containers/", 200, "application/json", "[]"); + .respond("/api/v1/systemd/units/", 200, "application/json", "{}") + .respond("/api/v1/docker/containers/", 200, "application/json", "[]"); let mut cfg = Config::default(); cfg.limits.max_tail = 1; let s = peer_svc_with(cfg, &fake); @@ -1754,8 +1754,11 @@ mod tests { .unwrap(); let calls = fake.calls(); - assert_eq!(calls[0].1.path, "/systemd/units/dev-disk-by%5Cx2did.device"); - assert_eq!(calls[1].1.path, "/docker/containers/web-1/logs"); + assert_eq!( + calls[0].1.path, + "/api/v1/systemd/units/dev-disk-by%5Cx2did.device" + ); + assert_eq!(calls[1].1.path, "/api/v1/docker/containers/web-1/logs"); assert_eq!(calls[1].1.query, vec![("tail".to_owned(), "1".to_owned())]); } diff --git a/src/peers/proxy.rs b/src/peers/proxy.rs index d2f14cd..93d0f6c 100644 --- a/src/peers/proxy.rs +++ b/src/peers/proxy.rs @@ -5,9 +5,9 @@ //! become an open proxy. Caller credentials are never copied: the transport //! attaches the peer token itself. //! -//! Paths are the route under `/api/v1` (`/systemd/units`). A caller that -//! includes the `/api/v1` prefix is accepted and that prefix is stripped, so -//! the transport always sees one form. +//! Callers may name a route with or without the `/api/v1` prefix; validation +//! works on the route alone (`/systemd/units`) and forwards the canonical peer +//! path (`/api/v1/systemd/units`), which is what a peer serves. use std::time::Duration; @@ -230,7 +230,9 @@ fn validate( let query = allow_query(peer, &route, query)?; let sse = accept_sse || route_is_follow(&route, &query); Ok(Validated { - path: format!("/{route}"), + // The allowlist and the query rules work on the route; the peer only + // serves it below its own API prefix. + path: format!("/api/v1/{route}"), query, sse, }) @@ -478,7 +480,7 @@ mod tests { let calls = fake.calls(); assert_eq!(calls.len(), 1); let req = &calls[0].1; - assert_eq!(req.path, "/journal/follow"); + assert_eq!(req.path, "/api/v1/journal/follow"); assert_eq!(req.query, vec![("lines".to_owned(), "10".to_owned())]); assert_eq!(req.accept.as_deref(), Some("text/event-stream")); assert_eq!(header_of(req, "x-slinuxm-hop"), Some("1")); @@ -491,7 +493,7 @@ mod tests { async fn maps_rejected_with_peer_name() { let fake = FakeTransport::new(); fake.peer("beta") - .respond_err("/systemd/units", PeerError::Rejected); + .respond_err("/api/v1/systemd/units", PeerError::Rejected); let err = get_with(fake.as_ref(), &peer_cfg(), "/systemd/units", &[]) .await .unwrap_err(); @@ -532,23 +534,23 @@ mod tests { assert_eq!( forwarded(&fake), vec![ - "/systemd/units", - "/systemd/units/nginx.service", - "/systemd/timers", - "/journal", - "/journal/follow", - "/docker/ps", - "/docker/containers/web", - "/docker/containers/web/logs", - "/systemd/units", - "/systemd/units/user@100.service", - "/systemd/units/-.mount", - "/systemd/units/-.slice", - "/systemd/units/foo%5Cx2dbar.mount", - "/systemd/units/mnt-data%5Cx2dbackup.mount", - "/systemd/units/dev-disk-by%5Cx2did.device", - "/systemd/units/foo..bar.mount", - "/docker/containers/foo..bar", + "/api/v1/systemd/units", + "/api/v1/systemd/units/nginx.service", + "/api/v1/systemd/timers", + "/api/v1/journal", + "/api/v1/journal/follow", + "/api/v1/docker/ps", + "/api/v1/docker/containers/web", + "/api/v1/docker/containers/web/logs", + "/api/v1/systemd/units", + "/api/v1/systemd/units/user@100.service", + "/api/v1/systemd/units/-.mount", + "/api/v1/systemd/units/-.slice", + "/api/v1/systemd/units/foo%5Cx2dbar.mount", + "/api/v1/systemd/units/mnt-data%5Cx2dbackup.mount", + "/api/v1/systemd/units/dev-disk-by%5Cx2did.device", + "/api/v1/systemd/units/foo..bar.mount", + "/api/v1/docker/containers/foo..bar", ] ); } @@ -568,10 +570,10 @@ mod tests { assert_eq!( forwarded(&fake), vec![ - "/systemd/units/-.mount", - "/systemd/units/foo%5Cx2dbar.mount", - "/systemd/units/foo%5Cx2dbar.mount", - "/systemd/units/mnt-data%5Cx2dbackup.mount", + "/api/v1/systemd/units/-.mount", + "/api/v1/systemd/units/foo%5Cx2dbar.mount", + "/api/v1/systemd/units/foo%5Cx2dbar.mount", + "/api/v1/systemd/units/mnt-data%5Cx2dbackup.mount", ] ); } @@ -632,8 +634,8 @@ mod tests { async fn rejects_traversal_excluded_routes_and_bad_query_without_calling() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units", 200, "application/json", "{}") - .respond("/journal/follow", 200, "application/json", "{}"); + .respond("/api/v1/systemd/units", 200, "application/json", "{}") + .respond("/api/v1/journal/follow", 200, "application/json", "{}"); let peer = peer_cfg(); let bad_paths = [ "/ping", diff --git a/src/peers/registry.rs b/src/peers/registry.rs index 415782a..25b0ee2 100644 --- a/src/peers/registry.rs +++ b/src/peers/registry.rs @@ -490,8 +490,12 @@ mod tests { #[tokio::test] async fn get_forwards_and_maps_errors() { let fake = FakeTransport::new(); - fake.peer("beta") - .respond("/systemd/units", 200, "application/json", "{\"ok\":true}"); + fake.peer("beta").respond( + "/api/v1/systemd/units", + 200, + "application/json", + "{\"ok\":true}", + ); let reg = registry_with(fake, fake_peers_config(&["beta"])); let resp = reg @@ -681,7 +685,7 @@ mod tests { async fn a_disabled_peer_is_never_proxied() { let fake = FakeTransport::new(); fake.peer("beta") - .respond("/systemd/units", 200, "application/json", "{}"); + .respond("/api/v1/systemd/units", 200, "application/json", "{}"); let reg = registry_with(fake.clone(), fake_peers_config(&["beta"])); reg.get("beta", "/systemd/units", &[], ProxyOpts::default()) .await diff --git a/tests/peers.rs b/tests/peers.rs index 1589664..5a0c54d 100644 --- a/tests/peers.rs +++ b/tests/peers.rs @@ -442,13 +442,6 @@ async fn a_plain_peer_reports_up_and_auth_ok() { consolidator.stop().await; } -// Blocked by a defect outside this workstream: the proxy hands the transport -// the stripped path ("/systemd/units") while an unmodified peer only serves -// "/api/v1/systemd/units", so every proxied call to a healthy peer answers -// "peer beta: not found on peer". The test states the contract; the Log entry -// in plan/phase-5-consolidator/ws-f-nixos-docs.md has the details to route the -// one-line fix to the owning stream. -#[ignore = "blocked: the proxy drops the /api/v1 prefix of the forwarded path"] #[tokio::test] async fn a_plain_peer_serves_its_own_data_through_the_proxy() { let dir = TempDir::new("proxy"); @@ -628,7 +621,6 @@ fn at_most_64_peers_may_be_configured() { assert!(err.contains("64"), "{err}"); } -#[ignore = "blocked: the proxy drops the /api/v1 prefix of the forwarded path"] #[tokio::test] async fn shutdown_ends_a_proxied_follow() { let dir = TempDir::new("follow");