From 55e473d04ed8f4d109dc256b930653cdad60b365 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Tue, 21 Jan 2020 17:46:34 +0000 Subject: [PATCH] Moved Pemstore as separate crate + defined pathfinder as trait --- Cargo.lock | 9 ++++++++ Cargo.toml | 1 + common/pemstore/Cargo.toml | 13 +++++++++++ .../mod.rs => common/pemstore/src/lib.rs | 0 common/pemstore/src/pathfinder.rs | 15 ++++++++++++ .../pemstore/src}/pemstore.rs | 19 ++++----------- nym-client/Cargo.toml | 1 + nym-client/src/commands/init.rs | 6 ++--- nym-client/src/commands/tcpsocket.rs | 9 ++++---- nym-client/src/commands/websocket.rs | 10 ++++---- nym-client/src/config/mod.rs | 1 + nym-client/src/config/persistance/mod.rs | 1 + .../persistance}/pathfinder.rs | 23 +++++++++++++++---- nym-client/src/main.rs | 2 +- 14 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 common/pemstore/Cargo.toml rename nym-client/src/persistence/mod.rs => common/pemstore/src/lib.rs (100%) create mode 100644 common/pemstore/src/pathfinder.rs rename {nym-client/src/persistence => common/pemstore/src}/pemstore.rs (84%) create mode 100644 nym-client/src/config/mod.rs create mode 100644 nym-client/src/config/persistance/mod.rs rename nym-client/src/{persistence => config/persistance}/pathfinder.rs (52%) diff --git a/Cargo.lock b/Cargo.lock index 6aa1d181a4..3e9d9da323 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1356,6 +1356,7 @@ dependencies = [ "log", "mix-client", "pem", + "pemstore", "provider-client", "rand 0.7.2", "rand_distr", @@ -1502,6 +1503,14 @@ dependencies = [ "regex", ] +[[package]] +name = "pemstore" +version = "0.1.0" +dependencies = [ + "crypto", + "pem", +] + [[package]] name = "percent-encoding" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index d03d085fa8..e233914c0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "common/addressing", "common/crypto", "common/healthcheck", + "common/pemstore", "common/topology", "mixnode", "nym-client", diff --git a/common/pemstore/Cargo.toml b/common/pemstore/Cargo.toml new file mode 100644 index 0000000000..2774ec2ccd --- /dev/null +++ b/common/pemstore/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "pemstore" +version = "0.1.0" +authors = ["Jedrzej Stuczynski "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pem = "0.7.0" + +## internal +crypto = {path = "../crypto"} diff --git a/nym-client/src/persistence/mod.rs b/common/pemstore/src/lib.rs similarity index 100% rename from nym-client/src/persistence/mod.rs rename to common/pemstore/src/lib.rs diff --git a/common/pemstore/src/pathfinder.rs b/common/pemstore/src/pathfinder.rs new file mode 100644 index 0000000000..8633f15103 --- /dev/null +++ b/common/pemstore/src/pathfinder.rs @@ -0,0 +1,15 @@ +use std::path::PathBuf; + +pub trait PathFinder { + fn config_dir(&self) -> PathBuf; + fn private_identity_key(&self) -> PathBuf; + fn public_identity_key(&self) -> PathBuf; + + // Optional: + fn private_encryption_key(&self) -> Option { + None + } + fn public_encryption_key(&self) -> Option { + None + } +} diff --git a/nym-client/src/persistence/pemstore.rs b/common/pemstore/src/pemstore.rs similarity index 84% rename from nym-client/src/persistence/pemstore.rs rename to common/pemstore/src/pemstore.rs index 19f6bb07df..d16f8574fa 100644 --- a/nym-client/src/persistence/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -1,18 +1,9 @@ -use crate::persistence::pathfinder::Pathfinder; +use crate::pathfinder::PathFinder; use pem::{encode, parse, Pem}; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; -pub fn read_mix_identity_keypair_from_disk( - id: String, -) -> crypto::identity::DummyMixIdentityKeyPair { - let pathfinder = Pathfinder::new(id); - let pem_store = PemStore::new(pathfinder); - let keypair = pem_store.read_identity(); - keypair -} - #[allow(dead_code)] pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::x25519::KeyPair { unimplemented!() @@ -25,11 +16,11 @@ pub struct PemStore { } impl PemStore { - pub fn new(pathfinder: Pathfinder) -> PemStore { + pub fn new(pathfinder: P) -> PemStore { PemStore { - config_dir: pathfinder.config_dir, - private_mix_key: pathfinder.private_mix_key, - public_mix_key: pathfinder.public_mix_key, + config_dir: pathfinder.config_dir(), + private_mix_key: pathfinder.private_identity_key(), + public_mix_key: pathfinder.public_identity_key(), } } diff --git a/nym-client/Cargo.toml b/nym-client/Cargo.toml index f336d11b5a..86e3073c6e 100644 --- a/nym-client/Cargo.toml +++ b/nym-client/Cargo.toml @@ -35,6 +35,7 @@ crypto = {path = "../common/crypto"} directory-client = { path = "../common/clients/directory-client" } healthcheck = { path = "../common/healthcheck" } mix-client = { path = "../common/clients/mix-client" } +pemstore = {path = "../common/pemstore"} provider-client = { path = "../common/clients/provider-client" } sfw-provider-requests = { path = "../sfw-provider/sfw-provider-requests" } topology = {path = "../common/topology" } diff --git a/nym-client/src/commands/init.rs b/nym-client/src/commands/init.rs index f6d675ea02..4ac9c47db4 100644 --- a/nym-client/src/commands/init.rs +++ b/nym-client/src/commands/init.rs @@ -1,13 +1,13 @@ -use crate::persistence::pathfinder::Pathfinder; -use crate::persistence::pemstore::PemStore; +use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; use crypto::identity::MixnetIdentityKeyPair; +use pemstore::pemstore::PemStore; pub fn execute(matches: &ArgMatches) { println!("Initialising client..."); let id = matches.value_of("id").unwrap().to_string(); // required for now - let pathfinder = Pathfinder::new(id); + let pathfinder = ClientPathfinder::new(id); println!("Writing keypairs to {:?}...", pathfinder.config_dir); let mix_keys = crypto::identity::DummyMixIdentityKeyPair::new(); diff --git a/nym-client/src/commands/tcpsocket.rs b/nym-client/src/commands/tcpsocket.rs index 053981d446..81e0ef72d5 100644 --- a/nym-client/src/commands/tcpsocket.rs +++ b/nym-client/src/commands/tcpsocket.rs @@ -1,8 +1,8 @@ use crate::client::{NymClient, SocketType}; -use crate::persistence::pemstore; - +use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use crypto::identity::{DummyMixIdentityKeyPair, MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use pemstore::pemstore::PemStore; use std::net::ToSocketAddrs; pub fn execute(matches: &ArgMatches) { @@ -26,7 +26,8 @@ pub fn execute(matches: &ArgMatches) { .next() .expect("Failed to extract the socket address from the iterator"); - let keypair = pemstore::read_mix_identity_keypair_from_disk(id); + // TODO: currently we know we are reading the 'DummyMixIdentityKeyPair', but how to properly assert the type? + let keypair: DummyMixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)).read_identity(); // TODO: reading auth_token from disk (if exists); println!("Public key: {}", keypair.public_key.to_b64_string()); diff --git a/nym-client/src/commands/websocket.rs b/nym-client/src/commands/websocket.rs index 0a66193a6f..644e023306 100644 --- a/nym-client/src/commands/websocket.rs +++ b/nym-client/src/commands/websocket.rs @@ -1,8 +1,8 @@ use crate::client::{NymClient, SocketType}; -use crate::persistence::pemstore; - +use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use crypto::identity::{DummyMixIdentityKeyPair, MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use pemstore::pemstore::PemStore; use std::net::ToSocketAddrs; pub fn execute(matches: &ArgMatches) { @@ -26,7 +26,9 @@ pub fn execute(matches: &ArgMatches) { .next() .expect("Failed to extract the socket address from the iterator"); - let keypair = pemstore::read_mix_identity_keypair_from_disk(id); + // TODO: currently we know we are reading the 'DummyMixIdentityKeyPair', but how to properly assert the type? + let keypair: DummyMixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)).read_identity(); + // TODO: reading auth_token from disk (if exists); println!("Public key: {}", keypair.public_key.to_b64_string()); diff --git a/nym-client/src/config/mod.rs b/nym-client/src/config/mod.rs new file mode 100644 index 0000000000..4bf0f95209 --- /dev/null +++ b/nym-client/src/config/mod.rs @@ -0,0 +1 @@ +pub mod persistance; diff --git a/nym-client/src/config/persistance/mod.rs b/nym-client/src/config/persistance/mod.rs new file mode 100644 index 0000000000..5692e9bc8b --- /dev/null +++ b/nym-client/src/config/persistance/mod.rs @@ -0,0 +1 @@ +pub mod pathfinder; diff --git a/nym-client/src/persistence/pathfinder.rs b/nym-client/src/config/persistance/pathfinder.rs similarity index 52% rename from nym-client/src/persistence/pathfinder.rs rename to nym-client/src/config/persistance/pathfinder.rs index 08110b9ae8..c71aab5b84 100644 --- a/nym-client/src/persistence/pathfinder.rs +++ b/nym-client/src/config/persistance/pathfinder.rs @@ -1,21 +1,36 @@ +use pemstore::pathfinder::PathFinder; use std::path::PathBuf; -pub struct Pathfinder { +pub struct ClientPathfinder { pub config_dir: PathBuf, pub private_mix_key: PathBuf, pub public_mix_key: PathBuf, } -impl Pathfinder { - pub fn new(id: String) -> Pathfinder { +impl ClientPathfinder { + pub fn new(id: String) -> Self { let os_config_dir = dirs::config_dir().unwrap(); // grabs the OS default config dir let config_dir = os_config_dir.join("nym").join("clients").join(id); let private_mix_key = config_dir.join("private.pem"); let public_mix_key = config_dir.join("public.pem"); - Pathfinder { + ClientPathfinder { config_dir, private_mix_key, public_mix_key, } } } + +impl PathFinder for ClientPathfinder { + fn config_dir(&self) -> PathBuf { + self.config_dir.clone() + } + + fn private_identity_key(&self) -> PathBuf { + self.private_mix_key.clone() + } + + fn public_identity_key(&self) -> PathBuf { + self.public_mix_key.clone() + } +} diff --git a/nym-client/src/main.rs b/nym-client/src/main.rs index 64daf4e515..d1e12a85d4 100644 --- a/nym-client/src/main.rs +++ b/nym-client/src/main.rs @@ -5,7 +5,7 @@ use clap::{App, Arg, ArgMatches, SubCommand}; pub mod built_info; pub mod client; mod commands; -mod persistence; +pub mod config; mod sockets; pub mod utils;