aerogramme/src/config.rs

94 lines
2.2 KiB
Rust
Raw Normal View History

2022-05-19 10:10:48 +00:00
use std::collections::HashMap;
use std::io::Read;
use std::net::SocketAddr;
2022-05-19 10:10:48 +00:00
use std::path::PathBuf;
use anyhow::Result;
2022-06-01 16:00:56 +00:00
use serde::{Deserialize, Serialize};
2022-05-19 10:10:48 +00:00
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2022-05-19 10:10:48 +00:00
pub struct Config {
pub s3_endpoint: String,
pub k2v_endpoint: String,
2022-05-19 13:14:36 +00:00
pub aws_region: String,
2022-05-19 10:10:48 +00:00
pub login_static: Option<LoginStaticConfig>,
pub login_ldap: Option<LoginLdapConfig>,
pub lmtp: Option<LmtpConfig>,
2022-06-17 16:39:36 +00:00
pub imap: Option<ImapConfig>,
2022-05-19 10:10:48 +00:00
}
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2022-05-19 10:10:48 +00:00
pub struct LoginStaticConfig {
pub default_bucket: Option<String>,
pub users: HashMap<String, LoginStaticUser>,
}
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2022-05-19 10:10:48 +00:00
pub struct LoginStaticUser {
2022-05-31 13:30:32 +00:00
#[serde(default)]
pub email_addresses: Vec<String>,
2022-05-19 10:10:48 +00:00
pub password: String,
2022-05-19 12:33:49 +00:00
2022-05-19 10:10:48 +00:00
pub aws_access_key_id: String,
pub aws_secret_access_key: String,
pub bucket: Option<String>,
2022-05-19 12:33:49 +00:00
pub user_secret: String,
#[serde(default)]
pub alternate_user_secrets: Vec<String>,
2022-05-19 10:10:48 +00:00
pub master_key: Option<String>,
2022-05-19 12:33:49 +00:00
pub secret_key: Option<String>,
2022-05-19 10:10:48 +00:00
}
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2022-05-19 10:10:48 +00:00
pub struct LoginLdapConfig {
pub ldap_server: String,
2022-05-23 16:19:33 +00:00
#[serde(default)]
pub pre_bind_on_login: bool,
pub bind_dn: Option<String>,
pub bind_password: Option<String>,
pub search_base: String,
2022-05-19 10:10:48 +00:00
pub username_attr: String,
2022-05-23 16:19:33 +00:00
#[serde(default = "default_mail_attr")]
pub mail_attr: String,
2022-05-19 10:10:48 +00:00
pub aws_access_key_id_attr: String,
pub aws_secret_access_key_attr: String,
pub user_secret_attr: String,
pub alternate_user_secrets_attr: Option<String>,
2022-05-19 10:10:48 +00:00
pub bucket: Option<String>,
pub bucket_attr: Option<String>,
}
2022-06-15 16:40:39 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LmtpConfig {
pub bind_addr: SocketAddr,
2022-05-31 15:07:34 +00:00
pub hostname: String,
}
2022-06-17 16:39:36 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ImapConfig {
pub bind_addr: SocketAddr,
}
2022-05-19 10:10:48 +00:00
pub fn read_config(config_file: PathBuf) -> Result<Config> {
let mut file = std::fs::OpenOptions::new()
.read(true)
.open(config_file.as_path())?;
let mut config = String::new();
file.read_to_string(&mut config)?;
Ok(toml::from_str(&config)?)
}
2022-05-23 16:19:33 +00:00
fn default_mail_attr() -> String {
"mail".into()
}