From f1fcd4585ba3e14b674cf0399e9449bbf69042e4 Mon Sep 17 00:00:00 2001 From: BoolPurist Date: Wed, 8 May 2024 15:02:46 +0200 Subject: [PATCH 1/3] Feat: ldap_default_user config field --- usermgmt/src/cli_user_input.rs | 11 +++++--- usermgmt/src/ldap_cli_credential.rs | 26 +++++++++++++++---- usermgmt/src/main.rs | 5 +++- usermgmt_gui/src/drawing/configuration.rs | 1 + .../src/drawing/draw_listing_of_users.rs | 2 +- usermgmt_gui/src/main_logic/query_io_tasks.rs | 7 +++++ usermgmt_lib/src/config.rs | 2 ++ usermgmt_lib/src/ldap.rs | 2 +- usermgmt_lib/src/ldap/ldap_config.rs | 1 + usermgmt_lib/src/operations.rs | 6 ++--- 10 files changed, 49 insertions(+), 14 deletions(-) diff --git a/usermgmt/src/cli_user_input.rs b/usermgmt/src/cli_user_input.rs index 3641588..15b2dc8 100644 --- a/usermgmt/src/cli_user_input.rs +++ b/usermgmt/src/cli_user_input.rs @@ -2,9 +2,14 @@ use usermgmt_lib::prelude::{anyhow, AppResult, Context}; use crate::user_input; -pub fn ask_cli_username() -> AppResult { - println!("Enter your LDAP username (defaults to admin):"); - let username = user_input::line_input_from_user()?.unwrap_or_else(|| "admin".to_string()); +pub fn ask_cli_username(default_username: Option<&str>) -> AppResult { + let default_prompt_name = default_username.unwrap_or("admin"); + println!( + "Enter your LDAP username (defaults to {}):", + default_prompt_name + ); + let username = + user_input::line_input_from_user()?.unwrap_or_else(|| default_prompt_name.to_string()); Ok(username) } diff --git a/usermgmt/src/ldap_cli_credential.rs b/usermgmt/src/ldap_cli_credential.rs index 158bcbc..d8aa9b1 100644 --- a/usermgmt/src/ldap_cli_credential.rs +++ b/usermgmt/src/ldap_cli_credential.rs @@ -1,19 +1,35 @@ use once_cell::unsync::OnceCell; -use usermgmt_lib::{ldap::LdapCredential, prelude::AppResult}; +use usermgmt_lib::{config::MgmtConfig, ldap::LdapCredential, prelude::AppResult}; use crate::cli_user_input; -#[derive(Debug, Default, Clone)] +#[derive(Debug, Clone)] pub struct LdapCliCredential { + default_username: Option, username: OnceCell, password: OnceCell, } +impl LdapCliCredential { + pub fn new(conf: &MgmtConfig) -> Self { + dbg!(); + let default_username = conf.ldap_default_user.to_owned(); + Self { + default_username, + username: Default::default(), + password: Default::default(), + } + } +} + impl LdapCredential for LdapCliCredential { fn username(&self) -> AppResult<&str> { - self.username - .get_or_try_init(cli_user_input::ask_cli_username) - .map(|string| string.as_str()) + let a = self + .username + .get_or_try_init(|| cli_user_input::ask_cli_username(self.default_username.as_deref())) + .map(|string| string.as_str()); + dbg!(self); + a } fn password(&self) -> AppResult<&str> { diff --git a/usermgmt/src/main.rs b/usermgmt/src/main.rs index e754505..1fb8bdf 100644 --- a/usermgmt/src/main.rs +++ b/usermgmt/src/main.rs @@ -44,7 +44,6 @@ fn execute_command() -> AppResult { /// credentials. /// - If some arguments in CLI, parameter `args`, for action are not valid. pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { - let ldap_credential = LdapCliCredential::default(); match args.command { Commands::GenerateConfig => { // To StdOut, user can then pipe this default configuration wherever they please. @@ -55,6 +54,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { on_which_sys, } => { let config = config::load_config(None)?.config; + let ldap_credential = LdapCliCredential::new(&config); let on_which_sys = &OnWhichSystem::from_config_for_all(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::add_user( @@ -67,6 +67,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { } Commands::Modify { data, on_which_sys } => { let config = config::load_config(None)?.config; + let ldap_credential = LdapCliCredential::new(&config); let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); let data = Entity::new_modifieble_conf(data, &config)?; @@ -81,6 +82,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { } Commands::Delete { user, on_which_sys } => { let config = config::load_config(None)?.config; + let ldap_credential = LdapCliCredential::new(&config); let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::delete_user( @@ -96,6 +98,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { simple_output_for_ldap, } => { let config = config::load_config(None)?.config; + let ldap_credential = LdapCliCredential::new(&config); let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::print_list_of_users_to_stdout( diff --git a/usermgmt_gui/src/drawing/configuration.rs b/usermgmt_gui/src/drawing/configuration.rs index 2eac442..a66ef0d 100644 --- a/usermgmt_gui/src/drawing/configuration.rs +++ b/usermgmt_gui/src/drawing/configuration.rs @@ -257,6 +257,7 @@ fn construct_fields(config: &mut MgmtConfig, map: CacheForConfFields) -> Vec, pub ldap_domain_components: Option, pub ldap_org_unit: Option, pub ldap_server: String, @@ -107,6 +108,7 @@ impl Default for MgmtConfig { staff_gid: 1001, faculty_gid: 1000, sacctmgr_path: "/usr/local/bin/sacctmgr".to_string(), + ldap_default_user: None, ldap_domain_components: None, ldap_org_unit: None, ldap_server: "ldap://localhost:389".to_string(), diff --git a/usermgmt_lib/src/ldap.rs b/usermgmt_lib/src/ldap.rs index 2c34cdc..abda712 100644 --- a/usermgmt_lib/src/ldap.rs +++ b/usermgmt_lib/src/ldap.rs @@ -223,7 +223,7 @@ where /// /// - If the connection to the LDAP instance fails. See [`make_ldap_connection`] /// - If the searching in LDAP failed -pub fn list_ldap_users(ldap_config: LDAPConfig) -> AppResult +pub fn list_ldap_users(ldap_config: &LDAPConfig) -> AppResult where T: LdapCredential, { diff --git a/usermgmt_lib/src/ldap/ldap_config.rs b/usermgmt_lib/src/ldap/ldap_config.rs index da029e4..f57e411 100644 --- a/usermgmt_lib/src/ldap/ldap_config.rs +++ b/usermgmt_lib/src/ldap/ldap_config.rs @@ -17,6 +17,7 @@ where { pub fn new_readonly(config: &MgmtConfig, mut credentials: T) -> AppResult { let ldap_server = config.ldap_server.clone(); + dbg!(); let (ldap_user, ldap_pass) = super::ask_credentials_if_not_provided( config.ldap_readonly_user.as_deref(), config.ldap_readonly_pw.as_deref(), diff --git a/usermgmt_lib/src/operations.rs b/usermgmt_lib/src/operations.rs index a89b9f9..f6e9538 100644 --- a/usermgmt_lib/src/operations.rs +++ b/usermgmt_lib/src/operations.rs @@ -4,7 +4,7 @@ use crate::{ cli::{OnWhichSystem, UserToAdd}, config::MgmtConfig, dir, - ldap::{self, text_list_output, LDAPConfig, LdapCredential, LdapSession}, + ldap::{self, text_list_output, LdapCredential, LdapSession}, slurm, ssh::{SshConnection, SshCredentials}, AppResult, ChangesToUser, NewEntity, @@ -127,8 +127,8 @@ where ldap_credentials.clone(), &credentials, true, - |_ldap_session| { - let ldap_config = LDAPConfig::new_readonly(config, ldap_credentials)?; + |ldap_session| { + let ldap_config = ldap_session.config(); let search_result_data = ldap::list_ldap_users(ldap_config)?; let output = if simple_output_ldap { From 73dbfa62874934f31001a5086f76804a32ad49d2 Mon Sep 17 00:00:00 2001 From: BoolPurist Date: Wed, 8 May 2024 15:33:08 +0200 Subject: [PATCH 2/3] Adjusted changelog Removed clippy warnings and dbg macros --- CHANGELOG.md | 1 + usermgmt/src/ldap_cli_credential.rs | 8 ++------ usermgmt_lib/src/ldap.rs | 2 +- usermgmt_lib/src/ldap/ldap_config.rs | 1 - 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 842703e..6a1fd26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Changelog for binary "usermgmt" and library "usermgmt_lib". ### Added +- Added configuration field "ldap_default_user". Allows to define username for LDAP login used by default. - Added CLI option to specify the configuration file - Logging also performed to logging file - Ssh key pair can be provided by field within configuration file or as a CLI argument. diff --git a/usermgmt/src/ldap_cli_credential.rs b/usermgmt/src/ldap_cli_credential.rs index d8aa9b1..d4a1f3d 100644 --- a/usermgmt/src/ldap_cli_credential.rs +++ b/usermgmt/src/ldap_cli_credential.rs @@ -12,7 +12,6 @@ pub struct LdapCliCredential { impl LdapCliCredential { pub fn new(conf: &MgmtConfig) -> Self { - dbg!(); let default_username = conf.ldap_default_user.to_owned(); Self { default_username, @@ -24,12 +23,9 @@ impl LdapCliCredential { impl LdapCredential for LdapCliCredential { fn username(&self) -> AppResult<&str> { - let a = self - .username + self.username .get_or_try_init(|| cli_user_input::ask_cli_username(self.default_username.as_deref())) - .map(|string| string.as_str()); - dbg!(self); - a + .map(|string| string.as_str()) } fn password(&self) -> AppResult<&str> { diff --git a/usermgmt_lib/src/ldap.rs b/usermgmt_lib/src/ldap.rs index abda712..466f518 100644 --- a/usermgmt_lib/src/ldap.rs +++ b/usermgmt_lib/src/ldap.rs @@ -229,7 +229,7 @@ where { // Establish LDAP connection and bind let mut ldap = - make_ldap_connection(&ldap_config).context("Error while connecting via LDAP !")?; + make_ldap_connection(ldap_config).context("Error while connecting via LDAP !")?; debug!( "LDAP connection established to {}. Will search under {}", diff --git a/usermgmt_lib/src/ldap/ldap_config.rs b/usermgmt_lib/src/ldap/ldap_config.rs index f57e411..da029e4 100644 --- a/usermgmt_lib/src/ldap/ldap_config.rs +++ b/usermgmt_lib/src/ldap/ldap_config.rs @@ -17,7 +17,6 @@ where { pub fn new_readonly(config: &MgmtConfig, mut credentials: T) -> AppResult { let ldap_server = config.ldap_server.clone(); - dbg!(); let (ldap_user, ldap_pass) = super::ask_credentials_if_not_provided( config.ldap_readonly_user.as_deref(), config.ldap_readonly_pw.as_deref(), From 1a728c413dd409e240b24bc3b0d3599813c778f3 Mon Sep 17 00:00:00 2001 From: BoolPurist Date: Wed, 8 May 2024 15:48:41 +0200 Subject: [PATCH 3/3] Docs: added ldap_default_user to readme docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d2c7b01..39ece41 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,8 @@ staff_gid = 1001 faculty_gid = 1000 # Path to sacctmgr binary sacctmgr_path = '/usr/local/bin/sacctmgr' +# Default ldap username used by default if no username for LDAP login is provided. +ldap_default_user = 'admim' # Domain components used for LDAP queries # Will be used in combination with ldap_org_unit # and the cn of the username you provided for ldap login