Moved Pemstore as separate crate + defined pathfinder as trait

This commit is contained in:
Jedrzej Stuczynski
2020-01-21 17:46:34 +00:00
parent ac988c62e9
commit 55e473d04e
14 changed files with 80 additions and 30 deletions
Generated
+9
View File
@@ -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"
+1
View File
@@ -8,6 +8,7 @@ members = [
"common/addressing",
"common/crypto",
"common/healthcheck",
"common/pemstore",
"common/topology",
"mixnode",
"nym-client",
+13
View File
@@ -0,0 +1,13 @@
[package]
name = "pemstore"
version = "0.1.0"
authors = ["Jedrzej Stuczynski <andrew@nymtech.net>"]
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"}
+15
View File
@@ -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<PathBuf> {
None
}
fn public_encryption_key(&self) -> Option<PathBuf> {
None
}
}
@@ -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<P: PathFinder>(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(),
}
}
+1
View File
@@ -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" }
+3 -3
View File
@@ -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();
+5 -4
View File
@@ -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());
+6 -4
View File
@@ -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());
+1
View File
@@ -0,0 +1 @@
pub mod persistance;
+1
View File
@@ -0,0 +1 @@
pub mod pathfinder;
@@ -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()
}
}
+1 -1
View File
@@ -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;