Skip to content

Commit

Permalink
Fix invalid email field error
Browse files Browse the repository at this point in the history
Instead of crashing with a cryptic message, instead we print an error
message.

Also, as a bonus, better testing instructions!

Fixes #3
Fixes #2
  • Loading branch information
ViViDboarder committed Oct 2, 2019
1 parent 829ed55 commit c493366
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit c493366

Please sign in to comment.