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;
|
2021-01-26 15:33:50 +01:00
|
|
|
use crate::config::Config;
|
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>,
|
2020-12-01 08:39:34 +01:00
|
|
|
pub machine: Arc<machine::internal::Internal>,
|
2020-11-30 07:23:47 +01:00
|
|
|
pub passdb: Arc<pass::PassDB>,
|
2020-12-16 13:51:47 +01:00
|
|
|
pub userdb: Arc<user::Internal>,
|
2020-11-17 12:09:45 +01:00
|
|
|
}
|
2020-11-30 15:05:25 +01:00
|
|
|
|
|
|
|
const LMDB_MAX_DB: u32 = 16;
|
|
|
|
|
|
|
|
impl Databases {
|
2021-01-26 15:33:50 +01:00
|
|
|
pub fn new(log: &Logger, config: &Config) -> Result<Self> {
|
2020-11-30 15:05:25 +01:00
|
|
|
|
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())?;
|
|
|
|
|
|
|
|
let permdb = access::init(log.new(o!("system" => "permissions")), &config, env.clone())?;
|
2020-12-16 14:04:50 +01:00
|
|
|
let mut ac = access::AccessControl::new(permdb);
|
2020-11-30 15:05:25 +01:00
|
|
|
|
|
|
|
let passdb = pass::PassDB::init(log.new(o!("system" => "passwords")), env.clone()).unwrap();
|
|
|
|
|
2020-12-16 13:51:47 +01:00
|
|
|
let userdb = user::init(log.new(o!("system" => "users")), &config, env.clone())?;
|
|
|
|
|
2020-11-30 15:05:25 +01:00
|
|
|
Ok(Self {
|
|
|
|
access: Arc::new(ac),
|
|
|
|
passdb: Arc::new(passdb),
|
2020-12-16 13:51:47 +01:00
|
|
|
machine: Arc::new(mdb),
|
|
|
|
userdb: Arc::new(userdb),
|
2020-11-30 15:05:25 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|