use serde::Deserialize;
use super::error::AgentError;
use crate::config::types::ServerConfig;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AgentsConfig {
pub max_per_owner: usize,
pub max_per_exchange: usize,
pub max_field_bytes: usize,
pub max_event_bytes: usize,
pub replay_page_rows: usize,
pub max_watchers: usize,
pub watcher_depth: usize,
pub watcher_bytes: usize,
pub max_listened_subjects: usize,
pub max_subject_bytes: usize,
}
impl Default for AgentsConfig {
fn default() -> Self {
Self {
max_per_owner: 8,
max_per_exchange: 64,
max_field_bytes: 512,
max_event_bytes: 16 * 1024,
replay_page_rows: 64,
max_watchers: 256,
watcher_depth: 16,
watcher_bytes: 256 * 1024,
max_listened_subjects: 32,
max_subject_bytes: 4096,
}
}
}
impl AgentsConfig {
pub fn validate(&self) -> Result<(), AgentError> {
for (name, value) in self.bounds() {
if value == 0 {
return Err(AgentError::Bound {
bound: name,
limit: 0,
count: 0,
});
}
}
if self.max_event_bytes < self.max_field_bytes {
return Err(AgentError::Invalid(
"max_event_bytes is below max_field_bytes",
));
}
Ok(())
}
const fn bounds(&self) -> [(&'static str, usize); 10] {
[
("max_per_owner", self.max_per_owner),
("max_per_exchange", self.max_per_exchange),
("max_field_bytes", self.max_field_bytes),
("max_event_bytes", self.max_event_bytes),
("replay_page_rows", self.replay_page_rows),
("max_watchers", self.max_watchers),
("watcher_depth", self.watcher_depth),
("watcher_bytes", self.watcher_bytes),
("max_listened_subjects", self.max_listened_subjects),
("max_subject_bytes", self.max_subject_bytes),
]
}
pub(crate) fn field(&self, value: &str, member: &'static str) -> Result<(), AgentError> {
if value.is_empty() || value.chars().any(char::is_control) {
return Err(AgentError::Invalid(member));
}
if value.len() > self.max_field_bytes {
return Err(AgentError::Bound {
bound: "max_field_bytes",
limit: self.max_field_bytes,
count: value.len(),
});
}
Ok(())
}
}
pub fn collect_errors(config: &ServerConfig, errors: &mut Vec<String>) {
let lys = config.auth.as_ref().and_then(|auth| auth.lys.as_ref());
if let Some(bounds) = lys.and_then(|lys| lys.agents.as_ref()) {
if let Err(error) = bounds.validate() {
errors.push(error.to_string());
}
} else if config
.channels
.iter()
.any(|channel| channel.name.starts_with("agents."))
|| lys
.and_then(|lys| lys.membership.as_ref())
.is_some_and(|membership| membership.channel_kind == "liminal.agent")
{
errors.push("[auth.lys.agents] is required when a liminal.agent is placed".into());
}
}