Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ organization names with `CACHE_RESERVED_NAMES` and exact DNS hosts with
`CACHE_RESERVED_HOSTS`; both accept whitespace-separated lowercase values.
Reserved hosts take precedence over canonical hosts. Core protocol names
remain reserved independently of these settings. Hosted DNS policy belongs
to the embedding application, not the cache core.
to the embedding application, not the cache core. On an organization host,
`/<cache>/go` and `/<cache>/sccache/` serve the same handlers as
`/_api/v1/<product>/<org-slug>/<cache>`, which keeps working; every other path
on that host is a Nix read.

### Migrating JWT-era tokens

Expand Down
2 changes: 1 addition & 1 deletion workers/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tokio = { workspace = true, features = ["io-util", "macros", "net", "rt-multi-th
tokio-util = { workspace = true, features = ["rt", "io-util"] }
tonic = "0.14"
tonic-prost = "0.14"
tower = { workspace = true, features = ["util"] }
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
tracing-opentelemetry = { version = "0.34.0", default-features = false, optional = true }
Expand All @@ -64,5 +65,4 @@ protoc-bin-vendored = "3"

[dev-dependencies]
hegeltest.workspace = true
tower = { workspace = true, features = ["util"] }
zstd = { workspace = true, features = ["pkg-config"] }
1 change: 1 addition & 0 deletions workers/server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub fn router(state: AppState) -> Router {
.merge(crate::go::router())
.merge(crate::sccache::router())
.merge(crate::bazel::router(state.clone()))
.merge(crate::cache_host::product_routes(state.clone()))
.merge(management::router(state.clone()))
.route("/_api/v1", any(api_not_found))
.route("/_api/v1/{*unmatched}", any(api_not_found))
Expand Down
113 changes: 108 additions & 5 deletions workers/server/src/cache_host.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,107 @@
//! Direct organization ingress. Only Host is authoritative, never forwarded headers.
use axum::{
extract::{Request, State},
http::{StatusCode, header, uri::Authority},
Router,
extract::{ConnectInfo, MatchedPath, Request, State},
http::{Extensions, StatusCode, header, uri::Authority},
middleware::Next,
response::{IntoResponse, Response},
routing::any,
};
use std::net::SocketAddr;
use tower::ServiceExt;

use crate::{AppState, cache, config::Config};

#[derive(Clone)]
pub(crate) struct OrganizationHost(pub String);

/// Products served at `/<cache>/<product>` on an organization host.
pub(crate) const PRODUCTS: &[&str] = &["go", "sccache"];

/// The path the client used on an organization host, before dispatch
/// rewrote it to the protocol's canonical route.
#[derive(Clone)]
pub(crate) struct PublicPath(pub String);

/// `/<cache>/<product>/…`. A product only on an organization host; on any
/// other host this shape is a path-based Nix read.
pub(crate) fn product_routes(state: AppState) -> Router<AppState> {
// Protocol routers only: an organization host must never reach
// management, upload, or operator routes.
let protocols: Router = Router::new()
.merge(crate::go::router())
.merge(crate::sccache::router())
.fallback(|| async { cache::not_found() })
.with_state(state);
let mut routes = Router::new();
for &product in PRODUCTS {
for tail in ["", "/", "/{*rest}"] {
let protocols = protocols.clone();
routes = routes.route(
&format!("/{{cache}}/{product}{tail}"),
any(move |State(state): State<AppState>, request: Request| {
dispatch(protocols.clone(), product, state, request)
}),
);
}
}
routes
}

async fn dispatch(
protocols: Router,
product: &'static str,
state: AppState,
request: Request,
) -> Response {
let Some(owner) = request
.extensions()
.get::<OrganizationHost>()
.map(|host| host.0.clone())
else {
return match crate::reads::handle(State(state), request).await {
Ok(response) => response,
Err(error) => error.into_response(),
};
};
let public = request.uri().path().to_owned();
let target = public
.strip_prefix('/')
.and_then(|path| path.split_once('/'))
.and_then(|(name, rest)| {
// The route matched `/<cache>/<product>` followed by `/` or nothing.
let tail = rest
.strip_prefix(product)
.filter(|tail| tail.is_empty() || tail.starts_with('/'))?;
let query = request
.uri()
.query()
.map(|q| format!("?{q}"))
.unwrap_or_default();
Some(format!("/_api/v1/{product}/{owner}/{name}{tail}{query}"))
})
.and_then(|target| target.parse::<axum::http::Uri>().ok());
let Some(target) = target else {
return cache::not_found().into_response();
};
// Rebuild the extensions: axum appends inner captures after the outer
// `{cache}` capture, which breaks the protocol handlers' `Path` extractors.
// Go and sccache read no request extensions; the peer address is carried
// for handlers that audit by IP.
let (mut parts, body) = request.into_parts();
let mut extensions = Extensions::new();
if let Some(peer) = parts.extensions.get::<ConnectInfo<SocketAddr>>() {
extensions.insert(*peer);
}
extensions.insert(PublicPath(public));
parts.extensions = extensions;
parts.uri = target;
match protocols.oneshot(Request::from_parts(parts, body)).await {
Ok(response) => response,
Err(never) => match never {},
}
}

/// Names that conflict with the core's path-based protocol namespace.
pub(crate) const PROTOCOL_NAMES: &[&str] = &["default", "nar", "private", "_api", "healthz"];

Expand Down Expand Up @@ -130,9 +221,21 @@ pub(crate) async fn ingress(
request
.extensions_mut()
.insert(OrganizationHost(owner.to_owned()));
match crate::reads::handle(State(state), request).await {
Ok(response) => response,
Err(error) => error.into_response(),
// Product routes are the only ones registered under `/{cache}/`.
// The cache router must be merged, not nested: a nest would prefix
// the matched path, so product requests would miss this check and
// fall back to Nix reads.
let product = request
.extensions()
.get::<MatchedPath>()
.is_some_and(|path| path.as_str().starts_with("/{cache}/"));
if product {
next.run(request).await
} else {
match crate::reads::handle(State(state), request).await {
Ok(response) => response,
Err(error) => error.into_response(),
}
}
};
// Every alias hit must observe current SQL policy, even with a warm CDN.
Expand Down
5 changes: 4 additions & 1 deletion workers/server/src/sccache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ async fn propfind(state: AppState, path: DavPath, request: Request) -> Result<Re
return Err(method_not_allowed());
}
let modified = httpdate::fmt_http_date(access.target.created_at.into());
let pathname = request.uri.path();
let pathname = request
.extensions
.get::<crate::cache_host::PublicPath>()
.map_or(request.uri.path(), |path| path.0.as_str());
let xml = format!(
"<?xml version=\"1.0\"?><d:multistatus xmlns:d=\"DAV:\"><d:response><d:href>{pathname}</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:getlastmodified>{modified}</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>"
);
Expand Down
Loading
Loading