use super::*;
use shep_core::permissions::{Grants, Permission};
use std::time::Duration;
use crate::rpc::ConnId;
fn adopted_with(grants: Grants) -> Request {
Request::EnableDog {
name: "otel".to_string(),
env: BTreeMap::default(),
source: DogSource::Adopted {
grants: Some(grants),
path: "/usr/local/bin/shep-otel".to_string(),
channel: false,
},
}
}
#[tokio::test(start_paused = true)]
async fn a_dispatch_cut_short_after_queueing_the_start_still_records_the_grants() {
let h = harness(vec![ProcScript::never_exits()]);
let grants = Grants::new([Permission::FlockRead]);
let cut = tokio::time::timeout(
Duration::ZERO,
dispatch(envelope(1, adopted_with(grants.clone())), &h.ctx),
)
.await;
assert!(cut.is_err(), "the dispatch was meant to be cut short");
assert_eq!(h.ctx.known_dogs.grants("otel"), Some(grants));
}
#[tokio::test(start_paused = true)]
async fn an_enable_refused_for_a_sheeps_name_remembers_no_dog() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
let request = Request::EnableDog {
name: "web".to_string(),
env: BTreeMap::default(),
source: DogSource::Adopted {
grants: Some(Grants::default()),
path: "/usr/local/bin/shep-web".to_string(),
channel: false,
},
};
let refused = reply_of(dispatch(envelope(2, request), &h.ctx).await).result;
let Err(err) = refused else {
panic!("a sheep's name must be refused, got {refused:?}")
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
assert!(!h.ctx.known_dogs.contains("web"));
}
#[tokio::test(start_paused = true)]
async fn a_refused_enable_leaves_the_grants_the_name_already_held() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
let before = Grants::new([Permission::LogsRead]);
h.ctx.known_dogs.insert("web", Some(before.clone()));
let request = Request::EnableDog {
name: "web".to_string(),
env: BTreeMap::default(),
source: DogSource::Adopted {
grants: Some(Grants::default()),
path: "/usr/local/bin/shep-web".to_string(),
channel: false,
},
};
let _ = dispatch(envelope(2, request), &h.ctx).await;
assert_eq!(h.ctx.known_dogs.grants("web"), Some(before));
}
async fn as_dog(h: &crate::testing::Harness, dog: &str, request: Request) -> Reply {
reply_of(super::super::dispatch(envelope(1, request), ConnId::next(), Some(dog), &h.ctx).await)
}
fn enabling(name: &str, grants: Option<Grants>) -> Request {
Request::EnableDog {
name: name.to_string(),
env: BTreeMap::default(),
source: DogSource::Adopted {
grants,
path: format!("/usr/local/bin/shep-{name}"),
channel: false,
},
}
}
#[tokio::test(start_paused = true)]
async fn a_dog_cannot_widen_its_own_grants_by_enabling_itself() {
let h = harness(vec![ProcScript::never_exits()]);
let held = Grants::new([Permission::DogsControl]);
h.ctx.known_dogs.insert("otel", Some(held.clone()));
let reply = as_dog(&h, "otel", enabling("otel", None)).await;
assert!(reply.result.is_ok(), "{reply:?}");
assert_eq!(h.ctx.known_dogs.recorded("otel"), Some(Some(held)));
}
#[tokio::test(start_paused = true)]
async fn a_dog_enabling_another_dog_cannot_choose_that_dogs_grants() {
let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
h.ctx
.known_dogs
.insert("otel", Some(Grants::new([Permission::DogsControl])));
let kept = Grants::new([Permission::LogsRead]);
h.ctx.known_dogs.insert("bark", Some(kept.clone()));
as_dog(&h, "otel", enabling("bark", None)).await;
as_dog(&h, "otel", enabling("fresh", None)).await;
assert_eq!(h.ctx.known_dogs.recorded("bark"), Some(Some(kept)));
assert_eq!(
h.ctx.known_dogs.recorded("fresh"),
Some(Some(Grants::default()))
);
}
#[tokio::test(start_paused = true)]
async fn the_control_socket_still_sets_the_grants_it_carries() {
let h = harness(vec![ProcScript::never_exits()]);
let grants = Grants::new([Permission::FlockRead]);
let reply =
reply_of(dispatch(envelope(1, enabling("otel", Some(grants.clone()))), &h.ctx).await);
assert!(reply.result.is_ok(), "{reply:?}");
assert_eq!(h.ctx.known_dogs.recorded("otel"), Some(Some(grants)));
}
#[tokio::test(start_paused = true)]
async fn a_dog_whose_spawn_failed_keeps_the_grants_it_was_enabled_with() {
let h = harness(vec![]);
let grants = Grants::new([Permission::FlockRead]);
let reply =
reply_of(dispatch(envelope(1, enabling("otel", Some(grants.clone()))), &h.ctx).await);
assert_eq!(
reply.result.as_ref().err().map(|e| e.code),
Some(RpcErrorCode::SpawnFailed),
"{reply:?}"
);
assert_eq!(h.ctx.known_dogs.recorded("otel"), Some(Some(grants)));
}