OXYGENHUB
◆ v0.5.0 oxygen / crates / oxy-server / src / lib.rs
17.2 KiB · Rust · last changed oxy-server: a host may refuse a push by what it adds (Backend::vet_push) 16 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
51d4862a Andy McCall ↶ 14
pub mod witness;
15
750251b2 Andy McCall ↶ 16
use axum::body::Bytes;
17
use axum::extract::{Path, State};
e02bf0cd Andy McCall ↶ 18
use axum::http::{header, HeaderMap, Method, StatusCode};
750251b2 Andy McCall ↶ 19
use axum::response::{IntoResponse, Response};
e02bf0cd Andy McCall ↶ 20
use axum::routing::any;
750251b2 Andy McCall ↶ 21
use axum::Router;
22
use oxy_core::protocol::{wire, CONTENT_TYPE};
23
use oxy_core::repo::Repo;
24
use std::collections::HashMap;
25
use std::path::PathBuf;
26
use std::sync::{Arc, Mutex};
27
28
#[derive(Clone, Debug)]
29
pub struct ServerConfig {
30
    /// Directory containing one repository per subdirectory.
31
    pub root: PathBuf,
32
    /// Bearer tokens allowed to push; empty means no authentication.
33
    pub tokens: Vec<String>,
2b5ee371 Andy McCall ↶ 34
    /// Serve HTTPS with this certificate chain and private key (PEM files).
35
    pub tls: Option<TlsFiles>,
750251b2 Andy McCall ↶ 36
}
37
2b5ee371 Andy McCall ↶ 38
#[derive(Clone, Debug)]
39
pub struct TlsFiles {
40
    pub cert: PathBuf,
41
    pub key: PathBuf,
42
}
43
e02bf0cd Andy McCall ↶ 44
/// Which kind of access a request needs (spec §15.1: reads may be open;
45
/// a push must be authenticated when any credential is configured).
46
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47
pub enum Access {
48
    Read,
49
    Push,
50
}
51
52
/// What a host provides to the protocol server: how a repository name maps
53
/// to a repository on disk and who may do what. `oxy serve` uses
54
/// [`DirBackend`]; a hub supplies its own with accounts and roles.
55
pub trait Backend: Send + Sync + 'static {
56
    /// Open the repository called `name`, or fail with the status to
57
    /// answer. An unknown name is `404`; a name the caller may not see
58
    /// SHOULD also be `404`, so private repositories are not enumerable.
59
    fn open(&self, name: &str) -> Result<Repo, Failure>;
60
61
    /// Decide whether this request may perform `access` on `name`.
62
    fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>;
63
64
    /// Called after a push was processed, with its per-reference results
7aa2dc93 Andy McCall ↶ 65
    /// (accepted or refused), the states each accepted reference moved
66
    /// between, and the changes stored, for logs and cache invalidation.
76f75250 Andy McCall ↶ 67
    fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {}
f8c69576 Andy McCall ↶ 68
67a4fe1a Andy McCall ↶ 69
    /// Vet a push by what it adds before anything of it is stored or any
70
    /// reference moves (spec §16.1): `bundle` is the push as decoded, its
71
    /// changes with their lines, `updates` the references it would move.
72
    /// A host refuses the whole push by returning a [`Failure`], whose
73
    /// text reaches the client as the push's error, as it is, several
74
    /// lines included; it should answer neither 401, 403 nor 404, which a
75
    /// client takes for a credential it should change. Called for every
76
    /// part of a push sent in parts (each part's changes are its own),
77
    /// outside the repository's push lock, so a slow vet holds up no
78
    /// other push. The default accepts everything at no cost.
79
    fn vet_push(
80
        &self,
81
        _name: &str,
82
        _repo: &Repo,
83
        _bundle: &oxy_core::protocol::Bundle,
84
        _updates: &[oxy_core::protocol::RefUpdate],
85
        _headers: &HeaderMap,
86
    ) -> Result<(), Failure> {
87
        Ok(())
88
    }
89
4b9d0677 Andy McCall ↶ 90
    /// Add the host's transparency log to a refs answer (spec §14.8): its
91
    /// checkpoint, and each attestation's entry and inclusion proof. A host
92
    /// without a log leaves it as it is.
93
    fn add_log(&self, _name: &str, _refs: &mut oxy_core::protocol::RefsResponse) {}
94
f8c69576 Andy McCall ↶ 95
    /// The bookmark of `name` that pushes may never delete (its default),
96
    /// when the host keeps one apart from the repository's own config.
97
    fn default_bookmark(&self, _name: &str) -> Option<String> {
98
        None
99
    }
e02bf0cd Andy McCall ↶ 100
}
101
102
/// A protocol server over any [`Backend`], serialising the pushes to each
103
/// repository. Hosts route requests to [`Server::handle`].
104
pub struct Server {
105
    backend: Box<dyn Backend>,
750251b2 Andy McCall ↶ 106
    locks: Mutex<HashMap<String, Arc<Mutex<()>>>>,
107
}
108
e02bf0cd Andy McCall ↶ 109
/// An error response: a status and a plain-text body.
110
#[derive(Debug)]
111
pub struct Failure(pub StatusCode, pub String);
750251b2 Andy McCall ↶ 112
113
impl IntoResponse for Failure {
114
    fn into_response(self) -> Response {
115
        (self.0, self.1).into_response()
116
    }
117
}
118
119
impl From<oxy_core::repo::RepoError> for Failure {
120
    fn from(e: oxy_core::repo::RepoError) -> Failure {
be451356 Andy McCall ↶ 121
        use oxy_core::repo::RepoError;
122
        // The server's own storage failing is not the client's fault.
123
        let status = match &e {
124
            RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR,
125
            _ => StatusCode::BAD_REQUEST,
126
        };
127
        Failure(status, e.to_string())
750251b2 Andy McCall ↶ 128
    }
129
}
130
e02bf0cd Andy McCall ↶ 131
/// The wire protocol's endpoints (spec §16.1), named as in the URL.
132
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
133
pub enum Endpoint {
134
    Capabilities,
135
    Refs,
136
    Negotiate,
137
    Fetch,
138
    Objects,
139
    Checkpoint,
140
    Shallow,
141
    Push,
142
}
143
144
impl Endpoint {
145
    pub fn parse(name: &str) -> Option<Endpoint> {
146
        Some(match name {
147
            "capabilities" => Endpoint::Capabilities,
148
            "refs" => Endpoint::Refs,
149
            "negotiate" => Endpoint::Negotiate,
150
            "fetch" => Endpoint::Fetch,
151
            "objects" => Endpoint::Objects,
152
            "checkpoint" => Endpoint::Checkpoint,
153
            "shallow" => Endpoint::Shallow,
154
            "push" => Endpoint::Push,
155
            _ => return None,
156
        })
157
    }
158
159
    pub fn name(self) -> &'static str {
160
        match self {
161
            Endpoint::Capabilities => "capabilities",
162
            Endpoint::Refs => "refs",
163
            Endpoint::Negotiate => "negotiate",
164
            Endpoint::Fetch => "fetch",
165
            Endpoint::Objects => "objects",
166
            Endpoint::Checkpoint => "checkpoint",
167
            Endpoint::Shallow => "shallow",
168
            Endpoint::Push => "push",
169
        }
170
    }
171
172
    /// The HTTP method the endpoint answers to.
173
    pub fn method(self) -> Method {
174
        match self {
175
            Endpoint::Capabilities | Endpoint::Refs => Method::GET,
176
            _ => Method::POST,
177
        }
178
    }
179
180
    fn access(self) -> Access {
181
        match self {
182
            Endpoint::Push => Access::Push,
183
            _ => Access::Read,
184
        }
185
    }
186
}
187
750251b2 Andy McCall ↶ 188
fn ok(body: Vec<u8>) -> Response {
189
    ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response()
190
}
191
e02bf0cd Andy McCall ↶ 192
pub fn valid_repo_name(name: &str) -> bool {
750251b2 Andy McCall ↶ 193
    !name.is_empty()
194
        && !name.starts_with('.')
75167672 Andy McCall 195
        && name
196
            .chars()
197
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
750251b2 Andy McCall ↶ 198
}
199
e02bf0cd Andy McCall ↶ 200
impl Server {
201
    pub fn new(backend: impl Backend) -> Arc<Server> {
75167672 Andy McCall 202
        Arc::new(Server {
203
            backend: Box::new(backend),
204
            locks: Mutex::new(HashMap::new()),
205
        })
e02bf0cd Andy McCall ↶ 206
    }
207
208
    pub fn backend(&self) -> &dyn Backend {
209
        self.backend.as_ref()
210
    }
211
212
    fn lock_for(&self, name: &str) -> Arc<Mutex<()>> {
75167672 Andy McCall 213
        self.locks
214
            .lock()
215
            .unwrap()
216
            .entry(name.to_string())
217
            .or_default()
218
            .clone()
e02bf0cd Andy McCall ↶ 219
    }
220
221
    /// Answer one protocol request for repository `name`. `method` is
222
    /// checked against the endpoint; the body is the raw CBOR request.
75167672 Andy McCall 223
    pub async fn handle(
224
        self: &Arc<Self>,
225
        name: String,
226
        endpoint: Endpoint,
227
        method: &Method,
228
        headers: &HeaderMap,
229
        body: Bytes,
230
    ) -> Response {
e02bf0cd Andy McCall ↶ 231
        if *method != endpoint.method() {
75167672 Andy McCall 232
            return Failure(
233
                StatusCode::METHOD_NOT_ALLOWED,
234
                format!("{} is {}", endpoint.name(), endpoint.method()),
235
            )
236
            .into_response();
e02bf0cd Andy McCall ↶ 237
        }
238
        if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) {
239
            return f.into_response();
240
        }
75167672 Andy McCall 241
        let claimed = headers
242
            .get(oxy_core::protocol::REPO_ID_HEADER)
243
            .and_then(|v| v.to_str().ok())
244
            .and_then(hex_id);
e02bf0cd Andy McCall ↶ 245
        let server = Arc::clone(self);
76f75250 Andy McCall ↶ 246
        let request_headers = headers.clone();
be451356 Andy McCall ↶ 247
        let repo = name.clone();
be0161a8 Andy McCall ↶ 248
        let result = tokio::task::spawn_blocking(move || {
be451356 Andy McCall ↶ 249
            server.respond(&repo, endpoint, body, claimed, &request_headers)
be0161a8 Andy McCall ↶ 250
        })
251
        .await;
be451356 Andy McCall ↶ 252
        let failure = match result {
253
            Ok(Ok(r)) => return r,
254
            Ok(Err(f)) => f,
255
            Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
256
        };
257
        // Name the operation and repository, in the log and for the client.
258
        let f = Failure(
259
            failure.0,
260
            format!("{} of {name}: {}", endpoint.name(), failure.1),
261
        );
262
        eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1);
263
        f.into_response()
e02bf0cd Andy McCall ↶ 264
    }
265
05e71ed6 Andy McCall ↶ 266
    fn respond(
267
        &self,
268
        name: &str,
269
        endpoint: Endpoint,
270
        body: Bytes,
271
        claimed: Option<[u8; 16]>,
76f75250 Andy McCall ↶ 272
        headers: &HeaderMap,
05e71ed6 Andy McCall ↶ 273
    ) -> Result<Response, Failure> {
e02bf0cd Andy McCall ↶ 274
        let r = self.backend.open(name)?;
275
        Ok(match endpoint {
276
            Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)),
14edbaaf Andy McCall ↶ 277
            Endpoint::Refs => {
278
                let mut refs = r.remote_refs()?;
279
                if let Some(d) = self.backend.default_bookmark(name) {
280
                    refs.default_bookmark = Some(d);
281
                }
4b9d0677 Andy McCall ↶ 282
                self.backend.add_log(name, &mut refs);
14edbaaf Andy McCall ↶ 283
                ok(wire::encode_refs(r.id()?, &refs))
284
            }
e02bf0cd Andy McCall ↶ 285
            Endpoint::Negotiate => {
286
                let (have, want) = wire::decode_negotiate(&body)?;
287
                ok(wire::encode_hashes(&r.negotiate(&have, &want)?))
288
            }
289
            Endpoint::Fetch => {
290
                let (changes, max_blob) = wire::decode_fetch(&body)?;
75167672 Andy McCall 291
                ok(wire::encode_bundle(
292
                    &r.bundle_for_filtered(&changes, max_blob)?,
293
                ))
e02bf0cd Andy McCall ↶ 294
            }
295
            Endpoint::Objects => {
296
                let refs = wire::decode_hashes(&body)?;
75167672 Andy McCall 297
                let bundle = oxy_core::protocol::Bundle {
298
                    objects: r.objects_for(&refs)?,
299
                    ..Default::default()
300
                };
e02bf0cd Andy McCall ↶ 301
                ok(wire::encode_bundle(&bundle))
302
            }
303
            Endpoint::Checkpoint => {
304
                let heads = wire::decode_hashes(&body)?;
305
                let lock = self.lock_for(name);
306
                let _guard = lock.lock().unwrap();
307
                ok(wire::encode_bundle(&r.checkpoint_bundle_for(&heads)?))
308
            }
309
            Endpoint::Shallow => {
310
                let (heads, depth) = wire::decode_shallow(&body)?;
311
                ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?))
312
            }
313
            Endpoint::Push => {
314
                let (bundle, updates, policy, tags) = wire::decode_push(&body)?;
67a4fe1a Andy McCall ↶ 315
                // The host vets what the push adds before anything is stored
316
                // or bound: a refused push leaves the repository as it was.
317
                self.backend
318
                    .vet_push(name, &r, &bundle, &updates, headers)?;
e02bf0cd Andy McCall ↶ 319
                let lock = self.lock_for(name);
320
                let _guard = lock.lock().unwrap();
75167672 Andy McCall 321
                if r.is_unbound() {
322
                    // The first push binds the repository to the pusher's id (spec §16.1).
323
                    match claimed {
324
                        Some(id) => r.bind_id(id)?,
325
                        None => {
326
                            return Err(Failure(
327
                                StatusCode::BAD_REQUEST,
05e71ed6 Andy McCall ↶ 328
                                format!(
329
                                    "this repository is unbound; push with a client that sends {}",
330
                                    oxy_core::protocol::REPO_ID_HEADER
331
                                ),
75167672 Andy McCall 332
                            ))
333
                        }
334
                    }
f8c69576 Andy McCall ↶ 335
                }
336
                let protected = self.backend.default_bookmark(name);
337
                let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| {
338
                    u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str())
339
                });
340
                let mut report = r.receive_push_report(bundle, updates, policy, tags)?;
cb8b8082 Andy McCall ↶ 341
                // The request named references, all refused here: it was no
342
                // push of objects alone, whatever the repository saw.
343
                if !refused.is_empty() {
344
                    report
345
                        .results
346
                        .retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT);
347
                }
f8c69576 Andy McCall ↶ 348
                for u in refused {
349
                    report.results.push(oxy_core::protocol::RefResult {
350
                        name: u.name.clone(),
351
                        ok: false,
352
                        reason: format!(
353
                            "{:?} is this repository's default bookmark and cannot be deleted",
354
                            u.name
355
                        ),
356
                    });
be451356 Andy McCall ↶ 357
                }
358
                for r in report.results.iter().filter(|r| !r.ok) {
359
                    eprintln!(
360
                        "oxy-server: push to {name}: {} refused: {}",
361
                        r.name, r.reason
362
                    );
75167672 Andy McCall 363
                }
76f75250 Andy McCall ↶ 364
                self.backend.after_push(name, &report, headers);
7aa2dc93 Andy McCall ↶ 365
                ok(wire::encode_push_response(&report.results))
e02bf0cd Andy McCall ↶ 366
            }
367
        })
368
    }
369
}
370
371
/// The backend of `oxy serve`: one repository per subdirectory of a root,
372
/// open reads, and pushes gated by a list of bearer tokens.
373
pub struct DirBackend {
374
    pub root: PathBuf,
375
    pub tokens: Vec<String>,
376
}
377
378
impl Backend for DirBackend {
750251b2 Andy McCall ↶ 379
    fn open(&self, name: &str) -> Result<Repo, Failure> {
380
        if !valid_repo_name(name) {
381
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
382
        }
e02bf0cd Andy McCall ↶ 383
        let dir = self.root.join(name);
750251b2 Andy McCall ↶ 384
        if !dir.join(".oxy").is_dir() {
385
            return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into()));
386
        }
387
        Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
388
    }
389
8c30d814 Andy McCall ↶ 390
    fn default_bookmark(&self, name: &str) -> Option<String> {
391
        self.open(name).ok().map(|r| r.config.bookmark.clone())
392
    }
393
e02bf0cd Andy McCall ↶ 394
    fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> {
395
        if access == Access::Read || self.tokens.is_empty() {
750251b2 Andy McCall ↶ 396
            return Ok(());
397
        }
e02bf0cd Andy McCall ↶ 398
        match bearer_token(headers) {
399
            Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()),
75167672 Andy McCall 400
            _ => Err(Failure(
401
                StatusCode::UNAUTHORIZED,
402
                "a valid bearer token is required to push".into(),
403
            )),
750251b2 Andy McCall ↶ 404
        }
405
    }
406
}
407
75167672 Andy McCall 408
fn hex_id(h: &str) -> Option<[u8; 16]> {
409
    let h = h.trim();
410
    if h.len() != 32 {
411
        return None;
412
    }
413
    let mut out = [0u8; 16];
414
    for (i, byte) in out.iter_mut().enumerate() {
415
        *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?;
416
    }
417
    Some(out)
418
}
419
e02bf0cd Andy McCall ↶ 420
/// The token of an `Authorization: Bearer` header, if any.
421
pub fn bearer_token(headers: &HeaderMap) -> Option<&str> {
75167672 Andy McCall 422
    headers
423
        .get(header::AUTHORIZATION)
424
        .and_then(|v| v.to_str().ok())
425
        .and_then(|v| v.strip_prefix("Bearer "))
426
        .map(str::trim)
b3c5a59a Andy McCall ↶ 427
}
428
75167672 Andy McCall 429
async fn dispatch(
430
    State(server): State<Arc<Server>>,
431
    Path((repo, endpoint)): Path<(String, String)>,
432
    method: Method,
433
    headers: HeaderMap,
434
    body: Bytes,
435
) -> Response {
e02bf0cd Andy McCall ↶ 436
    match Endpoint::parse(&endpoint) {
437
        Some(e) => server.handle(repo, e, &method, &headers, body).await,
438
        None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(),
439
    }
750251b2 Andy McCall ↶ 440
}
441
e02bf0cd Andy McCall ↶ 442
/// The router of `oxy serve`: `/{repo}/oxy/v1/{endpoint}` over a [`DirBackend`].
750251b2 Andy McCall ↶ 443
pub fn router(cfg: ServerConfig) -> Router {
75167672 Andy McCall 444
    let server = Server::new(DirBackend {
445
        root: cfg.root,
446
        tokens: cfg.tokens,
447
    });
448
    Router::new()
449
        .route("/{repo}/oxy/v1/{endpoint}", any(dispatch))
450
        .with_state(server)
750251b2 Andy McCall ↶ 451
}
452
2b5ee371 Andy McCall ↶ 453
/// Serve on an already-bound listener until the process ends; HTTPS when
454
/// `cfg.tls` is set.
750251b2 Andy McCall ↶ 455
pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
456
    listener.set_nonblocking(true)?;
2b5ee371 Andy McCall ↶ 457
    match cfg.tls.clone() {
458
        Some(tls) => {
459
            let _ = rustls::crypto::ring::default_provider().install_default();
75167672 Andy McCall 460
            let rustls_cfg =
461
                axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key)
462
                    .await
463
                    .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?;
464
            axum_server::from_tcp_rustls(listener, rustls_cfg)?
465
                .serve(router(cfg).into_make_service())
2b5ee371 Andy McCall ↶ 466
                .await
467
        }
468
        None => {
469
            let listener = tokio::net::TcpListener::from_std(listener)?;
470
            axum::serve(listener, router(cfg)).await
471
        }
472
    }
750251b2 Andy McCall ↶ 473
}
474
475
/// Build a runtime and serve; for the CLI and for tests on a background thread.
476
pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> {
75167672 Andy McCall 477
    let rt = tokio::runtime::Builder::new_multi_thread()
478
        .enable_all()
479
        .build()?;
750251b2 Andy McCall ↶ 480
    rt.block_on(serve(listener, cfg))
481
}