swarm-controller: ssh-connection handlers take their own sub-state via FromRef
This commit is contained in:
parent
c43c35e05b
commit
a8c1c0f5c2
2 changed files with 65 additions and 18 deletions
|
|
@ -34,6 +34,7 @@ use swarm_queue_client::agent_schedules;
|
|||
use swarm_wire::agent_schedules::{AgentSchedules, Schedule};
|
||||
|
||||
use crate::agent_renewal::unix_now;
|
||||
use crate::routes::hives::HiveEntry;
|
||||
use crate::routes::{AppState, error_problem};
|
||||
use crate::wanted::WantedWriter;
|
||||
|
||||
|
|
@ -657,13 +658,22 @@ pub(crate) fn refused((status, detail): (StatusCode, String)) -> ProblemDetails
|
|||
|
||||
/// Every agent some hive declares and has not destroyed.
|
||||
pub(crate) async fn live(state: &AppState) -> Result<BTreeSet<String>, ProblemDetails> {
|
||||
let Some(wanted) = state.wanted.as_ref() else {
|
||||
live_of(state.wanted.as_ref(), &state.hives).await
|
||||
}
|
||||
|
||||
/// [`live`] over the wanted-state writer and the hive list alone, for a caller
|
||||
/// that holds only those.
|
||||
pub(crate) async fn live_of(
|
||||
wanted: Option<&Arc<WantedWriter>>,
|
||||
hives: &[HiveEntry],
|
||||
) -> Result<BTreeSet<String>, ProblemDetails> {
|
||||
let Some(wanted) = wanted else {
|
||||
return Err(error_problem(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"no swarm queue is configured on this host",
|
||||
));
|
||||
};
|
||||
let hives: Vec<String> = state.hives.iter().map(|h| h.name.clone()).collect();
|
||||
let hives: Vec<String> = hives.iter().map(|h| h.name.clone()).collect();
|
||||
Ok(crate::subagent_term::live_agents(wanted, &hives).await)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,11 @@
|
|||
//! rest of the record. The agent renders the connections into its ssh config
|
||||
//! while its `ssh` switch is on; nothing here depends on the switch.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::rejection::JsonRejection;
|
||||
use axum::extract::{Path, State};
|
||||
use axum::extract::{FromRef, Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use problem_details::ProblemDetails;
|
||||
use swarm_controller_api::ssh_connections::{PutSshConnection, SshConnection, SshConnections};
|
||||
|
|
@ -18,8 +20,33 @@ use swarm_wire::agent_permissions::AgentPermissions;
|
|||
use swarm_wire::ssh_connection::{SshConnection as Stored, check_list};
|
||||
|
||||
use crate::agent_permissions::{agent_name, logged, open_or_create, queue_problem};
|
||||
use crate::routes::hives::HiveEntry;
|
||||
use crate::routes::{AppState, error_problem};
|
||||
use crate::schedules::{live, queue, read_or_default, refused, write};
|
||||
use crate::schedules::{live_of, queue_of, read_or_default, refused, write};
|
||||
use crate::status::StatusReader;
|
||||
use crate::wanted::WantedWriter;
|
||||
|
||||
/// What the ssh-connection routes may reach of [`AppState`]: the queue
|
||||
/// connection, the wanted-state writer and the hive list.
|
||||
///
|
||||
/// 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 SshConnectionsState {
|
||||
pub(crate) status: Option<Arc<StatusReader>>,
|
||||
pub(crate) wanted: Option<Arc<WantedWriter>>,
|
||||
pub(crate) hives: Arc<Vec<HiveEntry>>,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for SshConnectionsState {
|
||||
fn from_ref(state: &AppState) -> Self {
|
||||
Self {
|
||||
status: state.status.clone(),
|
||||
wanted: state.wanted.clone(),
|
||||
hives: Arc::clone(&state.hives),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How many times a losing writer re-reads and re-applies, as in
|
||||
/// `agent_permissions`.
|
||||
|
|
@ -55,11 +82,11 @@ fn view(stored: Vec<Stored>) -> SshConnections {
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn get_ssh_connections(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<SshConnectionsState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<SshConnections>, ProblemDetails> {
|
||||
let agent = agent_name(&name).map_err(refused)?;
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let client = queue_of(state.status.as_ref()).map_err(refused)?;
|
||||
let store = open_or_create(&client).await?;
|
||||
let (current, _, _) = read_or_default::<AgentPermissions>(&store, &agent)
|
||||
.await
|
||||
|
|
@ -93,7 +120,7 @@ pub(crate) async fn get_ssh_connections(
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn put_ssh_connection(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<SshConnectionsState>,
|
||||
Path((name, conn)): Path<(String, String)>,
|
||||
req: Result<Json<PutSshConnection>, JsonRejection>,
|
||||
) -> Result<Json<SshConnections>, ProblemDetails> {
|
||||
|
|
@ -109,8 +136,12 @@ pub(crate) async fn put_ssh_connection(
|
|||
}
|
||||
.checked()
|
||||
.map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?;
|
||||
let client = queue(&state).map_err(|r| refuse(refused(r)))?;
|
||||
if !live(&state).await.map_err(refuse)?.contains(&agent) {
|
||||
let client = queue_of(state.status.as_ref()).map_err(|r| refuse(refused(r)))?;
|
||||
if !live_of(state.wanted.as_ref(), &state.hives)
|
||||
.await
|
||||
.map_err(refuse)?
|
||||
.contains(&agent)
|
||||
{
|
||||
return Err(refuse(error_problem(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("no hive's wanted state declares an agent {agent:?} that is not destroyed"),
|
||||
|
|
@ -162,12 +193,12 @@ pub(crate) async fn put_ssh_connection(
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn delete_ssh_connection(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<SshConnectionsState>,
|
||||
Path((name, conn)): Path<(String, String)>,
|
||||
) -> Result<StatusCode, ProblemDetails> {
|
||||
let refuse = |p| logged("delete ssh connection", &name, p);
|
||||
let agent = agent_name(&name).map_err(|r| refuse(refused(r)))?;
|
||||
let client = queue(&state).map_err(|r| refuse(refused(r)))?;
|
||||
let client = queue_of(state.status.as_ref()).map_err(|r| refuse(refused(r)))?;
|
||||
let store = open_or_create(&client).await?;
|
||||
for _ in 0..MAX_ATTEMPTS {
|
||||
let (mut value, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
|
||||
|
|
@ -201,11 +232,14 @@ pub(crate) async fn delete_ssh_connection(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{delete_ssh_connection, get_ssh_connections, put_ssh_connection};
|
||||
use super::{
|
||||
SshConnectionsState, delete_ssh_connection, get_ssh_connections, put_ssh_connection,
|
||||
};
|
||||
use crate::agent_permissions::{delete_agent_permissions, put_agent_permissions};
|
||||
use crate::routes::AppState;
|
||||
use crate::test_nats::TestNats;
|
||||
use axum::Json;
|
||||
use axum::extract::FromRef;
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use problem_details::ProblemDetails;
|
||||
|
|
@ -227,7 +261,7 @@ mod tests {
|
|||
req: PutSshConnection,
|
||||
) -> Result<SshConnections, ProblemDetails> {
|
||||
put_ssh_connection(
|
||||
State(state.clone()),
|
||||
State(SshConnectionsState::from_ref(state)),
|
||||
Path((agent.to_owned(), name.to_owned())),
|
||||
Ok(Json(req)),
|
||||
)
|
||||
|
|
@ -236,15 +270,18 @@ mod tests {
|
|||
}
|
||||
|
||||
async fn get(state: &AppState, agent: &str) -> SshConnections {
|
||||
get_ssh_connections(State(state.clone()), Path(agent.to_owned()))
|
||||
.await
|
||||
.expect("read")
|
||||
.0
|
||||
get_ssh_connections(
|
||||
State(SshConnectionsState::from_ref(state)),
|
||||
Path(agent.to_owned()),
|
||||
)
|
||||
.await
|
||||
.expect("read")
|
||||
.0
|
||||
}
|
||||
|
||||
async fn remove(state: &AppState, agent: &str, name: &str) -> StatusCode {
|
||||
delete_ssh_connection(
|
||||
State(state.clone()),
|
||||
State(SshConnectionsState::from_ref(state)),
|
||||
Path((agent.to_owned(), name.to_owned())),
|
||||
)
|
||||
.await
|
||||
|
|
|
|||
Loading…
Reference in a new issue