liminal_server/agents/
config.rs1use serde::Deserialize;
4
5use super::error::AgentError;
6use crate::config::types::ServerConfig;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
10#[serde(default, deny_unknown_fields)]
11pub struct AgentsConfig {
12 pub max_per_owner: usize,
14 pub max_per_exchange: usize,
16 pub max_field_bytes: usize,
19 pub max_event_bytes: usize,
21 pub replay_page_rows: usize,
23 pub max_watchers: usize,
25 pub watcher_depth: usize,
27 pub watcher_bytes: usize,
29 pub max_listened_subjects: usize,
31 pub max_subject_bytes: usize,
33}
34
35impl Default for AgentsConfig {
36 fn default() -> Self {
37 Self {
38 max_per_owner: 8,
39 max_per_exchange: 64,
40 max_field_bytes: 512,
41 max_event_bytes: 16 * 1024,
42 replay_page_rows: 64,
43 max_watchers: 256,
44 watcher_depth: 16,
45 watcher_bytes: 256 * 1024,
46 max_listened_subjects: 32,
47 max_subject_bytes: 4096,
48 }
49 }
50}
51
52impl AgentsConfig {
53 pub fn validate(&self) -> Result<(), AgentError> {
58 for (name, value) in self.bounds() {
59 if value == 0 {
60 return Err(AgentError::Bound {
61 bound: name,
62 limit: 0,
63 count: 0,
64 });
65 }
66 }
67 if self.max_event_bytes < self.max_field_bytes {
68 return Err(AgentError::Invalid(
69 "max_event_bytes is below max_field_bytes",
70 ));
71 }
72 Ok(())
73 }
74
75 const fn bounds(&self) -> [(&'static str, usize); 10] {
76 [
77 ("max_per_owner", self.max_per_owner),
78 ("max_per_exchange", self.max_per_exchange),
79 ("max_field_bytes", self.max_field_bytes),
80 ("max_event_bytes", self.max_event_bytes),
81 ("replay_page_rows", self.replay_page_rows),
82 ("max_watchers", self.max_watchers),
83 ("watcher_depth", self.watcher_depth),
84 ("watcher_bytes", self.watcher_bytes),
85 ("max_listened_subjects", self.max_listened_subjects),
86 ("max_subject_bytes", self.max_subject_bytes),
87 ]
88 }
89
90 pub(crate) fn field(&self, value: &str, member: &'static str) -> Result<(), AgentError> {
91 if value.is_empty() || value.chars().any(char::is_control) {
92 return Err(AgentError::Invalid(member));
93 }
94 if value.len() > self.max_field_bytes {
95 return Err(AgentError::Bound {
96 bound: "max_field_bytes",
97 limit: self.max_field_bytes,
98 count: value.len(),
99 });
100 }
101 Ok(())
102 }
103}
104
105pub fn collect_errors(config: &ServerConfig, errors: &mut Vec<String>) {
108 let lys = config.auth.as_ref().and_then(|auth| auth.lys.as_ref());
109 if let Some(bounds) = lys.and_then(|lys| lys.agents.as_ref()) {
110 if let Err(error) = bounds.validate() {
111 errors.push(error.to_string());
112 }
113 } else if config
114 .channels
115 .iter()
116 .any(|channel| channel.name.starts_with("agents."))
117 || lys
118 .and_then(|lys| lys.membership.as_ref())
119 .is_some_and(|membership| membership.channel_kind == "liminal.agent")
120 {
121 errors.push("[auth.lys.agents] is required when a liminal.agent is placed".into());
122 }
123}