From ce93b4bfc9cc8786a043a2fc0d08dbe56991d20f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sun, 13 Feb 2022 15:07:43 +0100 Subject: [PATCH] git-server: Move function where it's used Signed-off-by: Alexis Sellier --- git-server/src/hooks/pre_receive.rs | 21 ++++++++++++++++++++- git-server/src/lib.rs | 17 ----------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/git-server/src/hooks/pre_receive.rs b/git-server/src/hooks/pre_receive.rs index 695342d9..4c954cd9 100644 --- a/git-server/src/hooks/pre_receive.rs +++ b/git-server/src/hooks/pre_receive.rs @@ -16,12 +16,14 @@ //! //! The `pre-receive` git hook provides access to GPG certificates for a signed push, useful for authorizing an //! update the repository. +use std::io; use std::io::prelude::*; use std::io::stdin; use std::str::FromStr; use envconfig::Envconfig; use git2::{Oid, Repository}; +use librad::PeerId; use super::{ types::{CertNonceStatus, CertStatus, ReceivePackEnv}, @@ -122,7 +124,7 @@ impl PreReceive { // key fingerpint. let (peer_id, _) = crate::parse_ref(refname) .map_err(|_| Error::InvalidRefPushed(refname.to_owned()))?; - let peer_fingerprint = crate::to_ssh_fingerprint(&peer_id)?; + let peer_fingerprint = to_ssh_fingerprint(&peer_id)?; if key_fingerprint[..] != peer_fingerprint[..] { return Err(Error::Unauthorized("signer does not match remote ref")); @@ -184,3 +186,20 @@ impl PreReceive { Err(Error::Unauthorized("key is not authorized to push")) } } + +/// Get the SSH key fingerprint from a peer id. +fn to_ssh_fingerprint(peer_id: &PeerId) -> Result, io::Error> { + use byteorder::{BigEndian, WriteBytesExt}; + use sha2::Digest; + + let mut buf = Vec::new(); + let name = b"ssh-ed25519"; + let key = peer_id.as_public_key().as_ref(); + + buf.write_u32::(name.len() as u32)?; + buf.extend_from_slice(name); + buf.write_u32::(key.len() as u32)?; + buf.extend_from_slice(key); + + Ok(sha2::Sha256::digest(&buf).to_vec()) +} diff --git a/git-server/src/lib.rs b/git-server/src/lib.rs index 78125812..ef8da3d0 100644 --- a/git-server/src/lib.rs +++ b/git-server/src/lib.rs @@ -595,23 +595,6 @@ fn gen_random_string() -> String { out } -/// Get the SSH key fingerprint from a peer id. -fn to_ssh_fingerprint(peer_id: &PeerId) -> Result, io::Error> { - use byteorder::{BigEndian, WriteBytesExt}; - use sha2::Digest; - - let mut buf = Vec::new(); - let name = b"ssh-ed25519"; - let key = peer_id.as_public_key().as_ref(); - - buf.write_u32::(name.len() as u32)?; - buf.extend_from_slice(name); - buf.write_u32::(key.len() as u32)?; - buf.extend_from_slice(key); - - Ok(sha2::Sha256::digest(&buf).to_vec()) -} - /// Parse a remote git ref into a peer id and return the remaining input. /// /// Eg. `refs/remotes//heads/master`