Skip to content
This repository was archived by the owner on Apr 16, 2025. It is now read-only.

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 all commits
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 @@ -15,81 +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 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::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())
.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())
remote_helper::run()
}
1 change: 1 addition & 0 deletions git-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
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.

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())
}