diff options
author | Quentin <quentin@dufour.io> | 2023-12-27 16:35:43 +0000 |
---|---|---|
committer | Quentin <quentin@dufour.io> | 2023-12-27 16:35:43 +0000 |
commit | 6ff3c6f71efd802da422a371e6168ae528fb2ddc (patch) | |
tree | 62b5d7d9bc7fd2bf3defd1a85ae1b3f34cd4b8ee /src/config.rs | |
parent | 609dde413972ebeeb8cd658a5ec9f62b34b5c402 (diff) | |
parent | ea4cd48bba96027882a637df08e313af92a3db46 (diff) | |
download | aerogramme-6ff3c6f71efd802da422a371e6168ae528fb2ddc.tar.gz aerogramme-6ff3c6f71efd802da422a371e6168ae528fb2ddc.zip |
Add storage behind a trait
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/aerogramme/pulls/32
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 152 |
1 files changed, 114 insertions, 38 deletions
diff --git a/src/config.rs b/src/config.rs index 074c192..1438910 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::io::Read; +use std::io::{Read, Write}; use std::net::SocketAddr; use std::path::PathBuf; @@ -7,77 +7,141 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Config { - pub s3_endpoint: String, - pub k2v_endpoint: String, - pub aws_region: String, +pub struct CompanionConfig { + pub pid: Option<PathBuf>, + pub imap: ImapConfig, - pub login_static: Option<LoginStaticConfig>, - pub login_ldap: Option<LoginLdapConfig>, + #[serde(flatten)] + pub users: LoginStaticConfig, +} - pub lmtp: Option<LmtpConfig>, - pub imap: Option<ImapConfig>, +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ProviderConfig { + pub pid: Option<PathBuf>, + pub imap: ImapConfig, + pub lmtp: LmtpConfig, + pub users: UserManagement, } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct LoginStaticConfig { - pub default_bucket: Option<String>, - pub users: HashMap<String, LoginStaticUser>, +#[serde(tag = "user_driver")] +pub enum UserManagement { + Static(LoginStaticConfig), + Ldap(LoginLdapConfig), } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct LoginStaticUser { - #[serde(default)] - pub email_addresses: Vec<String>, - pub password: String, +pub struct LmtpConfig { + pub bind_addr: SocketAddr, + pub hostname: String, +} - pub aws_access_key_id: String, - pub aws_secret_access_key: String, - pub bucket: Option<String>, +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ImapConfig { + pub bind_addr: SocketAddr, +} - pub user_secret: String, - #[serde(default)] - pub alternate_user_secrets: Vec<String>, +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct LoginStaticConfig { + pub user_list: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(tag = "storage_driver")] +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 master_key: Option<String>, - pub secret_key: Option<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>, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct LoginLdapConfig { + // LDAP connection info pub ldap_server: String, - #[serde(default)] pub pre_bind_on_login: bool, pub bind_dn: Option<String>, pub bind_password: Option<String>, - pub search_base: String, + + // Schema-like info required for Aerogramme's logic pub username_attr: String, #[serde(default = "default_mail_attr")] pub mail_attr: String, - 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>, + // The field that will contain the crypto root thingy + pub crypto_root_attr: String, - pub bucket: Option<String>, - pub bucket_attr: Option<String>, + // Storage related thing + #[serde(flatten)] + pub storage: LdapStorage, } +// ---- + #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct LmtpConfig { - pub bind_addr: SocketAddr, - pub hostname: String, +#[serde(tag = "storage_driver")] +pub enum StaticStorage { + Garage(StaticGarageConfig), + InMemory, } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ImapConfig { - pub bind_addr: SocketAddr, +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, } -pub fn read_config(config_file: PathBuf) -> Result<Config> { +pub type UserList = HashMap<String, UserEntry>; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct UserEntry { + #[serde(default)] + pub email_addresses: Vec<String>, + pub password: String, + pub crypto_root: String, + + #[serde(flatten)] + pub storage: StaticStorage, +} + +#[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, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(tag = "role")] +pub enum AnyConfig { + Companion(CompanionConfig), + Provider(ProviderConfig), +} + +// --- +pub fn read_config<T: serde::de::DeserializeOwned>(config_file: PathBuf) -> Result<T> { let mut file = std::fs::OpenOptions::new() .read(true) .open(config_file.as_path())?; @@ -88,6 +152,18 @@ pub fn read_config(config_file: PathBuf) -> Result<Config> { Ok(toml::from_str(&config)?) } +pub fn write_config<T: Serialize>(config_file: PathBuf, config: &T) -> Result<()> { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(config_file.as_path())?; + + file.write_all(toml::to_string(config)?.as_bytes())?; + + Ok(()) +} + fn default_mail_attr() -> String { "mail".into() } |