From ccc9b6abb66ebda0b91b4e21f8ec2fb2e87390f7 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 27 Dec 2023 18:33:06 +0100 Subject: [PATCH] add a --dev mode --- src/config.rs | 1 + src/login/demo_provider.rs | 48 +++++++++++++++++++ src/login/mod.rs | 1 + src/main.rs | 26 ++++++++-- src/server.rs | 3 +- tests/{ => instrumentation}/README.md | 0 .../{ => instrumentation}/docker-compose.yml | 0 .../docker/cyrus/Dockerfile | 0 .../docker/cyrus/entrypoint.sh | 0 .../docker/maddy/Dockerfile | 0 .../docker/maddy/entrypoint.sh | 0 tests/{ => instrumentation}/inject_emails.sh | 0 .../rm-mail-parser-expected-struct.py | 0 tests/{ => instrumentation}/send-to-imap.py | 0 tests/{ => instrumentation}/unix2dos.py | 0 15 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/login/demo_provider.rs rename tests/{ => instrumentation}/README.md (100%) rename tests/{ => instrumentation}/docker-compose.yml (100%) rename tests/{ => instrumentation}/docker/cyrus/Dockerfile (100%) rename tests/{ => instrumentation}/docker/cyrus/entrypoint.sh (100%) rename tests/{ => instrumentation}/docker/maddy/Dockerfile (100%) rename tests/{ => instrumentation}/docker/maddy/entrypoint.sh (100%) rename tests/{ => instrumentation}/inject_emails.sh (100%) rename tests/{ => instrumentation}/rm-mail-parser-expected-struct.py (100%) rename tests/{ => instrumentation}/send-to-imap.py (100%) rename tests/{ => instrumentation}/unix2dos.py (100%) diff --git a/src/config.rs b/src/config.rs index 1438910..b9c1f09 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,6 +26,7 @@ pub struct ProviderConfig { #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "user_driver")] pub enum UserManagement { + Demo, Static(LoginStaticConfig), Ldap(LoginLdapConfig), } diff --git a/src/login/demo_provider.rs b/src/login/demo_provider.rs new file mode 100644 index 0000000..0efb37c --- /dev/null +++ b/src/login/demo_provider.rs @@ -0,0 +1,48 @@ +use crate::login::*; +use crate::storage::*; + +pub struct DemoLoginProvider{ + keys: CryptoKeys, + in_memory_store: in_memory::MemDb, +} + +impl DemoLoginProvider { + pub fn new() -> Self { + Self { + keys: CryptoKeys::init(), + in_memory_store: in_memory::MemDb::new(), + } + } +} + +#[async_trait] +impl LoginProvider for DemoLoginProvider { + async fn login(&self, username: &str, password: &str) -> Result { + tracing::debug!(user=%username, "login"); + + if username != "alice" { + bail!("user does not exist"); + } + + if password != "hunter2" { + bail!("wrong password"); + } + + let storage = self.in_memory_store.builder("alice").await; + let keys = self.keys.clone(); + + Ok(Credentials { storage, keys }) + } + + async fn public_login(&self, email: &str) -> Result { + tracing::debug!(user=%email, "public_login"); + if email != "alice@example.tld" { + bail!("invalid email address"); + } + + let storage = self.in_memory_store.builder("alice").await; + let public_key = self.keys.public.clone(); + + Ok(PublicCredentials { storage, public_key }) + } +} diff --git a/src/login/mod.rs b/src/login/mod.rs index 2926738..6f2ca31 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -1,5 +1,6 @@ pub mod ldap_provider; pub mod static_provider; +pub mod demo_provider; use base64::Engine; use std::sync::Arc; diff --git a/src/main.rs b/src/main.rs index 3221c2e..3baa8e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,12 @@ struct Args { #[clap(subcommand)] command: Command, + /// A special mode dedicated to developers, NOT INTENDED FOR PRODUCTION + #[clap(long)] + dev: bool, + #[clap(short, long, env = "CONFIG_FILE", default_value = "aerogramme.toml")] + /// Path to the main Aerogramme configuration file config_file: PathBuf, } @@ -158,7 +163,22 @@ async fn main() -> Result<()> { tracing_subscriber::fmt::init(); let args = Args::parse(); - let any_config = read_config(args.config_file)?; + let any_config = if args.dev { + use std::net::*; + AnyConfig::Provider(ProviderConfig { + pid: None, + imap: ImapConfig { + bind_addr: SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 1143), + }, + lmtp: LmtpConfig { + bind_addr: SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 1025), + hostname: "example.tld".to_string(), + }, + users: UserManagement::Demo, + }) + } else { + read_config(args.config_file)? + }; match (&args.command, any_config) { (Command::Companion(subcommand), AnyConfig::Companion(config)) => match subcommand { @@ -184,8 +204,8 @@ async fn main() -> Result<()> { ProviderCommand::Account(cmd) => { let user_file = match config.users { UserManagement::Static(conf) => conf.user_list, - UserManagement::Ldap(_) => { - panic!("LDAP account management is not supported from Aerogramme.") + _ => { + panic!("Only static account management is supported from Aerogramme.") } }; account_management(&args.command, cmd, user_file)?; diff --git a/src/server.rs b/src/server.rs index 28e0b27..1b8677b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -11,7 +11,7 @@ use crate::config::*; use crate::imap; use crate::lmtp::*; use crate::login::ArcLoginProvider; -use crate::login::{ldap_provider::*, static_provider::*}; +use crate::login::{ldap_provider::*, static_provider::*, demo_provider::*}; pub struct Server { lmtp_server: Option>, @@ -36,6 +36,7 @@ impl Server { pub async fn from_provider_config(config: ProviderConfig) -> Result { tracing::info!("Init as provider"); let login: ArcLoginProvider = match config.users { + UserManagement::Demo => Arc::new(DemoLoginProvider::new()), UserManagement::Static(x) => Arc::new(StaticLoginProvider::new(x).await?), UserManagement::Ldap(x) => Arc::new(LdapLoginProvider::new(x)?), }; diff --git a/tests/README.md b/tests/instrumentation/README.md similarity index 100% rename from tests/README.md rename to tests/instrumentation/README.md diff --git a/tests/docker-compose.yml b/tests/instrumentation/docker-compose.yml similarity index 100% rename from tests/docker-compose.yml rename to tests/instrumentation/docker-compose.yml diff --git a/tests/docker/cyrus/Dockerfile b/tests/instrumentation/docker/cyrus/Dockerfile similarity index 100% rename from tests/docker/cyrus/Dockerfile rename to tests/instrumentation/docker/cyrus/Dockerfile diff --git a/tests/docker/cyrus/entrypoint.sh b/tests/instrumentation/docker/cyrus/entrypoint.sh similarity index 100% rename from tests/docker/cyrus/entrypoint.sh rename to tests/instrumentation/docker/cyrus/entrypoint.sh diff --git a/tests/docker/maddy/Dockerfile b/tests/instrumentation/docker/maddy/Dockerfile similarity index 100% rename from tests/docker/maddy/Dockerfile rename to tests/instrumentation/docker/maddy/Dockerfile diff --git a/tests/docker/maddy/entrypoint.sh b/tests/instrumentation/docker/maddy/entrypoint.sh similarity index 100% rename from tests/docker/maddy/entrypoint.sh rename to tests/instrumentation/docker/maddy/entrypoint.sh diff --git a/tests/inject_emails.sh b/tests/instrumentation/inject_emails.sh similarity index 100% rename from tests/inject_emails.sh rename to tests/instrumentation/inject_emails.sh diff --git a/tests/rm-mail-parser-expected-struct.py b/tests/instrumentation/rm-mail-parser-expected-struct.py similarity index 100% rename from tests/rm-mail-parser-expected-struct.py rename to tests/instrumentation/rm-mail-parser-expected-struct.py diff --git a/tests/send-to-imap.py b/tests/instrumentation/send-to-imap.py similarity index 100% rename from tests/send-to-imap.py rename to tests/instrumentation/send-to-imap.py diff --git a/tests/unix2dos.py b/tests/instrumentation/unix2dos.py similarity index 100% rename from tests/unix2dos.py rename to tests/instrumentation/unix2dos.py