server: improve error messages

This commit is contained in:
Valentin Tolmer 2021-11-03 16:01:55 +09:00 committed by nitnelave
parent adf088c74b
commit 350fdcdf9b
3 changed files with 35 additions and 19 deletions

View File

@ -8,7 +8,7 @@ use crate::{
use actix_rt::net::TcpStream; use actix_rt::net::TcpStream;
use actix_server::ServerBuilder; use actix_server::ServerBuilder;
use actix_service::{fn_service, ServiceFactoryExt}; use actix_service::{fn_service, ServiceFactoryExt};
use anyhow::{bail, Context, Result}; use anyhow::{Context, Result};
use futures_util::future::ok; use futures_util::future::ok;
use ldap3_server::{proto::LdapMsg, LdapCodec}; use ldap3_server::{proto::LdapMsg, LdapCodec};
use log::*; use log::*;
@ -43,9 +43,9 @@ where
.context("while sending a response: {:#}")? .context("while sending a response: {:#}")?
} }
if let Err(e) = resp.flush().await { resp.flush()
bail!("Error while flushing responses: {:?}", e); .await
} .context("while flushing responses: {:#}")?
} }
} }
Ok(true) Ok(true)
@ -63,8 +63,8 @@ where
let ldap_base_dn = config.ldap_base_dn.clone(); let ldap_base_dn = config.ldap_base_dn.clone();
let ldap_user_dn = config.ldap_user_dn.clone(); let ldap_user_dn = config.ldap_user_dn.clone();
Ok( server_builder
server_builder.bind("ldap", ("0.0.0.0", config.ldap_port), move || { .bind("ldap", ("0.0.0.0", config.ldap_port), move || {
let backend_handler = backend_handler.clone(); let backend_handler = backend_handler.clone();
let ldap_base_dn = ldap_base_dn.clone(); let ldap_base_dn = ldap_base_dn.clone();
let ldap_user_dn = ldap_user_dn.clone(); let ldap_user_dn = ldap_user_dn.clone();
@ -81,7 +81,10 @@ where
let mut session = LdapHandler::new(backend_handler, ldap_base_dn, ldap_user_dn); let mut session = LdapHandler::new(backend_handler, ldap_base_dn, ldap_user_dn);
while let Some(msg) = requests.next().await { while let Some(msg) = requests.next().await {
if !handle_incoming_message(msg, &mut resp, &mut session).await? { if !handle_incoming_message(msg, &mut resp, &mut session)
.await
.context("while handling incoming messages")?
{
break; break;
} }
} }
@ -89,12 +92,11 @@ where
Ok(stream) Ok(stream)
} }
}) })
.map_err(|err: anyhow::Error| error!("Service Error: {:?}", err)) .map_err(|err: anyhow::Error| error!("Service Error: {:#}", err))
// catch
.and_then(move |_| { .and_then(move |_| {
// finally // finally
ok(()) ok(())
}) })
})?, })
) .with_context(|| format!("while binding to the port {}", config.ldap_port))
} }

View File

@ -87,7 +87,10 @@ where
Backend: TcpBackendHandler + BackendHandler + LoginHandler + OpaqueHandler + Sync + 'static, Backend: TcpBackendHandler + BackendHandler + LoginHandler + OpaqueHandler + Sync + 'static,
{ {
let jwt_secret = config.jwt_secret.clone(); let jwt_secret = config.jwt_secret.clone();
let jwt_blacklist = backend_handler.get_jwt_blacklist().await?; let jwt_blacklist = backend_handler
.get_jwt_blacklist()
.await
.context("while getting the jwt blacklist")?;
server_builder server_builder
.bind("http", ("0.0.0.0", config.http_port), move || { .bind("http", ("0.0.0.0", config.http_port), move || {
let backend_handler = backend_handler.clone(); let backend_handler = backend_handler.clone();

View File

@ -47,27 +47,38 @@ async fn run_server(config: Configuration) -> Result<()> {
let sql_pool = PoolOptions::new() let sql_pool = PoolOptions::new()
.max_connections(5) .max_connections(5)
.connect(&config.database_url) .connect(&config.database_url)
.await?; .await
domain::sql_tables::init_table(&sql_pool).await?; .context("while connecting to the DB")?;
domain::sql_tables::init_table(&sql_pool)
.await
.context("while creating the tables")?;
let backend_handler = SqlBackendHandler::new(config.clone(), sql_pool.clone()); let backend_handler = SqlBackendHandler::new(config.clone(), sql_pool.clone());
if let Err(e) = backend_handler.get_user_details(&config.ldap_user_dn).await { 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); warn!("Could not get admin user, trying to create it: {:#}", e);
create_admin_user(&backend_handler, &config) create_admin_user(&backend_handler, &config)
.await .await
.map_err(|e| anyhow!("Error setting up admin login/account: {:#}", e))?; .map_err(|e| anyhow!("Error setting up admin login/account: {:#}", e))
.context("while creating the admin user")?;
} }
let server_builder = infra::ldap_server::build_ldap_server( let server_builder = infra::ldap_server::build_ldap_server(
&config, &config,
backend_handler.clone(), backend_handler.clone(),
actix_server::Server::build(), actix_server::Server::build(),
)?; )
.context("while binding the LDAP server")?;
infra::jwt_sql_tables::init_table(&sql_pool).await?; infra::jwt_sql_tables::init_table(&sql_pool).await?;
let server_builder = let server_builder =
infra::tcp_server::build_tcp_server(&config, backend_handler, server_builder).await?; infra::tcp_server::build_tcp_server(&config, backend_handler, server_builder)
.await
.context("while binding the TCP server")?;
// Run every hour. // Run every hour.
let scheduler = Scheduler::new("0 0 * * * * *", sql_pool); let scheduler = Scheduler::new("0 0 * * * * *", sql_pool);
scheduler.start(); scheduler.start();
server_builder.workers(1).run().await?; server_builder
.workers(1)
.run()
.await
.context("while starting the server")?;
Ok(()) Ok(())
} }
@ -81,7 +92,7 @@ fn run_server_command(opts: RunOpts) -> Result<()> {
debug!("Configuration: {:#?}", config); debug!("Configuration: {:#?}", config);
actix::run( actix::run(
run_server(config).unwrap_or_else(|e| error!("Could not bring up the servers: {:?}", e)), run_server(config).unwrap_or_else(|e| error!("Could not bring up the servers: {:#}", e)),
)?; )?;
info!("End."); info!("End.");