1
0
Fork 0

Begin implement protocol

This commit is contained in:
Florian RICHER 2023-01-30 21:28:03 +01:00
parent 368d5eca46
commit f145bcf383
8 changed files with 135 additions and 48 deletions

33
Cargo.lock generated
View file

@ -121,6 +121,8 @@ name = "command_gateway"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"prost", "prost",
"serde",
"serde_json",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tonic", "tonic",
@ -634,11 +636,42 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70" checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70"
[[package]]
name = "ryu"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.152" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" 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]] [[package]]
name = "slab" name = "slab"

View file

@ -7,7 +7,8 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib] [lib]
crate-type = ["cdylib"] name = "libcommand"
path = "src/lib.rs"
[[bin]] [[bin]]
name = "daemon" name = "daemon"
@ -18,6 +19,8 @@ name = "client"
path = "src/client/main.rs" path = "src/client/main.rs"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic 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 tokio-stream = { version = "0.1", features = ["net"] } # Required for tonic with unix socket
tower = "0.4" # Required for tonic with unix socket tower = "0.4" # Required for tonic with unix socket

View file

@ -1,4 +1,4 @@
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/helloworld.proto")?; tonic_build::compile_protos("proto/internal.proto")?;
Ok(()) Ok(())
} }

View file

@ -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;
}

44
proto/internal.proto Normal file
View file

@ -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;
}

View file

@ -1,10 +1,10 @@
#![cfg_attr(not(unix), allow(unused_imports))] #![cfg_attr(not(unix), allow(unused_imports))]
pub mod hello_world { pub mod internal {
tonic::include_proto!("helloworld"); tonic::include_proto!("internal");
} }
use hello_world::{greeter_client::GreeterClient, HelloRequest}; use internal::{unix_client::UnixClient, AuthorizeRequest};
#[cfg(unix)] #[cfg(unix)]
use tokio::net::UnixStream; use tokio::net::UnixStream;
use tonic::transport::{Endpoint, Uri}; use tonic::transport::{Endpoint, Uri};
@ -15,20 +15,19 @@ use tower::service_fn;
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let channel = Endpoint::try_from("http://[::]:50051")? let channel = Endpoint::try_from("http://[::]:50051")?
.connect_with_connector(service_fn(|_: Uri| { .connect_with_connector(service_fn(|_: Uri| {
let path = "helloworld.sock";
// Connect to a Uds socket // Connect to a Uds socket
UnixStream::connect(path) UnixStream::connect(libcommand::SOCK_FILE)
})) }))
.await?; .await?;
let mut client = GreeterClient::new(channel); let mut client = UnixClient::new(channel);
let request = tonic::Request::new(HelloRequest { let request = tonic::Request::new(AuthorizeRequest {
name: "Tonic".into(), identifier: "Tonic".into(),
public_ssh_keys: "Tonic".into(),
}); });
let response = client.say_hello(request).await?; let response = client.authorize(request).await?;
println!("RESPONSE={:?}", response); println!("RESPONSE={:?}", response);

View file

@ -9,32 +9,52 @@ use tokio_stream::wrappers::UnixListenerStream;
use tonic::transport::server::UdsConnectInfo; use tonic::transport::server::UdsConnectInfo;
use tonic::{transport::Server, Request, Response, Status}; use tonic::{transport::Server, Request, Response, Status};
pub mod hello_world { pub mod internal {
tonic::include_proto!("helloworld"); tonic::include_proto!("internal");
} }
use hello_world::{ use internal::{
greeter_server::{Greeter, GreeterServer}, unix_server::{Unix, UnixServer},
HelloReply, HelloRequest, AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
}; };
#[derive(Default)] #[derive(Default)]
pub struct MyGreeter {} pub struct DaemonServer {}
#[tonic::async_trait] #[tonic::async_trait]
impl Greeter for MyGreeter { impl Unix for DaemonServer {
async fn say_hello( async fn authorize(
&self, &self,
request: Request<HelloRequest>, request: Request<AuthorizeRequest>,
) -> Result<Response<HelloReply>, Status> { ) -> Result<Response<AuthorizeResponse>, Status> {
#[cfg(unix)] #[cfg(unix)]
{ {
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap(); let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
println!("Got a request {:?} with info {:?}", request, conn_info); println!("Got a request {:?} with info {:?}", request, conn_info);
} }
let reply = hello_world::HelloReply { let reply = internal::AuthorizeResponse {
message: format!("Hello {}!", request.into_inner().name), status: internal::AuthorizationStatus::Authorized.into(),
error_message: "".into(),
log_file: "".into(),
session_uuid: "".into()
};
Ok(Response::new(reply))
}
async fn terminate(
&self,
request: Request<TerminateRequest>,
) -> Result<Response<TerminateResponse>, Status> {
#[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().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)) Ok(Response::new(reply))
} }
@ -43,17 +63,15 @@ impl Greeter for MyGreeter {
#[cfg(unix)] #[cfg(unix)]
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(libcommand::SOCK_FILE)?;
let uds = UnixListener::bind(path)?;
let uds_stream = UnixListenerStream::new(uds); let uds_stream = UnixListenerStream::new(uds);
Server::builder() Server::builder()
.add_service(GreeterServer::new(greeter)) .add_service(UnixServer::new(server))
.serve_with_incoming(uds_stream) .serve_with_incoming(uds_stream)
.await?; .await?;

View file

@ -1 +1,8 @@
pub(self) mod common; use serde::{Serialize, Deserialize};
pub const SOCK_FILE : &'static str = "command_gateway.sock";
#[derive(Serialize, Deserialize, Debug)]
pub struct Command {
}