Skip to content
Open
9 changes: 7 additions & 2 deletions crates/cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::process::ExitCode;
use clap::Parser;
use figment::Figment;
use mas_config::{
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, MatrixConfig, PolicyConfig,
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, ExperimentalConfig,
MatrixConfig, PolicyConfig,
};
use mas_storage_pg::PgRepositoryFactory;
use tracing::{info, info_span};
Expand Down Expand Up @@ -45,8 +46,12 @@ impl Options {
PolicyConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
let matrix_config =
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
let experimental_config =
ExperimentalConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
info!("Loading and compiling the policy module");
let policy_factory = policy_factory_from_config(&config, &matrix_config).await?;
let policy_factory =
policy_factory_from_config(&config, &matrix_config, &experimental_config)
.await?;

if with_dynamic_data {
let database_config =
Expand Down
4 changes: 3 additions & 1 deletion crates/cli/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ impl Options {

// Load and compile the WASM policies (and fallback to the default embedded one)
info!("Loading and compiling the policy module");
let policy_factory = policy_factory_from_config(&config.policy, &config.matrix).await?;
let policy_factory =
policy_factory_from_config(&config.policy, &config.matrix, &config.experimental)
.await?;
let policy_factory = Arc::new(policy_factory);

load_policy_factory_dynamic_data_continuously(
Expand Down
14 changes: 12 additions & 2 deletions crates/cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub fn test_mailer_in_background(mailer: &Mailer, timeout: Duration) {
pub async fn policy_factory_from_config(
config: &PolicyConfig,
matrix_config: &MatrixConfig,
experimental_config: &ExperimentalConfig,
) -> Result<PolicyFactory, anyhow::Error> {
let policy_file = tokio::fs::File::open(&config.wasm_module)
.await
Expand All @@ -147,8 +148,17 @@ pub async fn policy_factory_from_config(
email: config.email_entrypoint.clone(),
};

let data =
mas_policy::Data::new(matrix_config.homeserver.clone()).with_rest(config.data.clone());
let session_limit_config =
experimental_config
.session_limit
.as_ref()
.map(|c| SessionLimitConfig {
soft_limit: c.soft_limit,
hard_limit: c.hard_limit,
});

let data = mas_policy::Data::new(matrix_config.homeserver.clone(), session_limit_config)
.with_rest(config.data.clone());

PolicyFactory::load(policy_file, data, entrypoints)
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/handlers/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) async fn policy_factory(
email: "email/violation".to_owned(),
};

let data = mas_policy::Data::new(server_name.to_owned()).with_rest(data);
let data = mas_policy::Data::new(server_name.to_owned(), None).with_rest(data);

let policy_factory = PolicyFactory::load(file, data, entrypoints).await?;
let policy_factory = Arc::new(policy_factory);
Expand Down
24 changes: 15 additions & 9 deletions crates/policy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ pub mod model;
use std::sync::Arc;

use arc_swap::ArcSwap;
use mas_data_model::Ulid;
use mas_data_model::{SessionLimitConfig, Ulid};
use opa_wasm::{
Runtime,
wasmtime::{Config, Engine, Module, OptLevel, Store},
};
use serde::Serialize;
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt};

Expand Down Expand Up @@ -85,18 +86,25 @@ impl Entrypoints {
}
}

#[derive(Debug)]
#[derive(Serialize, Debug)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively this could have a serializeable static part plus rest as two separate properties, which feels a smidge less 'smart', but also fine with this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok you tricked me into writing DataBase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub struct Data {
server_name: String,

/// Limits on the number of application sessions that each user can have
session_limit: Option<SessionLimitConfig>,

// We will merge this in a custom way, so don't emit as part of the base
#[serde(skip)]
rest: Option<serde_json::Value>,
}

impl Data {
#[must_use]
pub fn new(server_name: String) -> Self {
pub fn new(server_name: String, session_limit: Option<SessionLimitConfig>) -> Self {
Self {
server_name,
session_limit,

rest: None,
}
}
Expand All @@ -108,9 +116,7 @@ impl Data {
}

fn to_value(&self) -> Result<serde_json::Value, anyhow::Error> {
let base = serde_json::json!({
"server_name": self.server_name,
});
let base = serde_json::to_value(self)?;

if let Some(rest) = &self.rest {
merge_data(base, rest.clone())
Expand Down Expand Up @@ -458,7 +464,7 @@ mod tests {

#[tokio::test]
async fn test_register() {
let data = Data::new("example.com".to_owned()).with_rest(serde_json::json!({
let data = Data::new("example.com".to_owned(), None).with_rest(serde_json::json!({
"allowed_domains": ["element.io", "*.element.io"],
"banned_domains": ["staging.element.io"],
}));
Expand Down Expand Up @@ -528,7 +534,7 @@ mod tests {

#[tokio::test]
async fn test_dynamic_data() {
let data = Data::new("example.com".to_owned());
let data = Data::new("example.com".to_owned(), None);

#[allow(clippy::disallowed_types)]
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
Expand Down Expand Up @@ -597,7 +603,7 @@ mod tests {

#[tokio::test]
async fn test_big_dynamic_data() {
let data = Data::new("example.com".to_owned());
let data = Data::new("example.com".to_owned(), None);

#[allow(clippy::disallowed_types)]
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
Expand Down