Skip to content

Commit 0b09588

Browse files
authored
Introduce CLI for a configurable data-dir (#1331)
* [service/config] add base_dir and make config fields private favoring getters * pass base_dir into the enclave * [enclave-runtime] set the base_path * [service/config] use `pwd()` instead of `std::env::current_dir()` * [service] rename base-dir to data-dir in the cli * [enclave-runtime] fix rebase error: re-add env logger init in enclave * [enclave-runtime] create data-dir if it does not exist * [service/config] rename base_dir -> data_dir and return a Path instead of a PathBuf * [service] use data-dir instead of pwd for the sidechain storage * [service/config] rename forgotten base_dir's to data_dir * [service/enclave] fix wrongly copied documentation * [integritee-service] fix: use correct path for purging files * [integritee-service] fix: unnecessary reference * [integritee-service] fix: documentation * [integritee-service] fix tests * [integritee-service] remove warning in tests
1 parent 611cfcb commit 0b09588

File tree

15 files changed

+142
-59
lines changed

15 files changed

+142
-59
lines changed

cli/tests/basic_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clap::Parser;
22
use integritee_cli::Cli;
33

44
fn init() {
5-
env_logger::try_init();
5+
let _ = env_logger::try_init();
66
}
77

88
#[test]

core-primitives/enclave-api/ffi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extern "C" {
2525
mu_ra_addr_size: u32,
2626
untrusted_worker_addr: *const u8,
2727
untrusted_worker_addr_size: u32,
28+
encoded_base_dir_str: *const u8,
29+
encoded_base_dir_size: u32,
2830
) -> sgx_status_t;
2931

3032
pub fn init_enclave_sidechain_components(

core-primitives/enclave-api/src/enclave_base.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ use sp_core::ed25519;
3333
/// Trait for base/common Enclave API functions
3434
pub trait EnclaveBase: Send + Sync + 'static {
3535
/// Initialize the enclave (needs to be called once at application startup).
36-
fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()>;
36+
fn init(
37+
&self,
38+
mu_ra_addr: &str,
39+
untrusted_worker_addr: &str,
40+
base_dir: &str,
41+
) -> EnclaveResult<()>;
3742

3843
/// Initialize the enclave sidechain components.
3944
fn init_enclave_sidechain_components(&self) -> EnclaveResult<()>;
@@ -67,11 +72,17 @@ pub trait EnclaveBase: Send + Sync + 'static {
6772

6873
/// EnclaveApi implementation for Enclave struct
6974
impl EnclaveBase for Enclave {
70-
fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()> {
75+
fn init(
76+
&self,
77+
mu_ra_addr: &str,
78+
untrusted_worker_addr: &str,
79+
base_dir: &str,
80+
) -> EnclaveResult<()> {
7181
let mut retval = sgx_status_t::SGX_SUCCESS;
7282

7383
let encoded_mu_ra_addr = mu_ra_addr.encode();
7484
let encoded_untrusted_worker_addr = untrusted_worker_addr.encode();
85+
let encoded_base_dir = base_dir.encode();
7586

7687
let result = unsafe {
7788
ffi::init(
@@ -81,6 +92,8 @@ impl EnclaveBase for Enclave {
8192
encoded_mu_ra_addr.len() as u32,
8293
encoded_untrusted_worker_addr.as_ptr(),
8394
encoded_untrusted_worker_addr.len() as u32,
95+
encoded_base_dir.as_ptr(),
96+
encoded_base_dir.len() as u32,
8497
)
8598
};
8699

enclave-runtime/Enclave.edl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ enclave {
3939
/* define ECALLs here. */
4040
public sgx_status_t init(
4141
[in, size=mu_ra_addr_size] uint8_t* mu_ra_addr, uint32_t mu_ra_addr_size,
42-
[in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size
42+
[in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size,
43+
[in, size=encoded_base_dir_size] uint8_t* encoded_base_dir_str, uint32_t encoded_base_dir_size
4344
);
4445

4546
public sgx_status_t init_enclave_sidechain_components();

enclave-runtime/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,12 @@ pub unsafe extern "C" fn init(
106106
mu_ra_addr_size: u32,
107107
untrusted_worker_addr: *const u8,
108108
untrusted_worker_addr_size: u32,
109+
encoded_base_dir_str: *const u8,
110+
encoded_base_dir_size: u32,
109111
) -> sgx_status_t {
110112
// Initialize the logging environment in the enclave.
111113
env_logger::init();
112114

113-
// Todo: This will be changed to be a param of the `init` ecall:
114-
// https://github.com/integritee-network/worker/issues/1292
115-
//
116-
// Until the above task is finished, we just fall back to the
117-
// static behaviour, which uses the PWD already.
118-
let pwd = std::env::current_dir().expect("Works on all supported platforms; qed");
119-
info!("Setting base_dir to pwd: {}", pwd.display());
120-
BASE_PATH.set(pwd.clone()).expect("We only init this once here; qed.");
121-
122115
let mu_ra_url =
123116
match String::decode(&mut slice::from_raw_parts(mu_ra_addr, mu_ra_addr_size as usize))
124117
.map_err(Error::Codec)
@@ -137,7 +130,21 @@ pub unsafe extern "C" fn init(
137130
Err(e) => return e.into(),
138131
};
139132

140-
match initialization::init_enclave(mu_ra_url, untrusted_worker_url, pwd) {
133+
let base_dir = match String::decode(&mut slice::from_raw_parts(
134+
encoded_base_dir_str,
135+
encoded_base_dir_size as usize,
136+
))
137+
.map_err(Error::Codec)
138+
{
139+
Ok(b) => b,
140+
Err(e) => return e.into(),
141+
};
142+
143+
info!("Setting base_dir to {}", base_dir);
144+
let path = PathBuf::from(base_dir);
145+
BASE_PATH.set(path.clone()).expect("We only init this once here; qed.");
146+
147+
match initialization::init_enclave(mu_ra_url, untrusted_worker_url, path) {
141148
Err(e) => e.into(),
142149
Ok(()) => sgx_status_t::SGX_SUCCESS,
143150
}

local-setup/config/one-worker.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"2001",
3131
"-h",
3232
"4545",
33-
"--ws-external"
33+
"--ws-external",
34+
"--data-dir",
35+
"/tmp/data-dir"
3436
],
3537
"subcommand_flags": [
3638
"--skip-ra",

local-setup/config/two-workers.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"2001",
3131
"-h",
3232
"4545",
33-
"--ws-external"
33+
"--ws-external",
34+
"--data-dir",
35+
"/tmp/data-dir"
3436
],
3537
"subcommand_flags": [
3638
"--skip-ra",
@@ -51,7 +53,9 @@
5153
"3001",
5254
"-h",
5355
"4546",
54-
"--ws-external"
56+
"--ws-external",
57+
"--data-dir",
58+
"/tmp/data-dir"
5559
],
5660
"subcommand_flags": [
5761
"--skip-ra",

service/src/cli.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ args:
1919
takes_value: true
2020
default_value: "ws://127.0.0.1"
2121
- node-port:
22-
short: p
23-
long: node-port
24-
help: Set the websocket port to listen for substrate events
25-
takes_value: true
26-
default_value: "9944"
22+
short: p
23+
long: node-port
24+
help: Set the websocket port to listen for substrate events
25+
takes_value: true
26+
default_value: "9944"
27+
- data-dir:
28+
short: d
29+
long: data-dir
30+
help: Data dir where the worker stores it's keys and other data.
31+
takes_value: true
2732
- ws-external:
2833
long: ws-external
2934
help: Set this flag in case the worker should listen to external requests.

service/src/config.rs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ use clap::ArgMatches;
1919
use itc_rest_client::rest_client::Url;
2020
use parse_duration::parse;
2121
use serde::{Deserialize, Serialize};
22-
use std::time::Duration;
22+
use std::{
23+
fs,
24+
path::{Path, PathBuf},
25+
time::Duration,
26+
};
2327

2428
static DEFAULT_NODE_SERVER: &str = "ws://127.0.0.1";
2529
static DEFAULT_NODE_PORT: &str = "9944";
@@ -31,29 +35,31 @@ static DEFAULT_UNTRUSTED_HTTP_PORT: &str = "4545";
3135

3236
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
3337
pub struct Config {
34-
pub node_ip: String,
35-
pub node_port: String,
36-
pub worker_ip: String,
38+
node_ip: String,
39+
node_port: String,
40+
worker_ip: String,
3741
/// Trusted worker address that will be advertised on the parentchain.
38-
pub trusted_external_worker_address: Option<String>,
42+
trusted_external_worker_address: Option<String>,
3943
/// Port to directly communicate with the trusted tls server inside the enclave.
40-
pub trusted_worker_port: String,
44+
trusted_worker_port: String,
4145
/// Untrusted worker address that will be returned by the dedicated trusted ws rpc call.
42-
pub untrusted_external_worker_address: Option<String>,
46+
untrusted_external_worker_address: Option<String>,
4347
/// Port to the untrusted ws of the validateer.
44-
pub untrusted_worker_port: String,
48+
untrusted_worker_port: String,
4549
/// Mutual remote attestation address that will be returned by the dedicated trusted ws rpc call.
46-
pub mu_ra_external_address: Option<String>,
50+
mu_ra_external_address: Option<String>,
4751
/// Port for mutual-remote attestation requests.
48-
pub mu_ra_port: String,
52+
mu_ra_port: String,
4953
/// Enable the metrics server
50-
pub enable_metrics_server: bool,
54+
enable_metrics_server: bool,
5155
/// Port for the metrics server
52-
pub metrics_server_port: String,
56+
metrics_server_port: String,
5357
/// Port for the untrusted HTTP server (e.g. for `is_initialized`)
54-
pub untrusted_http_port: String,
58+
untrusted_http_port: String,
59+
/// Data directory used by all the services.
60+
data_dir: PathBuf,
5561
/// Config of the 'run' subcommand
56-
pub run_config: Option<RunConfig>,
62+
run_config: Option<RunConfig>,
5763
}
5864

5965
#[allow(clippy::too_many_arguments)]
@@ -71,6 +77,7 @@ impl Config {
7177
enable_metrics_server: bool,
7278
metrics_server_port: String,
7379
untrusted_http_port: String,
80+
data_dir: PathBuf,
7481
run_config: Option<RunConfig>,
7582
) -> Self {
7683
Self {
@@ -86,6 +93,7 @@ impl Config {
8693
enable_metrics_server,
8794
metrics_server_port,
8895
untrusted_http_port,
96+
data_dir,
8997
run_config,
9098
}
9199
}
@@ -131,6 +139,18 @@ impl Config {
131139
}
132140
}
133141

142+
pub fn data_dir(&self) -> &Path {
143+
self.data_dir.as_path()
144+
}
145+
146+
pub fn run_config(&self) -> &Option<RunConfig> {
147+
&self.run_config
148+
}
149+
150+
pub fn enable_metrics_server(&self) -> bool {
151+
self.enable_metrics_server
152+
}
153+
134154
pub fn try_parse_metrics_server_port(&self) -> Option<u16> {
135155
self.metrics_server_port.parse::<u16>().ok()
136156
}
@@ -149,6 +169,25 @@ impl From<&ArgMatches<'_>> for Config {
149169
let metrics_server_port = m.value_of("metrics-port").unwrap_or(DEFAULT_METRICS_PORT);
150170
let untrusted_http_port =
151171
m.value_of("untrusted-http-port").unwrap_or(DEFAULT_UNTRUSTED_HTTP_PORT);
172+
173+
let data_dir = match m.value_of("data-dir") {
174+
Some(d) => {
175+
let p = PathBuf::from(d);
176+
if !p.exists() {
177+
log::info!("Creating new data-directory for the service {}.", p.display());
178+
fs::create_dir_all(p.as_path()).unwrap();
179+
} else {
180+
log::info!("Starting service in existing directory {}.", p.display());
181+
}
182+
p
183+
},
184+
None => {
185+
log::warn!("[Config] defaulting to data-dir = PWD because it was previous behaviour. This might change soon.\
186+
Please pass the data-dir explicitly to ensure nothing breaks in your setup.");
187+
pwd()
188+
},
189+
};
190+
152191
let run_config = m.subcommand_matches("run").map(RunConfig::from);
153192

154193
Self::new(
@@ -167,6 +206,7 @@ impl From<&ArgMatches<'_>> for Config {
167206
is_metrics_server_enabled,
168207
metrics_server_port.to_string(),
169208
untrusted_http_port.to_string(),
209+
data_dir,
170210
run_config,
171211
)
172212
}
@@ -225,6 +265,10 @@ fn add_port_if_necessary(url: &str, port: &str) -> String {
225265
}
226266
}
227267

268+
pub fn pwd() -> PathBuf {
269+
std::env::current_dir().expect("works on all supported platforms; qed.")
270+
}
271+
228272
#[cfg(test)]
229273
mod test {
230274
use super::*;
@@ -247,6 +291,7 @@ mod test {
247291
assert!(config.mu_ra_external_address.is_none());
248292
assert!(!config.enable_metrics_server);
249293
assert_eq!(config.untrusted_http_port, DEFAULT_UNTRUSTED_HTTP_PORT);
294+
assert_eq!(config.data_dir, pwd());
250295
assert!(config.run_config.is_none());
251296
}
252297

service/src/enclave/api.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ use itp_settings::files::{ENCLAVE_FILE, ENCLAVE_TOKEN};
2323
use log::*;
2424
use sgx_types::*;
2525
use sgx_urts::SgxEnclave;
26-
/// keep this api free from chain-specific types!
27-
use std::io::{Read, Write};
28-
use std::{fs::File, path::PathBuf};
26+
use std::{
27+
fs::File,
28+
io::{Read, Write},
29+
path::PathBuf,
30+
};
2931

3032
pub fn enclave_init(config: &Config) -> EnclaveResult<Enclave> {
3133
const LEN: usize = 1024;
@@ -103,7 +105,11 @@ pub fn enclave_init(config: &Config) -> EnclaveResult<Enclave> {
103105

104106
// create an enclave API and initialize it
105107
let enclave_api = Enclave::new(enclave);
106-
enclave_api.init(&config.mu_ra_url_external(), &config.untrusted_worker_url_external())?;
108+
enclave_api.init(
109+
&config.mu_ra_url_external(),
110+
&config.untrusted_worker_url_external(),
111+
&config.data_dir().display().to_string(),
112+
)?;
107113

108114
Ok(enclave_api)
109115
}

0 commit comments

Comments
 (0)