From 2e5f343d528080db437c45fda54643ae89d853af Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Tue, 15 Mar 2022 19:56:41 +0100 Subject: [PATCH] Tracing and dumping --- bffhd/authorization/mod.rs | 4 ---- bffhd/capnp/authenticationsystem.rs | 2 ++ bffhd/capnp/connection.rs | 22 ++++++++++++++++++++-- bffhd/capnp/mod.rs | 19 ++++++++++++++++--- bffhd/db/mod.rs | 10 ++++------ bffhd/resources/state/db.rs | 2 +- bffhd/users/mod.rs | 5 ++++- bin/bffhd/main.rs | 4 +++- 8 files changed, 50 insertions(+), 18 deletions(-) diff --git a/bffhd/authorization/mod.rs b/bffhd/authorization/mod.rs index 93e043b..91bc1ae 100644 --- a/bffhd/authorization/mod.rs +++ b/bffhd/authorization/mod.rs @@ -22,8 +22,4 @@ impl AuthorizationHandle { let user = self.users.get_user(uid.as_ref())?; Some(user.userdata.roles.clone()) } - - pub fn is_permitted<'a>(&self, roles: impl IntoIterator, perm: impl AsRef) -> bool { - unimplemented!() - } } \ No newline at end of file diff --git a/bffhd/capnp/authenticationsystem.rs b/bffhd/capnp/authenticationsystem.rs index a02f96e..a4ee83b 100644 --- a/bffhd/capnp/authenticationsystem.rs +++ b/bffhd/capnp/authenticationsystem.rs @@ -54,6 +54,8 @@ enum State { impl AuthenticationSystem for Authentication { fn step(&mut self, params: StepParams, mut results: StepResults) -> Promise<(), Error> { + let span = tracing::trace_span!("step"); + let _guard = span.enter(); let mut builder = results.get(); if let State::Running(mut session, manager) = std::mem::replace(&mut self.state, State::Aborted) diff --git a/bffhd/capnp/connection.rs b/bffhd/capnp/connection.rs index f7f46d4..718e24d 100644 --- a/bffhd/capnp/connection.rs +++ b/bffhd/capnp/connection.rs @@ -1,3 +1,4 @@ +use std::net::SocketAddr; pub use api::connection_capnp::bootstrap::Client; use api::connection_capnp::bootstrap; @@ -10,13 +11,16 @@ use crate::session::SessionManager; /// Cap'n Proto API Handler pub struct BootCap { + peer_addr: SocketAddr, authentication: AuthenticationHandle, sessionmanager: SessionManager, } impl BootCap { - pub fn new(authentication: AuthenticationHandle, sessionmanager: SessionManager) -> Self { + pub fn new(peer_addr: SocketAddr, authentication: AuthenticationHandle, sessionmanager: SessionManager) -> Self { + tracing::trace!(%peer_addr, "bootstrapping RPC"); Self { + peer_addr, authentication, sessionmanager, } @@ -29,6 +33,8 @@ impl bootstrap::Server for BootCap { _: bootstrap::GetAPIVersionParams, _: bootstrap::GetAPIVersionResults, ) -> Promise<(), ::capnp::Error> { + let span = tracing::trace_span!("get_api_version", peer_addr=%self.peer_addr); + let _guard = span.enter(); Promise::ok(()) } @@ -37,6 +43,8 @@ impl bootstrap::Server for BootCap { _: bootstrap::GetServerReleaseParams, mut result: bootstrap::GetServerReleaseResults, ) -> Promise<(), ::capnp::Error> { + let span = tracing::trace_span!("get_server_release", peer_addr=%self.peer_addr); + let _guard = span.enter(); let mut builder = result.get(); builder.set_name("bffhd"); builder.set_release(crate::RELEASE_STRING); @@ -45,9 +53,14 @@ impl bootstrap::Server for BootCap { fn mechanisms( &mut self, - _: bootstrap::MechanismsParams, + params: bootstrap::MechanismsParams, mut result: bootstrap::MechanismsResults, ) -> Promise<(), ::capnp::Error> { + let span = tracing::trace_span!("mechanisms", peer_addr=%self.peer_addr); + let _guard = span.enter(); + + tracing::trace!("mechanisms"); + let mut builder = result.get(); let mechs: Vec<_> = self.authentication.list_available_mechs() .into_iter() @@ -66,9 +79,14 @@ impl bootstrap::Server for BootCap { params: bootstrap::CreateSessionParams, mut result: bootstrap::CreateSessionResults, ) -> Promise<(), ::capnp::Error> { + let span = tracing::trace_span!("create_session", peer_addr=%self.peer_addr); + let _guard = span.enter(); + let params = pry!(params.get()); let mechanism: &str = pry!(params.get_mechanism()); + tracing::trace!(mechanism); + let mechname = Mechname::new(mechanism.as_bytes()); let auth = if let Ok(mechname) = mechname { if let Ok(session) = self.authentication.start(mechname) { diff --git a/bffhd/capnp/mod.rs b/bffhd/capnp/mod.rs index 165aac9..f483b2f 100644 --- a/bffhd/capnp/mod.rs +++ b/bffhd/capnp/mod.rs @@ -16,7 +16,9 @@ use std::fs::File; use std::future::Future; use std::io; use std::io::BufReader; +use std::net::SocketAddr; use std::sync::Arc; +use nix::sys::socket::SockAddr; use crate::authentication::AuthenticationHandle; use crate::authorization::AuthorizationHandle; @@ -107,6 +109,8 @@ impl APIServer { .collect() .await; + tracing::info!("listening on {:?}", sockets); + if sockets.is_empty() { tracing::warn!("No usable listen addresses configured for the API server!"); } @@ -123,16 +127,25 @@ impl APIServer { .take_until(stop) .for_each(|stream| async { match stream { - Ok(stream) => self.handle(self.acceptor.accept(stream)), + Ok(stream) => { + if let Ok(peer_addr) = stream.peer_addr() { + self.handle(peer_addr, self.acceptor.accept(stream)) + } else { + tracing::error!(?stream, "failing a TCP connection with no peer addr"); + } + }, Err(e) => tracing::warn!("Failed to accept stream: {}", e), } - }); + }).await; + tracing::info!("closing down API handler"); } fn handle( &self, + peer_addr: SocketAddr, stream: impl Future>>, ) { + tracing::debug!("handling new API connection"); let f = async move { let stream = match stream.await { Ok(stream) => stream, @@ -144,7 +157,7 @@ impl APIServer { let (rx, tx) = futures_lite::io::split(stream); let vat = VatNetwork::new(rx, tx, Side::Server, Default::default()); - let bootstrap: connection::Client = capnp_rpc::new_client(connection::BootCap::new(self.authentication.clone(), self.sessionmanager.clone())); + let bootstrap: connection::Client = capnp_rpc::new_client(connection::BootCap::new(peer_addr, self.authentication.clone(), self.sessionmanager.clone())); if let Err(e) = RpcSystem::new(Box::new(vat), Some(bootstrap.client)).await { tracing::error!("Error during RPC handling: {}", e); diff --git a/bffhd/db/mod.rs b/bffhd/db/mod.rs index 00238dc..7be546c 100644 --- a/bffhd/db/mod.rs +++ b/bffhd/db/mod.rs @@ -49,10 +49,10 @@ use rkyv::Infallible; use crate::resources::state::{State, db::StateDB}; use std::iter::FromIterator; use std::ops::Deref; -use crate::authentication::db::PassDB; use crate::resources::search::ResourcesHandle; use crate::utils::oid::{ArchivedObjectIdentifier, ObjectIdentifier}; use crate::resources::state::value::SerializeValue; +use crate::Users; #[derive(Debug)] pub enum DBError { @@ -130,14 +130,12 @@ impl>> Adapter for AlignedAdapter #[derive(Debug, serde::Serialize)] pub struct Dump { users: HashMap, - passwds: HashMap, states: HashMap, } impl Dump { - pub fn new(userdb: UserDB, passdb: PassDB, resources: ResourcesHandle) -> Result { - let users = HashMap::from_iter(userdb.get_all()?.into_iter()); - let passwds = HashMap::from_iter(passdb.get_all()?.into_iter()); + pub fn new(userdb: Users, resources: ResourcesHandle) -> Result { + let users = HashMap::from_iter(userdb.into_inner().get_all()?.into_iter()); let mut states = HashMap::new(); for resource in resources.list_all().into_iter() { if let Some(output) = resource.get_raw_state() { @@ -147,6 +145,6 @@ impl Dump { } } - Ok(Self { users, passwds, states }) + Ok(Self { users, states }) } } \ No newline at end of file diff --git a/bffhd/resources/state/db.rs b/bffhd/resources/state/db.rs index 6634b16..4e3117c 100644 --- a/bffhd/resources/state/db.rs +++ b/bffhd/resources/state/db.rs @@ -47,7 +47,7 @@ impl StateDB { | EnvironmentFlags::NO_SUB_DIR | EnvironmentFlags::NO_TLS | EnvironmentFlags::NO_READAHEAD) - .set_max_dbs(2) + .set_max_dbs(4) .open(path.as_ref()) .map(Arc::new) } diff --git a/bffhd/users/mod.rs b/bffhd/users/mod.rs index bfec026..2a4639e 100644 --- a/bffhd/users/mod.rs +++ b/bffhd/users/mod.rs @@ -25,7 +25,6 @@ use std::sync::Arc; pub mod db; -pub use crate::authentication::db::PassDB; use crate::authorization::roles::Role; use crate::db::LMDBorrow; use crate::users::db::UserData; @@ -80,6 +79,10 @@ impl Users { Ok(Self { userdb }) } + pub(crate) fn into_inner(self) -> &'static UserDB { + self.userdb + } + pub fn get_user(&self, uid: &str) -> Option { tracing::trace!(uid, "Looking up user"); self.userdb diff --git a/bin/bffhd/main.rs b/bin/bffhd/main.rs index 394e3e6..9905aa0 100644 --- a/bin/bffhd/main.rs +++ b/bin/bffhd/main.rs @@ -112,7 +112,9 @@ fn main() -> anyhow::Result<()> { let mut config = config::read(&PathBuf::from_str(configpath).unwrap()).unwrap(); if matches.is_present("dump") { - unimplemented!() + let bffh = Diflouroborane::new(config)?; + let dump = Dump::new(bffh.users, bffh.resources)?; + println!("{:?}", dump); } else if matches.is_present("load") { let bffh = Diflouroborane::new(config)?; bffh.users.load_file(matches.value_of("load").unwrap());