C: C3 x-slinuxm-peer response header on every proxied outcome
This commit is contained in:
1 parent
bab6959a94
commit
58012018d9
4 files changed
+34
-8
No files matched your search
@@ -47,6 +47,7 @@ Present only when `[peers] enabled = true`. The same inbound token as the rest o
|
||||
|
||||
A request that already has `x-slinuxm-hop` is `400` `peer <name>: chained requests are not allowed`. Other methods are `405`. `journal/follow` and docker logs with `follow=1` are `text/event-stream`, cut off at `limits.max_follow_secs` or shutdown. Bodies larger than `peers.max_response_bytes` are `503` `peer <name>: oversize`.
|
||||
|
||||
Every proxied response, including the consolidator's own errors for that peer (`400`, `404`, `500`, `503`), carries `x-slinuxm-peer: <name>`.
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer $TOKEN" 'http://127.0.0.1:1984/api/v1/peers/beta/systemd/units?state=failed'
|
||||
|
||||
@@ -303,6 +303,10 @@ async fn call(
|
||||
(status, headers, String::from_utf8(body.to_vec()).unwrap())
|
||||
}
|
||||
|
||||
fn peer_header(headers: &axum::http::HeaderMap) -> Option<&str> {
|
||||
headers.get("x-slinuxm-peer").and_then(|v| v.to_str().ok())
|
||||
}
|
||||
|
||||
fn proxy_app(fake: Arc<FakeTransport>) -> axum::Router {
|
||||
router(fake_state_with_peers(
|
||||
Config::default(),
|
||||
@@ -320,7 +324,7 @@ async fn proxy_forwards_allowlisted_get_and_filters_the_query() {
|
||||
r#"[{"name":"nginx.service"}]"#,
|
||||
);
|
||||
let app = proxy_app(fake.clone());
|
||||
let (status, _headers, body) = call(
|
||||
let (status, headers, body) = call(
|
||||
&app,
|
||||
"GET",
|
||||
"/peers/beta/systemd/units?state=failed",
|
||||
@@ -336,6 +340,7 @@ async fn proxy_forwards_allowlisted_get_and_filters_the_query() {
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(body, r#"[{"name":"nginx.service"}]"#);
|
||||
assert_eq!(peer_header(&headers), Some("beta"));
|
||||
let calls = fake.calls();
|
||||
assert_eq!(calls.len(), 1);
|
||||
assert_eq!(calls[0].1.path, "/systemd/units");
|
||||
@@ -397,7 +402,7 @@ async fn proxy_rejects_non_get_traversal_hop_and_foreign_query() {
|
||||
json(&body)["error"]["message"],
|
||||
"peer beta: unsupported query parameter"
|
||||
);
|
||||
let (status, _, body) = call(
|
||||
let (status, headers, body) = call(
|
||||
&app,
|
||||
"GET",
|
||||
"/peers/beta/systemd/units",
|
||||
@@ -409,6 +414,7 @@ async fn proxy_rejects_non_get_traversal_hop_and_foreign_query() {
|
||||
json(&body)["error"]["message"],
|
||||
"peer beta: chained requests are not allowed"
|
||||
);
|
||||
assert_eq!(peer_header(&headers), Some("beta"));
|
||||
let (status, _, body) = call(
|
||||
&app,
|
||||
"GET",
|
||||
@@ -504,9 +510,10 @@ async fn proxy_unknown_peer_and_smuggled_content_type() {
|
||||
"{}",
|
||||
);
|
||||
let app = proxy_app(fake);
|
||||
let (status, _, body) = call(&app, "GET", "/peers/nope/systemd/units", &[]).await;
|
||||
let (status, headers, body) = call(&app, "GET", "/peers/nope/systemd/units", &[]).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(json(&body)["error"]["message"], "peer nope: unknown peer");
|
||||
assert_eq!(peer_header(&headers), Some("nope"));
|
||||
let (status, headers, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await;
|
||||
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert_eq!(
|
||||
|
||||
+20
-5
@@ -8,7 +8,7 @@ use std::time::Duration;
|
||||
|
||||
use axum::body::Body;
|
||||
use axum::extract::{Path, Request, State};
|
||||
use axum::http::{header, HeaderValue, StatusCode};
|
||||
use axum::http::{header, HeaderName, HeaderValue, StatusCode};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
@@ -57,16 +57,31 @@ async fn pass(
|
||||
Path((peer, _path)): Path<(String, String)>,
|
||||
request: Request,
|
||||
) -> Response {
|
||||
with_peer_header(&peer, pass_through(&st, &peer, request).await)
|
||||
}
|
||||
|
||||
/// Every outcome for a proxy call names the peer, including consolidator-side
|
||||
/// errors (bad path, dead peer), so a client can tell which peer failed.
|
||||
fn with_peer_header(peer: &str, mut response: Response) -> Response {
|
||||
if let Ok(value) = HeaderValue::from_str(peer) {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(HeaderName::from_static(proxy::PEER_HEADER), value);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
async fn pass_through(st: &AppState, peer: &str, request: Request) -> Response {
|
||||
let Some(reg) = st.peers.clone() else {
|
||||
return AppError::NotFound("no such route".into()).into_response();
|
||||
};
|
||||
if let Err(err) = proxy::reject_inbound_hop(&peer, request.headers()) {
|
||||
if let Err(err) = proxy::reject_inbound_hop(peer, request.headers()) {
|
||||
return err.into_response();
|
||||
}
|
||||
let Some(raw_path) = raw_peer_path(request.uri().path()) else {
|
||||
return AppError::BadRequest(format!("peer {peer}: path not allowed")).into_response();
|
||||
};
|
||||
let query = match proxy::query_from_raw(&peer, request.uri().query()) {
|
||||
let query = match proxy::query_from_raw(peer, request.uri().query()) {
|
||||
Ok(query) => query,
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
@@ -74,8 +89,8 @@ async fn pass(
|
||||
traceparent: inbound_traceparent(request.headers()),
|
||||
accept_sse: accepts_event_stream(request.headers()),
|
||||
};
|
||||
match reg.get(&peer, raw_path, &query, opts).await {
|
||||
Ok(resp) => proxy_response(&st, &peer, resp),
|
||||
match reg.get(peer, raw_path, &query, opts).await {
|
||||
Ok(resp) => proxy_response(st, peer, resp),
|
||||
Err(err) => err.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ use crate::error::AppError;
|
||||
const HOP_HEADER: &str = "x-slinuxm-hop";
|
||||
const TRACEPARENT: &str = "traceparent";
|
||||
|
||||
/// Response header naming the peer a proxied response came from.
|
||||
pub(crate) const PEER_HEADER: &str = "x-slinuxm-peer";
|
||||
|
||||
/// Reject an inbound hop marker. Any value counts: a consolidator must not
|
||||
/// forward a request that has already been proxied.
|
||||
pub(crate) fn reject_inbound_hop(peer: &str, headers: &HeaderMap) -> Result<(), AppError> {
|
||||
|
||||
Reference in new issue
Block a user