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/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 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..d4a1f3d 100644 --- a/usermgmt/src/ldap_cli_credential.rs +++ b/usermgmt/src/ldap_cli_credential.rs @@ -1,18 +1,30 @@ 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 { + 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) + .get_or_try_init(|| cli_user_input::ask_cli_username(self.default_username.as_deref())) .map(|string| string.as_str()) } diff --git a/usermgmt/src/main.rs b/usermgmt/src/main.rs index c2b1bf7..0d40f66 100644 --- a/usermgmt/src/main.rs +++ b/usermgmt/src/main.rs @@ -6,7 +6,7 @@ use cli_ssh_credential::CliSshCredential; use ldap_cli_credential::LdapCliCredential; use log::error; use std::process::ExitCode; -use usermgmt_lib::cli::{Commands, GeneralArgs, OnWhichSystem}; +use usermgmt_lib::cli::{self, Commands, GeneralArgs, OnWhichSystem}; use usermgmt_lib::config::{self}; use usermgmt_lib::{operations, prelude::*, ChangesToUser, Entity}; @@ -43,8 +43,7 @@ fn execute_command() -> AppResult { /// - If the LDAP or SSH session could not be established because of connection problems or invalid /// credentials. /// - If some arguments in CLI, parameter `args`, for action are not valid. -pub fn run_mgmt(args: GeneralArgs) -> AppResult { - let ldap_credential = LdapCliCredential::default(); +pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { 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: GeneralArgs) -> AppResult { on_which_sys, } => { let config = config::load_config(args.config_file)?.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: GeneralArgs) -> AppResult { } Commands::Modify { data, on_which_sys } => { let config = config::load_config(args.config_file)?.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: GeneralArgs) -> AppResult { } Commands::Delete { user, on_which_sys } => { let config = config::load_config(args.config_file)?.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: GeneralArgs) -> AppResult { simple_output_for_ldap, } => { let config = config::load_config(args.config_file)?.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..466f518 100644 --- a/usermgmt_lib/src/ldap.rs +++ b/usermgmt_lib/src/ldap.rs @@ -223,13 +223,13 @@ 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, { // 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/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 {