From 0d5abed00debf56d421c99de2c3aa075d527505b Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 17 Feb 2022 14:01:54 +0000 Subject: [PATCH] git-server: Add /info path Signed-off-by: xphoniex --- Cargo.lock | 1 + git-server/Cargo.toml | 1 + git-server/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51dc98eb..1267c3df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4114,6 +4114,7 @@ dependencies = [ "http", "librad", "radicle-source", + "serde_json", "sha2 0.9.8", "shared", "thiserror", diff --git a/git-server/Cargo.toml b/git-server/Cargo.toml index 570fdf22..dc4b2907 100644 --- a/git-server/Cargo.toml +++ b/git-server/Cargo.toml @@ -36,6 +36,7 @@ tracing = "0.1" tracing-subscriber = "0.2" warp = { version = "0.3.1", features = ["tls"] } radicle-source = { version = "0.2.0" } +serde_json = { version = "1", features = ["preserve_order"] } # hooks feature enabled dependencies envconfig = { version = "0.10.0", optional = true } diff --git a/git-server/src/lib.rs b/git-server/src/lib.rs index ef8da3d0..2d9fb8e0 100644 --- a/git-server/src/lib.rs +++ b/git-server/src/lib.rs @@ -26,6 +26,7 @@ use librad::identities::SomeIdentity; use librad::paths::Paths; use librad::profile::Profile; use librad::PeerId; +use serde_json::json; use tokio::sync::RwLock; use warp::hyper::StatusCode; use warp::reply; @@ -295,7 +296,15 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .map(Some) .or_else(|_| async { Ok::<(Option,), Infallible>((None,)) }); - let server = warp::filters::any::any() + let _ctx = ctx.clone(); + let info = warp::path("info").and( + warp::get() + .map(move || _ctx.clone()) + .and(path::end()) + .and_then(info_handler), + ); + + let git = warp::filters::any::any() .map(move || ctx.clone()) .and(warp::method()) .and(warp::filters::header::headers_cloned()) @@ -312,7 +321,9 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .and_then(git_handler) .recover(recover) .with(warp::log("radicle_git_server")); - let server = warp::serve(server); + + let routes = info.or(git); + let server = warp::serve(routes); if let (Some(cert), Some(key)) = (options.tls_cert, options.tls_key) { server @@ -384,6 +395,33 @@ async fn git_handler( Ok(Box::new(response)) } +async fn info_handler(ctx: Context) -> Result { + let git_version = Command::new("git") + .arg("version") + .output() + .map_err(Error::from)? + .stdout; + + let git_version = std::str::from_utf8(&git_version) + .map_err(Error::from)? + .trim(); + + let mut authorized_keys = ctx.load_authorized_keys().map_err(Error::from)?; + if authorized_keys.is_empty() && ctx.allow_unauthorized_keys { + authorized_keys.push(String::from("*")); + } + + let response = json!({ + "git": { + "version": git_version + }, + "root": ctx.root, + "authorized-keys": authorized_keys, + }); + + Ok(warp::reply::json(&response)) +} + async fn git( ctx: Context, method: Method,