From d1647b7a056f04ad5828976dd5a7e2e06b431feb Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 26 Jan 2025 23:55:50 +0100 Subject: Stop using current user in git hooks Want to support any authentication for the git server, so use git commiter as username for creating reviews instead of the local user that logged in to git. Also verify that pushed commits has a valid author in pre-receive. This is tricky as pre-receive must do this check in the hook, because pre-receive runs when before the objects are pushed so the server can't read the commits, the hook must do this. --- server/src/githook.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'server/src/githook.rs') diff --git a/server/src/githook.rs b/server/src/githook.rs index 057ee47..f0e872a 100644 --- a/server/src/githook.rs +++ b/server/src/githook.rs @@ -6,7 +6,6 @@ use std::os::unix::net::UnixStream; use std::path::PathBuf; use tokio::io::{self, AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::task; -use users::get_current_username; mod fs_utils; mod git; @@ -33,11 +32,6 @@ impl fmt::Display for IoError { impl Error for IoError {} -async fn get_socket() -> Result { - let repo = git::Repository::new(PathBuf::from("."), true, None::, None::); - repo.config_get("eyeballs.socket").await -} - #[tokio::main] async fn main() -> Result<(), Box> { let pre = match std::env::current_exe()? @@ -49,40 +43,41 @@ async fn main() -> Result<(), Box> { _ => return Err(Box::::from("Invalid hook executable name")), }; - let user = match get_current_username() { - Some(username) => match username.into_string() { - Ok(valid_username) => valid_username, - Err(_) => return Err(Box::::from("Invalid username for current user")), - }, - None => { - return Err(Box::::from( - "Unable to get username of current user", - )) - } - }; - let input = io::stdin(); let reader = BufReader::new(input); let mut lines = reader.lines(); let mut request = git_socket::GitHookRequest { pre, - user, receive: Vec::new(), }; + + let repo = git::Repository::new(PathBuf::from("."), true, None::, None::); + while let Some(line) = lines.next_line().await? { let data: Vec<&str> = line.split(' ').collect(); if data.len() == 3 { + let mut commiter: Option = None; + if pre && data[1] != git::EMPTY { + match repo.get_commiter(data[1]).await { + Ok(user) => { + commiter = Some(user.username); + } + Err(_) => {} + } + } + request.receive.push(git_socket::GitReceive { old_value: data[0].to_string(), new_value: data[1].to_string(), reference: data[2].to_string(), + commiter: commiter, }) } } - let socket = PathBuf::from(get_socket().await?); + let socket = PathBuf::from(repo.config_get("eyeballs.socket").await?); let response = task::spawn_blocking(move || { let stream = UnixStream::connect(socket).map_err(|e| IoError::new(e.to_string()))?; -- cgit v1.2.3-70-g09d2