diff --git a/swarm-nats-auth/src/policy.rs b/swarm-nats-auth/src/policy.rs index 092fa2cd..935e2da3 100644 --- a/swarm-nats-auth/src/policy.rs +++ b/swarm-nats-auth/src/policy.rs @@ -148,6 +148,14 @@ impl Policy { format!("KV_{}", swarm_queue_client::wanted::BUCKET) } + /// The stream backing the per-agent status bucket. + /// + /// Same shape and same reason as [`Self::wanted_stream`]: derived from + /// the crate constant rather than a third configurable bucket name. + fn agent_status_stream() -> String { + format!("KV_{}", swarm_queue_client::agent_status::BUCKET) + } + /// What *any* `JetStream` client must be able to ask before it can do /// anything at all, bucket-specific or not. /// @@ -324,6 +332,26 @@ impl Policy { format!("$JS.API.STREAM.CREATE.{}", Self::wanted_stream()), format!("$KV.{}.>", swarm_queue_client::wanted::BUCKET), ]); + // The per-agent status bucket, read-side only. Same five subjects as + // the hive-status block above and for the same measured reasons — + // including BOTH `CONSUMER.CREATE` forms, since `keys()` builds an + // ephemeral consumer whose subject carries no name. + // + // Deliberately **no `$KV..…` subject**: that is the write + // side, and writing is what picks a key layout. Who publishes agent + // status, and under which key, is still open — so granting a write + // subject here would answer a question this change has no business + // answering. Reading needs none of it: a KV read is a `DIRECT.GET`, + // and the `.>` form is bucket-wide rather than per-key, so nothing + // below encodes a layout. + let agent_status = Self::agent_status_stream(); + subjects.extend([ + format!("$JS.API.STREAM.INFO.{agent_status}"), + format!("$JS.API.STREAM.CREATE.{agent_status}"), + format!("$JS.API.DIRECT.GET.{agent_status}.>"), + format!("$JS.API.CONSUMER.CREATE.{agent_status}"), + format!("$JS.API.CONSUMER.CREATE.{agent_status}.>"), + ]); subjects } } @@ -342,6 +370,49 @@ mod tests { .expect("the default policy is valid") } + #[test] + fn the_reader_can_open_and_list_the_agent_status_bucket() { + let subjects = policy().reader_subjects(); + let s = Policy::agent_status_stream(); + for want in [ + format!("$JS.API.STREAM.INFO.{s}"), + format!("$JS.API.STREAM.CREATE.{s}"), + format!("$JS.API.DIRECT.GET.{s}.>"), + // The bare form is the one `keys()` needs: its ephemeral consumer + // has no name, and `>` never matches zero tokens. + format!("$JS.API.CONSUMER.CREATE.{s}"), + format!("$JS.API.CONSUMER.CREATE.{s}.>"), + ] { + assert!(subjects.contains(&want), "reader is missing {want}"); + } + // Control: the same reader really can be missing a subject, so the + // assertions above are not vacuously true of any string. + assert!(!subjects.contains(&format!("$JS.API.STREAM.DELETE.{s}"))); + } + + #[test] + fn no_grant_encodes_an_agent_status_key_layout() { + // Who writes agent status, and under which key, is undecided. A `$KV.` + // subject is the write side and would answer that by implication, so + // NOBODY gets one for this bucket until the layout is settled. + let bucket = swarm_queue_client::agent_status::BUCKET; + let prefix = format!("$KV.{bucket}."); + let p = policy(); + for (who, subjects) in [ + ("reader", p.reader_subjects()), + ("hive", p.hive_subjects("alpha")), + ] { + assert!( + !subjects.iter().any(|s| s.starts_with(&prefix)), + "{who} was granted a {prefix}* subject, which picks a key layout" + ); + } + // Control: the prefix test does fire on a bucket that legitimately has + // write grants, so a pass above is about agent-status, not the matcher. + let wanted = format!("$KV.{}.", swarm_queue_client::wanted::BUCKET); + assert!(p.reader_subjects().iter().any(|s| s.starts_with(&wanted))); + } + #[test] fn an_extra_subject_without_the_placeholder_is_refused() { // 🩸 The option exists to put a second stream inside ONE hive's