use ssh_key::{HashAlg, LineEnding, PrivateKey};
use std::path::PathBuf;
#[derive(thiserror::Error, Debug)]
pub enum SignBuilderError {
#[error("Failed to derive a commit signing method from git configuration 'gpg.format': {0}")]
InvalidFormat(String),
#[error("Failed to retrieve 'user.signingkey' from the git configuration: {0}")]
GPGSigningKey(String),
#[error("Failed to retrieve 'user.signingkey' from the git configuration: {0}")]
SSHSigningKey(String),
#[error("Failed to build signing signature: {0}")]
Signature(String),
#[error("Select signing method '{0}' has not been implemented")]
MethodNotImplemented(String),
}
#[derive(thiserror::Error, Debug)]
pub enum SignError {
#[error("Failed to spawn signing process: {0}")]
Spawn(String),
#[error("Failed to acquire standard input handler")]
Stdin,
#[error("Failed to write buffer to standard input of signing process: {0}")]
WriteBuffer(String),
#[error("Failed to get output of signing process call: {0}")]
Output(String),
#[error("Failed to execute signing process: {0}")]
Shellout(String),
}
pub trait Sign {
fn sign(
&self,
commit: &[u8],
) -> Result<(String, Option<String>), SignError>;
#[cfg(test)]
fn program(&self) -> &String;
#[cfg(test)]
fn signing_key(&self) -> &String;
}
pub struct SignBuilder;
impl SignBuilder {
pub fn from_gitconfig(
repo: &git2::Repository,
config: &git2::Config,
) -> Result<Box<dyn Sign>, SignBuilderError> {
let format = config
.get_string("gpg.format")
.unwrap_or_else(|_| "openpgp".to_string());
match format.as_str() {
"openpgp" => {
let program = config
.get_string("gpg.openpgp.program")
.or_else(|_| config.get_string("gpg.program"))
.unwrap_or_else(|_| "gpg".to_string());
let signing_key = config
.get_string("user.signingKey")
.or_else(
|_| -> Result<String, SignBuilderError> {
Ok(crate::sync::commit::signature_allow_undefined_name(repo)
.map_err(|err| {
SignBuilderError::Signature(
err.to_string(),
)
})?
.to_string())
},
)
.map_err(|err| {
SignBuilderError::GPGSigningKey(
err.to_string(),
)
})?;
Ok(Box::new(GPGSign {
program,
signing_key,
}))
}
"x509" => Err(SignBuilderError::MethodNotImplemented(
String::from("x509"),
)),
"ssh" => {
let ssh_signer = config
.get_string("user.signingKey")
.ok()
.and_then(|key_path| {
key_path.strip_prefix('~').map_or_else(
|| Some(PathBuf::from(&key_path)),
|ssh_key_path| {
dirs::home_dir().map(|home| {
home.join(
ssh_key_path
.strip_prefix('/')
.unwrap_or(ssh_key_path),
)
})
},
)
})
.ok_or_else(|| {
SignBuilderError::SSHSigningKey(String::from(
"ssh key setting absent",
))
})
.and_then(SSHSign::new)?;
let signer: Box<dyn Sign> = Box::new(ssh_signer);
Ok(signer)
}
_ => Err(SignBuilderError::InvalidFormat(format)),
}
}
}
pub struct GPGSign {
program: String,
signing_key: String,
}
impl GPGSign {
pub fn new(program: &str, signing_key: &str) -> Self {
Self {
program: program.to_string(),
signing_key: signing_key.to_string(),
}
}
}
impl Sign for GPGSign {
fn sign(
&self,
commit: &[u8],
) -> Result<(String, Option<String>), SignError> {
use std::io::Write;
use std::process::{Command, Stdio};
let mut cmd = Command::new(&self.program);
cmd.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--status-fd=2")
.arg("-bsau")
.arg(&self.signing_key);
log::trace!("signing command: {cmd:?}");
let mut child = cmd
.spawn()
.map_err(|e| SignError::Spawn(e.to_string()))?;
let mut stdin = child.stdin.take().ok_or(SignError::Stdin)?;
stdin
.write_all(commit)
.map_err(|e| SignError::WriteBuffer(e.to_string()))?;
drop(stdin);
let output = child
.wait_with_output()
.map_err(|e| SignError::Output(e.to_string()))?;
if !output.status.success() {
return Err(SignError::Shellout(format!(
"failed to sign data, program '{}' exited non-zero: {}",
&self.program,
std::str::from_utf8(&output.stderr)
.unwrap_or("[error could not be read from stderr]")
)));
}
let stderr = std::str::from_utf8(&output.stderr)
.map_err(|e| SignError::Shellout(e.to_string()))?;
if !stderr.contains("\n[GNUPG:] SIG_CREATED ") {
return Err(SignError::Shellout(
format!("failed to sign data, program '{}' failed, SIG_CREATED not seen in stderr", &self.program),
));
}
let signed_commit = std::str::from_utf8(&output.stdout)
.map_err(|e| SignError::Shellout(e.to_string()))?;
Ok((signed_commit.to_string(), Some("gpgsig".to_string())))
}
#[cfg(test)]
fn program(&self) -> &String {
&self.program
}
#[cfg(test)]
fn signing_key(&self) -> &String {
&self.signing_key
}
}
pub struct SSHSign {
#[cfg(test)]
program: String,
#[cfg(test)]
key_path: String,
secret_key: PrivateKey,
}
impl SSHSign {
pub fn new(mut key: PathBuf) -> Result<Self, SignBuilderError> {
key.set_extension("");
if key.is_file() {
#[cfg(test)]
let key_path = format!("{}", &key.display());
std::fs::read(key)
.ok()
.and_then(|bytes| {
PrivateKey::from_openssh(bytes).ok()
})
.map(|secret_key| Self {
#[cfg(test)]
program: "ssh".to_string(),
#[cfg(test)]
key_path,
secret_key,
})
.ok_or_else(|| {
SignBuilderError::SSHSigningKey(String::from(
"Fail to read the private key for sign.",
))
})
} else {
Err(SignBuilderError::SSHSigningKey(
String::from("Currently, we only support a pair of ssh key in disk."),
))
}
}
}
impl Sign for SSHSign {
fn sign(
&self,
commit: &[u8],
) -> Result<(String, Option<String>), SignError> {
let sig = self
.secret_key
.sign("git", HashAlg::Sha256, commit)
.map_err(|err| SignError::Spawn(err.to_string()))?
.to_pem(LineEnding::LF)
.map_err(|err| SignError::Spawn(err.to_string()))?;
Ok((sig, None))
}
#[cfg(test)]
fn program(&self) -> &String {
&self.program
}
#[cfg(test)]
fn signing_key(&self) -> &String {
&self.key_path
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;
use crate::sync::tests::repo_init_empty;
#[test]
fn test_invalid_signing_format() -> Result<()> {
let (_temp_dir, repo) = repo_init_empty()?;
{
let mut config = repo.config()?;
config.set_str("gpg.format", "INVALID_SIGNING_FORMAT")?;
}
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?);
assert!(sign.is_err());
Ok(())
}
#[test]
fn test_program_and_signing_key_defaults() -> Result<()> {
let (_tmp_dir, repo) = repo_init_empty()?;
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?)?;
assert_eq!("gpg", sign.program());
assert_eq!("name <email>", sign.signing_key());
Ok(())
}
#[test]
fn test_gpg_program_configs() -> Result<()> {
let (_tmp_dir, repo) = repo_init_empty()?;
{
let mut config = repo.config()?;
config.set_str("gpg.program", "GPG_PROGRAM_TEST")?;
}
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?)?;
assert_eq!("GPG_PROGRAM_TEST", sign.program());
{
let mut config = repo.config()?;
config.set_str(
"gpg.openpgp.program",
"GPG_OPENPGP_PROGRAM_TEST",
)?;
}
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?)?;
assert_eq!("GPG_OPENPGP_PROGRAM_TEST", sign.program());
Ok(())
}
#[test]
fn test_user_signingkey() -> Result<()> {
let (_tmp_dir, repo) = repo_init_empty()?;
{
let mut config = repo.config()?;
config.set_str("user.signingKey", "FFAA")?;
}
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?)?;
assert_eq!("FFAA", sign.signing_key());
Ok(())
}
#[test]
fn test_ssh_program_configs() -> Result<()> {
let (_tmp_dir, repo) = repo_init_empty()?;
{
let mut config = repo.config()?;
config.set_str("gpg.program", "ssh")?;
config.set_str("user.signingKey", "/tmp/key.pub")?;
}
let sign =
SignBuilder::from_gitconfig(&repo, &repo.config()?)?;
assert_eq!("ssh", sign.program());
assert_eq!("/tmp/key.pub", sign.signing_key());
Ok(())
}
}