| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
pub mod witness; |
| 15 |
|
| 16 |
use axum::body::Bytes; |
| 17 |
use axum::extract::{Path, State}; |
| 18 |
use axum::http::{header, HeaderMap, Method, StatusCode}; |
| 19 |
use axum::response::{IntoResponse, Response}; |
| 20 |
use axum::routing::any; |
| 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 |
|
| 31 |
pub root: PathBuf, |
| 32 |
|
| 33 |
pub tokens: Vec<String>, |
| 34 |
|
| 35 |
pub tls: Option<TlsFiles>, |
| 36 |
} |
| 37 |
|
| 38 |
#[derive(Clone, Debug)] |
| 39 |
pub struct TlsFiles { |
| 40 |
pub cert: PathBuf, |
| 41 |
pub key: PathBuf, |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 47 |
pub enum Access { |
| 48 |
Read, |
| 49 |
Push, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub trait Backend: Send + Sync + 'static { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
fn open(&self, name: &str) -> Result<Repo, Failure>; |
| 60 |
|
| 61 |
|
| 62 |
fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
fn add_log(&self, _name: &str, _refs: &mut oxy_core::protocol::RefsResponse) {} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
fn adjust_refs( |
| 102 |
&self, |
| 103 |
_name: &str, |
| 104 |
repo: &Repo, |
| 105 |
refs: &mut oxy_core::protocol::RefsResponse, |
| 106 |
headers: &HeaderMap, |
| 107 |
) { |
| 108 |
tailor_refs(repo, refs, headers); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
fn vet_tag_withdrawal(&self, _name: &str, _tag: &str) -> Result<(), String> { |
| 117 |
Ok(()) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
fn default_bookmark(&self, _name: &str) -> Option<String> { |
| 123 |
None |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pub struct Server { |
| 130 |
backend: Box<dyn Backend>, |
| 131 |
locks: Mutex<HashMap<String, Arc<Mutex<()>>>>, |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#[derive(Debug)] |
| 136 |
pub struct Failure(pub StatusCode, pub String); |
| 137 |
|
| 138 |
impl IntoResponse for Failure { |
| 139 |
fn into_response(self) -> Response { |
| 140 |
(self.0, self.1).into_response() |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl From<oxy_core::repo::RepoError> for Failure { |
| 145 |
fn from(e: oxy_core::repo::RepoError) -> Failure { |
| 146 |
use oxy_core::repo::RepoError; |
| 147 |
|
| 148 |
let status = match &e { |
| 149 |
RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 150 |
_ => StatusCode::BAD_REQUEST, |
| 151 |
}; |
| 152 |
Failure(status, e.to_string()) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 158 |
pub enum Endpoint { |
| 159 |
Capabilities, |
| 160 |
Refs, |
| 161 |
Negotiate, |
| 162 |
Fetch, |
| 163 |
Objects, |
| 164 |
Checkpoint, |
| 165 |
Shallow, |
| 166 |
Push, |
| 167 |
} |
| 168 |
|
| 169 |
impl Endpoint { |
| 170 |
pub fn parse(name: &str) -> Option<Endpoint> { |
| 171 |
Some(match name { |
| 172 |
"capabilities" => Endpoint::Capabilities, |
| 173 |
"refs" => Endpoint::Refs, |
| 174 |
"negotiate" => Endpoint::Negotiate, |
| 175 |
"fetch" => Endpoint::Fetch, |
| 176 |
"objects" => Endpoint::Objects, |
| 177 |
"checkpoint" => Endpoint::Checkpoint, |
| 178 |
"shallow" => Endpoint::Shallow, |
| 179 |
"push" => Endpoint::Push, |
| 180 |
_ => return None, |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
pub fn name(self) -> &'static str { |
| 185 |
match self { |
| 186 |
Endpoint::Capabilities => "capabilities", |
| 187 |
Endpoint::Refs => "refs", |
| 188 |
Endpoint::Negotiate => "negotiate", |
| 189 |
Endpoint::Fetch => "fetch", |
| 190 |
Endpoint::Objects => "objects", |
| 191 |
Endpoint::Checkpoint => "checkpoint", |
| 192 |
Endpoint::Shallow => "shallow", |
| 193 |
Endpoint::Push => "push", |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
pub fn method(self) -> Method { |
| 199 |
match self { |
| 200 |
Endpoint::Capabilities | Endpoint::Refs => Method::GET, |
| 201 |
_ => Method::POST, |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
fn access(self) -> Access { |
| 206 |
match self { |
| 207 |
Endpoint::Push => Access::Push, |
| 208 |
_ => Access::Read, |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
fn ok(body: Vec<u8>) -> Response { |
| 214 |
([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response() |
| 215 |
} |
| 216 |
|
| 217 |
pub fn valid_repo_name(name: &str) -> bool { |
| 218 |
!name.is_empty() |
| 219 |
&& !name.starts_with('.') |
| 220 |
&& name |
| 221 |
.chars() |
| 222 |
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) |
| 223 |
} |
| 224 |
|
| 225 |
impl Server { |
| 226 |
pub fn new(backend: impl Backend) -> Arc<Server> { |
| 227 |
Arc::new(Server { |
| 228 |
backend: Box::new(backend), |
| 229 |
locks: Mutex::new(HashMap::new()), |
| 230 |
}) |
| 231 |
} |
| 232 |
|
| 233 |
pub fn backend(&self) -> &dyn Backend { |
| 234 |
self.backend.as_ref() |
| 235 |
} |
| 236 |
|
| 237 |
fn lock_for(&self, name: &str) -> Arc<Mutex<()>> { |
| 238 |
self.locks |
| 239 |
.lock() |
| 240 |
.unwrap() |
| 241 |
.entry(name.to_string()) |
| 242 |
.or_default() |
| 243 |
.clone() |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
pub async fn handle( |
| 249 |
self: &Arc<Self>, |
| 250 |
name: String, |
| 251 |
endpoint: Endpoint, |
| 252 |
method: &Method, |
| 253 |
headers: &HeaderMap, |
| 254 |
body: Bytes, |
| 255 |
) -> Response { |
| 256 |
if *method != endpoint.method() { |
| 257 |
return Failure( |
| 258 |
StatusCode::METHOD_NOT_ALLOWED, |
| 259 |
format!("{} is {}", endpoint.name(), endpoint.method()), |
| 260 |
) |
| 261 |
.into_response(); |
| 262 |
} |
| 263 |
if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) { |
| 264 |
return f.into_response(); |
| 265 |
} |
| 266 |
let claimed = headers |
| 267 |
.get(oxy_core::protocol::REPO_ID_HEADER) |
| 268 |
.and_then(|v| v.to_str().ok()) |
| 269 |
.and_then(hex_id); |
| 270 |
let server = Arc::clone(self); |
| 271 |
let request_headers = headers.clone(); |
| 272 |
let repo = name.clone(); |
| 273 |
let result = tokio::task::spawn_blocking(move || { |
| 274 |
server.respond(&repo, endpoint, body, claimed, &request_headers) |
| 275 |
}) |
| 276 |
.await; |
| 277 |
let failure = match result { |
| 278 |
Ok(Ok(r)) => return r, |
| 279 |
Ok(Err(f)) => f, |
| 280 |
Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), |
| 281 |
}; |
| 282 |
|
| 283 |
let f = Failure( |
| 284 |
failure.0, |
| 285 |
format!("{} of {name}: {}", endpoint.name(), failure.1), |
| 286 |
); |
| 287 |
eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1); |
| 288 |
f.into_response() |
| 289 |
} |
| 290 |
|
| 291 |
fn respond( |
| 292 |
&self, |
| 293 |
name: &str, |
| 294 |
endpoint: Endpoint, |
| 295 |
body: Bytes, |
| 296 |
claimed: Option<[u8; 16]>, |
| 297 |
headers: &HeaderMap, |
| 298 |
) -> Result<Response, Failure> { |
| 299 |
let r = self.backend.open(name)?; |
| 300 |
Ok(match endpoint { |
| 301 |
Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)), |
| 302 |
Endpoint::Refs => { |
| 303 |
let mut refs = r.remote_refs()?; |
| 304 |
if let Some(d) = self.backend.default_bookmark(name) { |
| 305 |
refs.default_bookmark = Some(d); |
| 306 |
} |
| 307 |
self.backend.add_log(name, &mut refs); |
| 308 |
self.backend.adjust_refs(name, &r, &mut refs, headers); |
| 309 |
ok(wire::encode_refs(r.id()?, &refs)) |
| 310 |
} |
| 311 |
Endpoint::Negotiate => { |
| 312 |
let (have, want) = wire::decode_negotiate(&body)?; |
| 313 |
ok(wire::encode_hashes(&r.negotiate(&have, &want)?)) |
| 314 |
} |
| 315 |
Endpoint::Fetch => { |
| 316 |
let (changes, max_blob) = wire::decode_fetch(&body)?; |
| 317 |
ok(wire::encode_bundle( |
| 318 |
&r.bundle_for_filtered(&changes, max_blob)?, |
| 319 |
)) |
| 320 |
} |
| 321 |
Endpoint::Objects => { |
| 322 |
let refs = wire::decode_hashes(&body)?; |
| 323 |
let bundle = oxy_core::protocol::Bundle { |
| 324 |
objects: r.objects_for(&refs)?, |
| 325 |
..Default::default() |
| 326 |
}; |
| 327 |
ok(wire::encode_bundle(&bundle)) |
| 328 |
} |
| 329 |
Endpoint::Checkpoint => { |
| 330 |
let heads = wire::decode_hashes(&body)?; |
| 331 |
let lock = self.lock_for(name); |
| 332 |
let _guard = lock.lock().unwrap(); |
| 333 |
let bundle = r.checkpoint_bundle_for(&heads)?; |
| 334 |
let _t = oxy_core::timing::phase("serve: encode the checkpoint"); |
| 335 |
ok(wire::encode_bundle(&bundle)) |
| 336 |
} |
| 337 |
Endpoint::Shallow => { |
| 338 |
let (heads, depth) = wire::decode_shallow(&body)?; |
| 339 |
ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?)) |
| 340 |
} |
| 341 |
Endpoint::Push => { |
| 342 |
let (bundle, updates, policy, tags) = wire::decode_push(&body)?; |
| 343 |
|
| 344 |
|
| 345 |
self.backend |
| 346 |
.vet_push(name, &r, &bundle, &updates, headers)?; |
| 347 |
let lock = self.lock_for(name); |
| 348 |
let _guard = lock.lock().unwrap(); |
| 349 |
if r.is_unbound() { |
| 350 |
|
| 351 |
match claimed { |
| 352 |
Some(id) => r.bind_id(id)?, |
| 353 |
None => { |
| 354 |
return Err(Failure( |
| 355 |
StatusCode::BAD_REQUEST, |
| 356 |
format!( |
| 357 |
"this repository is unbound; push with a client that sends {}", |
| 358 |
oxy_core::protocol::REPO_ID_HEADER |
| 359 |
), |
| 360 |
)) |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
let protected = self.backend.default_bookmark(name); |
| 365 |
let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| { |
| 366 |
u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str()) |
| 367 |
}); |
| 368 |
let mut kept_tags = Vec::new(); |
| 369 |
let mut withdrawals_refused = Vec::new(); |
| 370 |
for t in tags { |
| 371 |
match t |
| 372 |
.is_withdrawal() |
| 373 |
.then(|| self.backend.vet_tag_withdrawal(name, &t.name)) |
| 374 |
{ |
| 375 |
Some(Err(reason)) => withdrawals_refused.push((t.name, reason)), |
| 376 |
_ => kept_tags.push(t), |
| 377 |
} |
| 378 |
} |
| 379 |
let mut report = r.receive_push_report(bundle, updates, policy, kept_tags)?; |
| 380 |
|
| 381 |
|
| 382 |
if !refused.is_empty() || !withdrawals_refused.is_empty() { |
| 383 |
report |
| 384 |
.results |
| 385 |
.retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT); |
| 386 |
} |
| 387 |
for u in refused { |
| 388 |
report.results.push(oxy_core::protocol::RefResult { |
| 389 |
name: u.name.clone(), |
| 390 |
ok: false, |
| 391 |
reason: format!( |
| 392 |
"{:?} is this repository's default bookmark and cannot be deleted", |
| 393 |
u.name |
| 394 |
), |
| 395 |
}); |
| 396 |
} |
| 397 |
for (tag, reason) in withdrawals_refused { |
| 398 |
report.results.push(oxy_core::protocol::RefResult { |
| 399 |
name: format!("tags/{tag}"), |
| 400 |
ok: false, |
| 401 |
reason, |
| 402 |
}); |
| 403 |
} |
| 404 |
for r in report.results.iter().filter(|r| !r.ok) { |
| 405 |
eprintln!( |
| 406 |
"oxy-server: push to {name}: {} refused: {}", |
| 407 |
r.name, r.reason |
| 408 |
); |
| 409 |
} |
| 410 |
self.backend.after_push(name, &report, headers); |
| 411 |
ok(wire::encode_push_response(&report.results)) |
| 412 |
} |
| 413 |
}) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
pub struct DirBackend { |
| 420 |
pub root: PathBuf, |
| 421 |
pub tokens: Vec<String>, |
| 422 |
} |
| 423 |
|
| 424 |
impl Backend for DirBackend { |
| 425 |
fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 426 |
if !valid_repo_name(name) { |
| 427 |
return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 428 |
} |
| 429 |
let dir = self.root.join(name); |
| 430 |
if !dir.join(".oxy").is_dir() { |
| 431 |
return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 432 |
} |
| 433 |
Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) |
| 434 |
} |
| 435 |
|
| 436 |
fn default_bookmark(&self, name: &str) -> Option<String> { |
| 437 |
self.open(name).ok().map(|r| r.config.bookmark.clone()) |
| 438 |
} |
| 439 |
|
| 440 |
fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 441 |
if access == Access::Read || self.tokens.is_empty() { |
| 442 |
return Ok(()); |
| 443 |
} |
| 444 |
match bearer_token(headers) { |
| 445 |
Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()), |
| 446 |
_ => Err(Failure( |
| 447 |
StatusCode::UNAUTHORIZED, |
| 448 |
"a valid bearer token is required to push".into(), |
| 449 |
)), |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
fn hex_id(h: &str) -> Option<[u8; 16]> { |
| 455 |
let h = h.trim(); |
| 456 |
if h.len() != 32 { |
| 457 |
return None; |
| 458 |
} |
| 459 |
let mut out = [0u8; 16]; |
| 460 |
for (i, byte) in out.iter_mut().enumerate() { |
| 461 |
*byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?; |
| 462 |
} |
| 463 |
Some(out) |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
pub fn client_features(headers: &HeaderMap) -> Option<&str> { |
| 469 |
headers |
| 470 |
.get(oxy_core::protocol::FEATURES_HEADER) |
| 471 |
.and_then(|v| v.to_str().ok()) |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
pub fn tailor_refs(repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) { |
| 477 |
repo.refs_for_client(refs, client_features(headers)); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
pub fn bearer_token(headers: &HeaderMap) -> Option<&str> { |
| 482 |
headers |
| 483 |
.get(header::AUTHORIZATION) |
| 484 |
.and_then(|v| v.to_str().ok()) |
| 485 |
.and_then(|v| v.strip_prefix("Bearer ")) |
| 486 |
.map(str::trim) |
| 487 |
} |
| 488 |
|
| 489 |
async fn dispatch( |
| 490 |
State(server): State<Arc<Server>>, |
| 491 |
Path((repo, endpoint)): Path<(String, String)>, |
| 492 |
method: Method, |
| 493 |
headers: HeaderMap, |
| 494 |
body: Bytes, |
| 495 |
) -> Response { |
| 496 |
match Endpoint::parse(&endpoint) { |
| 497 |
Some(e) => server.handle(repo, e, &method, &headers, body).await, |
| 498 |
None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(), |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pub const BODY_LIMIT: usize = if usize::BITS > 32 { |
| 506 |
4 << 30 |
| 507 |
} else { |
| 508 |
usize::MAX |
| 509 |
}; |
| 510 |
|
| 511 |
|
| 512 |
pub fn router(cfg: ServerConfig) -> Router { |
| 513 |
let server = Server::new(DirBackend { |
| 514 |
root: cfg.root, |
| 515 |
tokens: cfg.tokens, |
| 516 |
}); |
| 517 |
|
| 518 |
|
| 519 |
Router::new() |
| 520 |
.route("/{repo}/oxy/v1/{endpoint}", any(dispatch)) |
| 521 |
.layer(axum::extract::DefaultBodyLimit::max(BODY_LIMIT)) |
| 522 |
.with_state(server) |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 528 |
listener.set_nonblocking(true)?; |
| 529 |
match cfg.tls.clone() { |
| 530 |
Some(tls) => { |
| 531 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 532 |
let rustls_cfg = |
| 533 |
axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key) |
| 534 |
.await |
| 535 |
.map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?; |
| 536 |
axum_server::from_tcp_rustls(listener, rustls_cfg)? |
| 537 |
.serve(router(cfg).into_make_service()) |
| 538 |
.await |
| 539 |
} |
| 540 |
None => { |
| 541 |
let listener = tokio::net::TcpListener::from_std(listener)?; |
| 542 |
axum::serve(listener, router(cfg)).await |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 549 |
let rt = tokio::runtime::Builder::new_multi_thread() |
| 550 |
.enable_all() |
| 551 |
.build()?; |
| 552 |
rt.block_on(serve(listener, cfg)) |
| 553 |
} |