From c493366efdf32fc6951241d57a2eff000a06b347 Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 2 Oct 2019 13:02:51 -0700 Subject: [PATCH] Fix invalid email field error Instead of crashing with a cryptic message, instead we print an error message. Also, as a bonus, better testing instructions! Fixes #3 Fixes #2 --- README.md | 29 +++++++++++++++++++++++++++++ docker-compose.yml | 3 ++- src/config.rs | 3 +-- src/main.rs | 9 ++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 908fd5f..ee54c08 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,35 @@ Configuration values are as follows: |`ldap_sync_interval_seconds`|Integer|Optional|Number of seconds to wait between each LDAP request. Defaults to `60`| |`ldap_sync_loop`|Boolean|Optional|Indicates whether or not syncing should be polled in a loop or done once. Defaults to `true`| +## Testing + +All testing is manual right now. First step is to set up Bitwarden and the LDAP server. + +```bash +docker-compose up -d bitwarden ldap ldap_admin +``` + +1. After that, open the admin portal on http://localhost:8001 and log in using the default account info: + + Username: cn=admin,dc=example,dc=org + Password: admin + +From there you can set up your test group and users. + +2. Expand the `dc=example,dc=org` nav tree and select "Create new entry here" +3. Select "Generic: Posix Group" +4. Give it a name, eg. "Users" and then save and commit +5. Select "Create child object" +6. Select "Generic: User Account" +7. Give the user a name and select a group ID number and save and commit +8. Select "Add new attribute" and select "Email" and then add a test email address + +9. Run the ldap sync + +```bash +docker-compose up ldap_sync +``` + ## Future * Any kind of proper logging diff --git a/docker-compose.yml b/docker-compose.yml index fb1f223..365440c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,8 @@ services: # dockerfile: Dockerfile.alpine volumes: - ./example.config.toml:/usr/src/bitwarden_rs_ldap/config.toml:ro - # - ./example.config.toml:/config.toml:ro + environment: + RUST_BACKTRACE: 1 restart: always bitwarden: diff --git a/src/config.rs b/src/config.rs index c08324b..c63591c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,10 +131,9 @@ impl Config { } pub fn get_ldap_mail_field(&self) -> String { - let default = String::from("mail"); match &self.ldap_mail_field { Some(mail_field) => mail_field.clone(), - None => default.clone(), + None => String::from("mail").clone(), } } diff --git a/src/main.rs b/src/main.rs index fcbfc75..387417a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,7 +114,12 @@ fn invite_from_ldap( let mail_field = config.get_ldap_mail_field(); let mut num_users = 0; for ldap_user in search_entries(config)? { - if let Some(user_email) = ldap_user.attrs[mail_field.as_str()].first() { + // Safely get first email from list of emails in field + if let Some(user_email) = ldap_user + .attrs + .get(mail_field.as_str()) + .and_then(|l| (l.first())) + { if existing_users.contains(user_email) { println!("User with email already exists: {}", user_email); } else { @@ -124,6 +129,8 @@ fn invite_from_ldap( num_users = num_users + 1; // println!("Invite response: {:?}", response); } + } else { + println!("Warning: Email field, {:?}, not found on user", mail_field); } }