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

Feature/double init prevention #386

Merged
merged 6 commits into from
Oct 14, 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
14 changes: 7 additions & 7 deletions clients/client-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,31 +344,31 @@ impl<T: NymConfig> Default for Client<T> {

impl<T: NymConfig> Client<T> {
fn default_private_identity_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("private_identity.pem")
T::default_data_directory(id).join("private_identity.pem")
}

fn default_public_identity_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("public_identity.pem")
T::default_data_directory(id).join("public_identity.pem")
}

fn default_private_encryption_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("private_encryption.pem")
T::default_data_directory(id).join("private_encryption.pem")
}

fn default_public_encryption_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("public_encryption.pem")
T::default_data_directory(id).join("public_encryption.pem")
}

fn default_gateway_shared_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("gateway_shared.pem")
T::default_data_directory(id).join("gateway_shared.pem")
}

fn default_ack_key_file(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("ack_key.pem")
T::default_data_directory(id).join("ack_key.pem")
}

fn default_reply_encryption_key_store_path(id: &str) -> PathBuf {
T::default_data_directory(Some(id)).join("reply_key_store")
T::default_data_directory(id).join("reply_key_store")
}
}

Expand Down
4 changes: 0 additions & 4 deletions clients/native/src/client/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ impl NymConfig for Config {
config_template()
}

fn config_file_name() -> String {
"config.toml".to_string()
}

fn default_root_directory() -> PathBuf {
dirs::home_dir()
.expect("Failed to evaluate $HOME value")
Expand Down
8 changes: 8 additions & 0 deletions clients/native/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use gateway_requests::registration::handshake::SharedKeys;
use rand::rngs::OsRng;
use rand::seq::SliceRandom;
use std::convert::TryInto;
use std::process;
use std::sync::Arc;
use std::time::Duration;
use topology::{gateway, NymTopology};
Expand Down Expand Up @@ -131,7 +132,14 @@ pub fn execute(matches: &ArgMatches) {
println!("Initialising client...");

let id = matches.value_of("id").unwrap(); // required for now

if Config::default_config_file_path(id).exists() {
eprintln!("Client \"{}\" was already initialised before! If you wanted to upgrade your client to most recent version, try `upgrade` command instead!", id);
process::exit(1);
}

let mut config = Config::new(id);

let mut rng = OsRng;

// TODO: ideally that should be the last thing that's being done to config.
Expand Down
4 changes: 0 additions & 4 deletions clients/socks5/src/client/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ impl NymConfig for Config {
config_template()
}

fn config_file_name() -> String {
"config.toml".to_string()
}

fn default_root_directory() -> PathBuf {
dirs::home_dir()
.expect("Failed to evaluate $HOME value")
Expand Down
7 changes: 7 additions & 0 deletions clients/socks5/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use gateway_client::GatewayClient;
use gateway_requests::registration::handshake::SharedKeys;
use rand::{prelude::SliceRandom, rngs::OsRng};
use std::convert::TryInto;
use std::process;
use std::sync::Arc;
use std::time::Duration;
use topology::{gateway, NymTopology};
Expand Down Expand Up @@ -134,7 +135,13 @@ pub fn execute(matches: &ArgMatches) {
let id = matches.value_of("id").unwrap(); // required for now
let provider_address = matches.value_of("provider").unwrap();

if Config::default_config_file_path(id).exists() {
eprintln!("Socks5 client \"{}\" was already initialised before! If you wanted to upgrade your client to most recent version, try `upgrade` command instead!", id);
process::exit(1);
}

let mut config = Config::new(id, provider_address);

let mut rng = OsRng;

// TODO: ideally that should be the last thing that's being done to config.
Expand Down
34 changes: 23 additions & 11 deletions common/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ use std::{fs, io};
pub trait NymConfig: Default + Serialize + DeserializeOwned {
fn template() -> &'static str;

fn config_file_name() -> String;
fn config_file_name() -> String {
"config.toml".to_string()
}

fn default_root_directory() -> PathBuf;

// default, most probable, implementations; can be easily overridden where required
fn default_config_directory(id: Option<&str>) -> PathBuf {
Self::default_root_directory()
.join(id.unwrap_or(""))
.join("config")
fn default_config_directory(id: &str) -> PathBuf {
Self::default_root_directory().join(id).join("config")
}

fn default_data_directory(id: &str) -> PathBuf {
Self::default_root_directory().join(id).join("data")
}

fn default_data_directory(id: Option<&str>) -> PathBuf {
Self::default_root_directory()
.join(id.unwrap_or(""))
.join("data")
fn default_config_file_path(id: &str) -> PathBuf {
Self::default_config_directory(id).join(Self::config_file_name())
}

fn root_directory(&self) -> PathBuf;
Expand Down Expand Up @@ -66,10 +68,20 @@ pub trait NymConfig: Default + Serialize + DeserializeOwned {
)
}

// Hopefully should get simplified by https://github.com/nymtech/nym/issues/385
// so that `custom_location` could be completely removed
fn load_from_file(custom_location: Option<PathBuf>, id: Option<&str>) -> io::Result<Self> {
if custom_location.is_none() && id.is_none() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Both custom location and id are unspecified!",
));
}

// unwrap on id can't fail as we just checked whether at least one of custom location or id
// is not None
let config_contents = fs::read_to_string(
custom_location
.unwrap_or_else(|| Self::default_config_directory(id).join("config.toml")),
custom_location.unwrap_or_else(|| Self::default_config_file_path(id.unwrap())),
)?;

toml::from_str(&config_contents)
Expand Down
9 changes: 8 additions & 1 deletion gateway/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

use crate::commands::override_config;
use crate::config::persistence::pathfinder::GatewayPathfinder;
use crate::config::Config;
use clap::{App, Arg, ArgMatches};
use config::NymConfig;
use crypto::asymmetric::{encryption, identity};
use std::process;

pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
App::new("init")
Expand Down Expand Up @@ -116,7 +118,12 @@ pub fn execute(matches: &ArgMatches) {
let id = matches.value_of("id").unwrap();
println!("Initialising gateway {}...", id);

let mut config = crate::config::Config::new(id);
if Config::default_config_file_path(id).exists() {
eprintln!("Gateway \"{}\" was already initialised before! If you wanted to upgrade your gateway to most recent version, try `upgrade` command instead!", id);
process::exit(1);
}

let mut config = Config::new(id);

config = override_config(config, matches);

Expand Down
16 changes: 6 additions & 10 deletions gateway/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ impl NymConfig for Config {
config_template()
}

fn config_file_name() -> String {
"config.toml".to_string()
}

fn default_root_directory() -> PathBuf {
dirs::home_dir()
.expect("Failed to evaluate $HOME value")
Expand Down Expand Up @@ -441,19 +437,19 @@ pub struct Gateway {

impl Gateway {
fn default_private_sphinx_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("private_sphinx.pem")
Config::default_data_directory(id).join("private_sphinx.pem")
}

fn default_public_sphinx_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("public_sphinx.pem")
Config::default_data_directory(id).join("public_sphinx.pem")
}

fn default_private_identity_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("private_identity.pem")
Config::default_data_directory(id).join("private_identity.pem")
}

fn default_public_identity_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("public_identity.pem")
Config::default_data_directory(id).join("public_identity.pem")
}

fn default_location() -> String {
Expand Down Expand Up @@ -529,11 +525,11 @@ pub struct ClientsEndpoint {

impl ClientsEndpoint {
fn default_inboxes_directory(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("inboxes")
Config::default_data_directory(id).join("inboxes")
}

fn default_ledger_path(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("client_ledger.sled")
Config::default_data_directory(id).join("client_ledger.sled")
}
}

Expand Down
10 changes: 9 additions & 1 deletion mixnode/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

use crate::commands::override_config;
use crate::config::persistence::pathfinder::MixNodePathfinder;
use crate::config::Config;
use clap::{App, Arg, ArgMatches};
use config::NymConfig;
use crypto::asymmetric::encryption;
use directory_client::DirectoryClient;
use log::*;
use nymsphinx::params::DEFAULT_NUM_MIX_HOPS;
use std::convert::TryInto;
use std::process;
use tokio::runtime::Runtime;
use topology::NymTopology;

Expand Down Expand Up @@ -134,7 +136,13 @@ pub fn execute(matches: &ArgMatches) {
rt.block_on(async {
let id = matches.value_of("id").unwrap();
println!("Initialising mixnode {}...", id);
let mut config = crate::config::Config::new(id);

if Config::default_config_file_path(id).exists() {
eprintln!("Mixnode \"{}\" was already initialised before! If you wanted to upgrade your node to most recent version, try `upgrade` command instead!", id);
process::exit(1);
}

let mut config = Config::new(id);
config = override_config(config, matches);
let layer = choose_layer(matches, config.get_presence_directory_server()).await;
// TODO: I really don't like how we override config and are presumably done with it
Expand Down
8 changes: 2 additions & 6 deletions mixnode/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ impl NymConfig for Config {
config_template()
}

fn config_file_name() -> String {
"config.toml".to_string()
}

fn default_root_directory() -> PathBuf {
dirs::home_dir()
.expect("Failed to evaluate $HOME value")
Expand Down Expand Up @@ -314,11 +310,11 @@ pub struct MixNode {

impl MixNode {
fn default_private_sphinx_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("private_sphinx.pem")
Config::default_data_directory(id).join("private_sphinx.pem")
}

fn default_public_sphinx_key_file(id: &str) -> PathBuf {
Config::default_data_directory(Some(id)).join("public_sphinx.pem")
Config::default_data_directory(id).join("public_sphinx.pem")
}

fn default_location() -> String {
Expand Down