From e935801700c97c663c8060350ec25c800548cbbf Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Mon, 22 May 2023 14:56:47 +0200 Subject: [PATCH 01/16] [service/config] add base_dir and make config fields private favoring getters --- service/src/cli.yml | 15 ++++++--- service/src/config.rs | 61 +++++++++++++++++++++++++++--------- service/src/main.rs | 6 ++-- service/src/tests/commons.rs | 2 ++ 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/service/src/cli.yml b/service/src/cli.yml index 0b3f398fe4..8a7eeab74d 100644 --- a/service/src/cli.yml +++ b/service/src/cli.yml @@ -19,11 +19,16 @@ args: takes_value: true default_value: "ws://127.0.0.1" - node-port: - short: p - long: node-port - help: Set the websocket port to listen for substrate events - takes_value: true - default_value: "9944" + short: p + long: node-port + help: Set the websocket port to listen for substrate events + takes_value: true + default_value: "9944" + - base-dir: + short: d + long: base-dir + help: Base directory where the service operates. + takes_value: true - ws-external: long: ws-external help: Set this flag in case the worker should listen to external requests. diff --git a/service/src/config.rs b/service/src/config.rs index 51eb076c6f..14ec1133f5 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -19,7 +19,7 @@ use clap::ArgMatches; use itc_rest_client::rest_client::Url; use parse_duration::parse; use serde::{Deserialize, Serialize}; -use std::time::Duration; +use std::{path::PathBuf, time::Duration}; static DEFAULT_NODE_SERVER: &str = "ws://127.0.0.1"; static DEFAULT_NODE_PORT: &str = "9944"; @@ -31,29 +31,31 @@ static DEFAULT_UNTRUSTED_HTTP_PORT: &str = "4545"; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Config { - pub node_ip: String, - pub node_port: String, - pub worker_ip: String, + node_ip: String, + node_port: String, + worker_ip: String, /// Trusted worker address that will be advertised on the parentchain. - pub trusted_external_worker_address: Option, + trusted_external_worker_address: Option, /// Port to directly communicate with the trusted tls server inside the enclave. - pub trusted_worker_port: String, + trusted_worker_port: String, /// Untrusted worker address that will be returned by the dedicated trusted ws rpc call. - pub untrusted_external_worker_address: Option, + untrusted_external_worker_address: Option, /// Port to the untrusted ws of the validateer. - pub untrusted_worker_port: String, + untrusted_worker_port: String, /// Mutual remote attestation address that will be returned by the dedicated trusted ws rpc call. - pub mu_ra_external_address: Option, + mu_ra_external_address: Option, /// Port for mutual-remote attestation requests. - pub mu_ra_port: String, + mu_ra_port: String, /// Enable the metrics server - pub enable_metrics_server: bool, + enable_metrics_server: bool, /// Port for the metrics server - pub metrics_server_port: String, + metrics_server_port: String, /// Port for the untrusted HTTP server (e.g. for `is_initialized`) - pub untrusted_http_port: String, + untrusted_http_port: String, + /// Data directory used by all the services. + base_dir: PathBuf, /// Config of the 'run' subcommand - pub run_config: Option, + run_config: Option, } #[allow(clippy::too_many_arguments)] @@ -71,6 +73,7 @@ impl Config { enable_metrics_server: bool, metrics_server_port: String, untrusted_http_port: String, + base_dir: PathBuf, run_config: Option, ) -> Self { Self { @@ -86,6 +89,7 @@ impl Config { enable_metrics_server, metrics_server_port, untrusted_http_port, + base_dir, run_config, } } @@ -131,6 +135,18 @@ impl Config { } } + pub fn base_dir(&self) -> &PathBuf { + &self.base_dir + } + + pub fn run_config(&self) -> &Option { + &self.run_config + } + + pub fn enable_metrics_server(&self) -> bool { + self.enable_metrics_server + } + pub fn try_parse_metrics_server_port(&self) -> Option { self.metrics_server_port.parse::().ok() } @@ -149,6 +165,16 @@ impl From<&ArgMatches<'_>> for Config { let metrics_server_port = m.value_of("metrics-port").unwrap_or(DEFAULT_METRICS_PORT); let untrusted_http_port = m.value_of("untrusted-http-port").unwrap_or(DEFAULT_UNTRUSTED_HTTP_PORT); + + let base_dir = match m.value_of("base-dir") { + Some(d) => PathBuf::from(d), + None => { + log::warn!("[Config] defaulting to data-dir = PWD because it was previous behaviour. This might change soon.\ + Please pass the data-dir explicitly to ensure nothing breaks in your setup."); + pwd() + }, + }; + let run_config = m.subcommand_matches("run").map(RunConfig::from); Self::new( @@ -167,6 +193,7 @@ impl From<&ArgMatches<'_>> for Config { is_metrics_server_enabled, metrics_server_port.to_string(), untrusted_http_port.to_string(), + base_dir, run_config, ) } @@ -225,6 +252,10 @@ fn add_port_if_necessary(url: &str, port: &str) -> String { } } +pub fn pwd() -> PathBuf { + std::env::current_dir().expect("works on all supported platforms; qed.") +} + #[cfg(test)] mod test { use super::*; @@ -235,6 +266,7 @@ mod test { let empty_args = ArgMatches::default(); let config = Config::from(&empty_args); let expected_worker_ip = "127.0.0.1"; + let pwd = std::env::current_dir().unwrap().to_str().unwrap().to_string(); assert_eq!(config.node_ip, DEFAULT_NODE_SERVER); assert_eq!(config.node_port, DEFAULT_NODE_PORT); @@ -247,6 +279,7 @@ mod test { assert!(config.mu_ra_external_address.is_none()); assert!(!config.enable_metrics_server); assert_eq!(config.untrusted_http_port, DEFAULT_UNTRUSTED_HTTP_PORT); + assert_eq!(config.base_dir, pwd); assert!(config.run_config.is_none()); } diff --git a/service/src/main.rs b/service/src/main.rs index 5f6bef752b..a5acec5cb3 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -177,7 +177,7 @@ fn main() { enclave_metrics_receiver, ))); - if let Some(run_config) = &config.run_config { + if let Some(run_config) = config.run_config() { let shard = extract_shard(&run_config.shard, enclave.as_ref()); println!("Worker Config: {:?}", config); @@ -296,7 +296,7 @@ fn start_worker( InitializationHandler: TrackInitialization + IsInitialized + Sync + Send + 'static, WorkerModeProvider: ProvideWorkerMode, { - let run_config = config.run_config.clone().expect("Run config missing"); + let run_config = config.run_config().clone().expect("Run config missing"); let skip_ra = run_config.skip_ra; println!("Integritee Worker v{}", VERSION); @@ -356,7 +356,7 @@ fn start_worker( // ------------------------------------------------------------------------ // Start prometheus metrics server. - if config.enable_metrics_server { + if config.enable_metrics_server() { let enclave_wallet = Arc::new(EnclaveAccountInfoProvider::new(node_api.clone(), tee_accountid.clone())); let metrics_handler = Arc::new(MetricsHandler::new(enclave_wallet)); diff --git a/service/src/tests/commons.rs b/service/src/tests/commons.rs index 96ba10d595..c78ce4d949 100644 --- a/service/src/tests/commons.rs +++ b/service/src/tests/commons.rs @@ -36,6 +36,7 @@ pub fn local_worker_config( mu_ra_port: String, ) -> Config { let mut url = worker_url.split(':'); + Config::new( Default::default(), Default::default(), @@ -49,6 +50,7 @@ pub fn local_worker_config( false, "8787".to_string(), "4545".to_string(), + crate::config::pwd(), None, ) } From 61eae490902613378c0400689446e8afaa6e6c2f Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 23 May 2023 13:38:27 +0200 Subject: [PATCH 02/16] pass base_dir into the enclave --- core-primitives/enclave-api/ffi/src/lib.rs | 2 ++ .../enclave-api/src/enclave_base.rs | 17 +++++++++++++++-- enclave-runtime/Enclave.edl | 3 ++- enclave-runtime/src/lib.rs | 19 ++++++++++++++++++- service/src/enclave/api.rs | 16 ++++++++++++---- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core-primitives/enclave-api/ffi/src/lib.rs b/core-primitives/enclave-api/ffi/src/lib.rs index b4038c7094..fa00b22143 100644 --- a/core-primitives/enclave-api/ffi/src/lib.rs +++ b/core-primitives/enclave-api/ffi/src/lib.rs @@ -25,6 +25,8 @@ extern "C" { mu_ra_addr_size: u32, untrusted_worker_addr: *const u8, untrusted_worker_addr_size: u32, + encoded_base_dir_str: *const u8, + encoded_base_dir_size: u32, ) -> sgx_status_t; pub fn init_enclave_sidechain_components( diff --git a/core-primitives/enclave-api/src/enclave_base.rs b/core-primitives/enclave-api/src/enclave_base.rs index 389270ba4a..d6b67480bf 100644 --- a/core-primitives/enclave-api/src/enclave_base.rs +++ b/core-primitives/enclave-api/src/enclave_base.rs @@ -33,7 +33,12 @@ use sp_core::ed25519; /// Trait for base/common Enclave API functions pub trait EnclaveBase: Send + Sync + 'static { /// Initialize the enclave (needs to be called once at application startup). - fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()>; + fn init( + &self, + mu_ra_addr: &str, + untrusted_worker_addr: &str, + base_dir: &str, + ) -> EnclaveResult<()>; /// Initialize the enclave sidechain components. fn init_enclave_sidechain_components(&self) -> EnclaveResult<()>; @@ -67,11 +72,17 @@ pub trait EnclaveBase: Send + Sync + 'static { /// EnclaveApi implementation for Enclave struct impl EnclaveBase for Enclave { - fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()> { + fn init( + &self, + mu_ra_addr: &str, + untrusted_worker_addr: &str, + base_dir: &str, + ) -> EnclaveResult<()> { let mut retval = sgx_status_t::SGX_SUCCESS; let encoded_mu_ra_addr = mu_ra_addr.encode(); let encoded_untrusted_worker_addr = untrusted_worker_addr.encode(); + let encoded_base_dir = base_dir.encode(); let result = unsafe { ffi::init( @@ -81,6 +92,8 @@ impl EnclaveBase for Enclave { encoded_mu_ra_addr.len() as u32, encoded_untrusted_worker_addr.as_ptr(), encoded_untrusted_worker_addr.len() as u32, + encoded_base_dir.as_ptr(), + encoded_base_dir.len() as u32, ) }; diff --git a/enclave-runtime/Enclave.edl b/enclave-runtime/Enclave.edl index 625bb1e5fb..97737438c3 100644 --- a/enclave-runtime/Enclave.edl +++ b/enclave-runtime/Enclave.edl @@ -39,7 +39,8 @@ enclave { /* define ECALLs here. */ public sgx_status_t init( [in, size=mu_ra_addr_size] uint8_t* mu_ra_addr, uint32_t mu_ra_addr_size, - [in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size + [in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size, + [in, size=encoded_base_dir_size] uint8_t* encoded_base_dir_str, uint32_t encoded_base_dir_size ); public sgx_status_t init_enclave_sidechain_components(); diff --git a/enclave-runtime/src/lib.rs b/enclave-runtime/src/lib.rs index 809383c5ac..e0e91111cf 100644 --- a/enclave-runtime/src/lib.rs +++ b/enclave-runtime/src/lib.rs @@ -106,6 +106,8 @@ pub unsafe extern "C" fn init( mu_ra_addr_size: u32, untrusted_worker_addr: *const u8, untrusted_worker_addr_size: u32, + encoded_base_dir_str: *const u8, + encoded_base_dir_size: u32, ) -> sgx_status_t { // Initialize the logging environment in the enclave. env_logger::init(); @@ -137,7 +139,22 @@ pub unsafe extern "C" fn init( Err(e) => return e.into(), }; - match initialization::init_enclave(mu_ra_url, untrusted_worker_url, pwd) { + let base_dir = match String::decode(&mut slice::from_raw_parts( + encoded_base_dir_str, + encoded_base_dir_size as usize, + )) + .map_err(Error::Codec) + { + Ok(b) => b, + Err(e) => return e.into(), + }; + + info!("Setting base_dir to {}", base_dir); + BASE_PATH + .set(PathBuf::from(base_dir)) + .expect("We only init this once here; qed."); + + match initialization::init_enclave(mu_ra_url, untrusted_worker_url, base_dir) { Err(e) => e.into(), Ok(()) => sgx_status_t::SGX_SUCCESS, } diff --git a/service/src/enclave/api.rs b/service/src/enclave/api.rs index 48a900c89b..f3eba370e6 100644 --- a/service/src/enclave/api.rs +++ b/service/src/enclave/api.rs @@ -15,6 +15,8 @@ */ +//! keep this api free from chain-specific types! + use crate::config::Config; use itp_enclave_api::{ enclave_base::EnclaveBase, error::Error as EnclaveApiError, Enclave, EnclaveResult, @@ -23,9 +25,11 @@ use itp_settings::files::{ENCLAVE_FILE, ENCLAVE_TOKEN}; use log::*; use sgx_types::*; use sgx_urts::SgxEnclave; -/// keep this api free from chain-specific types! -use std::io::{Read, Write}; -use std::{fs::File, path::PathBuf}; +use std::{ + fs::File, + io::{Read, Write}, + path::PathBuf, +}; pub fn enclave_init(config: &Config) -> EnclaveResult { const LEN: usize = 1024; @@ -103,7 +107,11 @@ pub fn enclave_init(config: &Config) -> EnclaveResult { // create an enclave API and initialize it let enclave_api = Enclave::new(enclave); - enclave_api.init(&config.mu_ra_url_external(), &config.untrusted_worker_url_external())?; + enclave_api.init( + &config.mu_ra_url_external(), + &config.untrusted_worker_url_external(), + &config.base_dir().display().to_string(), + )?; Ok(enclave_api) } From ba6d51a49c0b45d241909d77b32c83f86427bc5b Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 23 May 2023 14:13:13 +0200 Subject: [PATCH 03/16] [enclave-runtime] set the base_path --- enclave-runtime/src/lib.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/enclave-runtime/src/lib.rs b/enclave-runtime/src/lib.rs index e0e91111cf..752668fca9 100644 --- a/enclave-runtime/src/lib.rs +++ b/enclave-runtime/src/lib.rs @@ -109,18 +109,6 @@ pub unsafe extern "C" fn init( encoded_base_dir_str: *const u8, encoded_base_dir_size: u32, ) -> sgx_status_t { - // Initialize the logging environment in the enclave. - env_logger::init(); - - // Todo: This will be changed to be a param of the `init` ecall: - // https://github.com/integritee-network/worker/issues/1292 - // - // Until the above task is finished, we just fall back to the - // static behaviour, which uses the PWD already. - let pwd = std::env::current_dir().expect("Works on all supported platforms; qed"); - info!("Setting base_dir to pwd: {}", pwd.display()); - BASE_PATH.set(pwd.clone()).expect("We only init this once here; qed."); - let mu_ra_url = match String::decode(&mut slice::from_raw_parts(mu_ra_addr, mu_ra_addr_size as usize)) .map_err(Error::Codec) @@ -150,11 +138,10 @@ pub unsafe extern "C" fn init( }; info!("Setting base_dir to {}", base_dir); - BASE_PATH - .set(PathBuf::from(base_dir)) - .expect("We only init this once here; qed."); + let path = PathBuf::from(base_dir); + BASE_PATH.set(path.clone()).expect("We only init this once here; qed."); - match initialization::init_enclave(mu_ra_url, untrusted_worker_url, base_dir) { + match initialization::init_enclave(mu_ra_url, untrusted_worker_url, path) { Err(e) => e.into(), Ok(()) => sgx_status_t::SGX_SUCCESS, } From 6e1200a16faff3e09a11027ae88d481d317192b5 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 23 May 2023 15:23:11 +0200 Subject: [PATCH 04/16] [service/config] use `pwd()` instead of `std::env::current_dir()` --- service/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/config.rs b/service/src/config.rs index 14ec1133f5..c82244215f 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -266,7 +266,7 @@ mod test { let empty_args = ArgMatches::default(); let config = Config::from(&empty_args); let expected_worker_ip = "127.0.0.1"; - let pwd = std::env::current_dir().unwrap().to_str().unwrap().to_string(); + let pwd = pwd().to_str().unwrap().to_string(); assert_eq!(config.node_ip, DEFAULT_NODE_SERVER); assert_eq!(config.node_port, DEFAULT_NODE_PORT); From fef000552b2dac41054531b88222e4cb770e88f5 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 30 May 2023 17:08:05 +0200 Subject: [PATCH 05/16] [service] rename base-dir to data-dir in the cli --- local-setup/config/one-worker.json | 4 +++- local-setup/config/two-workers.json | 8 ++++++-- service/src/cli.yml | 6 +++--- service/src/config.rs | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/local-setup/config/one-worker.json b/local-setup/config/one-worker.json index 1119abbaaa..477c9c85d3 100644 --- a/local-setup/config/one-worker.json +++ b/local-setup/config/one-worker.json @@ -30,7 +30,9 @@ "2001", "-h", "4545", - "--ws-external" + "--ws-external", + "--data-dir", + "/tmp/data-dir" ], "subcommand_flags": [ "--skip-ra", diff --git a/local-setup/config/two-workers.json b/local-setup/config/two-workers.json index daf7b05677..51acefce9b 100644 --- a/local-setup/config/two-workers.json +++ b/local-setup/config/two-workers.json @@ -30,7 +30,9 @@ "2001", "-h", "4545", - "--ws-external" + "--ws-external", + "--data-dir", + "/tmp/data-dir" ], "subcommand_flags": [ "--skip-ra", @@ -51,7 +53,9 @@ "3001", "-h", "4546", - "--ws-external" + "--ws-external", + "--data-dir", + "/tmp/data-dir" ], "subcommand_flags": [ "--skip-ra", diff --git a/service/src/cli.yml b/service/src/cli.yml index 8a7eeab74d..800c5647a8 100644 --- a/service/src/cli.yml +++ b/service/src/cli.yml @@ -24,10 +24,10 @@ args: help: Set the websocket port to listen for substrate events takes_value: true default_value: "9944" - - base-dir: + - data-dir: short: d - long: base-dir - help: Base directory where the service operates. + long: data-dir + help: Data dir where the worker stores it's keys and other data. takes_value: true - ws-external: long: ws-external diff --git a/service/src/config.rs b/service/src/config.rs index c82244215f..3d8e2c4fde 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -166,7 +166,7 @@ impl From<&ArgMatches<'_>> for Config { let untrusted_http_port = m.value_of("untrusted-http-port").unwrap_or(DEFAULT_UNTRUSTED_HTTP_PORT); - let base_dir = match m.value_of("base-dir") { + let base_dir = match m.value_of("data-dir") { Some(d) => PathBuf::from(d), None => { log::warn!("[Config] defaulting to data-dir = PWD because it was previous behaviour. This might change soon.\ From d962665d3be477cfb4942c4282a55ce67e738842 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 30 May 2023 17:23:38 +0200 Subject: [PATCH 06/16] [enclave-runtime] fix rebase error: re-add env logger init in enclave --- enclave-runtime/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/enclave-runtime/src/lib.rs b/enclave-runtime/src/lib.rs index 752668fca9..750b05b93b 100644 --- a/enclave-runtime/src/lib.rs +++ b/enclave-runtime/src/lib.rs @@ -109,6 +109,9 @@ pub unsafe extern "C" fn init( encoded_base_dir_str: *const u8, encoded_base_dir_size: u32, ) -> sgx_status_t { + // Initialize the logging environment in the enclave. + env_logger::init(); + let mu_ra_url = match String::decode(&mut slice::from_raw_parts(mu_ra_addr, mu_ra_addr_size as usize)) .map_err(Error::Codec) From 32cc5d5452d4925e64f2414b85f5f9f8f4d65ba8 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 30 May 2023 17:32:51 +0200 Subject: [PATCH 07/16] [enclave-runtime] create data-dir if it does not exist --- service/src/config.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/service/src/config.rs b/service/src/config.rs index 3d8e2c4fde..4728e5b7be 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -19,7 +19,7 @@ use clap::ArgMatches; use itc_rest_client::rest_client::Url; use parse_duration::parse; use serde::{Deserialize, Serialize}; -use std::{path::PathBuf, time::Duration}; +use std::{fs, path::PathBuf, time::Duration}; static DEFAULT_NODE_SERVER: &str = "ws://127.0.0.1"; static DEFAULT_NODE_PORT: &str = "9944"; @@ -167,7 +167,16 @@ impl From<&ArgMatches<'_>> for Config { m.value_of("untrusted-http-port").unwrap_or(DEFAULT_UNTRUSTED_HTTP_PORT); let base_dir = match m.value_of("data-dir") { - Some(d) => PathBuf::from(d), + Some(d) => { + let p = PathBuf::from(d); + if !p.exists() { + log::info!("Creating new data-directory for the service {}.", p.display()); + fs::create_dir_all(p.as_path()).unwrap(); + } else { + log::info!("Starting service in existing directory {}.", p.display()); + } + p + }, None => { log::warn!("[Config] defaulting to data-dir = PWD because it was previous behaviour. This might change soon.\ Please pass the data-dir explicitly to ensure nothing breaks in your setup."); From 366834bd198cef008232349b234b628823e1c196 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 30 May 2023 17:48:19 +0200 Subject: [PATCH 08/16] [service/config] rename base_dir -> data_dir and return a Path instead of a PathBuf --- service/src/config.rs | 16 ++++++++++------ service/src/enclave/api.rs | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/service/src/config.rs b/service/src/config.rs index 4728e5b7be..9c4f8c6b39 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -19,7 +19,11 @@ use clap::ArgMatches; use itc_rest_client::rest_client::Url; use parse_duration::parse; use serde::{Deserialize, Serialize}; -use std::{fs, path::PathBuf, time::Duration}; +use std::{ + fs, + path::{Path, PathBuf}, + time::Duration, +}; static DEFAULT_NODE_SERVER: &str = "ws://127.0.0.1"; static DEFAULT_NODE_PORT: &str = "9944"; @@ -53,7 +57,7 @@ pub struct Config { /// Port for the untrusted HTTP server (e.g. for `is_initialized`) untrusted_http_port: String, /// Data directory used by all the services. - base_dir: PathBuf, + data_dir: PathBuf, /// Config of the 'run' subcommand run_config: Option, } @@ -89,7 +93,7 @@ impl Config { enable_metrics_server, metrics_server_port, untrusted_http_port, - base_dir, + data_dir: base_dir, run_config, } } @@ -135,8 +139,8 @@ impl Config { } } - pub fn base_dir(&self) -> &PathBuf { - &self.base_dir + pub fn data_dir(&self) -> &Path { + self.data_dir.as_path() } pub fn run_config(&self) -> &Option { @@ -288,7 +292,7 @@ mod test { assert!(config.mu_ra_external_address.is_none()); assert!(!config.enable_metrics_server); assert_eq!(config.untrusted_http_port, DEFAULT_UNTRUSTED_HTTP_PORT); - assert_eq!(config.base_dir, pwd); + assert_eq!(config.data_dir, pwd); assert!(config.run_config.is_none()); } diff --git a/service/src/enclave/api.rs b/service/src/enclave/api.rs index f3eba370e6..a80457449f 100644 --- a/service/src/enclave/api.rs +++ b/service/src/enclave/api.rs @@ -110,7 +110,7 @@ pub fn enclave_init(config: &Config) -> EnclaveResult { enclave_api.init( &config.mu_ra_url_external(), &config.untrusted_worker_url_external(), - &config.base_dir().display().to_string(), + &config.data_dir().display().to_string(), )?; Ok(enclave_api) From eb397e1dc15694b6d8369de7efa181a762712194 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 30 May 2023 17:49:13 +0200 Subject: [PATCH 09/16] [service] use data-dir instead of pwd for the sidechain storage --- service/src/main.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/service/src/main.rs b/service/src/main.rs index a5acec5cb3..27b196f297 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -117,13 +117,6 @@ fn main() { let yml = load_yaml!("cli.yml"); let matches = App::from_yaml(yml).get_matches(); - // Todo: This will be changed to be a param of the CLI: - // https://github.com/integritee-network/worker/issues/1292 - // - // Until the above task is finished, we just fall back to the - // static behaviour, which uses the PWD already. - let pwd = std::env::current_dir().expect("Works on all supported platforms; qed"); - let config = Config::from(&matches); GlobalTokioHandle::initialize(); @@ -144,8 +137,12 @@ fn main() { // build the entire dependency tree let tokio_handle = Arc::new(GlobalTokioHandle {}); - let sidechain_blockstorage = - Arc::new(SidechainStorageLock::::from_base_path(pwd).unwrap()); + let sidechain_blockstorage = Arc::new( + SidechainStorageLock::::from_base_path( + config.data_dir().to_path_buf(), + ) + .unwrap(), + ); let node_api_factory = Arc::new(NodeApiFactory::new(config.node_url(), AccountKeyring::Alice.pair())); let enclave = Arc::new(enclave_init(&config).unwrap()); From 6c24c937e312da3b42690834f3996f41310eaa2b Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 13:24:44 +0200 Subject: [PATCH 10/16] [service/config] rename forgotten base_dir's to data_dir --- service/src/config.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service/src/config.rs b/service/src/config.rs index 9c4f8c6b39..9c22901c3b 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -77,7 +77,7 @@ impl Config { enable_metrics_server: bool, metrics_server_port: String, untrusted_http_port: String, - base_dir: PathBuf, + data_dir: PathBuf, run_config: Option, ) -> Self { Self { @@ -93,7 +93,7 @@ impl Config { enable_metrics_server, metrics_server_port, untrusted_http_port, - data_dir: base_dir, + data_dir, run_config, } } @@ -170,7 +170,7 @@ impl From<&ArgMatches<'_>> for Config { let untrusted_http_port = m.value_of("untrusted-http-port").unwrap_or(DEFAULT_UNTRUSTED_HTTP_PORT); - let base_dir = match m.value_of("data-dir") { + let data_dir = match m.value_of("data-dir") { Some(d) => { let p = PathBuf::from(d); if !p.exists() { @@ -206,7 +206,7 @@ impl From<&ArgMatches<'_>> for Config { is_metrics_server_enabled, metrics_server_port.to_string(), untrusted_http_port.to_string(), - base_dir, + data_dir, run_config, ) } From 00813e66972b560835ef2c5a684f19adc7c1a395 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 13:26:35 +0200 Subject: [PATCH 11/16] [service/enclave] fix wrongly copied documentation --- service/src/enclave/api.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/service/src/enclave/api.rs b/service/src/enclave/api.rs index a80457449f..860fd3e0bb 100644 --- a/service/src/enclave/api.rs +++ b/service/src/enclave/api.rs @@ -15,8 +15,6 @@ */ -//! keep this api free from chain-specific types! - use crate::config::Config; use itp_enclave_api::{ enclave_base::EnclaveBase, error::Error as EnclaveApiError, Enclave, EnclaveResult, From a3e837f2db00bb4678506afc5b6b1c7eb77084e6 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 16:23:04 +0200 Subject: [PATCH 12/16] [integritee-service] fix: use correct path for purging files --- service/src/main.rs | 2 +- service/src/setup.rs | 5 ++--- service/src/tests/mod.rs | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/service/src/main.rs b/service/src/main.rs index 27b196f297..79f04df54f 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -132,7 +132,7 @@ fn main() { let clean_reset = matches.is_present("clean-reset"); if clean_reset { - setup::purge_files_from_cwd().unwrap(); + setup::purge_files_from_dir(config.data_dir()).unwrap(); } // build the entire dependency tree diff --git a/service/src/setup.rs b/service/src/setup.rs index 202d0ed339..9291ebc206 100644 --- a/service/src/setup.rs +++ b/service/src/setup.rs @@ -27,12 +27,11 @@ use log::*; use std::{fs, fs::File, path::Path}; /// Purge all worker files from the current working directory (cwd). -pub(crate) fn purge_files_from_cwd() -> ServiceResult<()> { - let current_directory = std::env::current_dir().map_err(|e| Error::Custom(e.into()))?; +pub(crate) fn purge_files_from_dir(dir: &Path) -> ServiceResult<()> { println!("[+] Performing a clean reset of the worker"); println!("[+] Purge all files from previous runs"); - purge_files(¤t_directory)?; + purge_files(dir)?; Ok(()) } diff --git a/service/src/tests/mod.rs b/service/src/tests/mod.rs index 72cc066bb6..f2c032aefc 100644 --- a/service/src/tests/mod.rs +++ b/service/src/tests/mod.rs @@ -31,7 +31,7 @@ pub mod parentchain_handler_test; pub fn run_enclave_tests(matches: &ArgMatches) { println!("*** Starting Test enclave"); let config = Config::from(matches); - setup::purge_files_from_cwd().unwrap(); + setup::purge_files_from_dir(&config.data_dir()).unwrap(); let enclave = enclave_init(&config).unwrap(); if matches.is_present("all") || matches.is_present("unit") { From 5e908cc9ba7d80478d595c23d4d9ee4e8b19bbd9 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 16:26:21 +0200 Subject: [PATCH 13/16] [integritee-service] fix: unnecessary reference --- service/src/tests/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/tests/mod.rs b/service/src/tests/mod.rs index f2c032aefc..18c4763589 100644 --- a/service/src/tests/mod.rs +++ b/service/src/tests/mod.rs @@ -31,7 +31,7 @@ pub mod parentchain_handler_test; pub fn run_enclave_tests(matches: &ArgMatches) { println!("*** Starting Test enclave"); let config = Config::from(matches); - setup::purge_files_from_dir(&config.data_dir()).unwrap(); + setup::purge_files_from_dir(config.data_dir()).unwrap(); let enclave = enclave_init(&config).unwrap(); if matches.is_present("all") || matches.is_present("unit") { From 2e19e8a4b0677188c826ae9bf5e07c093fec1cd0 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 16:27:59 +0200 Subject: [PATCH 14/16] [integritee-service] fix: documentation --- service/src/setup.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/setup.rs b/service/src/setup.rs index 9291ebc206..bdf8fcce14 100644 --- a/service/src/setup.rs +++ b/service/src/setup.rs @@ -26,7 +26,7 @@ use itp_types::ShardIdentifier; use log::*; use std::{fs, fs::File, path::Path}; -/// Purge all worker files from the current working directory (cwd). +/// Purge all worker files from `dir`. pub(crate) fn purge_files_from_dir(dir: &Path) -> ServiceResult<()> { println!("[+] Performing a clean reset of the worker"); From aba918100900775a39f3beb008e07d0973cd4988 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 16:54:13 +0200 Subject: [PATCH 15/16] [integritee-service] fix tests --- service/src/config.rs | 3 +-- service/src/tests/mocks/enclave_api_mock.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/service/src/config.rs b/service/src/config.rs index 9c22901c3b..c16ccd63ac 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -279,7 +279,6 @@ mod test { let empty_args = ArgMatches::default(); let config = Config::from(&empty_args); let expected_worker_ip = "127.0.0.1"; - let pwd = pwd().to_str().unwrap().to_string(); assert_eq!(config.node_ip, DEFAULT_NODE_SERVER); assert_eq!(config.node_port, DEFAULT_NODE_PORT); @@ -292,7 +291,7 @@ mod test { assert!(config.mu_ra_external_address.is_none()); assert!(!config.enable_metrics_server); assert_eq!(config.untrusted_http_port, DEFAULT_UNTRUSTED_HTTP_PORT); - assert_eq!(config.data_dir, pwd); + assert_eq!(config.data_dir, pwd()); assert!(config.run_config.is_none()); } diff --git a/service/src/tests/mocks/enclave_api_mock.rs b/service/src/tests/mocks/enclave_api_mock.rs index b1a0ca2209..f32176332c 100644 --- a/service/src/tests/mocks/enclave_api_mock.rs +++ b/service/src/tests/mocks/enclave_api_mock.rs @@ -32,7 +32,7 @@ use sp_core::ed25519; pub struct EnclaveMock; impl EnclaveBase for EnclaveMock { - fn init(&self, _mu_ra_url: &str, _untrusted_url: &str) -> EnclaveResult<()> { + fn init(&self, _mu_ra_url: &str, _untrusted_url: &str, _base_dir: &str) -> EnclaveResult<()> { Ok(()) } From e707d14a13b764f8e5f4bd6dcaf3361eeef68703 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 31 May 2023 16:57:17 +0200 Subject: [PATCH 16/16] [integritee-service] remove warning in tests --- cli/tests/basic_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/tests/basic_tests.rs b/cli/tests/basic_tests.rs index 408e0717eb..2604312510 100644 --- a/cli/tests/basic_tests.rs +++ b/cli/tests/basic_tests.rs @@ -2,7 +2,7 @@ use clap::Parser; use integritee_cli::Cli; fn init() { - env_logger::try_init(); + let _ = env_logger::try_init(); } #[test]