Skip to content

Commit

Permalink
Merge pull request #88 from BoolPurist/impl_default_ldap_user_in_conf
Browse files Browse the repository at this point in the history
Impl default ldap user in conf
Relates to this issue #85
  • Loading branch information
BoolPurist authored May 8, 2024
2 parents ee54069 + 1a728c4 commit 667f4d4
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions usermgmt/src/cli_user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ use usermgmt_lib::prelude::{anyhow, AppResult, Context};

use crate::user_input;

pub fn ask_cli_username() -> AppResult<String> {
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<String> {
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)
}

Expand Down
18 changes: 15 additions & 3 deletions usermgmt/src/ldap_cli_credential.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
username: OnceCell<String>,
password: OnceCell<String>,
}

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())
}

Expand Down
9 changes: 6 additions & 3 deletions usermgmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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)?;
Expand All @@ -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(
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions usermgmt_gui/src/drawing/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ fn construct_fields(config: &mut MgmtConfig, map: CacheForConfFields) -> Vec<Con
create_conf_field!(staff_qos),
create_conf_field!(valid_slurm_groups),
create_conf_field!(compute_nodes),
create_conf_field!(ldap_default_user),
create_conf_field!(ldap_domain_components),
create_conf_field!(ldap_org_unit),
create_conf_field!(ldap_bind_org_unit),
Expand Down
2 changes: 1 addition & 1 deletion usermgmt_gui/src/drawing/draw_listing_of_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn draw(window: &mut UsermgmtWindow, ui: &mut egui::Ui) {
&mgmt_conf,
LdapSimpleCredential::new(username, password),
)?;
list_ldap_users(config)
list_ldap_users(&config)
},
"Listing ldap user".to_owned(),
);
Expand Down
7 changes: 7 additions & 0 deletions usermgmt_gui/src/main_logic/query_io_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn query(window: &mut UsermgmtWindow) {
if let Some(conf) = window.conf_state.io_conf.query_task() {
let listing_state = &mut window.listin_state;
let ssh_state = &mut window.ssh_state;
let ldap_cred = &mut window.ldap_state;
let path = &mut window.conf_path;
path.clone_from(&conf.path);
let config = &conf.config;
Expand All @@ -23,6 +24,12 @@ pub fn query(window: &mut UsermgmtWindow) {
debug!("GUI: Ssh user name taken from default ssh user in loaded config");
ssh_state.username = Some(config.default_ssh_user.to_owned());
}
if ldap_cred.username.is_none() {
if let Some(ldap_user_name) = config.ldap_default_user.as_deref() {
debug!("GUI: ldap user name taken from default ldap user in loaded config");
ldap_cred.username = Some(ldap_user_name.to_owned());
}
}
}
if let Some(path) = window.conf_state.io_save_conf.query_task() {
window.conf_path = path.to_path_buf();
Expand Down
2 changes: 2 additions & 0 deletions usermgmt_lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct MgmtConfig {
pub staff_gid: i32,
pub faculty_gid: i32,
pub sacctmgr_path: String,
pub ldap_default_user: Option<String>,
pub ldap_domain_components: Option<String>,
pub ldap_org_unit: Option<String>,
pub ldap_server: String,
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions usermgmt_lib/src/ldap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(ldap_config: LDAPConfig<T>) -> AppResult<LdapSearchResult>
pub fn list_ldap_users<T>(ldap_config: &LDAPConfig<T>) -> AppResult<LdapSearchResult>
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 {}",
Expand Down
6 changes: 3 additions & 3 deletions usermgmt_lib/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 667f4d4

Please sign in to comment.