Feature/double init prevention (#386)

* Updated NymConfig trait making id always obligatory. Also added extra default method implementations

* Preventing init on mixnodes that were initialised before

* Quotes before mix id

* Preventing init on gateways that were initialised before

* Preventing init on native clients that were initialised before

* Preventing init on socks5 clients that were initialised before
This commit is contained in:
Jędrzej Stuczyński
2020-10-14 13:30:57 +01:00
committed by GitHub
parent 2f7b3eec08
commit d940bc8f31
10 changed files with 70 additions and 44 deletions
+7 -7
View File
@@ -360,31 +360,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")
}
}
-4
View File
@@ -55,10 +55,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")
+8
View File
@@ -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};
@@ -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.
-4
View File
@@ -38,10 +38,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")
+7
View File
@@ -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};
@@ -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.
+23 -11
View File
@@ -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: Option<&str>) -> PathBuf {
Self::default_root_directory()
.join(id.unwrap_or(""))
.join("data")
fn default_data_directory(id: &str) -> PathBuf {
Self::default_root_directory().join(id).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;
@@ -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)
+8 -1
View File
@@ -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")
@@ -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);
+6 -10
View File
@@ -62,10 +62,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")
@@ -457,19 +453,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 {
@@ -545,11 +541,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")
}
}
+9 -1
View File
@@ -14,6 +14,7 @@
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, identity};
@@ -21,6 +22,7 @@ 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;
@@ -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
+2 -6
View File
@@ -56,10 +56,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")
@@ -377,11 +373,11 @@ 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 {