You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2019/12/16 17:53:00 UTC

[incubator-teaclave] branch master updated: [cli] Rewrite command line tool (#168)

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git


The following commit(s) were added to refs/heads/master by this push:
     new 6dace19  [cli] Rewrite command line tool (#168)
6dace19 is described below

commit 6dace1961d32d31aad9a5e79acf9fd690e312cb1
Author: Mingshen Sun <bo...@mssun.me>
AuthorDate: Mon Dec 16 09:52:52 2019 -0800

    [cli] Rewrite command line tool (#168)
---
 cmake/scripts/parse_cargo_packages.py     |   2 +-
 cmake/tomls/Cargo.unix_app.toml           |   2 +-
 mesatee_cli/README.md                     |  72 -----------------
 mesatee_core/src/rpc/mod.rs               |  10 ++-
 mesatee_core/src/rpc/sgx/mod.rs           |   4 +-
 mesatee_core/src/rpc/unix/mod.rs          |   4 +-
 {mesatee_cli => teaclave_cli}/Cargo.toml  |   7 +-
 teaclave_cli/README.md                    | 107 ++++++++++++++++++++++++++
 {mesatee_cli => teaclave_cli}/src/main.rs | 123 ++++++++++++++++++------------
 teaclave_utils/src/lib.rs                 |  12 +--
 10 files changed, 200 insertions(+), 143 deletions(-)

diff --git a/cmake/scripts/parse_cargo_packages.py b/cmake/scripts/parse_cargo_packages.py
index 71b68fc..e4d96d6 100644
--- a/cmake/scripts/parse_cargo_packages.py
+++ b/cmake/scripts/parse_cargo_packages.py
@@ -61,7 +61,7 @@ def pkg_path_2_category(pkg_path):
         return 'examples'
     elif pkg_path.startswith('tests/'):
         return 'tests'
-    elif pkg_path == 'mesatee_cli':
+    elif pkg_path == 'teaclave_cli':
         return 'bin'
     else:
         sys.stderr.write('[Error]: Unknown category for package_path {}\n'.format(pkg_path))
diff --git a/cmake/tomls/Cargo.unix_app.toml b/cmake/tomls/Cargo.unix_app.toml
index 0887fd1..8ef77b0 100644
--- a/cmake/tomls/Cargo.unix_app.toml
+++ b/cmake/tomls/Cargo.unix_app.toml
@@ -27,7 +27,7 @@ members = [
   "mesatee_sdk", # ignore
   "mesatee_sdk/c_sdk", # ignore
   "teaclave_common/protected_fs_rs", # ignore
-  "mesatee_cli"
+  "teaclave_cli"
 ]
 
 exclude = [
diff --git a/mesatee_cli/README.md b/mesatee_cli/README.md
deleted file mode 100644
index a051965..0000000
--- a/mesatee_cli/README.md
+++ /dev/null
@@ -1,72 +0,0 @@
-# MesaTEE CLI
-
-`mesatee-cli` is a command-line utility to communicate with MesaTEE external
-endpoints. 
-
-## Usage
-
-```shell
-mesatee_cli 0.1.0
-MesaTEE Authors <de...@mesatee.org>
-MeasTEE client
-
-USAGE:
-    mesatee_cli [FLAGS] [OPTIONS] <IP_ADDRESS:PORT> --auditor_keys <auditor_keys>... --auditor_sigs <auditor_sigs>... --enclave_info <enclave_info> --endpoint <endpoint>
-
-FLAGS:
-    -h, --help
-            Prints help information
-
-    -P, --pretty
-            Enable pretty printing.
-    
-    -V, --version
-            Prints version information
-    
-    -v, --verbosity
-            Pass many times for more log output
-    
-            By default, it'll only report errors. Passing `-v` one time also prints warnings, `-vv` enables info
-            logging, `-vvv` debug, and `-vvvv` trace.
-
-OPTIONS:
-    -o, --output <IN_FILE>
-            Write to FILE instead of stdout
-
-    -i, --input <OUT_FILE>
-            Read from FILE instead of stdin
-    
-    -k, --auditor_keys <auditor_keys>...
-            SPACE seperated paths of MesaTEE auditor public keys
-    
-    -s, --auditor_sigs <auditor_sigs>...
-            SPACE seperated paths of MesaTEE auditor endorsement signatures.
-    
-    -c, --enclave_info <enclave_info>
-            Path to Enclave info file
-    
-    -e, --endpoint <endpoint>
-            MesaTEE endpoint to connect to. Possible values are: tms, tdfs, fns.
-
-ARGS:
-    <IP_ADDRESS:PORT>
-            Address and port of the MeasTEE endpoint
-```
-
-Note the `--enclave_info`, `--auditor_keys`, `--auditor_sigs` are required
-options. These flags provide auditor information for MesaTEE enclaves. More
-details can be found
-[here](https://github.com/mesalock-linux/mesatee/blob/master/auditors/README.md).
-
-
-## Example
-
-Here we give an example of using `mesatee_cli`:
-
-```shell
-$ cd mesatee
-$ ./service.sh start
-$ ./bin/mesatee_cli 127.0.0.1:5554 -k auditors/albus_dumbledore/albus_dumbledore.public.der -k auditors/godzilla/godzilla.public.der -k auditors/optimus_prime/optimus_prime.public.der -s auditors/albus_dumbledore/albus_dumbledore.sign.sha256 -s auditors/godzilla/godzilla.sign.sha256 -s auditors/optimus_prime/optimus_prime.sign.sha256 -c out/enclave_info.toml --endpoint tms -i ~/tms_payload
-
-{"type":"Create","task_id":"7216dd3e-ab3a-4974-b03e-3833891bbb26","task_token":"08e0d4c807700ff24d31ca01d8695b61","ip":"127.0.0.1","port":3444}
-```
diff --git a/mesatee_core/src/rpc/mod.rs b/mesatee_core/src/rpc/mod.rs
index 822b93e..2a2feb3 100644
--- a/mesatee_core/src/rpc/mod.rs
+++ b/mesatee_core/src/rpc/mod.rs
@@ -37,8 +37,8 @@ use crate::rpc::sendrecv::*;
 // and V.
 pub trait RpcServer<U, V, X>: Read + Write
 where
-    U: DeserializeOwned,
-    V: Serialize,
+    U: DeserializeOwned + std::fmt::Debug,
+    V: Serialize + std::fmt::Debug,
     X: EnclaveService<U, V>,
     Self: Sized,
 {
@@ -65,12 +65,14 @@ where
             // recv_buf should be a serialized incoming request U
             // The server needs deser it into U first
             let request: U = serde_json::from_slice(&recv_buf)?;
+            debug!("request: {:?}", request);
             let result: V = x.handle_invoke(request)?;
-            let ret: Vec<u8> = serde_json::to_vec(&result)?;
+            let response: Vec<u8> = serde_json::to_vec(&result)?;
+            debug!("response {:?}", response);
 
             // Now the result is stored in ret and we need to sent it back.
             // `ret` is cleared here. Performance is not very good.
-            send_vec(self, ret)?;
+            send_vec(self, response)?;
         }
     }
 }
diff --git a/mesatee_core/src/rpc/sgx/mod.rs b/mesatee_core/src/rpc/sgx/mod.rs
index 75bd694..0281608 100644
--- a/mesatee_core/src/rpc/sgx/mod.rs
+++ b/mesatee_core/src/rpc/sgx/mod.rs
@@ -250,8 +250,8 @@ impl<U, V, X> Write for Pipe<U, V, X> {
 #[cfg(feature = "mesalock_sgx")]
 impl<U, V, X> RpcServer<U, V, X> for Pipe<U, V, X>
 where
-    U: DeserializeOwned,
-    V: Serialize,
+    U: DeserializeOwned + std::fmt::Debug,
+    V: Serialize + std::fmt::Debug,
     X: EnclaveService<U, V>,
 {
     type Config = PipeConfig;
diff --git a/mesatee_core/src/rpc/unix/mod.rs b/mesatee_core/src/rpc/unix/mod.rs
index dc8d893..bd9c585 100644
--- a/mesatee_core/src/rpc/unix/mod.rs
+++ b/mesatee_core/src/rpc/unix/mod.rs
@@ -107,8 +107,8 @@ impl<U, V, X> Write for Pipe<U, V, X> {
 
 impl<U, V, X> RpcServer<U, V, X> for Pipe<U, V, X>
 where
-    U: DeserializeOwned,
-    V: Serialize,
+    U: DeserializeOwned + std::fmt::Debug,
+    V: Serialize + std::fmt::Debug,
     X: EnclaveService<U, V>,
 {
     type Config = PipeConfig;
diff --git a/mesatee_cli/Cargo.toml b/teaclave_cli/Cargo.toml
similarity index 80%
rename from mesatee_cli/Cargo.toml
rename to teaclave_cli/Cargo.toml
index 48d227b..61e07ca 100644
--- a/mesatee_cli/Cargo.toml
+++ b/teaclave_cli/Cargo.toml
@@ -1,8 +1,8 @@
 [package]
-name = "mesatee_cli"
+name = "teaclave_cli"
 version = "0.1.0"
 authors = ["MesaTEE Authors <de...@mesatee.org>"]
-description = "MesaTEE client as a comand line tool"
+description = "Teclave comand line tool"
 license = "Apache-2.0"
 edition = "2018"
 
@@ -15,9 +15,6 @@ failure = "0.1.5"
 exitfailure = "0.5"
 structopt = "0.2"
 serde_json = "1.0.39"
-log = "0.4.6"
-env_logger = "0.7.1"
-clap_flags = "0.2.0"
 tms_external_proto = { path = "../mesatee_services/tms/external/proto" }
 tdfs_external_proto = { path = "../mesatee_services/tdfs/external/proto" }
 fns_proto = { path = "../mesatee_services/fns/proto" }
diff --git a/teaclave_cli/README.md b/teaclave_cli/README.md
new file mode 100644
index 0000000..4d01976
--- /dev/null
+++ b/teaclave_cli/README.md
@@ -0,0 +1,107 @@
+# Teaclave Command Line Tool
+
+`teaclave_cli` is a command-line tool to communicate with Teaclave's services.
+
+## Usage
+
+There are two sub-commands: `teaclave_cli audit` is for auditing enclave info
+with auditors' public keys and signatures and `teaclave_cli connect` is for
+communicating with Teclave's services.
+
+Here are details of the arguments for `teaclave_cli audit`:
+
+```
+$ ./teaclave_cli audit --help
+teaclave_cli-audit 0.1.0
+MesaTEE Authors <de...@mesatee.org>
+Audit enclave info with auditors' public keys and signatures.
+
+USAGE:
+    teaclave_cli audit --enclave_info <ENCLAVE_INFO_FILE> --auditor_public_keys <auditor_public_keys>... --auditor_signatures <auditor_signatures>...
+
+FLAGS:
+    -h, --help       Prints help information
+    -V, --version    Prints version information
+
+OPTIONS:
+    -c, --enclave_info <ENCLAVE_INFO_FILE>                Path to Enclave info file.
+    -k, --auditor_public_keys <auditor_public_keys>...    SPACE separated paths of Teaclave auditor public keys
+    -s, --auditor_signatures <auditor_signatures>...
+            SPACE separated paths of Teaclave auditor endorsement signatures.
+```
+
+Here are details of the arguments for `teaclave_cli connect`:
+
+```
+./teaclave_cli connect --help
+teaclave_cli-connect 0.1.0
+MesaTEE Authors <de...@mesatee.org>
+Connect and send messages to Teaclave services
+
+USAGE:
+    teaclave_cli connect [OPTIONS] <IP_ADDRESS:PORT> --enclave_info <ENCLAVE_INFO_FILE> --endpoint <endpoint>
+
+FLAGS:
+    -h, --help       Prints help information
+    -V, --version    Prints version information
+
+OPTIONS:
+    -c, --enclave_info <ENCLAVE_INFO_FILE>    Path to Enclave info file.
+    -o, --output <INPUT_FILE>                 Write to FILE instead of stdout.
+    -i, --input <OUTPUT_FILE>                 Read from FILE instead of stdin.
+    -e, --endpoint <endpoint>                 Teaclave endpoint to connect to. Possible values are: tms, tdfs, fns.
+
+ARGS:
+    <IP_ADDRESS:PORT>    Address and port of the Teaclave endpoint.
+```
+
+## Example
+
+The following is an example of verifying the enclave info with auditors' public
+keys and signatures.
+
+```
+$ ./teaclave_cli audit \
+  -c enclave_info.toml \
+  -k auditors/albus_dumbledore/albus_dumbledore.public.der \
+     auditors/godzilla/godzilla.public.der \
+     auditors/optimus_prime/optimus_prime.public.der \
+  -s auditors/albus_dumbledore/albus_dumbledore.sign.sha256 \
+     auditors/godzilla/godzilla.sign.sha256 \
+     auditors/optimus_prime/optimus_prime.sign.sha256
+Enclave info is successfully verified.
+```
+
+This example is to create a task and invoke the "echo" function.
+
+```
+# create a task
+$ cat create_task.json
+{
+  "type":"Create",
+  "function_name":"echo",
+  "collaborator_list":[],
+  "files":[],
+  "user_id":"user1",
+  "user_token":"token1"
+}
+$ cat create_task.json | ./teaclave_cli connect 127.0.0.1:5554 -c enclave_info.toml -e tms
+{"type":"Create","task_id":"20937006-2718-4f33-bae2-567933807436","task_token":"d20ce53ab743d69320712fd98555f5e5","ip":"127.0.0.1","port":3444}
+```
+
+Compose a invoke task request with `task_id`, `task_token`, `ip` and `port` in
+the previous response.
+
+```
+# invoke the "echo" function
+$ cat invoke_task.json
+{
+  "task_id":"20937006-2718-4f33-bae2-567933807436",
+  "function_name":"echo",
+  "task_token":"d20ce53ab743d69320712fd98555f5e5",
+  "payload":"Hello, World!"
+}
+
+$ cat invoke_task.json | ./teaclave_cli connect 127.0.0.1:3444 -c enclave_info.toml -e fns
+{"result":"Hello, World!"}
+```
diff --git a/mesatee_cli/src/main.rs b/teaclave_cli/src/main.rs
similarity index 63%
rename from mesatee_cli/src/main.rs
rename to teaclave_cli/src/main.rs
index a99ae48..251c387 100644
--- a/mesatee_cli/src/main.rs
+++ b/teaclave_cli/src/main.rs
@@ -15,7 +15,6 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use exitfailure::ExitFailure;
 use quicli::prelude::*;
 use structopt::StructOpt;
 
@@ -54,41 +53,58 @@ impl std::str::FromStr for Endpoint {
 }
 
 #[derive(Debug, StructOpt)]
-/// MeasTEE client
-struct Cli {
-    #[structopt(short = "o", long, name = "IN_FILE", parse(from_os_str))]
-    /// Write to FILE instead of stdout
-    output: Option<path::PathBuf>,
+struct AuditOpt {
+    #[structopt(short = "k", long, required = true)]
+    /// SPACE separated paths of Teaclave auditor public keys
+    auditor_public_keys: Vec<path::PathBuf>,
 
-    #[structopt(short = "i", long, name = "OUT_FILE", parse(from_os_str))]
-    /// Read from FILE instead of stdin
-    input: Option<path::PathBuf>,
+    #[structopt(short = "s", long, required = true)]
+    /// SPACE separated paths of Teaclave auditor endorsement signatures.
+    auditor_signatures: Vec<path::PathBuf>,
 
-    #[structopt(flatten)]
-    verbosity: clap_flags::Verbosity,
+    #[structopt(short = "c", long, required = true, name = "ENCLAVE_INFO_FILE")]
+    /// Path to Enclave info file.
+    enclave_info: path::PathBuf,
+}
+
+#[derive(Debug, StructOpt)]
+struct ConnectOpt {
+    #[structopt(short = "o", long, name = "INPUT_FILE", parse(from_os_str))]
+    /// Write to FILE instead of stdout.
+    output: Option<path::PathBuf>,
 
-    #[structopt(flatten)]
-    logger: clap_flags::Log,
+    #[structopt(short = "i", long, name = "OUTPUT_FILE", parse(from_os_str))]
+    /// Read from FILE instead of stdin.
+    input: Option<path::PathBuf>,
 
     #[structopt(short = "e", long, required = true)]
-    /// MesaTEE endpoint to connect to. Possible values are: tms, tdfs, fns.
+    /// Teaclave endpoint to connect to. Possible values are: tms, tdfs, fns.
     endpoint: Endpoint,
 
-    #[structopt(name = "SOCKET_ADDRESS", name = "IP_ADDRESS:PORT")]
-    /// Address and port of the MeasTEE endpoint
+    #[structopt(name = "IP_ADDRESS:PORT", required = true)]
+    /// Address and port of the Teaclave endpoint.
     addr: net::SocketAddr,
 
-    #[structopt(short = "k", long, required = true)]
-    /// SPACE seperated paths of MesaTEE auditor public keys
-    auditor_keys: Vec<path::PathBuf>,
+    #[structopt(short = "c", long, required = true, name = "ENCLAVE_INFO_FILE")]
+    /// Path to Enclave info file.
+    enclave_info: path::PathBuf,
+}
 
-    #[structopt(short = "s", long, required = true)]
-    /// SPACE seperated paths of MesaTEE auditor endorsement signatures.
-    auditor_sigs: Vec<path::PathBuf>,
+#[derive(Debug, StructOpt)]
+enum Command {
+    /// Audit enclave info with auditors' public keys and signatures.
+    #[structopt(name = "audit")]
+    Audit(AuditOpt),
+    /// Connect and send messages to Teaclave services
+    #[structopt(name = "connect")]
+    Connect(ConnectOpt),
+}
 
-    #[structopt(short = "c", long)]
-    /// Path to Enclave info file
-    enclave_info: path::PathBuf,
+#[derive(Debug, StructOpt)]
+/// Teaclave command line tool
+struct Cli {
+    #[structopt(subcommand)]
+    command: Command,
 }
 
 macro_rules! generate_runner_for {
@@ -98,7 +114,7 @@ macro_rules! generate_runner_for {
             addr: net::SocketAddr,
             reader: R,
             writer: W,
-        ) -> Result<(), ExitFailure> {
+        ) -> Result<(), failure::Error> {
             let outbound_desc =
                 OutboundDesc::new(*enclave_info.get(stringify!($endpoint)).unwrap());
             let target_desc = TargetDesc::new(addr, outbound_desc);
@@ -121,30 +137,8 @@ generate_runner_for!(tms, TaskRequest, TaskResponse);
 generate_runner_for!(tdfs, DFSRequest, DFSResponse);
 generate_runner_for!(fns, InvokeTaskRequest, InvokeTaskResponse);
 
-fn main() -> CliResult {
-    let args = Cli::from_args();
-    if args.auditor_keys.len() != args.auditor_sigs.len() {
-        return Err(
-            failure::err_msg("auditor_keys auditor_sigs have different sizes".to_string()).into(),
-        );
-    }
-
-    args.logger.log_all(args.verbosity.log_level())?;
-
-    let mut keys = vec![];
-    for key_path in args.auditor_keys.iter() {
-        let mut buf = vec![];
-        let mut f = fs::File::open(key_path)?;
-        let _ = f.read_to_end(&mut buf)?;
-        keys.push(buf);
-    }
-    let mut enclave_signers = vec![];
-    for auditor in keys.iter().zip(args.auditor_sigs.iter()) {
-        let (key, sig_path) = auditor;
-        enclave_signers.push((key.as_slice(), sig_path.as_path()));
-    }
-    let enclave_info_content = fs::read_to_string(&args.enclave_info)
-        .unwrap_or_else(|_| panic!("Cannot find enclave info at {:?}.", args.enclave_info));
+fn connect(args: ConnectOpt) -> Result<(), failure::Error> {
+    let enclave_info_content = fs::read_to_string(&args.enclave_info)?;
     let enclave_info = teaclave_utils::load_enclave_info(&enclave_info_content);
 
     let reader: Box<dyn Read> = match args.input {
@@ -162,3 +156,32 @@ fn main() -> CliResult {
         Endpoint::FNS => fns(&enclave_info, args.addr, reader, writer),
     }
 }
+
+fn audit(args: AuditOpt) -> Result<(), failure::Error> {
+    let enclave_info_content = fs::read_to_string(&args.enclave_info)?;
+    let mut keys: Vec<Vec<u8>> = vec![];
+    for key_path in args.auditor_public_keys.iter() {
+        let buf = fs::read(key_path)?;
+        keys.push(buf);
+    }
+    let mut signatures: Vec<Vec<u8>> = vec![];
+    for path in &args.auditor_signatures {
+        let signature = fs::read(path)?;
+        signatures.push(signature);
+    }
+
+    if teaclave_utils::verify_enclave_info(enclave_info_content.as_bytes(), &keys, &signatures) {
+        println!("Enclave info is successfully verified.");
+        Ok(())
+    } else {
+        Err(failure::err_msg("Cannot verify the enclave info."))
+    }
+}
+
+fn main() -> CliResult {
+    let args = Cli::from_args();
+    match args.command {
+        Command::Audit(audit_args) => Ok(audit(audit_args)?),
+        Command::Connect(connect_args) => Ok(connect(connect_args)?),
+    }
+}
diff --git a/teaclave_utils/src/lib.rs b/teaclave_utils/src/lib.rs
index 5372e77..4220309 100644
--- a/teaclave_utils/src/lib.rs
+++ b/teaclave_utils/src/lib.rs
@@ -109,18 +109,18 @@ impl EnclaveMeasurement {
     }
 }
 
-pub fn verify_enclave_info(
-    enclave_info: &[u8],
-    public_keys: &[&[u8]],
-    signatures: &[Vec<u8>],
-) -> bool {
+pub fn verify_enclave_info<T, U>(enclave_info: &[u8], public_keys: &[T], signatures: &[U]) -> bool
+where
+    T: AsRef<[u8]>,
+    U: AsRef<[u8]>,
+{
     use ring::signature;
 
     for k in public_keys {
         let mut verified = false;
         for s in signatures {
             if signature::UnparsedPublicKey::new(&signature::RSA_PKCS1_2048_8192_SHA256, k)
-                .verify(enclave_info, &s)
+                .verify(enclave_info, s.as_ref())
                 .is_ok()
             {
                 verified = true;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org