hessra_cap_policy/
config.rs1use serde::Deserialize;
4use std::collections::HashMap;
5use thiserror::Error;
6
7#[derive(Error, Debug)]
9pub enum PolicyConfigError {
10 #[error("failed to read policy file: {0}")]
11 Io(#[from] std::io::Error),
12 #[error("failed to parse policy TOML: {0}")]
13 Parse(#[from] toml::de::Error),
14}
15
16#[derive(Debug, Clone, Deserialize)]
18pub struct PolicyConfig {
19 #[serde(default)]
21 pub objects: Vec<ObjectConfig>,
22
23 #[serde(default)]
25 pub classifications: HashMap<String, Vec<String>>,
26
27 #[serde(default)]
29 pub exposure_rules: Vec<ExposureRuleConfig>,
30}
31
32#[derive(Debug, Clone, Deserialize)]
34pub struct ObjectConfig {
35 pub id: String,
37
38 #[serde(default)]
40 pub can_delegate: bool,
41
42 #[serde(default)]
44 pub identity: Option<IdentityConfigEntry>,
45
46 #[serde(default)]
48 pub capabilities: Vec<CapabilityConfig>,
49}
50
51#[derive(Debug, Clone, Deserialize)]
53pub struct IdentityConfigEntry {
54 #[serde(default = "default_ttl")]
56 pub ttl: i64,
57 #[serde(default)]
59 pub delegatable: bool,
60}
61
62fn default_ttl() -> i64 {
63 3600
64}
65
66#[derive(Debug, Clone, Deserialize)]
68pub struct CapabilityConfig {
69 pub target: String,
71 pub operations: Vec<String>,
73}
74
75#[derive(Debug, Clone, Deserialize)]
77pub struct ExposureRuleConfig {
78 pub labels: Vec<String>,
80
81 #[serde(default = "default_match_mode")]
85 pub r#match: String,
86
87 pub blocks: Vec<String>,
90}
91
92fn default_match_mode() -> String {
93 "any".to_string()
94}
95
96impl PolicyConfig {
97 pub fn from_file(path: &std::path::Path) -> Result<Self, PolicyConfigError> {
99 let content = std::fs::read_to_string(path)?;
100 Self::parse(&content)
101 }
102
103 pub fn parse(content: &str) -> Result<Self, PolicyConfigError> {
105 let config: PolicyConfig = toml::from_str(content)?;
106 Ok(config)
107 }
108}