| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 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 | | |
| 29 | | pub root: PathBuf, |
| 30 | | |
| 31 | | pub tokens: Vec<String>, |
| 32 | | |
| 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 | |
|
| 43 | |
|
| 44 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 45 | | pub enum Access { |
| 46 | | Read, |
| 47 | | Push, |
| 48 | | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | | pub trait Backend: Send + Sync + 'static { |
| 54 | | |
| 55 | | |
| 56 | | |
| 57 | | fn open(&self, name: &str) -> Result<Repo, Failure>; |
| 58 | |
|
| 59 | | |
| 60 | | fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>; |
| 61 | |
|
| 62 | | |
| 63 | | |
| 64 | | |
| 65 | 28× | fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {} |
| 66 | |
|
| 67 | | |
| 68 | | |
| 69 | 5× | fn default_bookmark(&self, _name: &str) -> Option<String> { |
| 70 | 5× | None |
| 71 | 5× | } |
| 72 | | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | | pub struct Server { |
| 77 | | backend: Box<dyn Backend>, |
| 78 | | locks: Mutex<HashMap<String, Arc<Mutex<()>>>>, |
| 79 | | } |
| 80 | |
|
| 81 | |
|
| 82 | | #[derive(Debug)] |
| 83 | | pub struct Failure(pub StatusCode, pub String); |
| 84 | |
|
| 85 | | impl IntoResponse for Failure { |
| 86 | 6× | fn into_response(self) -> Response { |
| 87 | 6× | (self.0, self.1).into_response() |
| 88 | 6× | } |
| 89 | | } |
| 90 | |
|
| 91 | | impl From<oxy_core::repo::RepoError> for Failure { |
| 92 | 0× | fn from(e: oxy_core::repo::RepoError) -> Failure { |
| 93 | | use oxy_core::repo::RepoError; |
| 94 | | |
| 95 | 0× | let status = match &e { |
| 96 | 0× | RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 97 | 0× | _ => StatusCode::BAD_REQUEST, |
| 98 | | }; |
| 99 | 0× | Failure(status, e.to_string()) |
| 100 | 0× | } |
| 101 | | } |
| 102 | |
|
| 103 | |
|
| 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 | 361× | pub fn parse(name: &str) -> Option<Endpoint> { |
| 118 | 361× | Some(match name { |
| 119 | 361× | "capabilities" => Endpoint::Capabilities, |
| 120 | 240× | "refs" => Endpoint::Refs, |
| 121 | 125× | "negotiate" => Endpoint::Negotiate, |
| 122 | 79× | "fetch" => Endpoint::Fetch, |
| 123 | 33× | "objects" => Endpoint::Objects, |
| 124 | 32× | "checkpoint" => Endpoint::Checkpoint, |
| 125 | 32× | "shallow" => Endpoint::Shallow, |
| 126 | 32× | "push" => Endpoint::Push, |
| 127 | 0× | _ => return None, |
| 128 | | }) |
| 129 | 361× | } |
| 130 | |
|
| 131 | 2× | pub fn name(self) -> &'static str { |
| 132 | 2× | match self { |
| 133 | 2× | Endpoint::Capabilities => "capabilities", |
| 134 | 0× | Endpoint::Refs => "refs", |
| 135 | 0× | Endpoint::Negotiate => "negotiate", |
| 136 | 0× | Endpoint::Fetch => "fetch", |
| 137 | 0× | Endpoint::Objects => "objects", |
| 138 | 0× | Endpoint::Checkpoint => "checkpoint", |
| 139 | 0× | Endpoint::Shallow => "shallow", |
| 140 | 0× | Endpoint::Push => "push", |
| 141 | | } |
| 142 | 2× | } |
| 143 | |
|
| 144 | | |
| 145 | 361× | pub fn method(self) -> Method { |
| 146 | 361× | match self { |
| 147 | 236× | Endpoint::Capabilities | Endpoint::Refs => Method::GET, |
| 148 | 125× | _ => Method::POST, |
| 149 | | } |
| 150 | 361× | } |
| 151 | |
|
| 152 | 361× | fn access(self) -> Access { |
| 153 | 361× | match self { |
| 154 | 32× | Endpoint::Push => Access::Push, |
| 155 | 329× | _ => Access::Read, |
| 156 | | } |
| 157 | 361× | } |
| 158 | | } |
| 159 | |
|
| 160 | 355× | fn ok(body: Vec<u8>) -> Response { |
| 161 | 355× | ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response() |
| 162 | 355× | } |
| 163 | |
|
| 164 | 485× | pub fn valid_repo_name(name: &str) -> bool { |
| 165 | 485× | !name.is_empty() |
| 166 | 485× | && !name.starts_with('.') |
| 167 | 484× | && name |
| 168 | 484× | .chars() |
| 169 | 2677× | .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) |
| 170 | 485× | } |
| 171 | |
|
| 172 | | impl Server { |
| 173 | 11× | pub fn new(backend: impl Backend) -> Arc<Server> { |
| 174 | 11× | Arc::new(Server { |
| 175 | 11× | backend: Box::new(backend), |
| 176 | 11× | locks: Mutex::new(HashMap::new()), |
| 177 | 11× | }) |
| 178 | 11× | } |
| 179 | |
|
| 180 | 0× | pub fn backend(&self) -> &dyn Backend { |
| 181 | 0× | self.backend.as_ref() |
| 182 | 0× | } |
| 183 | |
|
| 184 | 29× | fn lock_for(&self, name: &str) -> Arc<Mutex<()>> { |
| 185 | 29× | self.locks |
| 186 | 29× | .lock() |
| 187 | 29× | .unwrap() |
| 188 | 29× | .entry(name.to_string()) |
| 189 | 29× | .or_default() |
| 190 | 29× | .clone() |
| 191 | 29× | } |
| 192 | |
|
| 193 | | |
| 194 | | |
| 195 | 361× | pub async fn handle( |
| 196 | 361× | self: &Arc<Self>, |
| 197 | 361× | name: String, |
| 198 | 361× | endpoint: Endpoint, |
| 199 | 361× | method: &Method, |
| 200 | 361× | headers: &HeaderMap, |
| 201 | 361× | body: Bytes, |
| 202 | 361× | ) -> Response { |
| 203 | 361× | if *method != endpoint.method() { |
| 204 | 0× | return Failure( |
| 205 | 0× | StatusCode::METHOD_NOT_ALLOWED, |
| 206 | 0× | format!("{} is {}", endpoint.name(), endpoint.method()), |
| 207 | 0× | ) |
| 208 | 0× | .into_response(); |
| 209 | 361× | } |
| 210 | 361× | if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) { |
| 211 | 4× | return f.into_response(); |
| 212 | 357× | } |
| 213 | 357× | let claimed = headers |
| 214 | 357× | .get(oxy_core::protocol::REPO_ID_HEADER) |
| 215 | 357× | .and_then(|v| v.to_str().ok()) |
| 216 | 357× | .and_then(hex_id); |
| 217 | 357× | let server = Arc::clone(self); |
| 218 | 357× | let request_headers = headers.clone(); |
| 219 | 357× | let repo = name.clone(); |
| 220 | 357× | let result = tokio::task::spawn_blocking(move || { |
| 221 | 357× | server.respond(&repo, endpoint, body, claimed, &request_headers) |
| 222 | 357× | }) |
| 223 | 357× | .await; |
| 224 | 357× | let failure = match result { |
| 225 | 355× | Ok(Ok(r)) => return r, |
| 226 | 2× | Ok(Err(f)) => f, |
| 227 | 0× | Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), |
| 228 | | }; |
| 229 | | |
| 230 | 2× | let f = Failure( |
| 231 | 2× | failure.0, |
| 232 | 2× | format!("{} of {name}: {}", endpoint.name(), failure.1), |
| 233 | 2× | ); |
| 234 | 2× | eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1); |
| 235 | 2× | f.into_response() |
| 236 | 361× | } |
| 237 | |
|
| 238 | 357× | fn respond( |
| 239 | 357× | &self, |
| 240 | 357× | name: &str, |
| 241 | 357× | endpoint: Endpoint, |
| 242 | 357× | body: Bytes, |
| 243 | 357× | claimed: Option<[u8; 16]>, |
| 244 | 357× | headers: &HeaderMap, |
| 245 | 357× | ) -> Result<Response, Failure> { |
| 246 | 357× | let r = self.backend.open(name)?; |
| 247 | 355× | Ok(match endpoint { |
| 248 | 118× | Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)), |
| 249 | | Endpoint::Refs => { |
| 250 | 115× | let mut refs = r.remote_refs()?; |
| 251 | 115× | if let Some(d) = self.backend.default_bookmark(name) { |
| 252 | 111× | refs.default_bookmark = Some(d); |
| 253 | 111× | } |
| 254 | 115× | ok(wire::encode_refs(r.id()?, &refs)) |
| 255 | | } |
| 256 | | Endpoint::Negotiate => { |
| 257 | 46× | let (have, want) = wire::decode_negotiate(&body)?; |
| 258 | 46× | ok(wire::encode_hashes(&r.negotiate(&have, &want)?)) |
| 259 | | } |
| 260 | | Endpoint::Fetch => { |
| 261 | 46× | let (changes, max_blob) = wire::decode_fetch(&body)?; |
| 262 | 46× | ok(wire::encode_bundle( |
| 263 | 46× | &r.bundle_for_filtered(&changes, max_blob)?, |
| 264 | | )) |
| 265 | | } |
| 266 | | Endpoint::Objects => { |
| 267 | 1× | let refs = wire::decode_hashes(&body)?; |
| 268 | 1× | let bundle = oxy_core::protocol::Bundle { |
| 269 | 1× | objects: r.objects_for(&refs)?, |
| 270 | 1× | ..Default::default() |
| 271 | | }; |
| 272 | 1× | ok(wire::encode_bundle(&bundle)) |
| 273 | | } |
| 274 | | Endpoint::Checkpoint => { |
| 275 | 0× | let heads = wire::decode_hashes(&body)?; |
| 276 | 0× | let lock = self.lock_for(name); |
| 277 | 0× | let _guard = lock.lock().unwrap(); |
| 278 | 0× | ok(wire::encode_bundle(&r.checkpoint_bundle_for(&heads)?)) |
| 279 | | } |
| 280 | | Endpoint::Shallow => { |
| 281 | 0× | let (heads, depth) = wire::decode_shallow(&body)?; |
| 282 | 0× | ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?)) |
| 283 | | } |
| 284 | | Endpoint::Push => { |
| 285 | 29× | let (bundle, updates, policy, tags) = wire::decode_push(&body)?; |
| 286 | 29× | let lock = self.lock_for(name); |
| 287 | 29× | let _guard = lock.lock().unwrap(); |
| 288 | 29× | if r.is_unbound() { |
| 289 | | |
| 290 | 2× | match claimed { |
| 291 | 2× | Some(id) => r.bind_id(id)?, |
| 292 | | None => { |
| 293 | 0× | return Err(Failure( |
| 294 | 0× | StatusCode::BAD_REQUEST, |
| 295 | 0× | format!( |
| 296 | 0× | "this repository is unbound; push with a client that sends {}", |
| 297 | 0× | oxy_core::protocol::REPO_ID_HEADER |
| 298 | 0× | ), |
| 299 | 0× | )) |
| 300 | | } |
| 301 | | } |
| 302 | 27× | } |
| 303 | 29× | let protected = self.backend.default_bookmark(name); |
| 304 | 29× | let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| { |
| 305 | 29× | u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str()) |
| 306 | 29× | }); |
| 307 | 29× | let mut report = r.receive_push_report(bundle, updates, policy, tags)?; |
| 308 | 29× | for u in refused { |
| 309 | 1× | report.results.push(oxy_core::protocol::RefResult { |
| 310 | 1× | name: u.name.clone(), |
| 311 | 1× | ok: false, |
| 312 | 1× | reason: format!( |
| 313 | 1× | "{:?} is this repository's default bookmark and cannot be deleted", |
| 314 | 1× | u.name |
| 315 | 1× | ), |
| 316 | 1× | }); |
| 317 | 1× | } |
| 318 | 30× | for r in report.results.iter().filter(|r| !r.ok) { |
| 319 | 10× | eprintln!( |
| 320 | 10× | "oxy-server: push to {name}: {} refused: {}", |
| 321 | 10× | r.name, r.reason |
| 322 | 10× | ); |
| 323 | 10× | } |
| 324 | 29× | self.backend.after_push(name, &report, headers); |
| 325 | 29× | ok(wire::encode_push_response(&report.results)) |
| 326 | | } |
| 327 | | }) |
| 328 | 357× | } |
| 329 | | } |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | | pub struct DirBackend { |
| 334 | | pub root: PathBuf, |
| 335 | | pub tokens: Vec<String>, |
| 336 | | } |
| 337 | |
|
| 338 | | impl Backend for DirBackend { |
| 339 | 485× | fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 340 | 485× | if !valid_repo_name(name) { |
| 341 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 342 | 484× | } |
| 343 | 484× | let dir = self.root.join(name); |
| 344 | 484× | if !dir.join(".oxy").is_dir() { |
| 345 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 346 | 483× | } |
| 347 | 483× | Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) |
| 348 | 485× | } |
| 349 | |
|
| 350 | 139× | fn default_bookmark(&self, name: &str) -> Option<String> { |
| 351 | 139× | self.open(name).ok().map(|r| r.config.bookmark.clone()) |
| 352 | 139× | } |
| 353 | |
|
| 354 | 348× | fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 355 | 348× | if access == Access::Read || self.tokens.is_empty() { |
| 356 | 345× | return Ok(()); |
| 357 | 3× | } |
| 358 | 3× | match bearer_token(headers) { |
| 359 | 2× | Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()), |
| 360 | 2× | _ => Err(Failure( |
| 361 | 2× | StatusCode::UNAUTHORIZED, |
| 362 | 2× | "a valid bearer token is required to push".into(), |
| 363 | 2× | )), |
| 364 | | } |
| 365 | 348× | } |
| 366 | | } |
| 367 | |
|
| 368 | 336× | fn hex_id(h: &str) -> Option<[u8; 16]> { |
| 369 | 336× | let h = h.trim(); |
| 370 | 336× | if h.len() != 32 { |
| 371 | 0× | return None; |
| 372 | 336× | } |
| 373 | 336× | let mut out = [0u8; 16]; |
| 374 | 5376× | for (i, byte) in out.iter_mut().enumerate() { |
| 375 | 5376× | *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?; |
| 376 | | } |
| 377 | 336× | Some(out) |
| 378 | 336× | } |
| 379 | |
|
| 380 | |
|
| 381 | 16× | pub fn bearer_token(headers: &HeaderMap) -> Option<&str> { |
| 382 | 16× | headers |
| 383 | 16× | .get(header::AUTHORIZATION) |
| 384 | 16× | .and_then(|v| v.to_str().ok()) |
| 385 | 16× | .and_then(|v| v.strip_prefix("Bearer ")) |
| 386 | 16× | .map(str::trim) |
| 387 | 16× | } |
| 388 | |
|
| 389 | 348× | async fn dispatch( |
| 390 | 348× | State(server): State<Arc<Server>>, |
| 391 | 348× | Path((repo, endpoint)): Path<(String, String)>, |
| 392 | 348× | method: Method, |
| 393 | 348× | headers: HeaderMap, |
| 394 | 348× | body: Bytes, |
| 395 | 348× | ) -> Response { |
| 396 | 348× | match Endpoint::parse(&endpoint) { |
| 397 | 348× | Some(e) => server.handle(repo, e, &method, &headers, body).await, |
| 398 | 0× | None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(), |
| 399 | | } |
| 400 | 348× | } |
| 401 | |
|
| 402 | |
|
| 403 | 10× | pub fn router(cfg: ServerConfig) -> Router { |
| 404 | 10× | let server = Server::new(DirBackend { |
| 405 | 10× | root: cfg.root, |
| 406 | 10× | tokens: cfg.tokens, |
| 407 | 10× | }); |
| 408 | 10× | Router::new() |
| 409 | 10× | .route("/{repo}/oxy/v1/{endpoint}", any(dispatch)) |
| 410 | 10× | .with_state(server) |
| 411 | 10× | } |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | 10× | pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 416 | 10× | listener.set_nonblocking(true)?; |
| 417 | 10× | match cfg.tls.clone() { |
| 418 | 1× | Some(tls) => { |
| 419 | 1× | let _ = rustls::crypto::ring::default_provider().install_default(); |
| 420 | 1× | let rustls_cfg = |
| 421 | 1× | axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key) |
| 422 | 1× | .await |
| 423 | 1× | .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?; |
| 424 | 1× | axum_server::from_tcp_rustls(listener, rustls_cfg)? |
| 425 | 1× | .serve(router(cfg).into_make_service()) |
| 426 | 1× | .await |
| 427 | | } |
| 428 | | None => { |
| 429 | 9× | let listener = tokio::net::TcpListener::from_std(listener)?; |
| 430 | 9× | axum::serve(listener, router(cfg)).await |
| 431 | | } |
| 432 | | } |
| 433 | 0× | } |
| 434 | |
|
| 435 | |
|
| 436 | 10× | pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 437 | 10× | let rt = tokio::runtime::Builder::new_multi_thread() |
| 438 | 10× | .enable_all() |
| 439 | 10× | .build()?; |
| 440 | 10× | rt.block_on(serve(listener, cfg)) |
| 441 | 10× | } |