A0: contract stubs types, transport, fake and registry seams

Land the shared peer contracts: types, error mapping, transport trait,
fake transport and fixtures, registry/proxy/health skeletons, the
AppState.peers and Config.peers seams, the ping and peers route stubs,
and unit plus integration tests. No loops, no network, feature off by
default.
This commit is contained in:
ari committed 2026-10-19 20:02:17 -04:00
1 parent 746b389a2b
commit 4bfb097be6
22 files changed
+1781 -1

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
//! Proxy one GET to a peer.
//!
//! Path and query allowlists and caller header checks happen in the REST
//! layer; this layer builds the outbound request and maps peer failures. It
//! never forwards caller credentials.
use std::time::Duration;
use axum::http::{HeaderName, HeaderValue};
use super::error::peer_error_to_app;
use super::registry::ProxyOpts;
use super::transport::{PeerRequest, PeerResponse, PeerTransport};
use super::types::PeerConfig;
use crate::error::AppError;
const HOP_HEADER: &str = "x-slinuxm-hop";
const TRACEPARENT: &str = "traceparent";
fn header(name: &'static str, value: &str) -> (HeaderName, HeaderValue) {
let name = HeaderName::from_static(name);
let value = HeaderValue::from_str(value).expect("static header value");
(name, value)
}
pub(crate) async fn get(
transport: &dyn PeerTransport,
peer: &PeerConfig,
path: &str,
query: &[(String, String)],
opts: ProxyOpts,
timeout: Duration,
) -> Result<PeerResponse, AppError> {
// Marks the hop so a peer refuses chained consolidation.
let mut headers = vec![header(HOP_HEADER, "1")];
if let Some(traceparent) = opts.traceparent.as_deref() {
if let Ok(value) = HeaderValue::from_str(traceparent) {
headers.push((HeaderName::from_static(TRACEPARENT), value));
}
}
let req = PeerRequest {
path: path.to_owned(),
query: query.to_vec(),
headers,
accept: opts.accept_sse.then(|| "text/event-stream".to_owned()),
};
transport
.get(peer, req, timeout)
.await
.map_err(|e| peer_error_to_app(&peer.name, &e))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::peers::testing::{fake_peers_config, FakeTransport};
#[tokio::test]
async fn adds_hop_and_trace_context_but_no_credentials() {
let fake = FakeTransport::new();
let cfg = fake_peers_config(&["beta"]);
let opts = ProxyOpts {
traceparent: Some("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".into()),
accept_sse: true,
};
get(
fake.as_ref(),
&cfg.peer[0],
"/journal/follow",
&[("lines".into(), "10".into())],
opts,
Duration::from_secs(1),
)
.await
.unwrap();
let calls = fake.calls();
assert_eq!(calls.len(), 1);
let req = &calls[0].1;
assert_eq!(req.path, "/journal/follow");
assert_eq!(req.query, vec![("lines".to_owned(), "10".to_owned())]);
assert_eq!(req.accept.as_deref(), Some("text/event-stream"));
let header = |name: &str| {
req.headers
.iter()
.find(|(k, _)| k.as_str() == name)
.map(|(_, v)| v.to_str().unwrap())
};
assert_eq!(header("x-slinuxm-hop"), Some("1"));
assert!(header("traceparent").unwrap().starts_with("00-"));
assert_eq!(header("authorization"), None);
}
#[tokio::test]
async fn maps_rejected_with_peer_name() {
let fake = FakeTransport::new();
fake.peer("beta")
.respond_err("/systemd/units", crate::peers::error::PeerError::Rejected);
let cfg = fake_peers_config(&["beta"]);
let err = get(
fake.as_ref(),
&cfg.peer[0],
"/systemd/units",
&[],
ProxyOpts::default(),
Duration::from_secs(1),
)
.await
.unwrap_err();
assert_eq!(err.code(), "unavailable");
assert!(err.message().contains("beta"));
assert_eq!(err.message(), "peer beta: peer rejected credentials");
}
}