OXYGENHUB

Coverage / crates/oxy-server/src/lib.rs 85.3%

238 of 279 lines run, 35 of 78 functions · run #260 on main · run missed

1
//! Reference HTTP server for the Oxygen wire protocol (spec §15.1, §16).
2
//!
3
//! Serves every repository directory under a root at `/{repo}/oxy/v1/...`,
4
//! over TLS when a certificate and key are given, else plain HTTP for use
5
//! behind a TLS-terminating proxy or on a loopback interface (spec §15.1).
6
//!
7
//! Authentication: when bearer tokens are configured, `push` requires one;
8
//! reads are open. With no tokens configured everything is open (development).
9
//!
10
//! Hosts embed the protocol by implementing [`Backend`] (name resolution
11
//! and authorization) and routing to [`Server::handle`]; `router` is the
12
//! standalone server built on [`DirBackend`].
13
14
use axum::body::Bytes;
15
use axum::extract::{Path, State};
16
use axum::http::{header, HeaderMap, Method, StatusCode};
17
use axum::response::{IntoResponse, Response};
18
use axum::routing::any;
19
use axum::Router;
20
use oxy_core::protocol::{wire, CONTENT_TYPE};
21
use oxy_core::repo::Repo;
22
use std::collections::HashMap;
23
use std::path::PathBuf;
24
use std::sync::{Arc, Mutex};
25
26
#[derive(Clone, Debug)]
27
pub struct ServerConfig {
28
    /// Directory containing one repository per subdirectory.
29
    pub root: PathBuf,
30
    /// Bearer tokens allowed to push; empty means no authentication.
31
    pub tokens: Vec<String>,
32
    /// Serve HTTPS with this certificate chain and private key (PEM files).
33
    pub tls: Option<TlsFiles>,
34
}
35
36
#[derive(Clone, Debug)]
37
pub struct TlsFiles {
38
    pub cert: PathBuf,
39
    pub key: PathBuf,
40
}
41
42
/// Which kind of access a request needs (spec §15.1: reads may be open;
43
/// a push must be authenticated when any credential is configured).
44
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45
pub enum Access {
46
    Read,
47
    Push,
48
}
49
50
/// What a host provides to the protocol server: how a repository name maps
51
/// to a repository on disk and who may do what. `oxy serve` uses
52
/// [`DirBackend`]; a hub supplies its own with accounts and roles.
53
pub trait Backend: Send + Sync + 'static {
54
    /// Open the repository called `name`, or fail with the status to
55
    /// answer. An unknown name is `404`; a name the caller may not see
56
    /// SHOULD also be `404`, so private repositories are not enumerable.
57
    fn open(&self, name: &str) -> Result<Repo, Failure>;
58
59
    /// Decide whether this request may perform `access` on `name`.
60
    fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>;
61
62
    /// Called after a push was processed, with its per-reference results
63
    /// (accepted or refused), the states each accepted reference moved
64
    /// between, and the changes stored, for logs and cache invalidation.
6529×
    fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {}
66
67
    /// The bookmark of `name` that pushes may never delete (its default),
68
    /// when the host keeps one apart from the repository's own config.
695×
    fn default_bookmark(&self, _name: &str) -> Option<String> {
705×
        None
715×
    }
72
}
73
74
/// A protocol server over any [`Backend`], serialising the pushes to each
75
/// repository. Hosts route requests to [`Server::handle`].
76
pub struct Server {
77
    backend: Box<dyn Backend>,
78
    locks: Mutex<HashMap<String, Arc<Mutex<()>>>>,
79
}
80
81
/// An error response: a status and a plain-text body.
82
#[derive(Debug)]
83
pub struct Failure(pub StatusCode, pub String);
84
85
impl IntoResponse for Failure {
866×
    fn into_response(self) -> Response {
876×
        (self.0, self.1).into_response()
886×
    }
89
}
90
91
impl From<oxy_core::repo::RepoError> for Failure {
920×
    fn from(e: oxy_core::repo::RepoError) -> Failure {
93
        use oxy_core::repo::RepoError;
94
        // The server's own storage failing is not the client's fault.
950×
        let status = match &e {
960×
            RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR,
970×
            _ => StatusCode::BAD_REQUEST,
98
        };
990×
        Failure(status, e.to_string())
1000×
    }
101
}
102
103
/// The wire protocol's endpoints (spec §16.1), named as in the URL.
104
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105
pub enum Endpoint {
106
    Capabilities,
107
    Refs,
108
    Negotiate,
109
    Fetch,
110
    Objects,
111
    Checkpoint,
112
    Shallow,
113
    Push,
114
}
115
116
impl Endpoint {
117362×
    pub fn parse(name: &str) -> Option<Endpoint> {
118362×
        Some(match name {
119362×
            "capabilities" => Endpoint::Capabilities,
120241×
            "refs" => Endpoint::Refs,
121126×
            "negotiate" => Endpoint::Negotiate,
12280×
            "fetch" => Endpoint::Fetch,
12334×
            "objects" => Endpoint::Objects,
12433×
            "checkpoint" => Endpoint::Checkpoint,
12533×
            "shallow" => Endpoint::Shallow,
12633×
            "push" => Endpoint::Push,
1270×
            _ => return None,
128
        })
129362×
    }
130
1312×
    pub fn name(self) -> &'static str {
1322×
        match self {
1332×
            Endpoint::Capabilities => "capabilities",
1340×
            Endpoint::Refs => "refs",
1350×
            Endpoint::Negotiate => "negotiate",
1360×
            Endpoint::Fetch => "fetch",
1370×
            Endpoint::Objects => "objects",
1380×
            Endpoint::Checkpoint => "checkpoint",
1390×
            Endpoint::Shallow => "shallow",
1400×
            Endpoint::Push => "push",
141
        }
1422×
    }
143
144
    /// The HTTP method the endpoint answers to.
145362×
    pub fn method(self) -> Method {
146362×
        match self {
147236×
            Endpoint::Capabilities | Endpoint::Refs => Method::GET,
148126×
            _ => Method::POST,
149
        }
150362×
    }
151
152362×
    fn access(self) -> Access {
153362×
        match self {
15433×
            Endpoint::Push => Access::Push,
155329×
            _ => Access::Read,
156
        }
157362×
    }
158
}
159
160356×
fn ok(body: Vec<u8>) -> Response {
161356×
    ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response()
162356×
}
163
164487×
pub fn valid_repo_name(name: &str) -> bool {
165487×
    !name.is_empty()
166487×
        && !name.starts_with('.')
167486×
        && name
168486×
            .chars()
1692689×
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
170487×
}
171
172
impl Server {
17311×
    pub fn new(backend: impl Backend) -> Arc<Server> {
17411×
        Arc::new(Server {
17511×
            backend: Box::new(backend),
17611×
            locks: Mutex::new(HashMap::new()),
17711×
        })
17811×
    }
179
1800×
    pub fn backend(&self) -> &dyn Backend {
1810×
        self.backend.as_ref()
1820×
    }
183
18430×
    fn lock_for(&self, name: &str) -> Arc<Mutex<()>> {
18530×
        self.locks
18630×
            .lock()
18730×
            .unwrap()
18830×
            .entry(name.to_string())
18930×
            .or_default()
19030×
            .clone()
19130×
    }
192
193
    /// Answer one protocol request for repository `name`. `method` is
194
    /// checked against the endpoint; the body is the raw CBOR request.
195362×
    pub async fn handle(
196362×
        self: &Arc<Self>,
197362×
        name: String,
198362×
        endpoint: Endpoint,
199362×
        method: &Method,
200362×
        headers: &HeaderMap,
201362×
        body: Bytes,
202362×
    ) -> Response {
203362×
        if *method != endpoint.method() {
2040×
            return Failure(
2050×
                StatusCode::METHOD_NOT_ALLOWED,
2060×
                format!("{} is {}", endpoint.name(), endpoint.method()),
2070×
            )
2080×
            .into_response();
209362×
        }
210362×
        if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) {
2114×
            return f.into_response();
212358×
        }
213358×
        let claimed = headers
214358×
            .get(oxy_core::protocol::REPO_ID_HEADER)
215358×
            .and_then(|v| v.to_str().ok())
216358×
            .and_then(hex_id);
217358×
        let server = Arc::clone(self);
218358×
        let request_headers = headers.clone();
219358×
        let repo = name.clone();
220358×
        let result = tokio::task::spawn_blocking(move || {
221358×
            server.respond(&repo, endpoint, body, claimed, &request_headers)
222358×
        })
223358×
        .await;
224358×
        let failure = match result {
225356×
            Ok(Ok(r)) => return r,
2262×
            Ok(Err(f)) => f,
2270×
            Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
228
        };
229
        // Name the operation and repository, in the log and for the client.
2302×
        let f = Failure(
2312×
            failure.0,
2322×
            format!("{} of {name}: {}", endpoint.name(), failure.1),
2332×
        );
2342×
        eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1);
2352×
        f.into_response()
236362×
    }
237
238358×
    fn respond(
239358×
        &self,
240358×
        name: &str,
241358×
        endpoint: Endpoint,
242358×
        body: Bytes,
243358×
        claimed: Option<[u8; 16]>,
244358×
        headers: &HeaderMap,
245358×
    ) -> Result<Response, Failure> {
246358×
        let r = self.backend.open(name)?;
247356×
        Ok(match endpoint {
248118×
            Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)),
249
            Endpoint::Refs => {
250115×
                let mut refs = r.remote_refs()?;
251115×
                if let Some(d) = self.backend.default_bookmark(name) {
252111×
                    refs.default_bookmark = Some(d);
253111×
                }
254115×
                ok(wire::encode_refs(r.id()?, &refs))
255
            }
256
            Endpoint::Negotiate => {
25746×
                let (have, want) = wire::decode_negotiate(&body)?;
25846×
                ok(wire::encode_hashes(&r.negotiate(&have, &want)?))
259
            }
260
            Endpoint::Fetch => {
26146×
                let (changes, max_blob) = wire::decode_fetch(&body)?;
26246×
                ok(wire::encode_bundle(
26346×
                    &r.bundle_for_filtered(&changes, max_blob)?,
264
                ))
265
            }
266
            Endpoint::Objects => {
2671×
                let refs = wire::decode_hashes(&body)?;
2681×
                let bundle = oxy_core::protocol::Bundle {
2691×
                    objects: r.objects_for(&refs)?,
2701×
                    ..Default::default()
271
                };
2721×
                ok(wire::encode_bundle(&bundle))
273
            }
274
            Endpoint::Checkpoint => {
2750×
                let heads = wire::decode_hashes(&body)?;
2760×
                let lock = self.lock_for(name);
2770×
                let _guard = lock.lock().unwrap();
2780×
                ok(wire::encode_bundle(&r.checkpoint_bundle_for(&heads)?))
279
            }
280
            Endpoint::Shallow => {
2810×
                let (heads, depth) = wire::decode_shallow(&body)?;
2820×
                ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?))
283
            }
284
            Endpoint::Push => {
28530×
                let (bundle, updates, policy, tags) = wire::decode_push(&body)?;
28630×
                let lock = self.lock_for(name);
28730×
                let _guard = lock.lock().unwrap();
28830×
                if r.is_unbound() {
289
                    // The first push binds the repository to the pusher's id (spec §16.1).
2902×
                    match claimed {
2912×
                        Some(id) => r.bind_id(id)?,
292
                        None => {
2930×
                            return Err(Failure(
2940×
                                StatusCode::BAD_REQUEST,
2950×
                                format!(
2960×
                                    "this repository is unbound; push with a client that sends {}",
2970×
                                    oxy_core::protocol::REPO_ID_HEADER
2980×
                                ),
2990×
                            ))
300
                        }
301
                    }
30228×
                }
30330×
                let protected = self.backend.default_bookmark(name);
30430×
                let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| {
30530×
                    u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str())
30630×
                });
30730×
                let mut report = r.receive_push_report(bundle, updates, policy, tags)?;
30830×
                for u in refused {
3091×
                    report.results.push(oxy_core::protocol::RefResult {
3101×
                        name: u.name.clone(),
3111×
                        ok: false,
3121×
                        reason: format!(
3131×
                            "{:?} is this repository's default bookmark and cannot be deleted",
3141×
                            u.name
3151×
                        ),
3161×
                    });
3171×
                }
31831×
                for r in report.results.iter().filter(|r| !r.ok) {
31911×
                    eprintln!(
32011×
                        "oxy-server: push to {name}: {} refused: {}",
32111×
                        r.name, r.reason
32211×
                    );
32311×
                }
32430×
                self.backend.after_push(name, &report, headers);
32530×
                ok(wire::encode_push_response(&report.results))
326
            }
327
        })
328358×
    }
329
}
330
331
/// The backend of `oxy serve`: one repository per subdirectory of a root,
332
/// open reads, and pushes gated by a list of bearer tokens.
333
pub struct DirBackend {
334
    pub root: PathBuf,
335
    pub tokens: Vec<String>,
336
}
337
338
impl Backend for DirBackend {
339487×
    fn open(&self, name: &str) -> Result<Repo, Failure> {
340487×
        if !valid_repo_name(name) {
3411×
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
342486×
        }
343486×
        let dir = self.root.join(name);
344486×
        if !dir.join(".oxy").is_dir() {
3451×
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
346485×
        }
347485×
        Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
348487×
    }
349
350140×
    fn default_bookmark(&self, name: &str) -> Option<String> {
351140×
        self.open(name).ok().map(|r| r.config.bookmark.clone())
352140×
    }
353
354349×
    fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> {
355349×
        if access == Access::Read || self.tokens.is_empty() {
356346×
            return Ok(());
3573×
        }
3583×
        match bearer_token(headers) {
3592×
            Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()),
3602×
            _ => Err(Failure(
3612×
                StatusCode::UNAUTHORIZED,
3622×
                "a valid bearer token is required to push".into(),
3632×
            )),
364
        }
365349×
    }
366
}
367
368337×
fn hex_id(h: &str) -> Option<[u8; 16]> {
369337×
    let h = h.trim();
370337×
    if h.len() != 32 {
3710×
        return None;
372337×
    }
373337×
    let mut out = [0u8; 16];
3745392×
    for (i, byte) in out.iter_mut().enumerate() {
3755392×
        *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?;
376
    }
377337×
    Some(out)
378337×
}
379
380
/// The token of an `Authorization: Bearer` header, if any.
38116×
pub fn bearer_token(headers: &HeaderMap) -> Option<&str> {
38216×
    headers
38316×
        .get(header::AUTHORIZATION)
38416×
        .and_then(|v| v.to_str().ok())
38516×
        .and_then(|v| v.strip_prefix("Bearer "))
38616×
        .map(str::trim)
38716×
}
388
389349×
async fn dispatch(
390349×
    State(server): State<Arc<Server>>,
391349×
    Path((repo, endpoint)): Path<(String, String)>,
392349×
    method: Method,
393349×
    headers: HeaderMap,
394349×
    body: Bytes,
395349×
) -> Response {
396349×
    match Endpoint::parse(&endpoint) {
397349×
        Some(e) => server.handle(repo, e, &method, &headers, body).await,
3980×
        None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(),
399
    }
400349×
}
401
402
/// The router of `oxy serve`: `/{repo}/oxy/v1/{endpoint}` over a [`DirBackend`].
40310×
pub fn router(cfg: ServerConfig) -> Router {
40410×
    let server = Server::new(DirBackend {
40510×
        root: cfg.root,
40610×
        tokens: cfg.tokens,
40710×
    });
40810×
    Router::new()
40910×
        .route("/{repo}/oxy/v1/{endpoint}", any(dispatch))
41010×
        .with_state(server)
41110×
}
412
413
/// Serve on an already-bound listener until the process ends; HTTPS when
414
/// `cfg.tls` is set.
41510×
pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
41610×
    listener.set_nonblocking(true)?;
41710×
    match cfg.tls.clone() {
4181×
        Some(tls) => {
4191×
            let _ = rustls::crypto::ring::default_provider().install_default();
4201×
            let rustls_cfg =
4211×
                axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key)
4221×
                    .await
4231×
                    .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?;
4241×
            axum_server::from_tcp_rustls(listener, rustls_cfg)?
4251×
                .serve(router(cfg).into_make_service())
4261×
                .await
427
        }
428
        None => {
4299×
            let listener = tokio::net::TcpListener::from_std(listener)?;
4309×
            axum::serve(listener, router(cfg)).await
431
        }
432
    }
4330×
}
434
435
/// Build a runtime and serve; for the CLI and for tests on a background thread.
43610×
pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
43710×
    let rt = tokio::runtime::Builder::new_multi_thread()
43810×
        .enable_all()
43910×
        .build()?;
44010×
    rt.block_on(serve(listener, cfg))
44110×
}