From 5f75dd0925c32d089cf4f24ba2d0f8882de41c6d Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Tue, 17 Nov 2020 14:35:16 +0100 Subject: [PATCH] Api restructure zum zweiten --- src/api.rs | 61 +++++++++++++++++++++++++++++++++++++++++++ src/{ => api}/auth.rs | 0 src/api/machines.rs | 2 +- src/connection.rs | 55 +++----------------------------------- src/main.rs | 3 +-- 5 files changed, 67 insertions(+), 54 deletions(-) rename src/{ => api}/auth.rs (100%) diff --git a/src/api.rs b/src/api.rs index 98742e7..8d383f0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,2 +1,63 @@ +use std::sync::Arc; + +use capnp::capability::{Params, Results, Promise, FromServer}; + +use crate::schema::connection_capnp; +use crate::connection::Session; + +pub mod auth; mod machine; mod machines; + +use machines::Machines; + +pub struct Bootstrap { + session: Arc +} + +impl Bootstrap { + pub fn new(session: Arc) -> Self { + Self { session } + } +} + +use connection_capnp::bootstrap::*; +impl connection_capnp::bootstrap::Server for Bootstrap { + fn auth(&mut self, + _: Params, + mut res: Results + ) -> Promise<(), capnp::Error> { + // Forbid mutltiple authentication for now + // TODO: When should we allow multiple auth and how do me make sure that does not leak + // priviledges (e.g. due to previously issues caps)? + if self.session.user.is_none() { + res.get().set_auth(capnp_rpc::new_client(auth::Auth::new())) + } + + Promise::ok(()) + } + + fn permissions(&mut self, + _: Params, + mut res: Results + ) -> Promise<(), capnp::Error> { + if self.session.user.is_some() { + } + + Promise::ok(()) + } + + fn machines(&mut self, + _: Params, + mut res: Results + ) -> Promise<(), capnp::Error> { + // TODO actual permission check and stuff + if self.session.user.is_some() { + let c = capnp_rpc::new_client(Machines::new(self.session.clone())); + res.get().set_machines(c); + } + + Promise::ok(()) + } +} + diff --git a/src/auth.rs b/src/api/auth.rs similarity index 100% rename from src/auth.rs rename to src/api/auth.rs diff --git a/src/api/machines.rs b/src/api/machines.rs index 7ef8b9e..2034681 100644 --- a/src/api/machines.rs +++ b/src/api/machines.rs @@ -7,7 +7,7 @@ use crate::schema::api_capnp::machines; use crate::connection::Session; /// An implementation of the `Machines` API -struct Machines { +pub struct Machines { /// A reference to the connection — as long as at least one API endpoint is /// still alive the session has to be as well. session: Arc, diff --git a/src/connection.rs b/src/connection.rs index 37163ec..b72d642 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -5,21 +5,18 @@ use slog::Logger; use smol::net::TcpStream; use crate::error::{Error, Result}; -use crate::auth; -use crate::api; - -pub use crate::schema::connection_capnp; -use crate::db::Databases; +use crate::api::auth; +use crate::api::Bootstrap; use capnp_rpc::{twoparty, rpc_twoparty_capnp}; -use capnp::capability::{Params, Results, Promise, FromServer}; +use crate::schema::connection_capnp; /// Connection context // TODO this should track over several connections pub struct Session { log: Logger, - user: Option, + pub user: Option, } impl Session { @@ -30,50 +27,6 @@ impl Session { } } -struct Bootstrap { - session: Arc -} - -impl Bootstrap { - pub fn new(session: Arc) -> Self { - Self { session } - } -} - -use connection_capnp::bootstrap::*; -impl connection_capnp::bootstrap::Server for Bootstrap { - fn auth(&mut self, - _: Params, - mut res: Results - ) -> Promise<(), capnp::Error> { - // Forbid mutltiple authentication for now - // TODO: When should we allow multiple auth and how do me make sure that does not leak - // priviledges (e.g. due to previously issues caps)? - if self.session.user.is_none() { - res.get().set_auth(capnp_rpc::new_client(auth::Auth::new())) - } - - Promise::ok(()) - } - - fn permissions(&mut self, - _: Params, - mut res: Results - ) -> Promise<(), capnp::Error> { - if self.session.user.is_some() { - } - - Promise::ok(()) - } - - fn machines(&mut self, - _: Params, - mut res: Results - ) -> Promise<(), capnp::Error> { - Promise::ok(()) - } -} - async fn handshake(log: &Logger, stream: &mut TcpStream) -> Result<()> { if let Some(m) = capnp_futures::serialize::read_message(stream.clone(), Default::default()).await? { let greeting = m.get_root::()?; diff --git a/src/main.rs b/src/main.rs index 8ebd526..18a7539 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ extern crate capnp_rpc; #[macro_use] extern crate async_trait; -mod auth; mod modules; mod log; mod api; @@ -144,7 +143,7 @@ fn main() -> Result<(), Error> { let env = Arc::new(env); let mdb = db::machine::init(log.new(o!("system" => "machines")), &config, env.clone()); let pdb = db::access::init(log.new(o!("system" => "permissions")), &config, env.clone()); - let authentication_f = auth::init(log.new(o!("system" => "authentication")), config.clone()); + let authentication_f = api::auth::init(log.new(o!("system" => "authentication")), config.clone()); // If --load or --dump is given we can stop at this point and load/dump the database and then // exit.