Skip to content

Commit

Permalink
Address CR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolfs committed Jul 23, 2020
1 parent 62efdb0 commit 1547c52
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 80 deletions.
4 changes: 2 additions & 2 deletions git-helpers/src/bin/remote/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use radicle_git_helpers::run_rad_remote_helper;
use radicle_git_helpers::remote_helper;

fn main() -> anyhow::Result<()> {
run_rad_remote_helper()
remote_helper::run()
}
79 changes: 1 addition & 78 deletions git-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,4 @@
extern crate radicle_keystore as keystore;

pub mod credential;

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())
.map_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())
}
pub mod remote_helper;
95 changes: 95 additions & 0 deletions git-helpers/src/remote_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// This file is part of radicle-link
// <https://github.com/radicle-dev/radicle-link>
//
// Copyright (C) 2019-2020 The Radicle Team <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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

use crate::credential;
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() -> anyhow::Result<()> {
let url = {
let args = env::args().skip(1).take(2).collect::<Vec<_>>();
args[0]
.parse()
.or_else(|_| args[1].parse())
.map_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())
}

0 comments on commit 1547c52

Please sign in to comment.