Skip to content

Commit

Permalink
add enclave_debug flag to cli
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jan 15, 2024
1 parent 3bd77b9 commit d13e090
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 56 deletions.
27 changes: 16 additions & 11 deletions app/src/commands/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::opts::Opts;
use crate::opts::{EnclaveOpts, Opts};
use anyhow::{bail, Result};
use clap::Parser;
use crypto::Address;
Expand All @@ -22,7 +22,7 @@ impl AttestationCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>) -> Result<Enclave<S>>,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
where
S: CommitStore,
Expand All @@ -34,25 +34,30 @@ impl AttestationCmd {
if !home.exists() {
bail!("home directory doesn't exist at {:?}", home);
}
run_ias_remote_attestation(enclave_loader(opts, cmd.enclave.as_ref())?, cmd)
run_ias_remote_attestation(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
)
}
#[cfg(feature = "sgx-sw")]
AttestationCmd::Simulate(cmd) => {
if !home.exists() {
bail!("home directory doesn't exist at {:?}", home);
}
run_simulate_remote_attestation(enclave_loader(opts, cmd.enclave.as_ref())?, cmd)
run_simulate_remote_attestation(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
)
}
}
}
}

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct IASRemoteAttestation {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,

/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
/// An enclave key attested by Remote Attestation
#[clap(
long = "enclave_key",
Expand Down Expand Up @@ -81,9 +86,9 @@ fn run_ias_remote_attestation<E: EnclaveCommandAPI<S>, S: CommitStore>(
#[cfg(feature = "sgx-sw")]
#[derive(Clone, Debug, Parser, PartialEq)]
pub struct SimulateRemoteAttestation {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,

/// An enclave key attested by Remote Attestation
#[clap(
Expand Down
14 changes: 7 additions & 7 deletions app/src/commands/elc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::opts::Opts;
use crate::opts::{EnclaveOpts, Opts};
use anyhow::Result;
use clap::Parser;
use enclave_api::{Enclave, EnclaveProtoAPI};
Expand All @@ -24,11 +24,11 @@ impl ELCCmd {
}
}

#[derive(Clone, Debug, Parser, PartialEq)]
#[derive(Clone, Debug, Parser)]
pub struct ELCOpts {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
/// Path to the proto msg
#[clap(long = "msg", help = "Path to proto msg")]
pub msg: PathBuf,
Expand All @@ -45,14 +45,14 @@ impl ELCCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>) -> Result<Enclave<S>>,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
where
S: CommitStore,
Enclave<S>: EnclaveProtoAPI<S>,
{
let elc_opts = self.opts();
let enclave = enclave_loader(opts, elc_opts.enclave.as_ref())?;
let enclave = enclave_loader(opts, elc_opts.enclave.path.as_ref(), elc_opts.enclave.debug)?;
match self {
Self::CreateClient(_) => {
let _ = enclave.proto_create_client(elc_opts.load()?)?;
Expand Down
48 changes: 27 additions & 21 deletions app/src/commands/enclave.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::opts::Opts;
use crate::opts::{EnclaveOpts, Opts};
use anyhow::{anyhow, Result};
use clap::Parser;
use ecall_commands::GenerateEnclaveKeyInput;
Expand Down Expand Up @@ -26,7 +26,7 @@ impl EnclaveCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>) -> Result<Enclave<S>>,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
where
S: CommitStore,
Expand All @@ -38,23 +38,28 @@ impl EnclaveCmd {
info!("created home directory: {:?}", home);
}
match self {
Self::GenerateKey(cmd) => {
run_generate_key(enclave_loader(opts, cmd.enclave.as_ref())?, cmd)
}
Self::ListKeys(cmd) => run_list_keys(enclave_loader(opts, cmd.enclave.as_ref())?, cmd),
Self::PruneKeys(cmd) => {
run_prune_keys(enclave_loader(opts, cmd.enclave.as_ref())?, cmd)
}
Self::GenerateKey(cmd) => run_generate_key(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
),
Self::ListKeys(cmd) => run_list_keys(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
),
Self::PruneKeys(cmd) => run_prune_keys(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
),
Self::Metadata(cmd) => run_print_metadata(opts, cmd),
}
}
}

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct GenerateKey {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
}

fn run_generate_key<E: EnclaveCommandAPI<S>, S: CommitStore>(
Expand All @@ -70,9 +75,9 @@ fn run_generate_key<E: EnclaveCommandAPI<S>, S: CommitStore>(

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct ListKeys {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
#[clap(
long = "available_only",
short = 'a',
Expand Down Expand Up @@ -122,9 +127,9 @@ fn run_list_keys<E: EnclaveCommandAPI<S>, S: CommitStore>(

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct PruneKeys {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
/// expiration in seconds from attested_at
#[clap(long = "expiration", help = "expiration in seconds from attested_at")]
pub expiration: u64,
Expand All @@ -142,14 +147,15 @@ fn run_prune_keys<E: EnclaveCommandAPI<S>, S: CommitStore>(

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct Metadata {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to the enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
}

fn run_print_metadata(opts: &Opts, cmd: &Metadata) -> Result<()> {
let metadata = host::sgx_get_metadata(
cmd.enclave
.path
.clone()
.unwrap_or_else(|| opts.default_enclave()),
)?;
Expand Down
12 changes: 6 additions & 6 deletions app/src/commands/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::opts::Opts;
use crate::opts::{EnclaveOpts, Opts};
use anyhow::Result;
use clap::Parser;
use enclave_api::{Enclave, EnclaveProtoAPI};
Expand All @@ -18,9 +18,9 @@ pub enum ServiceCmd {

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct Start {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to enclave binary")]
pub enclave: Option<PathBuf>,
/// Options for enclave
#[clap(flatten)]
pub enclave: EnclaveOpts,
/// Address of the App service
#[clap(
long = "address",
Expand All @@ -41,7 +41,7 @@ impl ServiceCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>) -> Result<Enclave<S>>,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
where
S: CommitStore + 'static,
Expand All @@ -50,7 +50,7 @@ impl ServiceCmd {
match self {
Self::Start(cmd) => {
let addr = cmd.address.parse()?;
let enclave = enclave_loader(opts, cmd.enclave.as_ref())?;
let enclave = enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?;

let mut rb = Builder::new_multi_thread();
let rb = if let Some(threads) = cmd.threads {
Expand Down
6 changes: 3 additions & 3 deletions app/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use std::path::PathBuf;
use store::transaction::CommitStore;

pub(crate) fn build_enclave_loader<S: CommitStore>(
) -> impl FnOnce(&Opts, Option<&PathBuf>) -> Result<Enclave<S>>
) -> impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>
where
Enclave<S>: EnclaveProtoAPI<S>,
{
|opts, path| {
|opts, path, debug| {
let path = if let Some(path) = path {
path.clone()
} else {
opts.default_enclave()
};
let env = host::get_environment().unwrap();
let km = EnclaveKeyManager::new(&env.home)?;
match Enclave::create(&path, km, env.store.clone()) {
match Enclave::create(&path, debug, km, env.store.clone()) {
Ok(enclave) => Ok(enclave),
Err(x) => {
bail!(
Expand Down
11 changes: 10 additions & 1 deletion app/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use log::LevelFilter;
use std::{path::PathBuf, str::FromStr};

#[derive(Debug, Parser)]
#[derive(Debug, Clone, Parser)]
pub struct Opts {
/// Path to the home directory
#[clap(long = "home", help = "Path to LCP home directory")]
Expand All @@ -16,6 +16,15 @@ pub struct Opts {
pub log_level: Option<String>,
}

#[derive(Debug, Clone, Parser, PartialEq)]
pub struct EnclaveOpts {
/// Path to the enclave binary
#[clap(long = "enclave", help = "Path to enclave binary")]
pub path: Option<PathBuf>,
#[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() {
Expand Down
3 changes: 2 additions & 1 deletion modules/enclave-api/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ impl<S: CommitStore> Enclave<S> {

pub fn create(
path: impl Into<PathBuf>,
debug: bool,
key_manager: EnclaveKeyManager,
store: Arc<RwLock<HostStore>>,
) -> SgxResult<Self> {
let path = path.into();
let enclave = host::create_enclave(path.clone())?;
let enclave = host::create_enclave(path.clone(), debug)?;
Ok(Self::new(path, key_manager, store, enclave))
}

Expand Down
7 changes: 2 additions & 5 deletions modules/host/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ use sgx_types::{metadata::metadata_t, *};
use sgx_urts::SgxEnclave;
use std::{ffi::CString, mem::MaybeUninit, path::PathBuf};

pub fn create_enclave(path: impl Into<PathBuf>) -> SgxResult<SgxEnclave> {
pub fn create_enclave(path: impl Into<PathBuf>, debug: bool) -> SgxResult<SgxEnclave> {
let mut launch_token: sgx_launch_token_t = [0; 1024];
let mut launch_token_updated: i32 = 0;
// call sgx_create_enclave to initialize an enclave instance
// Debug Support: set 2nd parameter to 1
let debug = 1;
let mut misc_attr = sgx_misc_attribute_t {
secs_attr: sgx_attributes_t { flags: 0, xfrm: 0 },
misc_select: 0,
};
SgxEnclave::create(
path.into(),
debug,
debug.into(),
&mut launch_token,
&mut launch_token_updated,
&mut misc_attr,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod tests {

let env = host::get_environment().unwrap();
let km = EnclaveKeyManager::new(&env.home).unwrap();
let enclave = Enclave::create(ENCLAVE_FILE, km, env.store.clone()).unwrap();
let enclave = Enclave::create(ENCLAVE_FILE, false, km, env.store.clone()).unwrap();

match std::env::var(ENV_SETUP_NODES).map(|v| v.to_lowercase()) {
Ok(v) if v == "false" => run_test(&enclave).unwrap(),
Expand Down

0 comments on commit d13e090

Please sign in to comment.