config: Prevent loading the wrong server_key

This commit is contained in:
Valentin Tolmer 2021-11-27 16:34:50 +01:00 committed by nitnelave
parent 5b5395103a
commit 9653d64eb1
2 changed files with 14 additions and 15 deletions

View File

@ -45,6 +45,11 @@ pub struct RunOpts {
#[clap(flatten)] #[clap(flatten)]
pub general_config: GeneralConfigOpts, pub general_config: GeneralConfigOpts,
/// Path to the file that contains the private server key.
/// It will be created if it doesn't exist.
#[clap(long, env = "LLDAP_SERVER_KEY_FILE")]
pub server_key_file: Option<String>,
/// Change ldap port. Default: 3890 /// Change ldap port. Default: 3890
#[clap(long, env = "LLDAP_LDAP_PORT")] #[clap(long, env = "LLDAP_LDAP_PORT")]
pub ldap_port: Option<u16>, pub ldap_port: Option<u16>,

View File

@ -37,10 +37,7 @@ impl std::default::Default for MailOptions {
} }
#[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)] #[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
#[builder( #[builder(pattern = "owned", build_fn(name = "private_build"))]
pattern = "owned",
build_fn(name = "private_build", validate = "Self::validate")
)]
pub struct Configuration { pub struct Configuration {
#[builder(default = "3890")] #[builder(default = "3890")]
pub ldap_port: u16, pub ldap_port: u16,
@ -67,7 +64,7 @@ pub struct Configuration {
#[builder(default = r#"String::from("http://localhost")"#)] #[builder(default = r#"String::from("http://localhost")"#)]
pub http_url: String, pub http_url: String,
#[serde(skip)] #[serde(skip)]
#[builder(field(private), setter(strip_option))] #[builder(field(private), default = "None")]
server_setup: Option<ServerSetup>, server_setup: Option<ServerSetup>,
} }
@ -80,15 +77,7 @@ impl std::default::Default for Configuration {
impl ConfigurationBuilder { impl ConfigurationBuilder {
pub fn build(self) -> Result<Configuration> { pub fn build(self) -> Result<Configuration> {
let server_setup = get_server_setup(self.key_file.as_deref().unwrap_or("server_key"))?; let server_setup = get_server_setup(self.key_file.as_deref().unwrap_or("server_key"))?;
Ok(self.server_setup(server_setup).private_build()?) Ok(self.server_setup(Some(server_setup)).private_build()?)
}
fn validate(&self) -> Result<(), String> {
if self.server_setup.is_none() {
Err("Don't use `private_build`, use `build` instead".to_string())
} else {
Ok(())
}
} }
} }
@ -143,6 +132,11 @@ impl TopLevelCommandOpts for TestEmailOpts {
impl ConfigOverrider for RunOpts { impl ConfigOverrider for RunOpts {
fn override_config(&self, config: &mut Configuration) { fn override_config(&self, config: &mut Configuration) {
self.general_config.override_config(config); self.general_config.override_config(config);
if let Some(path) = self.server_key_file.as_ref() {
config.key_file = path.to_string();
}
if let Some(port) = self.ldap_port { if let Some(port) = self.ldap_port {
config.ldap_port = port; config.ldap_port = port;
} }
@ -215,7 +209,7 @@ where
); );
let mut config: Configuration = Figment::from(Serialized::defaults( let mut config: Configuration = Figment::from(Serialized::defaults(
ConfigurationBuilder::default().build().unwrap(), ConfigurationBuilder::default().private_build().unwrap(),
)) ))
.merge(Toml::file(config_file)) .merge(Toml::file(config_file))
.merge(Env::prefixed("LLDAP_").split("__")) .merge(Env::prefixed("LLDAP_").split("__"))