fabaccess-bffh/src/db.rs

70 lines
2.0 KiB
Rust
Raw Normal View History

2020-11-20 13:06:55 +01:00
use std::sync::Arc;
2020-11-30 15:05:25 +01:00
use std::path::PathBuf;
use std::str::FromStr;
use slog::Logger;
use crate::error::Result;
use crate::config::Settings;
2020-11-20 13:06:55 +01:00
2020-11-30 07:23:47 +01:00
/// (Hashed) password database
pub mod pass;
/// User storage
pub mod user;
2020-11-20 13:06:55 +01:00
2020-10-26 12:58:55 +01:00
/// Access control storage
///
/// Stores&Retrieves Permissions and Roles
2020-10-23 16:35:10 +02:00
pub mod access;
2020-10-26 12:58:55 +01:00
/// Machine storage
///
/// Stores&Retrieves Machines
pub mod machine;
2020-11-17 12:09:45 +01:00
2020-11-20 13:06:55 +01:00
#[derive(Clone)]
2020-11-17 12:09:45 +01:00
pub struct Databases {
2020-11-20 13:06:55 +01:00
pub access: Arc<access::AccessControl>,
pub machine: Arc<machine::MachineDB>,
2020-11-30 07:23:47 +01:00
pub passdb: Arc<pass::PassDB>,
2020-11-17 12:09:45 +01:00
}
2020-11-30 15:05:25 +01:00
const LMDB_MAX_DB: u32 = 16;
impl Databases {
pub fn new(log: &Logger, config: &Settings) -> Result<Self> {
2020-11-30 16:12:40 +01:00
// Initialize the LMDB environment. This blocks until the mmap() finishes
2020-11-30 15:05:25 +01:00
info!(log, "LMDB env");
let env = lmdb::Environment::new()
.set_flags(lmdb::EnvironmentFlags::MAP_ASYNC | lmdb::EnvironmentFlags::NO_SUB_DIR)
.set_max_dbs(LMDB_MAX_DB as libc::c_uint)
.open(&PathBuf::from_str("/tmp/a.db").unwrap())?;
// Start loading the machine database, authentication system and permission system
// All of those get a custom logger so the source of a log message can be better traced and
// filtered
let env = Arc::new(env);
let mdb = machine::init(log.new(o!("system" => "machines")), &config, env.clone())?;
// Error out if any of the subsystems failed to start.
let defs = crate::machine::MachineDescription::load_file(&config.machines)?;
let machdb = machine::MachineDB::new(mdb, defs);
let mut ac = access::AccessControl::new();
let permdb = access::init(log.new(o!("system" => "permissions")), &config, env.clone())?;
ac.add_source_unchecked("Internal".to_string(), Box::new(permdb));
let passdb = pass::PassDB::init(log.new(o!("system" => "passwords")), env.clone()).unwrap();
Ok(Self {
access: Arc::new(ac),
machine: Arc::new(machdb),
passdb: Arc::new(passdb),
})
}
}