Put the users in the db as well

This commit is contained in:
Gregor Reitzenstein 2020-12-16 13:51:47 +01:00
parent 78e7d45614
commit 8f5cea673b
3 changed files with 29 additions and 7 deletions

View File

@ -28,6 +28,7 @@ pub struct Databases {
pub access: Arc<access::AccessControl>, pub access: Arc<access::AccessControl>,
pub machine: Arc<machine::internal::Internal>, pub machine: Arc<machine::internal::Internal>,
pub passdb: Arc<pass::PassDB>, pub passdb: Arc<pass::PassDB>,
pub userdb: Arc<user::Internal>,
} }
const LMDB_MAX_DB: u32 = 16; const LMDB_MAX_DB: u32 = 16;
@ -55,10 +56,13 @@ impl Databases {
let passdb = pass::PassDB::init(log.new(o!("system" => "passwords")), env.clone()).unwrap(); let passdb = pass::PassDB::init(log.new(o!("system" => "passwords")), env.clone()).unwrap();
let userdb = user::init(log.new(o!("system" => "users")), &config, env.clone())?;
Ok(Self { Ok(Self {
access: Arc::new(ac), access: Arc::new(ac),
passdb: Arc::new(passdb), passdb: Arc::new(passdb),
machine: Arc::new(mdb) machine: Arc::new(mdb),
userdb: Arc::new(userdb),
}) })
} }
} }

View File

@ -4,14 +4,19 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::sync::Arc;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::Path; use std::path::Path;
use crate::db::access::RoleIdentifier; use crate::db::access::RoleIdentifier;
use std::collections::HashMap; use std::collections::HashMap;
use slog::Logger;
use crate::error::Result; use crate::error::Result;
use crate::config::Config;
mod internal; mod internal;
pub use internal::Internal;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// An user /// An user
@ -117,6 +122,15 @@ pub fn load_file<P: AsRef<Path>>(path: P) -> Result<HashMap<String, User>> {
))) )))
} }
pub fn init(log: Logger, config: &Config, env: Arc<lmdb::Environment>) -> Result<Internal> {
let mut flags = lmdb::DatabaseFlags::empty();
flags.set(lmdb::DatabaseFlags::INTEGER_KEY, true);
let db = env.create_db(Some("users"), flags)?;
debug!(&log, "Opened user db successfully.");
Ok(Internal::new(log, env, db))
}
#[cfg(test_DISABLED)] #[cfg(test_DISABLED)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -122,23 +122,27 @@ fn main() {
// Returning a `Result` from `main` allows us to use the `?` shorthand. // Returning a `Result` from `main` allows us to use the `?` shorthand.
// In the case of an Err it will be printed using `fmt::Debug` // In the case of an Err it will be printed using `fmt::Debug`
fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> { fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
// If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
debug!(log, "Loaded Config: {:?}", config);
if matches.is_present("dump") { if matches.is_present("dump") {
error!(log, "Dumping is currently not implemented"); error!(log, "Dumping is currently not implemented");
Ok(()) Ok(())
} else if matches.is_present("load") { } else if matches.is_present("load") {
let db = db::Databases::new(&log, &config)?;
let mut dir = PathBuf::from(matches.value_of_os("load").unwrap()); let mut dir = PathBuf::from(matches.value_of_os("load").unwrap());
dir.push("users.toml"); dir.push("users.toml");
let map = db::user::load_file(&dir); let map = db::user::load_file(&dir)?;
for (uid,user) in map.iter() {
db.userdb.put_user(uid, user)?;
}
debug!(log, "Loaded users: {:?}", map); debug!(log, "Loaded users: {:?}", map);
dir.pop(); dir.pop();
Ok(()) Ok(())
} else { } else {
// If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
debug!(log, "Loaded Config: {:?}", config);
let ex = Executor::new(); let ex = Executor::new();
let mqtt = AsyncClient::new(config.mqtt_url.clone())?; let mqtt = AsyncClient::new(config.mqtt_url.clone())?;