From a911806d9fa854f5aa8d6dd014eed6ffaec5a6a4 Mon Sep 17 00:00:00 2001 From: "Colton J. McCurdy" Date: Sun, 11 Jun 2023 09:22:32 -0400 Subject: [PATCH] [fs] fix clone to support configurable ssh params Signed-off-by: Colton J. McCurdy --- README.md | 7 +++++++ src/fs.rs | 38 +++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e7d8b76..d567fbf 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,13 @@ against repos. ## Usage +Environment variables + +- `SSH_PRIVKAY_PATH` - (default: `$HOME/.ssh/id_rsa`). Path to your SSH private +key if it is not in the default location. +- `SSH_PRIVKAY_PASS` - (default: `""`). SSH private key passphrase if it is not +the default. + Global arguments - `--root ` - specify `$GITRS_ROOT`. Defaults to `$HOME/src`. diff --git a/src/fs.rs b/src/fs.rs index ed7f410..5694568 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -2,7 +2,7 @@ use crate::repo; use anyhow::{anyhow, Result}; use git2::{Cred, RemoteCallbacks}; use home; -use log::debug; +use log::{debug, error}; use std::collections::HashMap; use std::{env, fs, path::Path, path::PathBuf}; use walkdir::WalkDir; @@ -47,20 +47,36 @@ pub fn sync(root: PathBuf, repos: &HashMap, _clean_only: &bo } // https://docs.rs/git2/latest/git2/build/struct.RepoBuilder.html -// TODO: make the SSH params configurable. fn clone_ssh(url: &str, dst: &Path) -> Result<()> { let mut callbacks = RemoteCallbacks::new(); - // TODO: fix this - callbacks.credentials(|_url, username_from_url, _allowed_types| { + + callbacks.credentials(|_url, username, _allowed_types| { + let mut ssh_privkey = PathBuf::new(); + let mut ssh_privkey_pass = String::from(""); + + // default + if let Some(h) = home::home_dir() { + ssh_privkey = h.join(".ssh/id_rsa"); + } + + // default + if let Ok(pw) = env::var("SSH_PRIVKEY_PASS") { + ssh_privkey_pass = pw; + } + + if !ssh_privkey.exists() { + ssh_privkey = PathBuf::from(env::var("SSH_PRIVKEY_PATH").expect("$HOME/.ssh/id_rsa doesn't exists, you must specify an ssh private key path via SSH_PRIVKEY_PATH")); + if !ssh_privkey.exists() { + error!("$SSH_PRIVKEY_PATH doesn't exists"); + } + } + // https://libgit2.org/libgit2/#HEAD/group/credential/git_credential_ssh_key_from_agent Cred::ssh_key( - username_from_url.unwrap(), - Some(Path::new(&format!( - "{}/.ssh/fastly_rsa.pub", - env::var("HOME").unwrap() - ))), - Path::new(&format!("{}/.ssh/fastly_rsa", env::var("HOME").unwrap())), - Some(env::var("SSH_PASS").unwrap().as_str()), + username.unwrap(), + None, + &ssh_privkey.as_path(), + Some(ssh_privkey_pass.as_str()), ) });