From eef3afd2e24b5c1f194ddb0bee7caf8119620cd2 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 20 Oct 2021 15:05:26 +0900 Subject: [PATCH] server: Improve startup error messages and fail fast --- server/src/infra/configuration.rs | 3 +++ server/src/main.rs | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/server/src/infra/configuration.rs b/server/src/infra/configuration.rs index b2cce8c..9284cbe 100644 --- a/server/src/infra/configuration.rs +++ b/server/src/infra/configuration.rs @@ -4,6 +4,7 @@ use figment::{ Figment, }; use lldap_auth::opaque::{server::ServerSetup, KeyPair}; +use log::*; use serde::{Deserialize, Serialize}; use crate::infra::cli::RunOpts; @@ -110,6 +111,8 @@ fn get_server_setup(file_path: &str) -> Result { pub fn init(cli_opts: RunOpts) -> Result { let config_file = cli_opts.config_file.clone(); + info!("Loading configuration from {}", cli_opts.config_file); + let config: Configuration = Figment::from(Serialized::defaults(Configuration::default())) .merge(Toml::file(config_file)) .merge(Env::prefixed("LLDAP_")) diff --git a/server/src/main.rs b/server/src/main.rs index 0d4fa90..9ca0b10 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -11,7 +11,7 @@ use crate::{ infra::{cli::*, configuration::Configuration, db_cleaner::Scheduler}, }; use actix::Actor; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, Context, Result}; use futures_util::TryFutureExt; use log::*; @@ -19,9 +19,11 @@ mod domain; mod infra; async fn create_admin_user(handler: &SqlBackendHandler, config: &Configuration) -> Result<()> { - if config.ldap_user_pass.len() < 8 { - bail!("Minimum password length is 8 characters"); - } + assert!( + config.ldap_user_pass.len() >= 8, + "Minimum password length is 8 characters, got {} characters", + config.ldap_user_pass.len() + ); handler .create_user(CreateUserRequest { user_id: config.ldap_user_dn.clone(), @@ -48,9 +50,12 @@ async fn run_server(config: Configuration) -> Result<()> { .await?; domain::sql_tables::init_table(&sql_pool).await?; let backend_handler = SqlBackendHandler::new(config.clone(), sql_pool.clone()); - create_admin_user(&backend_handler, &config) - .await - .unwrap_or_else(|e| warn!("Error setting up admin login/account: {}", e)); + if let Err(e) = backend_handler.get_user_details(&config.ldap_user_dn).await { + warn!("Could not get admin user, trying to create it: {:#}", e); + create_admin_user(&backend_handler, &config) + .await + .map_err(|e| anyhow!("Error setting up admin login/account: {:#}", e))?; + } let server_builder = infra::ldap_server::build_ldap_server( &config, backend_handler.clone(),