Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: forge-admin handlers take their own sub-state via FromRef

This commit is contained in:
atlas 2026-11-29 06:16:26 +01:00 • committed by mara
commit 871438afcc

View file

@ -1,6 +1,9 @@
//! Forge site admins and an agent's forge token, on request.
use std::sync::{Arc, Mutex};
use anyhow::Result;
use axum::extract::FromRef;
use axum::{
Json,
extract::{Path, State},
@ -8,11 +11,36 @@ use axum::{
use swarm_controller_api::agents::MintAgentForgeTokenResponse;
use swarm_controller_api::forge::{ForgeAdminChange, MakeForgeAdminResponse};
use crate::auth::AuthBridge;
use crate::deploy::jobq::Late;
use crate::forge;
use crate::jobq::queue_forge_token_mints;
use crate::jobq::{SwarmNodeKind, SwarmResourceKind, queue_forge_token_mints};
use crate::routes::agents::in_roster;
use crate::routes::{AppState, error_problem};
/// What the forge-admin routes may reach of [`AppState`]: the scheduler the
/// token mint queues on, the forge client, and the identity bridge that
/// names the agents.
///
/// Derived per request from [`AppState`] (`FromRef`), so each field is a
/// clone of the same `Arc` and nothing is held twice.
#[derive(Clone)]
pub(crate) struct ForgeAdminState {
pub(crate) jobq: Arc<Mutex<hive_jobq::scheduler::Scheduler<SwarmNodeKind, SwarmResourceKind>>>,
pub(crate) forge: Late<Arc<forge::Client>>,
pub(crate) auth: Option<Arc<AuthBridge>>,
}
impl FromRef<AppState> for ForgeAdminState {
fn from_ref(state: &AppState) -> Self {
Self {
jobq: Arc::clone(&state.jobq),
forge: Arc::clone(&state.forge),
auth: state.auth.clone(),
}
}
}
/// Check an agent's forge token now, and mint one if it is missing or stale.
///
/// The periodic pass (`forge::agent_token::spawn`) does the same every five
@ -31,7 +59,7 @@ use crate::routes::{AppState, error_problem};
tag = "agents"
)]
pub(crate) async fn mint_agent_forge_token(
State(state): State<AppState>,
State(state): State<ForgeAdminState>,
Path(name): Path<String>,
) -> Result<Json<MintAgentForgeTokenResponse>, problem_details::ProblemDetails> {
let agent = hive_types::Ident::parse(&name)
@ -71,7 +99,7 @@ pub(crate) async fn mint_agent_forge_token(
tag = "forge"
)]
pub(crate) async fn make_forge_admin(
State(state): State<AppState>,
State(state): State<ForgeAdminState>,
Path(name): Path<String>,
) -> Result<Json<MakeForgeAdminResponse>, problem_details::ProblemDetails> {
let name = hive_types::Ident::parse(&name)
@ -145,6 +173,57 @@ where
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt as _;
async fn post(state: AppState, uri: &str) -> (StatusCode, String) {
let req = Request::post(uri).body(Body::empty()).expect("a request");
let res = crate::startup::build_app(state)
.oneshot(req)
.await
.expect("infallible");
let status = res.status();
let bytes = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.expect("a body");
(status, String::from_utf8_lossy(&bytes).into_owned())
}
/// Through the router, so `FromRef` runs: the mint lands on the
/// scheduler `AppState` holds (the user node and the mint node).
#[tokio::test]
async fn the_token_route_queues_on_the_app_states_scheduler() {
let (state, sched) = crate::routes::tests::state_with_roster();
let (status, body) = post(state, "/api/agents/atlas/forge-token").await;
assert_eq!(status, StatusCode::OK, "{body}");
let guard = sched
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
assert_eq!(guard.graph().nodes().count(), 2);
}
/// Without a forge client the admin route is 503 "no forge"; with one
/// it gets past that check and stops at the unreadable roster (the
/// test state has no bridge), a different 503. Presence control for the
/// forge field.
#[tokio::test]
async fn the_admin_route_reads_the_forge_the_app_state_holds() {
let (state, _) = crate::routes::tests::state_with_roster();
let (status, body) = post(state, "/api/forge/users/someone/admin").await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
assert!(body.contains("no forge is configured"), "{body}");
let (state, _) = crate::routes::tests::state_with_roster();
let url = url::Url::parse("http://127.0.0.1:1/").expect("a url");
state
.forge
.set(Arc::new(forge::Client::for_test_forge(url)))
.unwrap_or_else(|_| panic!("the forge is unset"));
let (status, body) = post(state, "/api/forge/users/someone/admin").await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
assert!(body.contains("cannot tell whether"), "{body}");
}
/// `admin_unless_agent` with a `promote` that records whether it ran and
/// answers `plan`.