mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-06-05 11:02:44 +03:00
Make the optional configuration sections really optional
This commit is contained in:
parent
fa97916527
commit
49826c1aa4
@ -17,7 +17,7 @@ use std::process::ExitCode;
|
|||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
use mas_config::{ConfigurationSection, DatabaseConfig};
|
use mas_config::{ConfigurationSectionExt, DatabaseConfig};
|
||||||
use mas_storage_pg::MIGRATOR;
|
use mas_storage_pg::MIGRATOR;
|
||||||
use tracing::{info_span, Instrument};
|
use tracing::{info_span, Instrument};
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ enum Subcommand {
|
|||||||
impl Options {
|
impl Options {
|
||||||
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
|
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
|
||||||
let _span = info_span!("cli.database.migrate").entered();
|
let _span = info_span!("cli.database.migrate").entered();
|
||||||
let config = DatabaseConfig::extract(figment)?;
|
let config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&config).await?;
|
let mut conn = database_connection_from_config(&config).await?;
|
||||||
|
|
||||||
// Run pending migrations
|
// Run pending migrations
|
||||||
|
@ -18,7 +18,7 @@ use clap::Parser;
|
|||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
use http_body_util::BodyExt;
|
use http_body_util::BodyExt;
|
||||||
use hyper::{Response, Uri};
|
use hyper::{Response, Uri};
|
||||||
use mas_config::{ConfigurationSection, PolicyConfig};
|
use mas_config::{ConfigurationSectionExt, PolicyConfig};
|
||||||
use mas_handlers::HttpClientFactory;
|
use mas_handlers::HttpClientFactory;
|
||||||
use mas_http::HttpServiceExt;
|
use mas_http::HttpServiceExt;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
@ -124,7 +124,7 @@ impl Options {
|
|||||||
|
|
||||||
SC::Policy => {
|
SC::Policy => {
|
||||||
let _span = info_span!("cli.debug.policy").entered();
|
let _span = info_span!("cli.debug.policy").entered();
|
||||||
let config = PolicyConfig::extract(figment)?;
|
let config = PolicyConfig::extract_or_default(figment)?;
|
||||||
info!("Loading and compiling the policy module");
|
info!("Loading and compiling the policy module");
|
||||||
let policy_factory = policy_factory_from_config(&config).await?;
|
let policy_factory = policy_factory_from_config(&config).await?;
|
||||||
|
|
||||||
|
@ -19,7 +19,9 @@ use clap::{ArgAction, CommandFactory, Parser};
|
|||||||
use console::{pad_str, style, Alignment, Style, Term};
|
use console::{pad_str, style, Alignment, Style, Term};
|
||||||
use dialoguer::{theme::ColorfulTheme, Confirm, FuzzySelect, Input, Password};
|
use dialoguer::{theme::ColorfulTheme, Confirm, FuzzySelect, Input, Password};
|
||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
use mas_config::{ConfigurationSection, DatabaseConfig, MatrixConfig, PasswordsConfig};
|
use mas_config::{
|
||||||
|
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, MatrixConfig, PasswordsConfig,
|
||||||
|
};
|
||||||
use mas_data_model::{Device, TokenType, Ulid, UpstreamOAuthProvider, User};
|
use mas_data_model::{Device, TokenType, Ulid, UpstreamOAuthProvider, User};
|
||||||
use mas_email::Address;
|
use mas_email::Address;
|
||||||
use mas_handlers::HttpClientFactory;
|
use mas_handlers::HttpClientFactory;
|
||||||
@ -192,8 +194,8 @@ impl Options {
|
|||||||
let _span =
|
let _span =
|
||||||
info_span!("cli.manage.set_password", user.username = %username).entered();
|
info_span!("cli.manage.set_password", user.username = %username).entered();
|
||||||
|
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let passwords_config = PasswordsConfig::extract(figment)?;
|
let passwords_config = PasswordsConfig::extract_or_default(figment)?;
|
||||||
|
|
||||||
let mut conn = database_connection_from_config(&database_config).await?;
|
let mut conn = database_connection_from_config(&database_config).await?;
|
||||||
let password_manager = password_manager_from_config(&passwords_config).await?;
|
let password_manager = password_manager_from_config(&passwords_config).await?;
|
||||||
@ -233,7 +235,7 @@ impl Options {
|
|||||||
)
|
)
|
||||||
.entered();
|
.entered();
|
||||||
|
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&database_config).await?;
|
let mut conn = database_connection_from_config(&database_config).await?;
|
||||||
let txn = conn.begin().await?;
|
let txn = conn.begin().await?;
|
||||||
let mut repo = PgRepository::from_conn(txn);
|
let mut repo = PgRepository::from_conn(txn);
|
||||||
@ -267,7 +269,7 @@ impl Options {
|
|||||||
admin,
|
admin,
|
||||||
device_id,
|
device_id,
|
||||||
} => {
|
} => {
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&database_config).await?;
|
let mut conn = database_connection_from_config(&database_config).await?;
|
||||||
let txn = conn.begin().await?;
|
let txn = conn.begin().await?;
|
||||||
let mut repo = PgRepository::from_conn(txn);
|
let mut repo = PgRepository::from_conn(txn);
|
||||||
@ -312,7 +314,7 @@ impl Options {
|
|||||||
|
|
||||||
SC::ProvisionAllUsers => {
|
SC::ProvisionAllUsers => {
|
||||||
let _span = info_span!("cli.manage.provision_all_users").entered();
|
let _span = info_span!("cli.manage.provision_all_users").entered();
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&database_config).await?;
|
let mut conn = database_connection_from_config(&database_config).await?;
|
||||||
let mut txn = conn.begin().await?;
|
let mut txn = conn.begin().await?;
|
||||||
|
|
||||||
@ -338,7 +340,7 @@ impl Options {
|
|||||||
SC::KillSessions { username, dry_run } => {
|
SC::KillSessions { username, dry_run } => {
|
||||||
let _span =
|
let _span =
|
||||||
info_span!("cli.manage.kill_sessions", user.username = username).entered();
|
info_span!("cli.manage.kill_sessions", user.username = username).entered();
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&database_config).await?;
|
let mut conn = database_connection_from_config(&database_config).await?;
|
||||||
let txn = conn.begin().await?;
|
let txn = conn.begin().await?;
|
||||||
let mut repo = PgRepository::from_conn(txn);
|
let mut repo = PgRepository::from_conn(txn);
|
||||||
@ -408,7 +410,7 @@ impl Options {
|
|||||||
deactivate,
|
deactivate,
|
||||||
} => {
|
} => {
|
||||||
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
|
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
|
||||||
let config = DatabaseConfig::extract(figment)?;
|
let config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&config).await?;
|
let mut conn = database_connection_from_config(&config).await?;
|
||||||
let txn = conn.begin().await?;
|
let txn = conn.begin().await?;
|
||||||
let mut repo = PgRepository::from_conn(txn);
|
let mut repo = PgRepository::from_conn(txn);
|
||||||
@ -440,7 +442,7 @@ impl Options {
|
|||||||
|
|
||||||
SC::UnlockUser { username } => {
|
SC::UnlockUser { username } => {
|
||||||
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
|
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
|
||||||
let config = DatabaseConfig::extract(figment)?;
|
let config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let mut conn = database_connection_from_config(&config).await?;
|
let mut conn = database_connection_from_config(&config).await?;
|
||||||
let txn = conn.begin().await?;
|
let txn = conn.begin().await?;
|
||||||
let mut repo = PgRepository::from_conn(txn);
|
let mut repo = PgRepository::from_conn(txn);
|
||||||
@ -473,8 +475,8 @@ impl Options {
|
|||||||
ignore_password_complexity,
|
ignore_password_complexity,
|
||||||
} => {
|
} => {
|
||||||
let http_client_factory = HttpClientFactory::new();
|
let http_client_factory = HttpClientFactory::new();
|
||||||
let password_config = PasswordsConfig::extract(figment)?;
|
let password_config = PasswordsConfig::extract_or_default(figment)?;
|
||||||
let database_config = DatabaseConfig::extract(figment)?;
|
let database_config = DatabaseConfig::extract_or_default(figment)?;
|
||||||
let matrix_config = MatrixConfig::extract(figment)?;
|
let matrix_config = MatrixConfig::extract(figment)?;
|
||||||
|
|
||||||
let password_manager = password_manager_from_config(&password_config).await?;
|
let password_manager = password_manager_from_config(&password_config).await?;
|
||||||
|
@ -18,7 +18,9 @@ use anyhow::Context;
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use mas_config::{AppConfig, ClientsConfig, ConfigurationSection, UpstreamOAuth2Config};
|
use mas_config::{
|
||||||
|
AppConfig, ClientsConfig, ConfigurationSection, ConfigurationSectionExt, UpstreamOAuth2Config,
|
||||||
|
};
|
||||||
use mas_handlers::{ActivityTracker, CookieManager, HttpClientFactory, Limiter, MetadataCache};
|
use mas_handlers::{ActivityTracker, CookieManager, HttpClientFactory, Limiter, MetadataCache};
|
||||||
use mas_listener::{server::Server, shutdown::ShutdownStream};
|
use mas_listener::{server::Server, shutdown::ShutdownStream};
|
||||||
use mas_matrix_synapse::SynapseConnection;
|
use mas_matrix_synapse::SynapseConnection;
|
||||||
@ -103,8 +105,8 @@ impl Options {
|
|||||||
} else {
|
} else {
|
||||||
// Sync the configuration with the database
|
// Sync the configuration with the database
|
||||||
let mut conn = pool.acquire().await?;
|
let mut conn = pool.acquire().await?;
|
||||||
let clients_config = ClientsConfig::extract(figment)?;
|
let clients_config = ClientsConfig::extract_or_default(figment)?;
|
||||||
let upstream_oauth2_config = UpstreamOAuth2Config::extract(figment)?;
|
let upstream_oauth2_config = UpstreamOAuth2Config::extract_or_default(figment)?;
|
||||||
|
|
||||||
crate::sync::config_sync(
|
crate::sync::config_sync(
|
||||||
upstream_oauth2_config,
|
upstream_oauth2_config,
|
||||||
|
@ -17,8 +17,8 @@ use std::process::ExitCode;
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
use mas_config::{
|
use mas_config::{
|
||||||
AccountConfig, BrandingConfig, CaptchaConfig, ConfigurationSection, ExperimentalConfig,
|
AccountConfig, BrandingConfig, CaptchaConfig, ConfigurationSection, ConfigurationSectionExt,
|
||||||
MatrixConfig, PasswordsConfig, TemplatesConfig,
|
ExperimentalConfig, MatrixConfig, PasswordsConfig, TemplatesConfig,
|
||||||
};
|
};
|
||||||
use mas_storage::{Clock, SystemClock};
|
use mas_storage::{Clock, SystemClock};
|
||||||
use rand::SeedableRng;
|
use rand::SeedableRng;
|
||||||
@ -45,13 +45,13 @@ impl Options {
|
|||||||
SC::Check => {
|
SC::Check => {
|
||||||
let _span = info_span!("cli.templates.check").entered();
|
let _span = info_span!("cli.templates.check").entered();
|
||||||
|
|
||||||
let template_config = TemplatesConfig::extract(figment)?;
|
let template_config = TemplatesConfig::extract_or_default(figment)?;
|
||||||
let branding_config = BrandingConfig::extract(figment)?;
|
let branding_config = BrandingConfig::extract_or_default(figment)?;
|
||||||
let matrix_config = MatrixConfig::extract(figment)?;
|
let matrix_config = MatrixConfig::extract(figment)?;
|
||||||
let experimental_config = ExperimentalConfig::extract(figment)?;
|
let experimental_config = ExperimentalConfig::extract_or_default(figment)?;
|
||||||
let password_config = PasswordsConfig::extract(figment)?;
|
let password_config = PasswordsConfig::extract_or_default(figment)?;
|
||||||
let account_config = AccountConfig::extract(figment)?;
|
let account_config = AccountConfig::extract_or_default(figment)?;
|
||||||
let captcha_config = CaptchaConfig::extract(figment)?;
|
let captcha_config = CaptchaConfig::extract_or_default(figment)?;
|
||||||
|
|
||||||
let clock = SystemClock::default();
|
let clock = SystemClock::default();
|
||||||
// XXX: we should disallow SeedableRng::from_entropy
|
// XXX: we should disallow SeedableRng::from_entropy
|
||||||
|
@ -26,4 +26,7 @@ pub(crate) mod schema;
|
|||||||
mod sections;
|
mod sections;
|
||||||
pub(crate) mod util;
|
pub(crate) mod util;
|
||||||
|
|
||||||
pub use self::{sections::*, util::ConfigurationSection};
|
pub use self::{
|
||||||
|
sections::*,
|
||||||
|
util::{ConfigurationSection, ConfigurationSectionExt},
|
||||||
|
};
|
||||||
|
@ -46,3 +46,32 @@ pub trait ConfigurationSection: Sized + DeserializeOwned {
|
|||||||
Ok(this)
|
Ok(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extension trait for [`ConfigurationSection`] to allow extracting the
|
||||||
|
/// configuration section from a [`Figment`] or return the default value if the
|
||||||
|
/// section is not present.
|
||||||
|
pub trait ConfigurationSectionExt: ConfigurationSection + Default {
|
||||||
|
/// Extract the configuration section from the given [`Figment`], or return
|
||||||
|
/// the default value if the section is not present.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if the configuration section is invalid.
|
||||||
|
fn extract_or_default(figment: &Figment) -> Result<Self, figment::Error> {
|
||||||
|
let this: Self = if let Some(path) = Self::PATH {
|
||||||
|
// If the configuration section is not present, we return the default value
|
||||||
|
if !figment.contains(path) {
|
||||||
|
return Ok(Self::default());
|
||||||
|
}
|
||||||
|
|
||||||
|
figment.extract_inner(path)?
|
||||||
|
} else {
|
||||||
|
figment.extract()?
|
||||||
|
};
|
||||||
|
|
||||||
|
this.validate(figment)?;
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ConfigurationSection + Default> ConfigurationSectionExt for T {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user