2020-02-14 12:20:17 +01:00
|
|
|
// module needs to be top level for generated functions to be in scope:
|
|
|
|
// https://github.com/capnproto/capnproto-rust/issues/16
|
2020-02-17 03:44:02 +01:00
|
|
|
pub mod api {
|
2020-02-14 12:20:17 +01:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/schema/api_capnp.rs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::default::Default;
|
|
|
|
use async_std::net::TcpStream;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use futures::task::Spawn;
|
|
|
|
use futures::FutureExt;
|
2020-02-14 12:20:17 +01:00
|
|
|
use futures_signals::signal::Mutable;
|
|
|
|
use casbin::Enforcer;
|
|
|
|
use casbin::MgmtApi;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use slog::Logger;
|
|
|
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
use async_std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
use crate::machine::{MachinesProvider, Machines};
|
|
|
|
use crate::auth::{AuthenticationProvider, Authentication};
|
|
|
|
use crate::access::{PermissionsProvider, Permissions};
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-02-17 00:50:42 +01:00
|
|
|
use capnp::{Error};
|
2020-02-17 03:44:02 +01:00
|
|
|
use capnp::capability::Promise;
|
2020-02-17 00:50:42 +01:00
|
|
|
use capnp_rpc::RpcSystem;
|
|
|
|
use capnp_rpc::twoparty::VatNetwork;
|
|
|
|
use capnp_rpc::rpc_twoparty_capnp::Side;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2020-02-17 00:50:42 +01:00
|
|
|
use api::diflouroborane;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct API<S> {
|
|
|
|
auth: Arc<RwLock<AuthenticationProvider>>,
|
|
|
|
perm: Arc<RwLock<PermissionsProvider>>,
|
|
|
|
mach: Arc<RwLock<MachinesProvider>>,
|
|
|
|
|
|
|
|
spawner: S,
|
|
|
|
}
|
|
|
|
impl<S: Spawn> API<S> {
|
|
|
|
pub fn new(auth: AuthenticationProvider,
|
|
|
|
perm: PermissionsProvider,
|
|
|
|
mach: MachinesProvider,
|
|
|
|
spawner: S)
|
|
|
|
-> Self
|
|
|
|
{
|
|
|
|
let auth = Arc::new(RwLock::new(auth));
|
|
|
|
let perm = Arc::new(RwLock::new(perm));
|
|
|
|
let mach = Arc::new(RwLock::new(mach));
|
|
|
|
|
|
|
|
Self { auth, perm, mach, spawner }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_connection(self) -> Bootstrap {
|
|
|
|
let auth = Rc::new(Authentication::new(self.auth));
|
|
|
|
let perm = Rc::new(Permissions::new(self.perm, auth.clone()));
|
|
|
|
let mach = Machines::new(self.mach, perm.clone());
|
|
|
|
Bootstrap {
|
|
|
|
auth: auth,
|
|
|
|
perm: perm,
|
|
|
|
mach: mach,
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
pub async fn handle_connection<S: Spawn>(api: API<S>, log: Logger, socket: TcpStream) -> Result<(), Error> {
|
|
|
|
info!(log, "A new connection");
|
|
|
|
let client = api.into_connection();
|
|
|
|
let a = api::diflouroborane::ToClient::new(client).into_client::<capnp_rpc::Server>();
|
|
|
|
|
2020-02-17 00:50:42 +01:00
|
|
|
let netw = VatNetwork::new(socket.clone(), socket, Side::Server, Default::default());
|
2020-02-18 16:55:19 +01:00
|
|
|
|
|
|
|
let rpc = RpcSystem::new(Box::new(netw), Some(a.clone().client)).map(|_| ());
|
|
|
|
|
|
|
|
rpc.await;
|
|
|
|
|
|
|
|
Ok(())
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
/// Bootstrap capability of the Diflouroborane API
|
|
|
|
///
|
|
|
|
/// This is the starting point for any client connecting
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Bootstrap {
|
|
|
|
auth: Rc<Authentication>,
|
|
|
|
perm: Rc<Permissions>,
|
2020-02-17 03:44:02 +01:00
|
|
|
mach: Machines,
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
impl diflouroborane::Server for Bootstrap {
|
2020-02-17 03:44:02 +01:00
|
|
|
fn authentication(&mut self,
|
|
|
|
_params: diflouroborane::AuthenticationParams,
|
|
|
|
mut results: diflouroborane::AuthenticationResults)
|
|
|
|
-> Promise<(), Error>
|
2020-02-16 16:02:03 +01:00
|
|
|
{
|
2020-02-17 03:44:02 +01:00
|
|
|
let mut b = results.get();
|
2020-02-18 16:55:19 +01:00
|
|
|
let auth = api::authentication::ToClient::new(self.auth.deref().clone()).into_client::<capnp_rpc::Server>();
|
2020-02-17 03:44:02 +01:00
|
|
|
b.set_auth(auth);
|
|
|
|
Promise::ok(())
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 03:44:02 +01:00
|
|
|
fn permissions(&mut self,
|
|
|
|
_params: diflouroborane::PermissionsParams,
|
|
|
|
mut results: diflouroborane::PermissionsResults)
|
|
|
|
-> Promise<(), Error>
|
2020-02-16 16:02:03 +01:00
|
|
|
{
|
2020-02-18 16:55:19 +01:00
|
|
|
//let mut b = results.get();
|
|
|
|
//let perm = api::permissions::ToClient::new(self.perm).into_client::<capnp_rpc::Server>();
|
|
|
|
//b.set_perm(perm);
|
2020-02-17 03:44:02 +01:00
|
|
|
Promise::ok(())
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 03:44:02 +01:00
|
|
|
fn machines(&mut self,
|
|
|
|
_params: diflouroborane::MachinesParams,
|
|
|
|
mut results: diflouroborane::MachinesResults)
|
|
|
|
-> Promise<(), Error>
|
2020-02-16 16:02:03 +01:00
|
|
|
{
|
|
|
|
let mut b = results.get();
|
2020-02-17 03:44:02 +01:00
|
|
|
let mach = api::machines::ToClient::new(self.mach.clone()).into_client::<capnp_rpc::Server>();
|
|
|
|
b.set_mach(mach);
|
|
|
|
Promise::ok(())
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|