diff --git a/Cargo.lock b/Cargo.lock index 44ab4ce..53793ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,6 +121,8 @@ name = "command_gateway" version = "0.1.0" dependencies = [ "prost", + "serde", + "serde_json", "tokio", "tokio-stream", "tonic", @@ -634,11 +636,42 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70" +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + [[package]] name = "serde" version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +dependencies = [ + "itoa", + "ryu", + "serde", +] [[package]] name = "slab" diff --git a/Cargo.toml b/Cargo.toml index 1cc7021..f2567f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,8 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] -crate-type = ["cdylib"] +name = "libcommand" +path = "src/lib.rs" [[bin]] name = "daemon" @@ -18,6 +19,8 @@ name = "client" path = "src/client/main.rs" [dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic tokio-stream = { version = "0.1", features = ["net"] } # Required for tonic with unix socket tower = "0.4" # Required for tonic with unix socket diff --git a/build.rs b/build.rs index 0c941bb..66c5a14 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,4 @@ fn main() -> Result<(), Box> { - tonic_build::compile_protos("proto/helloworld.proto")?; + tonic_build::compile_protos("proto/internal.proto")?; Ok(()) } \ No newline at end of file diff --git a/proto/helloworld.proto b/proto/helloworld.proto deleted file mode 100644 index a4e029b..0000000 --- a/proto/helloworld.proto +++ /dev/null @@ -1,17 +0,0 @@ -syntax = "proto3"; -package helloworld; - -service Greeter { - // Our SayHello rpc accepts HelloRequests and returns HelloReplies - rpc SayHello (HelloRequest) returns (HelloReply); -} - -message HelloRequest { - // Request message contains the name to be greeted - string name = 1; -} - -message HelloReply { - // Reply contains the greeting message - string message = 1; -} \ No newline at end of file diff --git a/proto/internal.proto b/proto/internal.proto new file mode 100644 index 0000000..87deafb --- /dev/null +++ b/proto/internal.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; +package internal; + +service Unix { + // Message send by the command gateway to the daemon + rpc authorize(AuthorizeRequest) returns (AuthorizeResponse); + + // Message send when user quit shell + rpc terminate(TerminateRequest) returns (TerminateResponse); +} + +message AuthorizeRequest { + // identifier of the project + string identifier = 1; + // ssh_keys from ssh agent + string public_ssh_keys = 2; + // command? +} + +enum AuthorizationStatus { + AUTHORIZED = 0; + PERMISSION_DENIED = 1; +} + +message AuthorizeResponse { + AuthorizationStatus status = 1; + string error_message = 2; + string session_uuid = 3; + string log_file = 4; +} + +message TerminateRequest { + string session_uuid = 1; +} + +enum TerminateStatus { + OK = 0; + FAILED = 1; +} + +message TerminateResponse { + TerminateStatus status = 1; + string error_message = 2; +} \ No newline at end of file diff --git a/src/client/main.rs b/src/client/main.rs index f9f80e3..d9707fa 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,10 +1,10 @@ #![cfg_attr(not(unix), allow(unused_imports))] -pub mod hello_world { - tonic::include_proto!("helloworld"); +pub mod internal { + tonic::include_proto!("internal"); } -use hello_world::{greeter_client::GreeterClient, HelloRequest}; +use internal::{unix_client::UnixClient, AuthorizeRequest}; #[cfg(unix)] use tokio::net::UnixStream; use tonic::transport::{Endpoint, Uri}; @@ -15,20 +15,19 @@ use tower::service_fn; async fn main() -> Result<(), Box> { let channel = Endpoint::try_from("http://[::]:50051")? .connect_with_connector(service_fn(|_: Uri| { - let path = "helloworld.sock"; - // Connect to a Uds socket - UnixStream::connect(path) + UnixStream::connect(libcommand::SOCK_FILE) })) .await?; - let mut client = GreeterClient::new(channel); + let mut client = UnixClient::new(channel); - let request = tonic::Request::new(HelloRequest { - name: "Tonic".into(), + let request = tonic::Request::new(AuthorizeRequest { + identifier: "Tonic".into(), + public_ssh_keys: "Tonic".into(), }); - let response = client.say_hello(request).await?; + let response = client.authorize(request).await?; println!("RESPONSE={:?}", response); diff --git a/src/daemon/main.rs b/src/daemon/main.rs index f8d00a1..6a517b3 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -9,32 +9,52 @@ use tokio_stream::wrappers::UnixListenerStream; use tonic::transport::server::UdsConnectInfo; use tonic::{transport::Server, Request, Response, Status}; -pub mod hello_world { - tonic::include_proto!("helloworld"); +pub mod internal { + tonic::include_proto!("internal"); } -use hello_world::{ - greeter_server::{Greeter, GreeterServer}, - HelloReply, HelloRequest, +use internal::{ + unix_server::{Unix, UnixServer}, + AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse }; #[derive(Default)] -pub struct MyGreeter {} +pub struct DaemonServer {} #[tonic::async_trait] -impl Greeter for MyGreeter { - async fn say_hello( +impl Unix for DaemonServer { + async fn authorize( &self, - request: Request, - ) -> Result, Status> { + request: Request, + ) -> Result, Status> { #[cfg(unix)] { let conn_info = request.extensions().get::().unwrap(); println!("Got a request {:?} with info {:?}", request, conn_info); } - let reply = hello_world::HelloReply { - message: format!("Hello {}!", request.into_inner().name), + let reply = internal::AuthorizeResponse { + status: internal::AuthorizationStatus::Authorized.into(), + error_message: "".into(), + log_file: "".into(), + session_uuid: "".into() + }; + Ok(Response::new(reply)) + } + + async fn terminate( + &self, + request: Request, + ) -> Result, Status> { + #[cfg(unix)] + { + let conn_info = request.extensions().get::().unwrap(); + println!("Got a request {:?} with info {:?}", request, conn_info); + } + + let reply = internal::TerminateResponse { + status: internal::TerminateStatus::Ok.into(), + error_message: "".into(), }; Ok(Response::new(reply)) } @@ -43,17 +63,15 @@ impl Greeter for MyGreeter { #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { - let path = "helloworld.sock"; + std::fs::create_dir_all(Path::new(libcommand::SOCK_FILE).parent().unwrap())?; - std::fs::create_dir_all(Path::new(path).parent().unwrap())?; + let server = DaemonServer::default(); - let greeter = MyGreeter::default(); - - let uds = UnixListener::bind(path)?; + let uds = UnixListener::bind(libcommand::SOCK_FILE)?; let uds_stream = UnixListenerStream::new(uds); Server::builder() - .add_service(GreeterServer::new(greeter)) + .add_service(UnixServer::new(server)) .serve_with_incoming(uds_stream) .await?; diff --git a/src/lib.rs b/src/lib.rs index 69bd6be..aa72d3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,8 @@ -pub(self) mod common; \ No newline at end of file +use serde::{Serialize, Deserialize}; + +pub const SOCK_FILE : &'static str = "command_gateway.sock"; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Command { + +} \ No newline at end of file