teaclave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ms...@apache.org
Subject [incubator-teaclave] 02/02: [services] Use jinja2 template for proto_gen
Date Sun, 05 Jan 2020 07:19:49 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 3aad0d8d6a865d54fdca5a7093467c35acd43e80
Author: Mingshen Sun <bob@mssun.me>
AuthorDate: Sat Jan 4 23:14:32 2020 -0800

    [services] Use jinja2 template for proto_gen
    
    Fix #201
---
 .drone.yml                                         |  20 +--
 ipc/src/macros.rs                                  |   3 +-
 services/proto_gen/Cargo.toml                      |   1 +
 services/proto_gen/main.rs                         | 193 ++-------------------
 services/proto_gen/templates/proto.j2              |  59 +++++++
 services/teaclave_frontend/proto/src/proto.rs      |   4 -
 services/teaclave_frontend/sgx_app/src/main.rs     |   2 +-
 .../sgx_trusted_lib/src/service.rs                 |   2 +-
 8 files changed, 84 insertions(+), 200 deletions(-)

diff --git a/.drone.yml b/.drone.yml
index 2ef245e..562e1b5 100644
--- a/.drone.yml
+++ b/.drone.yml
@@ -17,7 +17,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
   commands:
   - . /root/.cargo/env
-  # - export RUSTFLAGS="-D warnings"
+  - export RUSTFLAGS="-D warnings"
   - cd build && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
@@ -68,7 +68,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
   commands:
   - . /root/.cargo/env
-  # - export RUSTFLAGS="-D warnings"
+  - export RUSTFLAGS="-D warnings"
   - cd build && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
@@ -119,8 +119,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
   commands:
   - . /root/.cargo/env
-  - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DSGX_SIM_MODE=ON .. && make
VERBOSE=1 -j2
-  # - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON
.. && make VERBOSE=1 -j2
+  - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON
.. && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
 #   commands:
@@ -151,8 +150,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
   commands:
   - . /root/.cargo/env
-  - cd build && cmake -DSGX_SIM_MODE=ON .. && make VERBOSE=1 -j2
-  # - cd build && cmake -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON .. &&
make VERBOSE=1 -j2
+  - cd build && cmake -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON .. && make
VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1804:0.1.2
 #   commands:
@@ -183,7 +181,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
   commands:
   - . /root/.cargo/env
-  # - export RUSTFLAGS="-D warnings"
+  - export RUSTFLAGS="-D warnings"
   - cd build && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
@@ -234,7 +232,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
   commands:
   - . /root/.cargo/env
-  # - export RUSTFLAGS="-D warnings"
+  - export RUSTFLAGS="-D warnings"
   - cd build && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
@@ -285,8 +283,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
   commands:
   - . /root/.cargo/env
-  - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DSGX_SIM_MODE=ON .. && make
VERBOSE=1 -j2
-  # - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON
.. && make VERBOSE=1 -j2
+  - cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON
.. && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
 #   commands:
@@ -317,8 +314,7 @@ steps:
   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
   commands:
   - . /root/.cargo/env
-  - cd build && cmake -DSGX_SIM_MODE=ON -DTEST_MODE=ON .. && make VERBOSE=1
-j2
-  # - cd build && cmake -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON -DTEST_MODE=ON
.. && make VERBOSE=1 -j2
+  - cd build && cmake -DRUSTFLAGS="-D warnings" -DSGX_SIM_MODE=ON -DTEST_MODE=ON
.. && make VERBOSE=1 -j2
 # - name: sgx-test
 #   image: mesalocklinux/mesatee-build-ubuntu-1604:0.1.2
 #   commands:
diff --git a/ipc/src/macros.rs b/ipc/src/macros.rs
index 5db3bee..410487e 100644
--- a/ipc/src/macros.rs
+++ b/ipc/src/macros.rs
@@ -79,7 +79,8 @@ macro_rules! register_ecall_handler {
             teaclave_ipc::channel::ECallReceiver::dispatch(input, instance)
         }
 
-        // The actual ecall funcation defined in .edl.
+        /// The actual ecall funcation defined in .edl.
+        #[allow(clippy::not_unsafe_ptr_arg_deref)]
         #[no_mangle]
         pub extern "C" fn ecall_ipc_entry_point(
             cmd: u32,
diff --git a/services/proto_gen/Cargo.toml b/services/proto_gen/Cargo.toml
index 06fd52b..b1046bb 100644
--- a/services/proto_gen/Cargo.toml
+++ b/services/proto_gen/Cargo.toml
@@ -15,3 +15,4 @@ structopt = { version = "0.3" }
 prost-build = { version = "0.5.0" }
 prost = { version = "0.5.0" }
 prost-types = { version = "0.5.0" }
+askama = "0.8"
diff --git a/services/proto_gen/main.rs b/services/proto_gen/main.rs
index 4035d48..cb2087a 100644
--- a/services/proto_gen/main.rs
+++ b/services/proto_gen/main.rs
@@ -15,6 +15,8 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use askama;
+use askama::Template;
 use prost_build;
 use std::path;
 use structopt::StructOpt;
@@ -22,193 +24,22 @@ use structopt::StructOpt;
 #[derive(Debug)]
 pub struct MesaTEEServiceGenerator;
 
-/// How to use prost. See kms as an example.
-/// 1. Define rpc messages with protobuf 2/3 syntax. protobuf 2 is recommended
-/// because we can avoid unneccessary option.
-/// 2. Define services. Prost will generate corresponding sevices and clients.
-/// 3. Include ```${OUT_DIR}/kms_proto.rs``` and provide serializer and
-/// deserializer if needed..
-/// 4. Todo: add support for automatic authentication
-const LINE_ENDING: &'static str = "\n";
-impl MesaTEEServiceGenerator {
-    fn generate_structure(&mut self, service: &prost_build::Service, buf: &mut
String) {
-        let request_name = format!("{}{}", &service.proto_name, "Request");
-        let response_name = format!("{}{}", &service.proto_name, "Response");
-        // Generate request enum structure
-        buf.push_str("#[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize,
Debug)]");
-        buf.push_str(LINE_ENDING);
-        buf.push_str(r#"#[serde(tag = "type", rename_all = "snake_case")]"#);
-
-        buf.push_str(&format!("pub enum {} {{", &request_name));
-        buf.push_str(LINE_ENDING);
-        for method in &service.methods {
-            buf.push_str(&format!(
-                "    {}({}),",
-                method.proto_name, method.input_type
-            ));
-            buf.push_str(LINE_ENDING);
-        }
-        buf.push_str("}");
-        buf.push_str(LINE_ENDING);
-
-        // Generate response enum structure
-        buf.push_str("#[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize,
Debug)]");
-        buf.push_str(LINE_ENDING);
-        buf.push_str(r#"#[serde(tag = "type", rename_all = "snake_case")]"#);
-        buf.push_str(LINE_ENDING);
-        buf.push_str(&format!("pub enum {} {{", &response_name));
-        buf.push_str(LINE_ENDING);
-        for method in &service.methods {
-            buf.push_str(&format!(
-                "    {}({}),",
-                method.proto_name, method.output_type
-            ));
-            buf.push_str(LINE_ENDING);
-        }
-        buf.push_str("}");
-        buf.push_str(LINE_ENDING);
-    }
-
-    fn generate_service(&mut self, service: &prost_build::Service, buf: &mut
String) {
-        let request_name = format!("{}{}", &service.proto_name, "Request");
-        let response_name = format!("{}{}", &service.proto_name, "Response");
-        let service_trait_name = &service.proto_name;
-        // Genreate trait
-        buf.push_str(&format!("pub trait {} {{", &service_trait_name));
-        buf.push_str(LINE_ENDING);
-        for method in &service.methods {
-            buf.push_str(&format!(
-                "    fn {}(req: crate::{}) -> Result<crate::{}>;",
-                method.name, method.input_type, method.output_type
-            ));
-            buf.push_str(LINE_ENDING);
-        }
-        // Generate dispatch
-        buf.push_str(&format!(
-            "    fn dispatch(&self, req: {}) -> Result<{}> {{",
-            &request_name, &response_name
-        ));
-        buf.push_str(LINE_ENDING);
-
-        // authentication
-        let mut need_authentication: bool = false;
-        for comment in service.comments.leading.iter() {
-            if comment.contains("@need_authentication") {
-                need_authentication = true;
-            }
-        }
-        if need_authentication {
-            buf.push_str("        let authenticated = match req {");
-            buf.push_str(LINE_ENDING);
-            for method in &service.methods {
-                buf.push_str(&format!(
-                    "            {}::{}(ref req) => req.creds.auth(),",
-                    &request_name, &method.proto_name
-                ));
-                buf.push_str(LINE_ENDING);
-            }
-            buf.push_str("        };");
-            buf.push_str(
-                r#"
-        if !authenticated {
-            return Err(anyhow!("permission deny error"));
-        }
-"#,
-            )
-        }
-
-        // dispatch request
-        buf.push_str("        match req {");
-        buf.push_str(LINE_ENDING);
-        for method in &service.methods {
-            let request_str = format!("{}Request", &method.proto_name);
-            let response_str = format!("{}Response", &method.proto_name);
-            buf.push_str(&format!(
-                "            {}::{}(req) => {{",
-                &request_name, &method.proto_name
-            ));
-            buf.push_str(&format!(
-                "let req = crate::{}::try_from(req)?;",
-                &request_str
-            ));
-            buf.push_str(&format!(
-                "                let resp = {}::from(Self::{}(req)?);",
-                &response_str, method.name,
-            ));
-            buf.push_str(&format!(
-                "                Ok(resp).map({}::{})",
-                &response_name, &method.proto_name
-            ));
-            buf.push_str(&format!("            }},",));
-        }
-        buf.push_str("        }");
-        buf.push_str(LINE_ENDING);
-        buf.push_str("    }");
-        buf.push_str(LINE_ENDING);
-        buf.push_str("}");
-        buf.push_str(LINE_ENDING);
-    }
-
-    fn generate_client(&mut self, service: &prost_build::Service, buf: &mut String)
{
-        let request_name = format!("{}{}", &service.proto_name, "Request");
-        let response_name = format!("{}{}", &service.proto_name, "Response");
-        let client_name = format!("{}{}", &service.proto_name, "Client");
-        buf.push_str(&format!("pub struct {} {{", &client_name));
-        buf.push_str(LINE_ENDING);
-        buf.push_str(&format!(
-            "    channel: channel::SgxTrustedTlsChannel<{}, {}>,",
-            request_name, response_name
-        ));
-        buf.push_str(LINE_ENDING);
-        buf.push_str("}");
-        buf.push_str(LINE_ENDING);
-
-        // impl new
-        buf.push_str(&format!(
-            r#"
-impl {} {{
-    pub fn new(channel: channel::SgxTrustedTlsChannel<{}, {}>) -> Result<Self>
{{
-        Ok(Self {{ channel }})
-    }}
-}}
-"#,
-            client_name, request_name, response_name
-        ));
+#[derive(Template)]
+#[template(path = "proto.j2")]
+struct ProtoTemplate<'a> {
+    service: &'a prost_build::Service,
+}
 
-        // impl operation
-        buf.push_str(&format!("impl {} {{", client_name));
-        for method in &service.methods {
-            buf.push_str(&format!(
-                r#"
-    pub fn {}(&mut self, req: {}) -> Result<{}> {{
-        let req = {}::{}(req);
-        let resp = self.channel.invoke(req)?;
-        match resp {{
-            {}::{}(resp) => Ok(resp),
-            _ => Err(anyhow!("rpc error"))
-        }}
-    }}
-"#,
-                &method.name,
-                &method.input_type,
-                &method.output_type,
-                &request_name,
-                &method.proto_name,
-                &response_name,
-                &method.proto_name
-            ));
-        }
-        buf.push_str("}");
-        buf.push_str(LINE_ENDING);
+impl MesaTEEServiceGenerator {
+    fn generate_from_template(&mut self, service: &prost_build::Service, buf: &mut
String) {
+        let proto_template = ProtoTemplate { service };
+        buf.push_str(&proto_template.render().unwrap());
     }
 }
 
 impl prost_build::ServiceGenerator for MesaTEEServiceGenerator {
     fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
-        buf.push_str("use core::convert::TryFrom;");
-        self.generate_structure(&service, buf);
-        self.generate_service(&service, buf);
-        self.generate_client(&service, buf);
+        self.generate_from_template(&service, buf);
     }
 }
 
diff --git a/services/proto_gen/templates/proto.j2 b/services/proto_gen/templates/proto.j2
new file mode 100644
index 0000000..bc28e90
--- /dev/null
+++ b/services/proto_gen/templates/proto.j2
@@ -0,0 +1,59 @@
+use anyhow;
+use teaclave_rpc;
+use core::convert::TryFrom;
+
+#[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize, Debug)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum {{ service.proto_name }}Request {
+    {% for m in service.methods %}
+    {{ m.proto_name }}({{ m.input_type }}),
+    {% endfor %}
+}
+
+#[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize, Debug)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum {{ service.proto_name }}Response {
+    {% for m in service.methods %}
+    {{ m.proto_name }}({{ m.output_type }}),
+    {% endfor %}
+}
+
+pub trait {{ service.proto_name }} {
+    {% for m in service.methods %}
+    fn {{ m.name }}(request: crate::{{ m.input_type }}) -> anyhow::Result<crate::{{
m.output_type }}>;
+    {% endfor %}
+    fn dispatch(&self, request: {{ service.proto_name }}Request) -> anyhow::Result<{{
service.proto_name }}Response> {
+        match request {
+            {% for m in service.methods %}
+            {{ service.proto_name }}Request::{{ m.proto_name }}(request) => {
+                let request = crate::{{ m.input_type }}::try_from(request)?;
+                let response = {{ m.output_type }}::from(Self::{{ m.name }}(request)?);
+                Ok(response).map({{ service.proto_name }}Response::{{ m.proto_name }})
+            },
+            {% endfor %}
+        }
+    }
+}
+
+pub struct {{ service.proto_name }}Client {
+    channel: teaclave_rpc::channel::SgxTrustedTlsChannel<{{ service.proto_name }}Request,
{{ service.proto_name }}Response>,
+}
+
+impl {{ service.proto_name }}Client {
+    pub fn new(channel: teaclave_rpc::channel::SgxTrustedTlsChannel<{{ service.proto_name
}}Request, {{ service.proto_name }}Response>) -> anyhow::Result<Self> {
+        Ok(Self { channel })
+    }
+
+    {% for m in service.methods %}
+    pub fn {{ m.name }}(&mut self, request: {{ m.input_type }}) -> anyhow::Result<{{
m.output_type }}> {
+        let request = {{ service.proto_name }}Request::UserLogin(request);
+        let response = self.channel.invoke(request)?;
+        match response {
+            {{ service.proto_name }}Response::{{ m.proto_name }}(response) => Ok(response),
+            {% if service.methods.len() > 1 -%}
+            _ => Err(anyhow!("rpc error")),
+            {% endif %}
+        }
+    }
+    {% endfor %}
+}
\ No newline at end of file
diff --git a/services/teaclave_frontend/proto/src/proto.rs b/services/teaclave_frontend/proto/src/proto.rs
index 96cec67..a9745a5 100644
--- a/services/teaclave_frontend/proto/src/proto.rs
+++ b/services/teaclave_frontend/proto/src/proto.rs
@@ -1,7 +1,3 @@
 #![allow(clippy::all)]
 
-use anyhow::anyhow;
-use anyhow::Result;
-use teaclave_rpc::channel;
-
 include!(concat!(env!("OUT_DIR"), "/teaclave_frontend_proto.rs"));
diff --git a/services/teaclave_frontend/sgx_app/src/main.rs b/services/teaclave_frontend/sgx_app/src/main.rs
index 8fd63ac..23ccd1f 100644
--- a/services/teaclave_frontend/sgx_app/src/main.rs
+++ b/services/teaclave_frontend/sgx_app/src/main.rs
@@ -82,7 +82,7 @@ fn run(tee: Arc<TeeBinder>) -> anyhow::Result<()> {
     info!("Running {}...", env!("CARGO_PKG_NAME"));
 
     start_enclave_service(tee.clone());
-    handle_connection(tee.clone())?;
+    handle_connection(tee)?;
 
     Ok(())
 }
diff --git a/services/teaclave_frontend/sgx_trusted_lib/src/service.rs b/services/teaclave_frontend/sgx_trusted_lib/src/service.rs
index 5d0bbad..9e4fe7e 100644
--- a/services/teaclave_frontend/sgx_trusted_lib/src/service.rs
+++ b/services/teaclave_frontend/sgx_trusted_lib/src/service.rs
@@ -6,7 +6,7 @@ use teaclave_service_sgx_utils::teaclave_service;
 pub(crate) struct TeaclaveFrontendService;
 
 impl TeaclaveFrontend for TeaclaveFrontendService {
-    fn user_login(request: UserLoginRequest) -> anyhow::Result<UserLoginResponse>
{
+    fn user_login(_request: UserLoginRequest) -> anyhow::Result<UserLoginResponse>
{
         let response = UserLoginResponse {
             token: "test_token".to_string(),
         };


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


Mime
View raw message