Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract rad remote helper into lib #254

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 2 additions & 75 deletions git-helpers/src/bin/remote/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,81 +17,8 @@

#![feature(str_strip)]

use std::{
env,
io,
path::{Path, PathBuf},
};

use librad::{
git::local::{
transport::{LocalTransport, Localio, Mode::Stateful, Settings},
url::LocalUrl,
},
keys::{PublicKey, SecretKey},
paths::Paths,
};
use radicle_git_helpers::credential;
use radicle_keystore::{crypto::Pwhash, FileStorage, Keystore};

// FIXME: this should be defined elsewhere to be consistent between applications
const SECRET_KEY_FILE: &str = "librad.key";
use radicle_git_helpers::run_rad_remote_helper;

fn main() -> anyhow::Result<()> {
let url = {
let args = env::args().skip(1).take(2).collect::<Vec<_>>();
args[0]
.parse()
.or_else(|_| args[1].parse())
.or_else(|_| Err(anyhow::anyhow!("invalid args: {:?}", args)))
}?;

let git_dir = env::var("GIT_DIR").map(PathBuf::from)?;

let mut transport = {
let paths = Paths::from_env()?;
let key = get_signer(&git_dir, paths.keys_dir(), &url)?;
LocalTransport::new(Settings { paths, signer: key })
}?;

loop {
let mut buf = String::with_capacity(32);
io::stdin().read_line(&mut buf)?;
let line = buf.trim();

if line == "capabilities" {
println!("connect\n\n");
continue;
}

if let Some(service) = line.strip_prefix("connect ") {
let service = match service {
"git-upload-pack" => Ok(git2::transport::Service::UploadPack),
"git-receive-pack" => Ok(git2::transport::Service::ReceivePack),
unknown => Err(anyhow::anyhow!("unknown service: {}", unknown)),
}?;

println!();

transport
.connect(url, service, Stateful, Localio::inherit())?
.wait()?;

break;
}

return Err(anyhow::anyhow!("unexpected command: {}", line));
}

Ok(())
}

fn get_signer(git_dir: &Path, keys_dir: &Path, url: &LocalUrl) -> anyhow::Result<SecretKey> {
let pass = credential::Git::new(git_dir).get(url)?;
let file = keys_dir.join(SECRET_KEY_FILE);
let keystore = FileStorage::<_, PublicKey, _, _>::new(&file, Pwhash::new(pass));
keystore
.get_key()
.map(|keypair| keypair.secret_key)
.map_err(|e| e.into())
run_rad_remote_helper()
}
78 changes: 78 additions & 0 deletions git-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,81 @@
extern crate radicle_keystore as keystore;

pub mod credential;
Copy link
Contributor

@cloudhead cloudhead Jul 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of putting this code here, I'd suggest:

  • Adding pub mod remote_helper here
  • Creating a remote_helper.rs file and moving this code into that file
  • Renaming the run_rad_remote_helper function to run
  • Invoking the function from main with remote_helper::run()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


use std::{
env,
io,
path::{Path, PathBuf},
};

use librad::{
git::local::{
transport::{LocalTransport, Localio, Mode::Stateful, Settings},
url::LocalUrl,
},
keys::{PublicKey, SecretKey},
paths::Paths,
};
use radicle_keystore::{crypto::Pwhash, FileStorage, Keystore};

// FIXME: this should be defined elsewhere to be consistent between applications
const SECRET_KEY_FILE: &str = "librad.key";

pub fn run_rad_remote_helper() -> anyhow::Result<()> {
let url = {
let args = env::args().skip(1).take(2).collect::<Vec<_>>();
args[0]
.parse()
.or_else(|_| args[1].parse())
.or_else(|_| Err(anyhow::anyhow!("invalid args: {:?}", args)))
}?;

let git_dir = env::var("GIT_DIR").map(PathBuf::from)?;

let mut transport = {
let paths = Paths::from_env()?;
let key = get_signer(&git_dir, paths.keys_dir(), &url)?;
LocalTransport::new(Settings { paths, signer: key })
}?;

loop {
let mut buf = String::with_capacity(32);
io::stdin().read_line(&mut buf)?;
let line = buf.trim();

if line == "capabilities" {
println!("connect\n\n");
continue;
}

if let Some(service) = line.strip_prefix("connect ") {
let service = match service {
"git-upload-pack" => Ok(git2::transport::Service::UploadPack),
"git-receive-pack" => Ok(git2::transport::Service::ReceivePack),
unknown => Err(anyhow::anyhow!("unknown service: {}", unknown)),
}?;

println!();

transport
.connect(url, service, Stateful, Localio::inherit())?
.wait()?;

break;
}

return Err(anyhow::anyhow!("unexpected command: {}", line));
}

Ok(())
}

fn get_signer(git_dir: &Path, keys_dir: &Path, url: &LocalUrl) -> anyhow::Result<SecretKey> {
let pass = credential::Git::new(git_dir).get(url)?;
let file = keys_dir.join(SECRET_KEY_FILE);
let keystore = FileStorage::<_, PublicKey, _, _>::new(&file, Pwhash::new(pass));
keystore
.get_key()
.map(|keypair| keypair.secret_key)
.map_err(|e| e.into())
}