Files
cargo-cxcloud-human-simulat…/src/policy.rs
T
2026-04-26 16:48:22 +00:00

125 lines
4.0 KiB
Rust

use serde::{Deserialize, Serialize};
/// A policy definition loaded from YAML.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
pub id: String,
pub name: String,
pub description: String,
pub enabled: bool,
#[serde(default)]
pub rules: Vec<PolicyRule>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRule {
pub field: String,
pub operator: String, // "contains", "equals", "not_contains", "matches"
pub value: String,
pub action: String, // "reject", "warn", "modify"
#[serde(default = "default_severity")]
pub severity: String,
}
fn default_severity() -> String {
"warning".to_string()
}
impl Policy {
/// Check if a plan violates this policy.
pub fn evaluate(&self, plan: &serde_json::Value) -> Vec<Violation> {
if !self.enabled {
return vec![];
}
let plan_str = serde_json::to_string(plan).unwrap_or_default();
let mut violations = Vec::new();
for rule in &self.rules {
let matched = match rule.operator.as_str() {
"contains" => plan_str.contains(&rule.value),
"not_contains" => !plan_str.contains(&rule.value),
"equals" => {
plan.get(&rule.field)
.and_then(|v| v.as_str())
.map(|v| v == rule.value)
.unwrap_or(false)
}
_ => false,
};
if matched && rule.action == "reject" {
violations.push(Violation {
policy_id: self.id.clone(),
policy_name: self.name.clone(),
rule_field: rule.field.clone(),
severity: rule.severity.clone(),
description: format!(
"Policy '{}' violated: field '{}' {} '{}'",
self.name, rule.field, rule.operator, rule.value
),
});
}
}
violations
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Violation {
pub policy_id: String,
pub policy_name: String,
pub rule_field: String,
pub severity: String,
pub description: String,
}
/// Load default built-in policies.
pub fn default_policies() -> Vec<Policy> {
vec![
Policy {
id: "no-destructive-ops".to_string(),
name: "No Destructive Operations".to_string(),
description: "Block plans that delete or drop resources".to_string(),
enabled: true,
rules: vec![
PolicyRule {
field: "tool_calls".to_string(),
operator: "contains".to_string(),
value: "DROP TABLE".to_string(),
action: "reject".to_string(),
severity: "critical".to_string(),
},
PolicyRule {
field: "tool_calls".to_string(),
operator: "contains".to_string(),
value: "rm -rf".to_string(),
action: "reject".to_string(),
severity: "critical".to_string(),
},
],
},
Policy {
id: "no-external-network".to_string(),
name: "No External Network Calls".to_string(),
description: "Block plans that make calls to unknown external services".to_string(),
enabled: true,
rules: vec![PolicyRule {
field: "tool_calls".to_string(),
operator: "contains".to_string(),
value: "0.0.0.0".to_string(),
action: "reject".to_string(),
severity: "warning".to_string(),
}],
},
Policy {
id: "max-tool-calls".to_string(),
name: "Maximum Tool Calls".to_string(),
description: "Limit the number of tool calls per plan".to_string(),
enabled: true,
rules: vec![],
},
]
}