From ccd35d51a71f65c63957f760174d8de14d58092e Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Tue, 30 Jan 2024 23:58:57 +0900 Subject: [PATCH] add env variable `LCP_ENCLAVE_DEBUG` Signed-off-by: Jun Kimura --- app/src/commands/attestation.rs | 4 +-- app/src/commands/elc.rs | 7 ++++-- app/src/commands/enclave.rs | 6 ++--- app/src/commands/service.rs | 2 +- app/src/opts.rs | 43 ++++++++++++++++++++++++--------- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/app/src/commands/attestation.rs b/app/src/commands/attestation.rs index 78b22173..2513bbde 100644 --- a/app/src/commands/attestation.rs +++ b/app/src/commands/attestation.rs @@ -34,7 +34,7 @@ impl AttestationCmd { bail!("home directory doesn't exist at {:?}", home); } run_ias_remote_attestation( - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?, + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?, cmd, ) } @@ -44,7 +44,7 @@ impl AttestationCmd { bail!("home directory doesn't exist at {:?}", home); } run_simulate_remote_attestation( - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?, + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?, cmd, ) } diff --git a/app/src/commands/elc.rs b/app/src/commands/elc.rs index 07aa0f86..e613f316 100644 --- a/app/src/commands/elc.rs +++ b/app/src/commands/elc.rs @@ -52,8 +52,11 @@ impl ELCCmd { L: EnclaveLoader, { let elc_opts = self.opts(); - let enclave = - enclave_loader.load(opts, elc_opts.enclave.path.as_ref(), elc_opts.enclave.debug)?; + let enclave = enclave_loader.load( + opts, + elc_opts.enclave.path.as_ref(), + elc_opts.enclave.is_debug(), + )?; match self { Self::CreateClient(_) => { let _ = enclave.proto_create_client(elc_opts.load()?)?; diff --git a/app/src/commands/enclave.rs b/app/src/commands/enclave.rs index 224b1b1f..37de84f6 100644 --- a/app/src/commands/enclave.rs +++ b/app/src/commands/enclave.rs @@ -38,15 +38,15 @@ impl EnclaveCmd { } match self { Self::GenerateKey(cmd) => run_generate_key( - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?, + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?, cmd, ), Self::ListKeys(cmd) => run_list_keys( - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?, + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?, cmd, ), Self::PruneKeys(cmd) => run_prune_keys( - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?, + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?, cmd, ), Self::Metadata(cmd) => run_print_metadata(opts, cmd), diff --git a/app/src/commands/service.rs b/app/src/commands/service.rs index 0f5a81ae..99b83210 100644 --- a/app/src/commands/service.rs +++ b/app/src/commands/service.rs @@ -48,7 +48,7 @@ impl ServiceCmd { Self::Start(cmd) => { let addr = cmd.address.parse()?; let enclave = - enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?; + enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.is_debug())?; let mut rb = Builder::new_multi_thread(); let rb = if let Some(threads) = cmd.threads { diff --git a/app/src/opts.rs b/app/src/opts.rs index 47eb5a20..b79d7f0c 100644 --- a/app/src/opts.rs +++ b/app/src/opts.rs @@ -3,6 +3,11 @@ use clap::Parser; use log::LevelFilter; use std::{path::PathBuf, str::FromStr}; +const ENV_VAR_DEBUG: &str = "LCP_ENCLAVE_DEBUG"; + +const DEFAULT_HOME: &str = "~/.lcp"; +const DEFAULT_ENCLAVE: &str = "enclave.signed.so"; + #[derive(Debug, Clone, Parser)] pub struct Opts { /// Path to the home directory @@ -16,26 +21,17 @@ pub struct Opts { pub log_level: Option, } -#[derive(Debug, Clone, Parser, PartialEq)] -pub struct EnclaveOpts { - /// Path to the enclave binary - #[clap(long = "enclave", help = "Path to enclave binary")] - pub path: Option, - #[clap(long = "enclave_debug", help = "Enable enclave debug mode")] - pub debug: bool, -} - impl Opts { pub fn get_home(&self) -> PathBuf { if let Some(home) = self.home.as_ref() { home.clone() } else { - dirs::home_dir().unwrap().join(".lcp") + dirs::home_dir().unwrap().join(DEFAULT_HOME) } } pub fn default_enclave(&self) -> PathBuf { - self.get_home().join("enclave.signed.so") + self.get_home().join(DEFAULT_ENCLAVE) } pub fn get_state_store_path(&self) -> PathBuf { @@ -52,3 +48,28 @@ impl Opts { } } } + +#[derive(Debug, Clone, Parser, PartialEq)] +pub struct EnclaveOpts { + /// Path to the enclave binary + #[clap(long = "enclave", help = "Path to enclave binary")] + pub path: Option, + /// Priority for debug flag: + /// 1. command line option + /// 2. environment variable + #[clap(long = "enclave_debug", help = "Enable enclave debug mode")] + debug: bool, +} + +impl EnclaveOpts { + pub fn is_debug(&self) -> bool { + if self.debug { + true + } else { + match std::env::var(ENV_VAR_DEBUG).map(|val| val.to_lowercase()) { + Ok(val) => val == "1" || val == "true", + Err(_) => false, + } + } + } +}