This commit is contained in:
ViViDboarder 2019-04-11 12:01:06 -07:00
parent cbdd7b8c5b
commit 57851489cb

View File

@ -1,6 +1,8 @@
extern crate ldap3; extern crate ldap3;
use std::collections::HashSet;
use std::error::Error; use std::error::Error;
use std::env;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
@ -9,6 +11,24 @@ use ldap3::{DerefAliases, LdapConn, Scope, SearchEntry, SearchOptions};
mod bw_admin; mod bw_admin;
mod config; mod config;
/// Container for args parsed from the command line
struct ParsedArgs {
start_loop: bool,
}
impl ParsedArgs {
pub parse() -> ParsedArgs {
let mut parsed_args = ParsedArgs {};
for arg in env::args().collect() {
if arg == "--loop" {
parsed_args.start_loop = true;
}
}
parsed_args.clone()
}
}
fn main() { fn main() {
let config = config::Config::from_file(); let config = config::Config::from_file();
let mut client = bw_admin::Client::new( let mut client = bw_admin::Client::new(
@ -16,26 +36,38 @@ fn main() {
config.get_bitwarden_admin_token().clone(), config.get_bitwarden_admin_token().clone(),
); );
match client.users() { let parsed_args = ParsedArgs::parse();
Ok(users) => { if let Err(e) = invite_users(&config, &mut client, parsed_args.start_loop) {
for user in users { panic!("{}", e);
println!("Existing user: {}", user.get_email()); }
} }
}
Err(e) => { /// Invites new users to Bitwarden from LDAP
println!("Could not get users"); fn invite_users(
panic!("{}", e); config: &config::Config,
} client: &mut bw_admin::Client,
start_loop: bool,
) -> Result((), Box<Error>> {
let user_emails = get_existing_users(&mut client)?;
if start_loop {
start_sync_loop(&config, &mut client)?;
} else {
invite_from_ldap(&config, &mut client)?;
} }
// TODO: Use command line args to differentiate if we invite once or start loop Ok(())
if let Err(e) = invite_from_ldap(&config, &mut client) { }
println!("{}", e);
/// Creates set of email addresses for users that already exist in Bitwarden
fn get_existing_users(client: &mut bw_admin::Client) -> Result<HashSet<String>, Box<Error>> {
let all_users = client.users()?;
let mut user_emails = HashSet::with_capacity(all_users.len());
for user in client.users()? {
user_emails.insert(user.get_email());
} }
if let Err(e) = start_sync_loop(&config, &mut client) { Ok(user_emails)
println!("{}", e);
}
} }
/// Creates an LDAP connection, authenticating if necessary /// Creates an LDAP connection, authenticating if necessary