swarm-nats-auth: take the subscribe subjects from the shared definitions
The reader's subscribe allow-list spelled `$SWARM.term.*`, `$SWARM.term.*.sub.*` and `$SWARM.agent-state.*` as literals, copies of private prefixes in hive-agent and swarm-controller. A rename on either side left the policy behind and the stream stopped with only a connection-level server error. swarm-queue-client now owns `term_subject`, `agent_state_subject` and their `*_wildcard` forms, and `subagent_term::subject_wildcard`; hive-agent's publishers, swarm-controller's relays and the policy all build from them. Subjects are byte-identical.
This commit is contained in:
parent
d0c2b85d45
commit
1b061fc9d5
7 changed files with 92 additions and 85 deletions
|
|
@ -31,13 +31,6 @@ use futures_util::{Stream, StreamExt as _};
|
|||
|
||||
use crate::AppState;
|
||||
|
||||
/// Subject family carrying agent terminal rows — must match
|
||||
/// `hive-agent::swarm_term::SUBJECT_PREFIX` exactly, since the two ends
|
||||
/// never see the constant together. [`agent_subject`] spells the full
|
||||
/// subject; there is no way to check the two sides agree short of
|
||||
/// this comment and the module docs on both ends staying honest about it.
|
||||
const SUBJECT_PREFIX: &str = "$SWARM.term";
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/agents/{name}/term/stream",
|
||||
|
|
@ -112,17 +105,12 @@ pub(crate) async fn stream_agent_term(
|
|||
)
|
||||
})?;
|
||||
|
||||
let subscriber = subscribe(&client, agent_subject(SUBJECT_PREFIX, &agent), "term").await?;
|
||||
let subscriber = subscribe(&client, swarm_queue_client::term_subject(&agent), "term").await?;
|
||||
let stream =
|
||||
subscriber.map(|msg| Ok(Event::default().data(String::from_utf8_lossy(&msg.payload))));
|
||||
Ok(Sse::new(stream).keep_alive(KeepAlive::default()))
|
||||
}
|
||||
|
||||
/// The subject one agent publishes on under `prefix`: `<prefix>.<agent>`.
|
||||
pub(crate) fn agent_subject(prefix: &str, agent: &str) -> String {
|
||||
format!("{prefix}.{agent}")
|
||||
}
|
||||
|
||||
/// Subscribe to `subject`. `what` names the relay in logs and errors.
|
||||
pub(crate) async fn subscribe(
|
||||
client: &async_nats::Client,
|
||||
|
|
@ -139,14 +127,3 @@ pub(crate) async fn subscribe(
|
|||
tracing::info!(%subject, "{what} stream: client attached");
|
||||
Ok(subscriber)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The subject the agent's own queue credential is granted.
|
||||
#[test]
|
||||
fn a_relay_subscribes_to_the_agents_own_subject() {
|
||||
assert_eq!(agent_subject(SUBJECT_PREFIX, "atlas"), "$SWARM.term.atlas");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue