agentmux_srv_core/backend/
audience.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum HostCap {
16 Exec,
18 Write,
20 Read,
22}
23
24impl HostCap {
25 pub fn name(self) -> &'static str {
26 match self {
27 HostCap::Exec => "host_exec",
28 HostCap::Write => "host_write",
29 HostCap::Read => "host_read",
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct RuntimeCaps {
37 pub host_exec: bool,
38 pub host_write: bool,
39 pub host_read: bool,
40}
41
42impl RuntimeCaps {
43 pub const NATIVE: RuntimeCaps = RuntimeCaps { host_exec: true, host_write: true, host_read: true };
44 pub const CONTAINER: RuntimeCaps = RuntimeCaps { host_exec: false, host_write: false, host_read: false };
45
46 pub fn of(container: bool) -> RuntimeCaps {
47 if container { Self::CONTAINER } else { Self::NATIVE }
48 }
49
50 pub fn has(self, cap: HostCap) -> bool {
51 match cap {
52 HostCap::Exec => self.host_exec,
53 HostCap::Write => self.host_write,
54 HostCap::Read => self.host_read,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub enum Audience {
63 Everyone,
65 NativeOnly(HostCap, &'static str),
67 AppOnly(&'static str),
70 ContainerOnly,
72}
73
74impl Audience {
75 pub fn permits(&self, caps: RuntimeCaps) -> bool {
77 match *self {
78 Audience::Everyone => true,
79 Audience::NativeOnly(cap, _) => caps.has(cap),
80 Audience::AppOnly(_) => caps == RuntimeCaps::NATIVE,
81 Audience::ContainerOnly => caps == RuntimeCaps::CONTAINER,
82 }
83 }
84
85 pub fn refusal(&self, caps: RuntimeCaps) -> Option<&'static str> {
87 if self.permits(caps) {
88 return None;
89 }
90 match *self {
91 Audience::NativeOnly(_, why) | Audience::AppOnly(why) => Some(why),
92 Audience::ContainerOnly => Some("this is for container agents."),
93 Audience::Everyone => None,
94 }
95 }
96
97 pub fn cap_name(&self) -> &'static str {
100 match *self {
101 Audience::NativeOnly(cap, _) => cap.name(),
102 _ => "-",
103 }
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn everyone_reaches_both_runtimes() {
113 assert!(Audience::Everyone.permits(RuntimeCaps::NATIVE));
114 assert!(Audience::Everyone.permits(RuntimeCaps::CONTAINER));
115 assert_eq!(Audience::Everyone.refusal(RuntimeCaps::CONTAINER), None);
116 }
117
118 #[test]
119 fn a_native_only_thing_needs_its_capability_and_a_container_has_none() {
120 for cap in [HostCap::Exec, HostCap::Write, HostCap::Read] {
121 let a = Audience::NativeOnly(cap, "why");
122 assert!(a.permits(RuntimeCaps::NATIVE));
123 assert!(!a.permits(RuntimeCaps::CONTAINER));
124 assert_eq!(a.refusal(RuntimeCaps::CONTAINER), Some("why"));
125 }
126 let read_only = RuntimeCaps { host_exec: false, host_write: false, host_read: true };
128 assert!(Audience::NativeOnly(HostCap::Read, "r").permits(read_only));
129 assert!(!Audience::NativeOnly(HostCap::Exec, "e").permits(read_only));
130 }
131
132 #[test]
133 fn app_only_and_container_only_split_the_two_runtimes() {
134 assert!(!Audience::AppOnly("app").permits(RuntimeCaps::CONTAINER));
135 assert!(Audience::AppOnly("app").permits(RuntimeCaps::NATIVE));
136 assert!(Audience::ContainerOnly.permits(RuntimeCaps::CONTAINER));
137 assert!(!Audience::ContainerOnly.permits(RuntimeCaps::NATIVE));
138 }
139}