Skip to content

Commit b6097cb

Browse files
committed
Add session limit config to policy data
1 parent 86612b1 commit b6097cb

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

crates/cli/src/commands/debug.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use std::process::ExitCode;
99
use clap::Parser;
1010
use figment::Figment;
1111
use mas_config::{
12-
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, MatrixConfig, PolicyConfig,
12+
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, ExperimentalConfig,
13+
MatrixConfig, PolicyConfig,
1314
};
1415
use mas_storage_pg::PgRepositoryFactory;
1516
use tracing::{info, info_span};
@@ -45,8 +46,12 @@ impl Options {
4546
PolicyConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
4647
let matrix_config =
4748
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
49+
let experimental_config =
50+
ExperimentalConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
4851
info!("Loading and compiling the policy module");
49-
let policy_factory = policy_factory_from_config(&config, &matrix_config).await?;
52+
let policy_factory =
53+
policy_factory_from_config(&config, &matrix_config, &experimental_config)
54+
.await?;
5055

5156
if with_dynamic_data {
5257
let database_config =

crates/cli/src/commands/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ impl Options {
132132

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

138140
load_policy_factory_dynamic_data_continuously(

crates/cli/src/util.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub fn test_mailer_in_background(mailer: &Mailer, timeout: Duration) {
135135
pub async fn policy_factory_from_config(
136136
config: &PolicyConfig,
137137
matrix_config: &MatrixConfig,
138+
experimental_config: &ExperimentalConfig,
138139
) -> Result<PolicyFactory, anyhow::Error> {
139140
let policy_file = tokio::fs::File::open(&config.wasm_module)
140141
.await
@@ -147,8 +148,17 @@ pub async fn policy_factory_from_config(
147148
email: config.email_entrypoint.clone(),
148149
};
149150

150-
let data =
151-
mas_policy::Data::new(matrix_config.homeserver.clone()).with_rest(config.data.clone());
151+
let session_limit_config =
152+
experimental_config
153+
.session_limit
154+
.as_ref()
155+
.map(|c| SessionLimitConfig {
156+
soft_limit: c.soft_limit,
157+
hard_limit: c.hard_limit,
158+
});
159+
160+
let data = mas_policy::Data::new(matrix_config.homeserver.clone(), session_limit_config)
161+
.with_rest(config.data.clone());
152162

153163
PolicyFactory::load(policy_file, data, entrypoints)
154164
.await

crates/policy/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ pub mod model;
99
use std::sync::Arc;
1010

1111
use arc_swap::ArcSwap;
12-
use mas_data_model::Ulid;
12+
use mas_data_model::{SessionLimitConfig, Ulid};
1313
use opa_wasm::{
1414
Runtime,
1515
wasmtime::{Config, Engine, Module, OptLevel, Store},
1616
};
17+
use serde::Serialize;
1718
use thiserror::Error;
1819
use tokio::io::{AsyncRead, AsyncReadExt};
1920

@@ -85,18 +86,25 @@ impl Entrypoints {
8586
}
8687
}
8788

88-
#[derive(Debug)]
89+
#[derive(Serialize, Debug)]
8990
pub struct Data {
9091
server_name: String,
9192

93+
/// Limits on the number of application sessions that each user can have
94+
session_limit: Option<SessionLimitConfig>,
95+
96+
// We will merge this in a custom way, so don't emit as part of the base
97+
#[serde(skip)]
9298
rest: Option<serde_json::Value>,
9399
}
94100

95101
impl Data {
96102
#[must_use]
97-
pub fn new(server_name: String) -> Self {
103+
pub fn new(server_name: String, session_limit: Option<SessionLimitConfig>) -> Self {
98104
Self {
99105
server_name,
106+
session_limit,
107+
100108
rest: None,
101109
}
102110
}
@@ -108,9 +116,7 @@ impl Data {
108116
}
109117

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

115121
if let Some(rest) = &self.rest {
116122
merge_data(base, rest.clone())
@@ -458,7 +464,7 @@ mod tests {
458464

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

529535
#[tokio::test]
530536
async fn test_dynamic_data() {
531-
let data = Data::new("example.com".to_owned());
537+
let data = Data::new("example.com".to_owned(), None);
532538

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

598604
#[tokio::test]
599605
async fn test_big_dynamic_data() {
600-
let data = Data::new("example.com".to_owned());
606+
let data = Data::new("example.com".to_owned(), None);
601607

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

0 commit comments

Comments
 (0)