| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use crate::step::{Action, ObserveKind}; |
| 14 |
use serde::{Deserialize, Serialize}; |
| 15 |
use std::collections::BTreeSet; |
| 16 |
|
| 17 |
|
| 18 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 19 |
pub struct CapabilitySet { |
| 20 |
|
| 21 |
|
| 22 |
#[serde(default)] |
| 23 |
actuate: BTreeSet<String>, |
| 24 |
|
| 25 |
#[serde(default)] |
| 26 |
observe: BTreeSet<ObserveKind>, |
| 27 |
} |
| 28 |
|
| 29 |
impl CapabilitySet { |
| 30 |
|
| 31 |
|
| 32 |
pub fn from_tokens<A, O>(actuate: A, observe: O) -> Self |
| 33 |
where |
| 34 |
A: IntoIterator, |
| 35 |
A::Item: AsRef<str>, |
| 36 |
O: IntoIterator, |
| 37 |
O::Item: AsRef<str>, |
| 38 |
{ |
| 39 |
let actuate: BTreeSet<String> = actuate |
| 40 |
.into_iter() |
| 41 |
.map(|t| t.as_ref().to_string()) |
| 42 |
.collect(); |
| 43 |
let mut observe: BTreeSet<ObserveKind> = observe |
| 44 |
.into_iter() |
| 45 |
.map(|t| ObserveKind::from_token(t.as_ref())) |
| 46 |
.collect(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if actuate.contains("sign") { |
| 56 |
observe.insert(ObserveKind::Custom("gatekeeper".into())); |
| 57 |
} |
| 58 |
Self { actuate, observe } |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub fn actuate_only<I>(actions: I) -> Self |
| 63 |
where |
| 64 |
I: IntoIterator<Item = Action>, |
| 65 |
{ |
| 66 |
Self { |
| 67 |
actuate: actions.into_iter().filter_map(|a| a.token()).collect(), |
| 68 |
observe: BTreeSet::new(), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
pub fn permits(&self, action: &Action) -> bool { |
| 74 |
match action { |
| 75 |
Action::Observe(kind) => self.observe.contains(kind), |
| 76 |
other => other.token().is_some_and(|t| self.actuate.contains(&t)), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
pub fn permits_observe(&self, kind: &ObserveKind) -> bool { |
| 81 |
self.observe.contains(kind) |
| 82 |
} |
| 83 |
|
| 84 |
pub fn has_any_observe(&self) -> bool { |
| 85 |
!self.observe.is_empty() |
| 86 |
} |
| 87 |
|
| 88 |
pub fn actuate_tokens(&self) -> impl Iterator<Item = &str> { |
| 89 |
self.actuate.iter().map(String::as_str) |
| 90 |
} |
| 91 |
|
| 92 |
pub fn observe_kinds(&self) -> impl Iterator<Item = &ObserveKind> { |
| 93 |
self.observe.iter() |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#[must_use] |
| 100 |
pub fn intersect(&self, other: &CapabilitySet) -> CapabilitySet { |
| 101 |
CapabilitySet { |
| 102 |
actuate: self.actuate.intersection(&other.actuate).cloned().collect(), |
| 103 |
observe: self.observe.intersection(&other.observe).cloned().collect(), |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[derive(Debug, Clone, thiserror::Error)] |
| 111 |
#[error("capability denied: host `{host}` is not granted action `{action}`")] |
| 112 |
pub struct CapabilityDenied { |
| 113 |
pub host: String, |
| 114 |
pub action: String, |
| 115 |
} |
| 116 |
|
| 117 |
impl CapabilityDenied { |
| 118 |
pub fn new(host: impl Into<String>, action: &Action) -> Self { |
| 119 |
Self { |
| 120 |
host: host.into(), |
| 121 |
action: match action { |
| 122 |
Action::Observe(k) => format!("observe:{}", k.token()), |
| 123 |
other => other.token().unwrap_or_else(|| "unknown".into()), |
| 124 |
}, |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(test)] |
| 130 |
mod tests { |
| 131 |
use super::*; |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn permits_granted_actuate_only() { |
| 135 |
let caps = CapabilitySet::from_tokens(["deploy", "restart"], ["health"]); |
| 136 |
assert!(caps.permits(&Action::Deploy)); |
| 137 |
assert!(caps.permits(&Action::Restart)); |
| 138 |
assert!(!caps.permits(&Action::Rollback)); |
| 139 |
assert!(!caps.permits(&Action::Sign)); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn permits_observe_by_kind() { |
| 144 |
let caps = CapabilitySet::from_tokens(["deploy"], ["journal", "health"]); |
| 145 |
assert!(caps.permits(&Action::Observe(ObserveKind::Health))); |
| 146 |
assert!(caps.permits(&Action::Observe(ObserveKind::Journal))); |
| 147 |
assert!(!caps.permits(&Action::Observe(ObserveKind::Metrics))); |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn sign_grant_implies_gatekeeper_observe() { |
| 152 |
|
| 153 |
|
| 154 |
let caps = |
| 155 |
CapabilitySet::from_tokens(["build", "sign", "notarize", "staple"], ["build-log"]); |
| 156 |
assert!(caps.permits(&Action::Observe(ObserveKind::Custom("gatekeeper".into())))); |
| 157 |
|
| 158 |
let plain = CapabilitySet::from_tokens(["build"], ["build-log"]); |
| 159 |
assert!(!plain.permits(&Action::Observe(ObserveKind::Custom("gatekeeper".into())))); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn custom_action_needs_exact_grant() { |
| 164 |
let caps = CapabilitySet::from_tokens(["smoke-test"], Vec::<&str>::new()); |
| 165 |
assert!(caps.permits(&Action::Custom("smoke-test".into()))); |
| 166 |
assert!(!caps.permits(&Action::Custom("rm-rf".into()))); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn intersect_is_the_floor_of_both() { |
| 171 |
|
| 172 |
|
| 173 |
let caller = |
| 174 |
CapabilitySet::from_tokens(["deploy", "restart", "sign"], ["health", "journal"]); |
| 175 |
let agent = CapabilitySet::from_tokens(Vec::<&str>::new(), ["health"]); |
| 176 |
let eff = caller.intersect(&agent); |
| 177 |
assert!(!eff.permits(&Action::Deploy)); |
| 178 |
assert!(eff.permits(&Action::Observe(ObserveKind::Health))); |
| 179 |
assert!(!eff.permits(&Action::Observe(ObserveKind::Journal))); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn denied_error_renders_action() { |
| 184 |
let d = CapabilityDenied::new("prod", &Action::Sign); |
| 185 |
assert!(d.to_string().contains("prod")); |
| 186 |
assert!(d.to_string().contains("sign")); |
| 187 |
let d2 = CapabilityDenied::new("prod", &Action::Observe(ObserveKind::Metrics)); |
| 188 |
assert!(d2.to_string().contains("observe:metrics")); |
| 189 |
} |
| 190 |
} |
| 191 |
|