fabaccess-bffh/bffhd/lib.rs

146 lines
4.0 KiB
Rust
Raw Permalink Normal View History

2022-03-10 20:52:34 +01:00
#![warn(unused_imports, unused_import_braces)]
//#![warn(missing_debug_implementations)]
//#![warn(missing_docs)]
//#![warn(missing_crate_level_docs)]
2021-10-28 00:32:25 +02:00
2021-10-28 01:10:35 +02:00
//! Diflouroborane
//!
2022-03-08 16:41:38 +01:00
//! This is the capnp component of the FabAccess project.
2021-10-28 01:10:35 +02:00
//! The entry point of bffhd can be found in [bin/bffhd/main.rs](../bin/bffhd/main.rs)
2021-10-27 23:20:35 +02:00
pub mod config;
2021-10-28 01:10:35 +02:00
/// Internal Databases build on top of LMDB, a mmap()'ed B-tree DB optimized for reads
2021-10-27 23:20:35 +02:00
pub mod db;
2021-11-26 02:25:48 +01:00
2021-10-28 01:10:35 +02:00
/// Shared error type
2021-10-27 23:20:35 +02:00
pub mod error;
2021-11-26 02:25:48 +01:00
2021-11-26 21:01:43 +01:00
pub mod users;
2022-03-08 18:52:49 +01:00
pub mod authentication;
2022-03-10 20:52:34 +01:00
pub mod authorization;
2021-11-26 21:01:43 +01:00
2021-10-28 01:10:35 +02:00
/// Resources
2021-11-26 02:25:48 +01:00
pub mod resources;
pub mod actors;
pub mod sensors;
2022-03-08 16:41:38 +01:00
pub mod capnp;
2021-11-26 21:01:43 +01:00
pub mod utils;
mod tls;
mod keylog;
mod logging;
2022-03-11 23:00:02 +01:00
mod audit;
2022-03-12 17:31:53 +01:00
mod session;
2022-03-15 20:00:43 +01:00
use std::sync::{Arc};
use anyhow::Context;
2022-03-15 20:00:43 +01:00
2022-03-11 22:43:50 +01:00
use futures_util::StreamExt;
2022-03-13 20:33:26 +01:00
use once_cell::sync::OnceCell;
2022-03-15 20:00:43 +01:00
use signal_hook::consts::signal::*;
use executor::pool::Executor;
2022-03-20 22:46:04 +01:00
use crate::audit::AuditLog;
2022-03-12 17:31:53 +01:00
use crate::authentication::AuthenticationHandle;
2022-03-15 19:13:55 +01:00
use crate::authorization::roles::Roles;
use crate::capnp::APIServer;
2022-03-15 20:00:43 +01:00
use crate::config::{Config};
2022-03-13 20:11:37 +01:00
use crate::resources::modules::fabaccess::MachineState;
2022-03-13 20:33:26 +01:00
use crate::resources::Resource;
use crate::resources::search::ResourcesHandle;
use crate::resources::state::db::StateDB;
2022-03-12 17:31:53 +01:00
use crate::session::SessionManager;
use crate::tls::TlsConfig;
2022-03-13 22:50:37 +01:00
use crate::users::db::UserDB;
use crate::users::Users;
pub const VERSION_STRING: &'static str = env!("BFFHD_VERSION_STRING");
2022-03-12 17:31:53 +01:00
pub const RELEASE_STRING: &'static str = env!("BFFHD_RELEASE_STRING");
pub struct Diflouroborane {
2022-03-15 19:13:55 +01:00
config: Config,
executor: Executor<'static>,
2022-03-16 18:10:59 +01:00
pub statedb: StateDB,
2022-03-15 19:13:55 +01:00
pub users: Users,
pub roles: Roles,
pub resources: ResourcesHandle,
}
2022-03-13 20:33:26 +01:00
pub static RESOURCES: OnceCell<ResourcesHandle> = OnceCell::new();
impl Diflouroborane {
2022-03-15 19:13:55 +01:00
pub fn new(config: Config) -> anyhow::Result<Self> {
2022-03-20 21:22:15 +01:00
logging::init(&config.logging);
tracing::info!(version=VERSION_STRING, "Starting BFFH");
let span = tracing::info_span!("setup");
let _guard = span.enter();
2022-03-15 19:13:55 +01:00
let executor = Executor::new();
2022-03-13 22:50:37 +01:00
let env = StateDB::open_env(&config.db_path)?;
2022-03-16 18:10:59 +01:00
let statedb = StateDB::create_with_env(env.clone())
.context("Failed to open state DB file")?;
2022-03-13 22:50:37 +01:00
2022-03-15 19:13:55 +01:00
let users = Users::new(env.clone()).context("Failed to open users DB file")?;
let roles = Roles::new(config.roles.clone());
2022-03-13 22:50:37 +01:00
2022-03-20 22:46:04 +01:00
let _audit_log = AuditLog::new(&config).context("Failed to initialize audit log")?;
2022-03-13 20:33:26 +01:00
let resources = ResourcesHandle::new(config.machines.iter().map(|(id, desc)| {
Resource::new(Arc::new(resources::Inner::new(id.to_string(), statedb.clone(), desc.clone())))
}));
2022-03-13 21:54:48 +01:00
RESOURCES.set(resources.clone());
2022-03-15 19:13:55 +01:00
Ok(Self { config, executor, statedb, users, roles, resources })
}
pub fn run(&mut self) -> anyhow::Result<()> {
let mut signals = signal_hook_async_std::Signals::new(&[
SIGINT,
SIGQUIT,
SIGTERM,
]).context("Failed to construct signal handler")?;
actors::load(self.executor.clone(), &self.config, self.resources.clone())?;
2022-03-13 21:54:48 +01:00
2022-03-15 19:13:55 +01:00
let tlsconfig = TlsConfig::new(self.config.tlskeylog.as_ref(), !self.config.is_quiet())?;
let acceptor = tlsconfig.make_tls_acceptor(&self.config.tlsconfig)?;
2022-03-15 19:13:55 +01:00
let sessionmanager = SessionManager::new(self.users.clone(), self.roles.clone());
let authentication = AuthenticationHandle::new(self.users.clone());
2022-03-12 17:31:53 +01:00
2022-03-15 20:00:43 +01:00
let apiserver = self.executor.run(APIServer::bind(self.executor.clone(), &self.config.listens, acceptor, sessionmanager, authentication))?;
2022-03-11 22:43:50 +01:00
let (mut tx, rx) = async_oneshot::oneshot();
self.executor.spawn(apiserver.handle_until(rx));
let f = async {
let mut sig = None;
while {
sig = signals.next().await;
sig.is_none()
} {}
tracing::info!(signal = %sig.unwrap(), "Received signal");
tx.send(());
};
self.executor.run(f);
Ok(())
}
2022-03-13 21:30:26 +01:00
}