Skip to content

Commit

Permalink
add EnclaveLoader trait and implementation
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 d13e090 commit 4bcc8ec
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
21 changes: 10 additions & 11 deletions app/src/commands/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::opts::{EnclaveOpts, Opts};
use crate::{
enclave::EnclaveLoader,
opts::{EnclaveOpts, Opts},
};
use anyhow::{bail, Result};
use clap::Parser;
use crypto::Address;
use ecall_commands::IASRemoteAttestationInput;
use enclave_api::{Enclave, EnclaveCommandAPI, EnclaveProtoAPI};
use std::path::PathBuf;
use store::transaction::CommitStore;

/// `attestation` subcommand
Expand All @@ -19,14 +21,11 @@ pub enum AttestationCmd {
}

impl AttestationCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
pub fn run<S, L>(&self, opts: &Opts, enclave_loader: L) -> Result<()>
where
S: CommitStore,
Enclave<S>: EnclaveProtoAPI<S>,
L: EnclaveLoader<S>,
{
let home = opts.get_home();
match self {
Expand All @@ -35,7 +34,7 @@ impl AttestationCmd {
bail!("home directory doesn't exist at {:?}", home);
}
run_ias_remote_attestation(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
)
}
Expand All @@ -45,7 +44,7 @@ impl AttestationCmd {
bail!("home directory doesn't exist at {:?}", home);
}
run_simulate_remote_attestation(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
)
}
Expand Down Expand Up @@ -102,14 +101,14 @@ pub struct SimulateRemoteAttestation {
long = "signing_cert_path",
help = "Path to a der-encoded file that contains X.509 certificate"
)]
pub signing_cert_path: PathBuf,
pub signing_cert_path: std::path::PathBuf,

/// Path to a PEM-encoded file that contains PKCS#8 private key
#[clap(
long = "signing_key",
help = "Path to a PEM-encoded file that contains PKCS#8 private key"
)]
pub signing_key_path: PathBuf,
pub signing_key_path: std::path::PathBuf,

/// Validate a signing certificate using openssl command
#[clap(
Expand Down
15 changes: 8 additions & 7 deletions app/src/commands/elc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::opts::{EnclaveOpts, Opts};
use crate::{
enclave::EnclaveLoader,
opts::{EnclaveOpts, Opts},
};
use anyhow::Result;
use clap::Parser;
use enclave_api::{Enclave, EnclaveProtoAPI};
Expand Down Expand Up @@ -42,17 +45,15 @@ impl ELCOpts {
}

impl ELCCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
pub fn run<S, L>(&self, opts: &Opts, enclave_loader: L) -> Result<()>
where
S: CommitStore,
Enclave<S>: EnclaveProtoAPI<S>,
L: EnclaveLoader<S>,
{
let elc_opts = self.opts();
let enclave = enclave_loader(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.debug)?;
match self {
Self::CreateClient(_) => {
let _ = enclave.proto_create_client(elc_opts.load()?)?;
Expand Down
19 changes: 9 additions & 10 deletions app/src/commands/enclave.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::opts::{EnclaveOpts, Opts};
use crate::{
enclave::EnclaveLoader,
opts::{EnclaveOpts, Opts},
};
use anyhow::{anyhow, Result};
use clap::Parser;
use ecall_commands::GenerateEnclaveKeyInput;
use enclave_api::{Enclave, EnclaveCommandAPI, EnclaveProtoAPI};
use lcp_types::Mrenclave;
use log::*;
use serde_json::json;
use std::path::PathBuf;
use store::transaction::CommitStore;

// `enclave` subcommand
Expand All @@ -23,14 +25,11 @@ pub enum EnclaveCmd {
}

impl EnclaveCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
pub fn run<S, L>(&self, opts: &Opts, enclave_loader: L) -> Result<()>
where
S: CommitStore,
Enclave<S>: EnclaveProtoAPI<S>,
L: EnclaveLoader<S>,
{
let home = opts.get_home();
if !home.exists() {
Expand All @@ -39,15 +38,15 @@ impl EnclaveCmd {
}
match self {
Self::GenerateKey(cmd) => run_generate_key(
enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
enclave_loader.load(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)?,
enclave_loader.load(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)?,
enclave_loader.load(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?,
cmd,
),
Self::Metadata(cmd) => run_print_metadata(opts, cmd),
Expand Down
12 changes: 5 additions & 7 deletions app/src/commands/service.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::enclave::EnclaveLoader;
use crate::opts::{EnclaveOpts, Opts};
use anyhow::Result;
use clap::Parser;
use enclave_api::{Enclave, EnclaveProtoAPI};
use log::*;
use service::{run_service, AppService};
use std::path::PathBuf;
use std::sync::Arc;
use store::transaction::CommitStore;
use tokio::runtime::Builder;
Expand Down Expand Up @@ -38,19 +38,17 @@ pub struct Start {
}

impl ServiceCmd {
pub fn run<S>(
&self,
opts: &Opts,
enclave_loader: impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>,
) -> Result<()>
pub fn run<S, L>(&self, opts: &Opts, enclave_loader: L) -> Result<()>
where
S: CommitStore + 'static,
Enclave<S>: EnclaveProtoAPI<S>,
L: EnclaveLoader<S>,
{
match self {
Self::Start(cmd) => {
let addr = cmd.address.parse()?;
let enclave = enclave_loader(opts, cmd.enclave.path.as_ref(), cmd.enclave.debug)?;
let enclave =
enclave_loader.load(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
19 changes: 16 additions & 3 deletions app/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ use keymanager::EnclaveKeyManager;
use std::path::PathBuf;
use store::transaction::CommitStore;

pub(crate) fn build_enclave_loader<S: CommitStore>(
) -> impl FnOnce(&Opts, Option<&PathBuf>, bool) -> Result<Enclave<S>>
pub trait EnclaveLoader<S: CommitStore> {
fn load(&self, opts: &Opts, path: Option<&PathBuf>, debug: bool) -> Result<Enclave<S>>;
}

#[derive(Debug)]
pub struct DefaultEnclaveLoader<S: CommitStore>(std::marker::PhantomData<S>);

impl<S: CommitStore> EnclaveLoader<S> for DefaultEnclaveLoader<S>
where
Enclave<S>: EnclaveProtoAPI<S>,
{
|opts, path, debug| {
fn load(&self, opts: &Opts, path: Option<&PathBuf>, debug: bool) -> Result<Enclave<S>> {
let path = if let Some(path) = path {
path.clone()
} else {
Expand All @@ -30,3 +36,10 @@ where
}
}
}

pub const fn build_enclave_loader<S: CommitStore>() -> DefaultEnclaveLoader<S>
where
Enclave<S>: EnclaveProtoAPI<S>,
{
DefaultEnclaveLoader(std::marker::PhantomData)
}

0 comments on commit 4bcc8ec

Please sign in to comment.