1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Remove dependency on mas-email and lettre in mas-config

This commit is contained in:
Quentin Gliech
2022-12-15 12:27:12 +01:00
parent 5cc17921e0
commit fbbb842255
8 changed files with 90 additions and 104 deletions

View File

@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::Context;
use clap::Parser;
use hyper::{Response, Uri};
use mas_config::PolicyConfig;
use mas_handlers::HttpClientFactory;
use mas_http::HttpServiceExt;
use mas_policy::PolicyFactory;
use tokio::io::AsyncWriteExt;
use tower::{Service, ServiceExt};
use tracing::info;
use crate::util::policy_factory_from_config;
#[derive(Parser, Debug)]
pub(super) struct Options {
#[command(subcommand)]
@ -124,19 +124,7 @@ impl Options {
SC::Policy => {
let config: PolicyConfig = root.load_config()?;
info!("Loading and compiling the policy module");
let policy_file = tokio::fs::File::open(&config.wasm_module)
.await
.context("failed to open OPA WASM policy file")?;
let policy_factory = PolicyFactory::load(
policy_file,
config.data.clone().unwrap_or_default(),
config.register_entrypoint.clone(),
config.client_registration_entrypoint.clone(),
config.authorization_grant_entrypoint.clone(),
)
.await
.context("failed to load the policy")?;
let policy_factory = policy_factory_from_config(&config).await?;
let _instance = policy_factory.instantiate().await?;
Ok(())

View File

@ -19,10 +19,8 @@ use clap::Parser;
use futures_util::stream::{StreamExt, TryStreamExt};
use itertools::Itertools;
use mas_config::RootConfig;
use mas_email::Mailer;
use mas_handlers::{AppState, HttpClientFactory, MatrixHomeserver};
use mas_listener::{server::Server, shutdown::ShutdownStream};
use mas_policy::PolicyFactory;
use mas_router::UrlBuilder;
use mas_storage::MIGRATOR;
use mas_tasks::TaskQueue;
@ -30,7 +28,7 @@ use mas_templates::Templates;
use tokio::signal::unix::SignalKind;
use tracing::{error, info, log::warn};
use crate::util::password_manager_from_config;
use crate::util::{mailer_from_config, password_manager_from_config, policy_factory_from_config};
#[derive(Parser, Debug, Default)]
pub(super) struct Options {
@ -106,10 +104,6 @@ impl Options {
pub async fn run(&self, root: &super::Options) -> anyhow::Result<()> {
let config: RootConfig = root.load_config()?;
// Connect to the mail server
let mail_transport = config.email.transport.to_transport().await?;
mail_transport.test_connection().await?;
// Connect to the database
let pool = config.database.connect().await?;
@ -126,6 +120,7 @@ impl Options {
queue.recuring(Duration::from_secs(15), mas_tasks::cleanup_expired(&pool));
queue.start();
// TODO: task queue, key store, encrypter, url builder, http client
// Initialize the key store
let key_store = config
.secrets
@ -137,19 +132,7 @@ impl Options {
// Load and compile the WASM policies (and fallback to the default embedded one)
info!("Loading and compiling the policy module");
let policy_file = tokio::fs::File::open(&config.policy.wasm_module)
.await
.context("failed to open OPA WASM policy file")?;
let policy_factory = PolicyFactory::load(
policy_file,
config.policy.data.clone().unwrap_or_default(),
config.policy.register_entrypoint.clone(),
config.policy.client_registration_entrypoint.clone(),
config.policy.authorization_grant_entrypoint.clone(),
)
.await
.context("failed to load the policy")?;
let policy_factory = policy_factory_from_config(&config.policy).await?;
let policy_factory = Arc::new(policy_factory);
let url_builder = UrlBuilder::new(config.http.public_base.clone());
@ -159,12 +142,8 @@ impl Options {
.await
.context("could not load templates")?;
let mailer = Mailer::new(
&templates,
&mail_transport,
&config.email.from,
&config.email.reply_to,
);
let mailer = mailer_from_config(&config.email, &templates).await?;
mailer.test_connection().await?;
let homeserver = MatrixHomeserver::new(config.matrix.homeserver.clone());

View File

@ -12,8 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use mas_config::PasswordsConfig;
use anyhow::Context;
use mas_config::{EmailConfig, EmailSmtpMode, EmailTransportConfig, PasswordsConfig, PolicyConfig};
use mas_email::{MailTransport, Mailer};
use mas_handlers::passwords::PasswordManager;
use mas_policy::PolicyFactory;
use mas_templates::Templates;
pub async fn password_manager_from_config(
config: &PasswordsConfig,
@ -35,3 +39,55 @@ pub async fn password_manager_from_config(
PasswordManager::new(schemes)
}
pub async fn mailer_from_config(
config: &EmailConfig,
templates: &Templates,
) -> Result<Mailer, anyhow::Error> {
let from = config.from.parse()?;
let reply_to = config.reply_to.parse()?;
let transport = match &config.transport {
EmailTransportConfig::Blackhole => MailTransport::blackhole(),
EmailTransportConfig::Smtp {
mode,
hostname,
credentials,
port,
} => {
let credentials = credentials
.clone()
.map(|c| mas_email::SmtpCredentials::new(c.username, c.password));
let mode = match mode {
EmailSmtpMode::Plain => mas_email::SmtpMode::Plain,
EmailSmtpMode::StartTls => mas_email::SmtpMode::StartTls,
EmailSmtpMode::Tls => mas_email::SmtpMode::Tls,
};
MailTransport::smtp(mode, hostname, port.as_ref().copied(), credentials)
.context("failed to build SMTP transport")?
}
EmailTransportConfig::Sendmail { command } => MailTransport::sendmail(command),
EmailTransportConfig::AwsSes => MailTransport::aws_ses().await?,
};
Ok(Mailer::new(templates.clone(), transport, from, reply_to))
}
pub async fn policy_factory_from_config(
config: &PolicyConfig,
) -> Result<PolicyFactory, anyhow::Error> {
let policy_file = tokio::fs::File::open(&config.wasm_module)
.await
.context("failed to open OPA WASM policy file")?;
PolicyFactory::load(
policy_file,
config.data.clone().unwrap_or_default(),
config.register_entrypoint.clone(),
config.client_registration_entrypoint.clone(),
config.authorization_grant_entrypoint.clone(),
)
.await
.context("failed to load the policy")
}