OXYGENHUB
# c895bc52e4e0 oxygen / crates / oxy-server / src / lib.rs
15.7 KiB · Rust · last changed Push a long history in parts 26 days ago source blame history raw
750251b2 Andy McCall ↶ 1
//! Reference HTTP server for the Oxygen wire protocol (spec §15.1, §16).
2
//!
2b5ee371 Andy McCall ↶ 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).
750251b2 Andy McCall ↶ 6
//!
7
//! Authentication: when bearer tokens are configured, `push` requires one;
8
//! reads are open. With no tokens configured everything is open (development).
e02bf0cd Andy McCall ↶ 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`].
750251b2 Andy McCall ↶ 13
14
use axum::body::Bytes;
15
use axum::extract::{Path, State};
e02bf0cd Andy McCall ↶ 16
use axum::http::{header, HeaderMap, Method, StatusCode};
750251b2 Andy McCall ↶ 17
use axum::response::{IntoResponse, Response};
e02bf0cd Andy McCall ↶ 18
use axum::routing::any;
750251b2 Andy McCall ↶ 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>,
2b5ee371 Andy McCall ↶ 32
    /// Serve HTTPS with this certificate chain and private key (PEM files).
33
    pub tls: Option<TlsFiles>,
750251b2 Andy McCall ↶ 34
}
35
2b5ee371 Andy McCall ↶ 36
#[derive(Clone, Debug)]
37
pub struct TlsFiles {
38
    pub cert: PathBuf,
39
    pub key: PathBuf,
40
}
41
e02bf0cd Andy McCall ↶ 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
7aa2dc93 Andy McCall ↶ 63
    /// (accepted or refused), the states each accepted reference moved
64
    /// between, and the changes stored, for logs and cache invalidation.
76f75250 Andy McCall ↶ 65
    fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {}
f8c69576 Andy McCall ↶ 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.
69
    fn default_bookmark(&self, _name: &str) -> Option<String> {
70
        None
71
    }
e02bf0cd Andy McCall ↶ 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>,
750251b2 Andy McCall ↶ 78
    locks: Mutex<HashMap<String, Arc<Mutex<()>>>>,
79
}
80
e02bf0cd Andy McCall ↶ 81
/// An error response: a status and a plain-text body.
82
#[derive(Debug)]
83
pub struct Failure(pub StatusCode, pub String);
750251b2 Andy McCall ↶ 84
85
impl IntoResponse for Failure {
86
    fn into_response(self) -> Response {
87
        (self.0, self.1).into_response()
88
    }
89
}
90
91
impl From<oxy_core::repo::RepoError> for Failure {
92
    fn from(e: oxy_core::repo::RepoError) -> Failure {
be451356 Andy McCall ↶ 93
        use oxy_core::repo::RepoError;
94
        // The server's own storage failing is not the client's fault.
95
        let status = match &e {
96
            RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR,
97
            _ => StatusCode::BAD_REQUEST,
98
        };
99
        Failure(status, e.to_string())
750251b2 Andy McCall ↶ 100
    }
101
}
102
e02bf0cd Andy McCall ↶ 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 {
117
    pub fn parse(name: &str) -> Option<Endpoint> {
118
        Some(match name {
119
            "capabilities" => Endpoint::Capabilities,
120
            "refs" => Endpoint::Refs,
121
            "negotiate" => Endpoint::Negotiate,
122
            "fetch" => Endpoint::Fetch,
123
            "objects" => Endpoint::Objects,
124
            "checkpoint" => Endpoint::Checkpoint,
125
            "shallow" => Endpoint::Shallow,
126
            "push" => Endpoint::Push,
127
            _ => return None,
128
        })
129
    }
130
131
    pub fn name(self) -> &'static str {
132
        match self {
133
            Endpoint::Capabilities => "capabilities",
134
            Endpoint::Refs => "refs",
135
            Endpoint::Negotiate => "negotiate",
136
            Endpoint::Fetch => "fetch",
137
            Endpoint::Objects => "objects",
138
            Endpoint::Checkpoint => "checkpoint",
139
            Endpoint::Shallow => "shallow",
140
            Endpoint::Push => "push",
141
        }
142
    }
143
144
    /// The HTTP method the endpoint answers to.
145
    pub fn method(self) -> Method {
146
        match self {
147
            Endpoint::Capabilities | Endpoint::Refs => Method::GET,
148
            _ => Method::POST,
149
        }
150
    }
151
152
    fn access(self) -> Access {
153
        match self {
154
            Endpoint::Push => Access::Push,
155
            _ => Access::Read,
156
        }
157
    }
158
}
159
750251b2 Andy McCall ↶ 160
fn ok(body: Vec<u8>) -> Response {
161
    ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response()
162
}
163
e02bf0cd Andy McCall ↶ 164
pub fn valid_repo_name(name: &str) -> bool {
750251b2 Andy McCall ↶ 165
    !name.is_empty()
166
        && !name.starts_with('.')
75167672 Andy McCall 167
        && name
168
            .chars()
169
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
750251b2 Andy McCall ↶ 170
}
171
e02bf0cd Andy McCall ↶ 172
impl Server {
173
    pub fn new(backend: impl Backend) -> Arc<Server> {
75167672 Andy McCall 174
        Arc::new(Server {
175
            backend: Box::new(backend),
176
            locks: Mutex::new(HashMap::new()),
177
        })
e02bf0cd Andy McCall ↶ 178
    }
179
180
    pub fn backend(&self) -> &dyn Backend {
181
        self.backend.as_ref()
182
    }
183
184
    fn lock_for(&self, name: &str) -> Arc<Mutex<()>> {
75167672 Andy McCall 185
        self.locks
186
            .lock()
187
            .unwrap()
188
            .entry(name.to_string())
189
            .or_default()
190
            .clone()
e02bf0cd Andy McCall ↶ 191
    }
192
193
    /// Answer one protocol request for repository `name`. `method` is
194
    /// checked against the endpoint; the body is the raw CBOR request.
75167672 Andy McCall 195
    pub async fn handle(
196
        self: &Arc<Self>,
197
        name: String,
198
        endpoint: Endpoint,
199
        method: &Method,
200
        headers: &HeaderMap,
201
        body: Bytes,
202
    ) -> Response {
e02bf0cd Andy McCall ↶ 203
        if *method != endpoint.method() {
75167672 Andy McCall 204
            return Failure(
205
                StatusCode::METHOD_NOT_ALLOWED,
206
                format!("{} is {}", endpoint.name(), endpoint.method()),
207
            )
208
            .into_response();
e02bf0cd Andy McCall ↶ 209
        }
210
        if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) {
211
            return f.into_response();
212
        }
75167672 Andy McCall 213
        let claimed = headers
214
            .get(oxy_core::protocol::REPO_ID_HEADER)
215
            .and_then(|v| v.to_str().ok())
216
            .and_then(hex_id);
e02bf0cd Andy McCall ↶ 217
        let server = Arc::clone(self);
76f75250 Andy McCall ↶ 218
        let request_headers = headers.clone();
be451356 Andy McCall ↶ 219
        let repo = name.clone();
be0161a8 Andy McCall ↶ 220
        let result = tokio::task::spawn_blocking(move || {
be451356 Andy McCall ↶ 221
            server.respond(&repo, endpoint, body, claimed, &request_headers)
be0161a8 Andy McCall ↶ 222
        })
223
        .await;
be451356 Andy McCall ↶ 224
        let failure = match result {
225
            Ok(Ok(r)) => return r,
226
            Ok(Err(f)) => f,
227
            Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
228
        };
229
        // Name the operation and repository, in the log and for the client.
230
        let f = Failure(
231
            failure.0,
232
            format!("{} of {name}: {}", endpoint.name(), failure.1),
233
        );
234
        eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1);
235
        f.into_response()
e02bf0cd Andy McCall ↶ 236
    }
237
05e71ed6 Andy McCall ↶ 238
    fn respond(
239
        &self,
240
        name: &str,
241
        endpoint: Endpoint,
242
        body: Bytes,
243
        claimed: Option<[u8; 16]>,
76f75250 Andy McCall ↶ 244
        headers: &HeaderMap,
05e71ed6 Andy McCall ↶ 245
    ) -> Result<Response, Failure> {
e02bf0cd Andy McCall ↶ 246
        let r = self.backend.open(name)?;
247
        Ok(match endpoint {
248
            Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)),
14edbaaf Andy McCall ↶ 249
            Endpoint::Refs => {
250
                let mut refs = r.remote_refs()?;
251
                if let Some(d) = self.backend.default_bookmark(name) {
252
                    refs.default_bookmark = Some(d);
253
                }
254
                ok(wire::encode_refs(r.id()?, &refs))
255
            }
e02bf0cd Andy McCall ↶ 256
            Endpoint::Negotiate => {
257
                let (have, want) = wire::decode_negotiate(&body)?;
258
                ok(wire::encode_hashes(&r.negotiate(&have, &want)?))
259
            }
260
            Endpoint::Fetch => {
261
                let (changes, max_blob) = wire::decode_fetch(&body)?;
75167672 Andy McCall 262
                ok(wire::encode_bundle(
263
                    &r.bundle_for_filtered(&changes, max_blob)?,
264
                ))
e02bf0cd Andy McCall ↶ 265
            }
266
            Endpoint::Objects => {
267
                let refs = wire::decode_hashes(&body)?;
75167672 Andy McCall 268
                let bundle = oxy_core::protocol::Bundle {
269
                    objects: r.objects_for(&refs)?,
270
                    ..Default::default()
271
                };
e02bf0cd Andy McCall ↶ 272
                ok(wire::encode_bundle(&bundle))
273
            }
274
            Endpoint::Checkpoint => {
275
                let heads = wire::decode_hashes(&body)?;
276
                let lock = self.lock_for(name);
277
                let _guard = lock.lock().unwrap();
278
                ok(wire::encode_bundle(&r.checkpoint_bundle_for(&heads)?))
279
            }
280
            Endpoint::Shallow => {
281
                let (heads, depth) = wire::decode_shallow(&body)?;
282
                ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?))
283
            }
284
            Endpoint::Push => {
285
                let (bundle, updates, policy, tags) = wire::decode_push(&body)?;
286
                let lock = self.lock_for(name);
287
                let _guard = lock.lock().unwrap();
75167672 Andy McCall 288
                if r.is_unbound() {
289
                    // The first push binds the repository to the pusher's id (spec §16.1).
290
                    match claimed {
291
                        Some(id) => r.bind_id(id)?,
292
                        None => {
293
                            return Err(Failure(
294
                                StatusCode::BAD_REQUEST,
05e71ed6 Andy McCall ↶ 295
                                format!(
296
                                    "this repository is unbound; push with a client that sends {}",
297
                                    oxy_core::protocol::REPO_ID_HEADER
298
                                ),
75167672 Andy McCall 299
                            ))
300
                        }
301
                    }
f8c69576 Andy McCall ↶ 302
                }
303
                let protected = self.backend.default_bookmark(name);
304
                let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| {
305
                    u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str())
306
                });
307
                let mut report = r.receive_push_report(bundle, updates, policy, tags)?;
cb8b8082 Andy McCall ↶ 308
                // The request named references, all refused here: it was no
309
                // push of objects alone, whatever the repository saw.
310
                if !refused.is_empty() {
311
                    report
312
                        .results
313
                        .retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT);
314
                }
f8c69576 Andy McCall ↶ 315
                for u in refused {
316
                    report.results.push(oxy_core::protocol::RefResult {
317
                        name: u.name.clone(),
318
                        ok: false,
319
                        reason: format!(
320
                            "{:?} is this repository's default bookmark and cannot be deleted",
321
                            u.name
322
                        ),
323
                    });
be451356 Andy McCall ↶ 324
                }
325
                for r in report.results.iter().filter(|r| !r.ok) {
326
                    eprintln!(
327
                        "oxy-server: push to {name}: {} refused: {}",
328
                        r.name, r.reason
329
                    );
75167672 Andy McCall 330
                }
76f75250 Andy McCall ↶ 331
                self.backend.after_push(name, &report, headers);
7aa2dc93 Andy McCall ↶ 332
                ok(wire::encode_push_response(&report.results))
e02bf0cd Andy McCall ↶ 333
            }
334
        })
335
    }
336
}
337
338
/// The backend of `oxy serve`: one repository per subdirectory of a root,
339
/// open reads, and pushes gated by a list of bearer tokens.
340
pub struct DirBackend {
341
    pub root: PathBuf,
342
    pub tokens: Vec<String>,
343
}
344
345
impl Backend for DirBackend {
750251b2 Andy McCall ↶ 346
    fn open(&self, name: &str) -> Result<Repo, Failure> {
347
        if !valid_repo_name(name) {
348
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
349
        }
e02bf0cd Andy McCall ↶ 350
        let dir = self.root.join(name);
750251b2 Andy McCall ↶ 351
        if !dir.join(".oxy").is_dir() {
352
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
353
        }
354
        Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
355
    }
356
8c30d814 Andy McCall ↶ 357
    fn default_bookmark(&self, name: &str) -> Option<String> {
358
        self.open(name).ok().map(|r| r.config.bookmark.clone())
359
    }
360
e02bf0cd Andy McCall ↶ 361
    fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> {
362
        if access == Access::Read || self.tokens.is_empty() {
750251b2 Andy McCall ↶ 363
            return Ok(());
364
        }
e02bf0cd Andy McCall ↶ 365
        match bearer_token(headers) {
366
            Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()),
75167672 Andy McCall 367
            _ => Err(Failure(
368
                StatusCode::UNAUTHORIZED,
369
                "a valid bearer token is required to push".into(),
370
            )),
750251b2 Andy McCall ↶ 371
        }
372
    }
373
}
374
75167672 Andy McCall 375
fn hex_id(h: &str) -> Option<[u8; 16]> {
376
    let h = h.trim();
377
    if h.len() != 32 {
378
        return None;
379
    }
380
    let mut out = [0u8; 16];
381
    for (i, byte) in out.iter_mut().enumerate() {
382
        *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?;
383
    }
384
    Some(out)
385
}
386
e02bf0cd Andy McCall ↶ 387
/// The token of an `Authorization: Bearer` header, if any.
388
pub fn bearer_token(headers: &HeaderMap) -> Option<&str> {
75167672 Andy McCall 389
    headers
390
        .get(header::AUTHORIZATION)
391
        .and_then(|v| v.to_str().ok())
392
        .and_then(|v| v.strip_prefix("Bearer "))
393
        .map(str::trim)
b3c5a59a Andy McCall ↶ 394
}
395
75167672 Andy McCall 396
async fn dispatch(
397
    State(server): State<Arc<Server>>,
398
    Path((repo, endpoint)): Path<(String, String)>,
399
    method: Method,
400
    headers: HeaderMap,
401
    body: Bytes,
402
) -> Response {
e02bf0cd Andy McCall ↶ 403
    match Endpoint::parse(&endpoint) {
404
        Some(e) => server.handle(repo, e, &method, &headers, body).await,
405
        None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(),
406
    }
750251b2 Andy McCall ↶ 407
}
408
e02bf0cd Andy McCall ↶ 409
/// The router of `oxy serve`: `/{repo}/oxy/v1/{endpoint}` over a [`DirBackend`].
750251b2 Andy McCall ↶ 410
pub fn router(cfg: ServerConfig) -> Router {
75167672 Andy McCall 411
    let server = Server::new(DirBackend {
412
        root: cfg.root,
413
        tokens: cfg.tokens,
414
    });
415
    Router::new()
416
        .route("/{repo}/oxy/v1/{endpoint}", any(dispatch))
417
        .with_state(server)
750251b2 Andy McCall ↶ 418
}
419
2b5ee371 Andy McCall ↶ 420
/// Serve on an already-bound listener until the process ends; HTTPS when
421
/// `cfg.tls` is set.
750251b2 Andy McCall ↶ 422
pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
423
    listener.set_nonblocking(true)?;
2b5ee371 Andy McCall ↶ 424
    match cfg.tls.clone() {
425
        Some(tls) => {
426
            let _ = rustls::crypto::ring::default_provider().install_default();
75167672 Andy McCall 427
            let rustls_cfg =
428
                axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key)
429
                    .await
430
                    .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?;
431
            axum_server::from_tcp_rustls(listener, rustls_cfg)?
432
                .serve(router(cfg).into_make_service())
2b5ee371 Andy McCall ↶ 433
                .await
434
        }
435
        None => {
436
            let listener = tokio::net::TcpListener::from_std(listener)?;
437
            axum::serve(listener, router(cfg)).await
438
        }
439
    }
750251b2 Andy McCall ↶ 440
}
441
442
/// Build a runtime and serve; for the CLI and for tests on a background thread.
443
pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
75167672 Andy McCall 444
    let rt = tokio::runtime::Builder::new_multi_thread()
445
        .enable_all()
446
        .build()?;
750251b2 Andy McCall ↶ 447
    rt.block_on(serve(listener, cfg))
448
}