lldap/src/main.rs

92 lines
2.9 KiB
Rust
Raw Normal View History

2021-05-14 07:38:36 +00:00
#![forbid(unsafe_code)]
#![allow(clippy::nonstandard_macro_braces)]
2021-05-20 17:18:15 +00:00
use crate::{
2021-05-26 13:13:17 +00:00
domain::{
handler::BackendHandler, sql_backend_handler::SqlBackendHandler,
sql_opaque_handler::register_password, sql_tables::PoolOptions,
2021-05-26 13:13:17 +00:00
},
infra::{cli::*, configuration::Configuration, db_cleaner::Scheduler},
2021-05-20 17:18:15 +00:00
};
2021-05-25 08:39:09 +00:00
use actix::Actor;
2021-08-26 19:56:42 +00:00
use anyhow::{Context, Result};
use futures_util::TryFutureExt;
2021-03-02 19:51:33 +00:00
use log::*;
mod domain;
2021-03-02 19:13:58 +00:00
mod infra;
async fn create_admin_user(handler: &SqlBackendHandler, config: &Configuration) -> Result<()> {
2021-05-26 13:13:17 +00:00
handler
.create_user(lldap_model::CreateUserRequest {
user_id: config.ldap_user_dn.clone(),
..Default::default()
})
.and_then(|_| register_password(handler, &config.ldap_user_dn, &config.ldap_user_pass))
2021-05-26 13:13:17 +00:00
.await
2021-08-26 19:56:42 +00:00
.context("Error creating admin user")?;
let admin_group_id = handler
.create_group(lldap_model::CreateGroupRequest {
display_name: "lldap_admin".to_string(),
})
.await
2021-08-26 19:56:42 +00:00
.context("Error creating admin group")?;
handler
.add_user_to_group(lldap_model::AddUserToGroupRequest {
user_id: config.ldap_user_dn.clone(),
group_id: admin_group_id,
})
.await
2021-08-26 19:56:42 +00:00
.context("Error adding admin user to group")
2021-05-26 13:13:17 +00:00
}
async fn run_server(config: Configuration) -> Result<()> {
2021-04-11 20:07:28 +00:00
let sql_pool = PoolOptions::new()
2021-03-07 15:13:50 +00:00
.max_connections(5)
2021-03-12 08:33:43 +00:00
.connect(&config.database_url)
2021-03-07 15:13:50 +00:00
.await?;
2021-03-12 16:01:04 +00:00
domain::sql_tables::init_table(&sql_pool).await?;
2021-05-20 17:18:15 +00:00
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));
2021-03-07 15:13:50 +00:00
let server_builder = infra::ldap_server::build_ldap_server(
&config,
backend_handler.clone(),
2021-03-07 15:13:50 +00:00
actix_server::Server::build(),
)?;
2021-05-20 15:40:30 +00:00
infra::jwt_sql_tables::init_table(&sql_pool).await?;
let server_builder =
2021-05-20 15:40:30 +00:00
infra::tcp_server::build_tcp_server(&config, backend_handler, server_builder).await?;
2021-05-25 08:39:09 +00:00
// Run every hour.
let scheduler = Scheduler::new("0 0 * * * * *", sql_pool);
scheduler.start();
server_builder.workers(1).run().await?;
Ok(())
}
fn run_server_command(opts: RunOpts) -> Result<()> {
let config = infra::configuration::init(opts.clone())?;
2021-03-02 19:51:33 +00:00
infra::logging::init(config.clone())?;
info!("Starting LLDAP....");
debug!("CLI: {:#?}", opts);
debug!("Configuration: {:#?}", config);
2021-03-02 22:07:01 +00:00
actix::run(
run_server(config).unwrap_or_else(|e| error!("Could not bring up the servers: {:?}", e)),
)?;
2021-03-02 22:07:01 +00:00
2021-03-02 19:51:33 +00:00
info!("End.");
Ok(())
2021-03-02 11:45:30 +00:00
}
fn main() -> Result<()> {
let cli_opts = infra::cli::init();
match cli_opts.command {
Command::ExportGraphQLSchema(opts) => infra::graphql::api::export_schema(opts),
Command::Run(opts) => run_server_command(opts),
}
}