aerogramme/src/config.rs

156 lines
3.5 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)]
2023-12-04 15:51:27 +00:00
pub struct CompanionConfig {
pub pid: Option<String>,
pub imap: ImapConfig,
2023-12-04 15:51:27 +00:00
#[serde(flatten)]
2023-12-06 19:57:25 +00:00
pub users: LoginStaticConfig,
2022-05-19 10:10:48 +00:00
}
2023-12-04 15:51:27 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProviderConfig {
pub pid: Option<String>,
pub imap: ImapConfig,
pub lmtp: LmtpConfig,
pub users: UserManagement,
}
2023-11-17 15:42:25 +00:00
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-12-04 15:51:27 +00:00
#[serde(tag = "user_driver")]
pub enum UserManagement {
2023-12-06 19:57:25 +00:00
Static(LoginStaticConfig),
2023-12-04 15:51:27 +00:00
Ldap(LoginLdapConfig),
2022-05-19 10:10:48 +00:00
}
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-12-04 15:51:27 +00:00
pub struct LmtpConfig {
pub bind_addr: SocketAddr,
pub hostname: String,
}
2022-05-19 12:33:49 +00:00
2023-12-04 15:51:27 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ImapConfig {
pub bind_addr: SocketAddr,
2023-11-17 15:42:25 +00:00
}
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-12-06 19:57:25 +00:00
pub struct LoginStaticConfig {
pub user_list: PathBuf,
2022-05-19 10:10:48 +00:00
}
2023-11-17 15:42:25 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-11-24 10:44:42 +00:00
#[serde(tag = "storage_driver")]
2023-11-17 15:42:25 +00:00
pub enum LdapStorage {
Garage(LdapGarageConfig),
InMemory,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LdapGarageConfig {
pub s3_endpoint: String,
pub k2v_endpoint: String,
pub aws_region: String,
pub aws_access_key_id_attr: String,
pub aws_secret_access_key_attr: String,
pub bucket_attr: Option<String>,
pub default_bucket: Option<String>,
}
2022-05-25 11:07:19 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2022-05-19 10:10:48 +00:00
pub struct LoginLdapConfig {
2023-11-17 15:42:25 +00:00
// LDAP connection info
2022-05-19 10:10:48 +00:00
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,
2023-11-17 15:42:25 +00:00
// Schema-like info required for Aerogramme's logic
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
2023-11-17 15:42:25 +00:00
// Storage related thing
2023-11-24 10:44:42 +00:00
#[serde(flatten)]
2023-11-17 15:42:25 +00:00
pub storage: LdapStorage,
2022-05-19 10:10:48 +00:00
}
2023-12-04 15:51:27 +00:00
// ----
2022-06-15 16:40:39 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-12-04 15:51:27 +00:00
#[serde(tag = "storage_driver")]
pub enum StaticStorage {
Garage(StaticGarageConfig),
InMemory,
}
2022-06-17 16:39:36 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2023-12-04 15:51:27 +00:00
pub struct StaticGarageConfig {
pub s3_endpoint: String,
pub k2v_endpoint: String,
pub aws_region: String,
pub aws_access_key_id: String,
pub aws_secret_access_key: String,
pub bucket: String,
}
2023-12-06 19:57:25 +00:00
pub type UserList = HashMap<String, UserEntry>;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "crypto_root")]
pub enum CryptographyRoot {
PasswordProtected,
Keyring,
InPlace {
master_key: String,
secret_key: String,
}
}
2023-12-04 15:51:27 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserEntry {
#[serde(default)]
pub email_addresses: Vec<String>,
pub password: String,
2023-12-08 17:13:00 +00:00
#[serde(flatten)]
2023-12-06 19:57:25 +00:00
pub crypto_root: CryptographyRoot,
2023-12-04 15:51:27 +00:00
#[serde(flatten)]
pub storage: StaticStorage,
2022-06-17 16:39:36 +00:00
}
2023-12-06 19:57:25 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "role")]
pub enum AnyConfig {
Companion(CompanionConfig),
Provider(ProviderConfig),
}
2023-12-04 15:51:27 +00:00
// ---
2023-12-08 14:23:50 +00:00
pub fn read_config<T: serde::de::DeserializeOwned>(config_file: PathBuf) -> Result<T> {
2022-05-19 10:10:48 +00:00
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()
}