aerogramme/src/config.rs

185 lines
4.3 KiB
Rust
Raw Normal View History

2022-05-19 10:10:48 +00:00
use std::collections::HashMap;
2023-12-08 18:06:12 +00:00
use std::io::{Read, Write};
use std::net::SocketAddr;
2022-05-19 10:10:48 +00:00
use std::path::PathBuf;
use anyhow::Result;
2023-12-13 15:09:01 +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 {
2023-12-14 14:36:54 +00:00
pub pid: Option<PathBuf>,
2024-01-23 15:14:58 +00:00
pub imap: ImapUnsecureConfig,
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 {
2023-12-14 14:36:54 +00:00
pub pid: Option<PathBuf>,
2024-01-23 15:14:58 +00:00
pub imap: Option<ImapConfig>,
pub imap_unsecure: Option<ImapUnsecureConfig>,
pub lmtp: Option<LmtpConfig>,
pub auth: Option<AuthConfig>,
2023-12-04 15:51:27 +00:00
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-27 17:33:06 +00:00
Demo,
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
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthConfig {
pub bind_addr: SocketAddr,
}
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 {
2024-01-23 15:14:58 +00:00
pub bind_addr: SocketAddr,
pub certs: PathBuf,
pub key: PathBuf,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ImapUnsecureConfig {
2023-12-04 15:51:27 +00:00
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,
2023-12-13 15:09:01 +00:00
// The field that will contain the crypto root thingy
2023-12-12 08:17:59 +00:00
pub crypto_root_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>;
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-13 15:09:01 +00:00
pub crypto_root: String,
2023-12-04 15:51:27 +00:00
#[serde(flatten)]
pub storage: StaticStorage,
2022-06-17 16:39:36 +00:00
}
2023-12-08 18:06:12 +00:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SetupEntry {
#[serde(default)]
pub email_addresses: Vec<String>,
#[serde(default)]
pub clear_password: Option<String>,
#[serde(flatten)]
pub storage: StaticStorage,
}
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
2023-12-08 18:06:12 +00:00
pub fn write_config<T: Serialize>(config_file: PathBuf, config: &T) -> Result<()> {
let mut file = std::fs::OpenOptions::new()
.write(true)
2023-12-13 17:04:04 +00:00
.create(true)
.truncate(true)
2023-12-08 18:06:12 +00:00
.open(config_file.as_path())?;
file.write_all(toml::to_string(config)?.as_bytes())?;
Ok(())
}
2022-05-23 16:19:33 +00:00
fn default_mail_attr() -> String {
"mail".into()
}