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

make ssh operations sync #796

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions cli/linkd-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ features = ["std", "derive"]
path = "../lnk-clib"
version = "0.1.0"

[dependencies.lnk-thrussh-agent]
version = "0.1.0"
features = [ "tokio-agent" ]

[dependencies.radicle-macros]
path = "../../macros"

Expand Down
14 changes: 2 additions & 12 deletions cli/lnk-clib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@ test = false
unsafe = []

[dependencies]
async-trait = "0.1"
futures = "0.3"
itertools = "0.10.0"
nix = "0.23.1"
once_cell = "1.10"
serde = "1.0"
serde_json = "1.0"
socket2 = "0.4.4"
thiserror = "1.0"
tracing = "0.1"
agent = { version = "0.1.0", git = "https://github.com/radicle-dev/radicle-ssh" }
dns-lookup = "1.0.8"

[dependencies.librad]
path = "../../librad"

[dependencies.lnk-thrussh-agent]
version = "0.1.0"
features = [ "tokio-agent" ]

[dependencies.minicbor]
version = "0.13"
features = ["std"]

[dependencies.tokio]
version = "1.17"
default-features = false
features = [ "fs", "io-std", "macros", "process", "rt-multi-thread", "signal" ]
55 changes: 24 additions & 31 deletions cli/lnk-clib/src/keys/ssh/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.

use std::{fmt, sync::Arc};
use std::fmt;
use std::os::unix::net::UnixStream;
use std::sync::Arc;

use async_trait::async_trait;
use lnk_thrussh_agent::{client::tokio::UnixStream, Constraint};
use agent::Constraint;
use serde::{de::DeserializeOwned, Serialize};

use librad::{
Expand All @@ -19,17 +20,15 @@ use librad::{
},
Keystore as _,
},
BoxedSignError,
BoxedSigner,
Signer as _,
BoxedSignError, BoxedSigner, Signer as _,
},
git::storage::ReadOnly,
keystore::sign::Signer,
profile::Profile,
Signature,
};

use crate::{keys, runtime};
use crate::keys;

use super::{with_socket, SshAuthSock};

Expand All @@ -38,27 +37,24 @@ pub struct SshSigner {
signer: Arc<dyn sign::ed25519::Signer<Error = ssh::error::Sign> + Send + Sync>,
}

#[async_trait]
impl Signer for SshSigner {
type Error = BoxedSignError;

fn public_key(&self) -> sign::ed25519::PublicKey {
self.signer.public_key()
}

async fn sign(&self, data: &[u8]) -> Result<sign::ed25519::Signature, BoxedSignError> {
fn sign(&self, data: &[u8]) -> Result<sign::ed25519::Signature, BoxedSignError> {
self.signer
.sign(data)
.await
.map_err(BoxedSignError::from_std_error)
}
}

impl librad::Signer for SshSigner {
fn sign_blocking(&self, data: &[u8]) -> Result<sign::Signature, <Self as sign::Signer>::Error> {
let data = data.to_vec();
let signer = self.clone();
runtime::block_on(async move { signer.sign(&data).await })
self.sign(&data)
}
}

Expand All @@ -70,18 +66,16 @@ pub fn signer(profile: &Profile, sock: SshAuthSock) -> Result<BoxedSigner, super
let peer_id = *storage.peer_id();
let pk = (*peer_id.as_public_key()).into();
let agent = with_socket(SshAgent::new(pk), sock);
runtime::block_on(async move {
let keys = ssh::list_keys::<UnixStream>(&agent).await?;
if keys.contains(&pk) {
let signer = agent.connect::<UnixStream>().await?;
let signer = SshSigner {
signer: Arc::new(signer),
};
Ok(BoxedSigner::new(signer))
} else {
Err(super::Error::NoSuchKey(peer_id))
}
})
let keys = ssh::list_keys::<UnixStream>(&agent)?;
if keys.contains(&pk) {
let signer = agent.connect::<UnixStream>()?;
let signer = SshSigner {
signer: Arc::new(signer),
};
Ok(BoxedSigner::new(signer))
} else {
Err(super::Error::NoSuchKey(peer_id))
}
}

/// Add the signing key associated with this `profile` to the `ssh-agent`.
Expand All @@ -106,9 +100,7 @@ where
.get_key()
.map_err(|err| super::Error::GetKey(err.into()))?;
let agent = with_socket(SshAgent::new(key.public_key.into()), sock);
runtime::block_on(async move {
ssh::add_key::<UnixStream>(&agent, key.secret_key.into(), &constraints).await
})?;
ssh::add_key::<UnixStream>(&agent, key.secret_key.into(), &constraints)?;
Ok(())
}

Expand All @@ -129,9 +121,10 @@ where
.get_key()
.map_err(|err| super::Error::GetKey(err.into()))?;
let agent = with_socket(SshAgent::new(key.public_key.into()), sock);
Ok(runtime::block_on(async move {
ssh::remove_key::<UnixStream>(&agent, &key.public_key.into()).await
})?)
Ok(ssh::remove_key::<UnixStream>(
&agent,
&key.public_key.into(),
)?)
}

/// Test whether the signing key associated with this `profile` is present on
Expand All @@ -144,7 +137,7 @@ pub fn is_signer_present(profile: &Profile, sock: SshAuthSock) -> Result<bool, s
let peer_id = storage.peer_id();
let pk = (*peer_id.as_public_key()).into();
let agent = with_socket(SshAgent::new(pk), sock);
let keys = runtime::block_on(async move { ssh::list_keys::<UnixStream>(&agent).await })?;
let keys = ssh::list_keys::<UnixStream>(&agent)?;
Ok(keys.contains(&pk))
}

Expand Down
1 change: 0 additions & 1 deletion cli/lnk-clib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Linking Exception. For full terms see the included LICENSE file.

pub mod keys;
pub mod runtime;
pub mod seed;
pub mod ser;
#[cfg(unix)]
Expand Down
112 changes: 0 additions & 112 deletions cli/lnk-clib/src/runtime.rs

This file was deleted.

24 changes: 14 additions & 10 deletions cli/lnk-clib/src/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.

use std::{convert::TryFrom, fmt, io, net::SocketAddr, str::FromStr};
use std::net::{SocketAddr, ToSocketAddrs};
use std::{convert::TryFrom, fmt, io, str::FromStr};

use dns_lookup::lookup_host;
use serde::Serialize;

use librad::{net::discovery, PeerId};
use tokio::net::{lookup_host, ToSocketAddrs};

pub mod store;
pub use store::Store;
Expand Down Expand Up @@ -72,17 +73,20 @@ where
}

impl<T> Seed<T> {
/// Resolve the `Seed`'s address by calling [`tokio::net::lookup_host`].
/// Resolve the `Seed`'s address by calling [`dns_lookup::lookup_host`].
///
/// # Errors
///
/// If the addresses returned by `lookup_host` are empty, this will result
/// in an [`error::Resolve::DnsLookupFailed`].
pub async fn resolve(&self) -> Result<Seed<Vec<SocketAddr>>, error::Resolve>
pub fn resolve(&self) -> Result<Seed<Vec<SocketAddr>>, error::Resolve>
where
T: Clone + ToSocketAddrs + fmt::Display,
{
let addrs = lookup_host(self.addrs.clone()).await?.collect::<Vec<_>>();
let addrs = lookup_host(&self.addrs.clone().to_string())?
.into_iter()
.map(|e| SocketAddr::new(e, 0))
.collect::<Vec<_>>();
if !addrs.is_empty() {
Ok(Seed {
peer: self.peer,
Expand Down Expand Up @@ -118,7 +122,7 @@ impl Seeds {
///
/// If any seeds failed to be resolved they will be returned alongside the
/// successful seeds.
pub async fn load<S, T>(
pub fn load<S, T>(
store: &S,
cutoff: impl Into<Option<usize>>,
) -> Result<(Seeds, Vec<error::Load>), S::Scan>
Expand All @@ -135,7 +139,7 @@ impl Seeds {
for seed in store.scan()? {
match seed {
Err(err) => failures.push(error::Load::MalformedSeed(Box::new(err))),
Ok(seed) => match seed.resolve().await {
Ok(seed) => match seed.resolve() {
Ok(r) => {
resolved.push(r);
if Some(resolved.len()) == cutoff {
Expand All @@ -154,14 +158,14 @@ impl Seeds {
///
/// If any seeds failed to be resolved they will be returned alongside the
/// successful seeds.
pub async fn resolve(
seeds: impl ExactSizeIterator<Item = &Seed<String>>,
pub fn resolve<'a>(
seeds: impl ExactSizeIterator<Item = &'a Seed<String>>,
) -> (Self, Vec<error::Resolve>) {
let mut resolved = Vec::with_capacity(seeds.len());
let mut failures = Vec::new();

for seed in seeds {
match seed.resolve().await {
match seed.resolve() {
Ok(r) => resolved.push(r),
Err(err) => failures.push(err),
}
Expand Down
4 changes: 0 additions & 4 deletions cli/lnk-exe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ path = "../lnk-profile"
[dependencies.lnk-sync]
path = "../lnk-sync"

[dependencies.lnk-thrussh-agent]
version = "0.1.0"
default-features = false

[dependencies.tokio]
version = "1.17"
features = ["rt"]
Expand Down
4 changes: 0 additions & 4 deletions cli/lnk-identities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ features = ["vendored"]
[dependencies.librad]
path = "../../librad"

[dependencies.lnk-thrussh-agent]
version = "0.1.0"
default-features = false

[dependencies.radicle-git-ext]
path = "../../git-ext"

Expand Down
2 changes: 1 addition & 1 deletion cli/lnk-profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test = false
[dependencies]
anyhow = "1"
futures-lite = "1.12.0"
lnk-thrussh-agent = "0.1.0"
agent = { version = "0.1.0", git = "https://github.com/radicle-dev/radicle-ssh" }
thiserror = "1"
serde = "1"

Expand Down
Loading