2019-03-29 18:18:56 +00:00
|
|
|
extern crate serde;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
pub type Pass = String;
|
|
|
|
|
|
|
|
const CONFIG_PATH_DEFAULT: &str = "config.toml";
|
|
|
|
|
|
|
|
/// Returns config path from envioronment or a provided default value
|
|
|
|
pub fn get_config_path() -> String {
|
|
|
|
match env::var("CONFIG_PATH") {
|
|
|
|
Ok(config_path) => config_path,
|
|
|
|
Err(_) => String::from(CONFIG_PATH_DEFAULT),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:01:46 +00:00
|
|
|
// Tries to read configuration from file, failing that from the environment,
|
|
|
|
// panics if it can't
|
2019-03-29 18:18:56 +00:00
|
|
|
pub fn read_config() -> Config {
|
2021-05-19 07:01:46 +00:00
|
|
|
match read_config_from_file() {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(err) => {
|
|
|
|
println!("{}", err);
|
|
|
|
match read_config_from_env() {
|
|
|
|
Ok(config) => config,
|
2021-07-23 22:27:45 +00:00
|
|
|
Err(err) => panic!("{}", err),
|
2021-05-19 07:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to read configuration from file
|
|
|
|
pub fn read_config_from_file() -> Result<Config, String> {
|
2019-03-29 18:18:56 +00:00
|
|
|
let config_path = get_config_path();
|
|
|
|
|
2021-07-23 22:27:45 +00:00
|
|
|
let contents = fs::read_to_string(&config_path)
|
2021-09-21 00:01:27 +00:00
|
|
|
.map_err(|err| format!("Failed to open config file at {}: {}", config_path, err))?;
|
2021-07-23 22:27:45 +00:00
|
|
|
let config: Config = toml::from_str(contents.as_str())
|
2021-09-21 00:01:27 +00:00
|
|
|
.map_err(|err| format!("Failed to parse config file at {}: {}", config_path, err))?;
|
2021-05-19 07:01:46 +00:00
|
|
|
|
2021-07-23 22:27:45 +00:00
|
|
|
println!("Config read from file at {}", config_path);
|
2021-05-19 07:01:46 +00:00
|
|
|
Ok(config)
|
|
|
|
}
|
2019-03-29 18:18:56 +00:00
|
|
|
|
2021-05-19 07:01:46 +00:00
|
|
|
// Tries to read configuration from environment
|
|
|
|
pub fn read_config_from_env() -> Result<Config, String> {
|
2021-07-23 22:27:45 +00:00
|
|
|
let config = envy::prefixed("APP_")
|
|
|
|
.from_env()
|
|
|
|
.map_err(|err| format!("Error parsing config from env: {}", err))?;
|
|
|
|
println!("Config read from environment");
|
2021-05-19 07:01:46 +00:00
|
|
|
Ok(config)
|
2019-03-29 18:18:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 22:27:45 +00:00
|
|
|
#[derive(Deserialize)]
|
2019-03-29 18:18:56 +00:00
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
/// Contains all config values for LDAP syncing
|
|
|
|
pub struct Config {
|
2019-03-29 22:18:25 +00:00
|
|
|
// Bitwarden connection config
|
2021-05-07 19:55:29 +00:00
|
|
|
vaultwarden_url: String,
|
|
|
|
vaultwarden_admin_token: String,
|
|
|
|
vaultwarden_root_cert_file: Option<String>,
|
2019-03-29 22:18:25 +00:00
|
|
|
// LDAP Connection config
|
2019-03-29 18:18:56 +00:00
|
|
|
ldap_host: String,
|
|
|
|
ldap_scheme: Option<String>,
|
|
|
|
ldap_ssl: Option<bool>,
|
2020-12-27 15:50:10 +00:00
|
|
|
ldap_starttls: Option<bool>,
|
2019-03-29 18:18:56 +00:00
|
|
|
ldap_port: Option<u16>,
|
2020-07-09 06:24:36 +00:00
|
|
|
ldap_no_tls_verify: Option<bool>,
|
2019-03-29 22:18:25 +00:00
|
|
|
// LDAP auth config
|
2024-10-26 06:12:46 +00:00
|
|
|
ldap_bind_dn: Option<String>,
|
|
|
|
ldap_bind_password: Option<Pass>,
|
2019-03-29 22:18:25 +00:00
|
|
|
// LDAP search config
|
2019-03-29 18:18:56 +00:00
|
|
|
ldap_search_base_dn: String,
|
|
|
|
ldap_search_filter: String,
|
2019-03-29 22:18:25 +00:00
|
|
|
// LDAP record attributes
|
2019-03-29 18:18:56 +00:00
|
|
|
ldap_mail_field: Option<String>,
|
2019-03-29 22:18:25 +00:00
|
|
|
// Interval syncing config
|
2019-03-29 18:18:56 +00:00
|
|
|
ldap_sync_interval_seconds: Option<u64>,
|
2019-04-11 23:20:00 +00:00
|
|
|
// Should start background sync loop
|
|
|
|
ldap_sync_loop: Option<bool>,
|
2019-03-29 18:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
/// Create a config instance from file
|
|
|
|
pub fn from_file() -> Config {
|
|
|
|
read_config()
|
|
|
|
}
|
|
|
|
|
2021-05-07 19:55:29 +00:00
|
|
|
pub fn get_vaultwarden_url(&self) -> String {
|
|
|
|
self.vaultwarden_url.clone()
|
2019-03-29 22:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 19:55:29 +00:00
|
|
|
pub fn get_vaultwarden_admin_token(&self) -> String {
|
|
|
|
self.vaultwarden_admin_token.clone()
|
2019-03-29 22:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 19:55:29 +00:00
|
|
|
pub fn get_vaultwarden_root_cert_file(&self) -> String {
|
|
|
|
match &self.vaultwarden_root_cert_file {
|
|
|
|
Some(vaultwarden_root_cert_file) => vaultwarden_root_cert_file.clone(),
|
2020-07-09 06:02:10 +00:00
|
|
|
None => String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:56 +00:00
|
|
|
pub fn get_ldap_url(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"{}://{}:{}",
|
|
|
|
self.get_ldap_scheme(),
|
|
|
|
self.get_ldap_host(),
|
|
|
|
self.get_ldap_port()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_host(&self) -> String {
|
|
|
|
self.ldap_host.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_scheme(&self) -> String {
|
|
|
|
match &self.ldap_scheme {
|
|
|
|
Some(ldap_scheme) => ldap_scheme.clone(),
|
|
|
|
None => {
|
|
|
|
if self.get_ldap_ssl() {
|
|
|
|
String::from("ldaps")
|
|
|
|
} else {
|
|
|
|
String::from("ldap")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_ssl(&self) -> bool {
|
|
|
|
self.ldap_ssl.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:50:10 +00:00
|
|
|
pub fn get_ldap_starttls(&self) -> bool {
|
|
|
|
self.ldap_starttls.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2020-07-09 06:24:36 +00:00
|
|
|
pub fn get_ldap_no_tls_verify(&self) -> bool {
|
|
|
|
self.ldap_no_tls_verify.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:56 +00:00
|
|
|
pub fn get_ldap_port(&self) -> u16 {
|
|
|
|
match self.ldap_port {
|
|
|
|
Some(ldap_port) => ldap_port,
|
|
|
|
None => {
|
|
|
|
if self.get_ldap_ssl() {
|
|
|
|
636
|
|
|
|
} else {
|
|
|
|
389
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-26 06:12:46 +00:00
|
|
|
pub fn get_ldap_bind_dn(&self) -> Option<String> {
|
2019-03-29 18:18:56 +00:00
|
|
|
self.ldap_bind_dn.clone()
|
|
|
|
}
|
|
|
|
|
2024-10-26 06:12:46 +00:00
|
|
|
pub fn get_ldap_bind_password(&self) -> Option<String> {
|
2019-03-29 18:18:56 +00:00
|
|
|
self.ldap_bind_password.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_search_base_dn(&self) -> String {
|
|
|
|
self.ldap_search_base_dn.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_search_filter(&self) -> String {
|
|
|
|
self.ldap_search_filter.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_mail_field(&self) -> String {
|
|
|
|
match &self.ldap_mail_field {
|
|
|
|
Some(mail_field) => mail_field.clone(),
|
2021-06-14 01:01:23 +00:00
|
|
|
None => String::from("mail"),
|
2019-03-29 18:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ldap_sync_interval_seconds(&self) -> u64 {
|
|
|
|
self.ldap_sync_interval_seconds.unwrap_or(60)
|
|
|
|
}
|
2019-04-11 23:20:00 +00:00
|
|
|
|
|
|
|
pub fn get_ldap_sync_loop(&self) -> bool {
|
|
|
|
self.ldap_sync_loop.unwrap_or(true)
|
|
|
|
}
|
2019-03-29 18:18:56 +00:00
|
|
|
}
|