Splitting bffhd setup routines

This commit is contained in:
Nadja Reitzenstein 2022-03-15 19:13:55 +01:00
parent 5c9b72c37d
commit d7467989ef
2 changed files with 34 additions and 44 deletions

View File

@ -54,6 +54,7 @@ use rustls::server::NoClientAuth;
use signal_hook::consts::signal::*; use signal_hook::consts::signal::*;
use executor::pool::Executor; use executor::pool::Executor;
use crate::authentication::AuthenticationHandle; use crate::authentication::AuthenticationHandle;
use crate::authorization::roles::Roles;
use crate::capnp::APIServer; use crate::capnp::APIServer;
use crate::config::{Config, TlsListen}; use crate::config::{Config, TlsListen};
use crate::resources::modules::fabaccess::MachineState; use crate::resources::modules::fabaccess::MachineState;
@ -68,61 +69,59 @@ use crate::users::Users;
pub const RELEASE_STRING: &'static str = env!("BFFHD_RELEASE_STRING"); pub const RELEASE_STRING: &'static str = env!("BFFHD_RELEASE_STRING");
pub struct Diflouroborane { pub struct Diflouroborane {
config: Config,
executor: Executor<'static>, executor: Executor<'static>,
pub statedb: Arc<StateDB>,
pub users: Users,
pub roles: Roles,
pub resources: ResourcesHandle,
} }
pub static RESOURCES: OnceCell<ResourcesHandle> = OnceCell::new(); pub static RESOURCES: OnceCell<ResourcesHandle> = OnceCell::new();
impl Diflouroborane { impl Diflouroborane {
pub fn new() -> Self { pub fn new(config: Config) -> anyhow::Result<Self> {
let executor = Executor::new();
Self { executor }
}
fn log_version_number(&self) {
tracing::info!(version=RELEASE_STRING, "Starting");
}
pub fn init_logging(config: &Config) {
logging::init(&config); logging::init(&config);
} tracing::info!(version=RELEASE_STRING, "Starting");
pub fn setup(&mut self, config: &Config) -> anyhow::Result<()> {
Self::init_logging(config);
let span = tracing::info_span!("setup"); let span = tracing::info_span!("setup");
let _guard = span.enter(); let _guard = span.enter();
self.log_version_number(); let executor = Executor::new();
let mut signals = signal_hook_async_std::Signals::new(&[
SIGINT,
SIGQUIT,
SIGTERM,
]).context("Failed to construct signal handler")?;
let env = StateDB::open_env(&config.db_path)?; let env = StateDB::open_env(&config.db_path)?;
let statedb = Arc::new(StateDB::create_with_env(env.clone()) let statedb = Arc::new(StateDB::create_with_env(env.clone())
.context("Failed to open state DB file")?); .context("Failed to open state DB file")?);
let userdb = Users::new(env.clone()).context("Failed to open users DB file")?; let users = Users::new(env.clone()).context("Failed to open users DB file")?;
let roles = Roles::new(config.roles.clone());
let resources = ResourcesHandle::new(config.machines.iter().map(|(id, desc)| { let resources = ResourcesHandle::new(config.machines.iter().map(|(id, desc)| {
Resource::new(Arc::new(resources::Inner::new(id.to_string(), statedb.clone(), desc.clone()))) Resource::new(Arc::new(resources::Inner::new(id.to_string(), statedb.clone(), desc.clone())))
})); }));
RESOURCES.set(resources.clone()); RESOURCES.set(resources.clone());
actors::load(self.executor.clone(), &config, resources.clone())?;
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())?;
let tlsconfig = TlsConfig::new(config.tlskeylog.as_ref(), !config.is_quiet())?; let tlsconfig = TlsConfig::new(self.config.tlskeylog.as_ref(), !self.config.is_quiet())?;
let acceptor = tlsconfig.make_tls_acceptor(&config.tlsconfig)?; let acceptor = tlsconfig.make_tls_acceptor(&self.config.tlsconfig)?;
let sessionmanager = SessionManager::new(userdb.clone()); let sessionmanager = SessionManager::new(self.users.clone(), self.roles.clone());
let authentication = AuthenticationHandle::new(userdb.clone()); let authentication = AuthenticationHandle::new(self.users.clone());
let mut apiserver = self.executor.run(APIServer::bind(self.executor.clone(), &config.listens, acceptor, sessionmanager, authentication))?; let mut apiserver = self.executor.run(APIServer::bind(self.executor.clone(), &self.config.listens, acceptor, sessionmanager, authentication))?;
let (mut tx, rx) = async_oneshot::oneshot(); let (mut tx, rx) = async_oneshot::oneshot();

View File

@ -97,8 +97,8 @@ fn main() -> anyhow::Result<()> {
return Ok(()); return Ok(());
} else if matches.is_present("check config") { } else if matches.is_present("check config") {
match config::read(&PathBuf::from_str(configpath).unwrap()) { match config::read(&PathBuf::from_str(configpath).unwrap()) {
Ok(_) => { Ok(c) => {
//TODO: print a normalized version of the supplied config println!("{:#?}", c);
println!("config is valid"); println!("config is valid");
std::process::exit(0); std::process::exit(0);
} }
@ -114,17 +114,8 @@ fn main() -> anyhow::Result<()> {
if matches.is_present("dump") { if matches.is_present("dump") {
unimplemented!() unimplemented!()
} else if matches.is_present("load") { } else if matches.is_present("load") {
Diflouroborane::init_logging(&config); let bffh = Diflouroborane::new(config)?;
let env = Environment::new() bffh.users.load_file(matches.value_of("load").unwrap());
.set_flags( EnvironmentFlags::WRITE_MAP
| EnvironmentFlags::NO_SUB_DIR
| EnvironmentFlags::NO_TLS
| EnvironmentFlags::NO_READAHEAD)
.set_max_dbs(2)
.open(config.db_path.as_ref())
.map(Arc::new)?;
let userdb = Users::new(env).context("Failed to open users DB file")?;
userdb.load_file(matches.value_of("load").unwrap());
return Ok(()) return Ok(())
} else { } else {
let keylog = matches.value_of("keylog"); let keylog = matches.value_of("keylog");
@ -147,8 +138,8 @@ fn main() -> anyhow::Result<()> {
} }
config.log_format = matches.value_of("log format").unwrap_or("Full").to_string(); config.log_format = matches.value_of("log format").unwrap_or("Full").to_string();
let mut bffh = Diflouroborane::new(); let mut bffh = Diflouroborane::new(config)?;
bffh.setup(&config)?; bffh.run()?;
} }
Ok(()) Ok(())