Watch
0
0
Fork
You've already forked hyperhive
0

agent ssh connections: declare per agent, render into the agent's ssh config

This commit is contained in:
atlas 2026-11-20 01:38:49 +01:00
commit 934da6f31f

View file

@ -0,0 +1,471 @@
//! `/api/agents/{name}/ssh-connections` — the named SSH connections declared
//! for one agent.
//!
//! They live in the `ssh_connections` field of the agent's `agent-permissions`
//! record, which this daemon alone writes (`crate::agent_permissions`). Every
//! write here reads the record, changes only that field and writes it back
//! against the revision it read, so it cannot undo a concurrent change to the
//! 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 axum::Json;
use axum::extract::rejection::JsonRejection;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use problem_details::ProblemDetails;
use swarm_controller_api::ssh_connections::{PutSshConnection, SshConnection, SshConnections};
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::{AppState, error_problem};
use crate::schedules::{live, queue, read_or_default, refused, write};
/// How many times a losing writer re-reads and re-applies, as in
/// `agent_permissions`.
const MAX_ATTEMPTS: usize = 5;
fn view(stored: Vec<Stored>) -> SshConnections {
SshConnections {
connections: stored
.into_iter()
.map(|c| SshConnection {
name: c.name,
user: c.user,
host: c.host,
port: c.port,
})
.collect(),
}
}
#[utoipa::path(
get,
path = "/api/agents/{name}/ssh-connections",
params(("name" = String, Path, description = "agent name")),
responses(
(status = 200, description = "the agent's named SSH connections; empty when it has \
none", body = SshConnections),
(status = 400, description = "not an identifier (problem+json)", body = String),
(status = 503, description = "no swarm queue is configured on this host, or it is \
not connected (problem+json)", body = String),
(status = 500, description = "the record could not be read (problem+json)",
body = String),
),
tag = "agents"
)]
pub(crate) async fn get_ssh_connections(
State(state): State<AppState>,
Path(name): Path<String>,
) -> Result<Json<SshConnections>, ProblemDetails> {
let agent = agent_name(&name).map_err(refused)?;
let client = queue(&state).map_err(refused)?;
let store = open_or_create(&client).await?;
let (current, _, _) = read_or_default::<AgentPermissions>(&store, &agent)
.await
.map_err(|e| queue_problem(&e))?;
Ok(Json(view(current.ssh_connections)))
}
/// Add the connection `conn` to the agent, or replace the one of that name.
///
/// The agent's ssh config follows within ten seconds, while its `ssh` switch
/// is on. The connection does not open the hive's network by itself.
#[utoipa::path(
put,
path = "/api/agents/{name}/ssh-connections/{conn}",
params(
("name" = String, Path, description = "agent name"),
("conn" = String, Path, description = "the ssh alias"),
),
request_body = PutSshConnection,
responses(
(status = 200, description = "the agent's connections as stored", body = SshConnections),
(status = 400, description = "not an agent no hive declares, or a connection that \
fails validation: the alias, user, host or port, or more than 32 connections \
(problem+json)", body = String),
(status = 422, description = "an unknown field"),
(status = 503, description = "no swarm queue is configured on this host, or it is \
not connected (problem+json)", body = String),
(status = 500, description = "the record could not be written (problem+json)",
body = String),
),
tag = "agents"
)]
pub(crate) async fn put_ssh_connection(
State(state): State<AppState>,
Path((name, conn)): Path<(String, String)>,
req: Result<Json<PutSshConnection>, JsonRejection>,
) -> Result<Json<SshConnections>, ProblemDetails> {
let refuse = |p| logged("put ssh connection", &name, p);
let Json(req) =
req.map_err(|rejection| refuse(error_problem(rejection.status(), &rejection.body_text())))?;
let agent = agent_name(&name).map_err(|r| refuse(refused(r)))?;
let new = Stored {
name: conn,
user: req.user,
host: req.host,
port: req.port,
}
.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) {
return Err(refuse(error_problem(
StatusCode::BAD_REQUEST,
&format!("no hive's wanted state declares an agent {agent:?} that is not destroyed"),
)));
}
let store = open_or_create(&client).await?;
for _ in 0..MAX_ATTEMPTS {
let (mut value, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
.await
.map_err(|e| queue_problem(&e))?;
match value
.ssh_connections
.iter_mut()
.find(|c| c.name == new.name)
{
Some(slot) => *slot = new.clone(),
None => value.ssh_connections.push(new.clone()),
}
check_list(&value.ssh_connections)
.map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?;
if write(&store, &agent, &value, revision)
.await
.map_err(|e| queue_problem(&e))?
{
tracing::info!(%agent, connection = ?new, "set agent ssh connection");
return Ok(Json(view(value.ssh_connections)));
}
}
Err(queue_problem(&anyhow::anyhow!(
"gave up writing the ssh connections of {agent} after {MAX_ATTEMPTS} conflicting writes"
)))
}
#[utoipa::path(
delete,
path = "/api/agents/{name}/ssh-connections/{conn}",
params(
("name" = String, Path, description = "agent name"),
("conn" = String, Path, description = "the ssh alias"),
),
responses(
(status = 204, description = "removed, or there was none of that name"),
(status = 400, description = "not an identifier (problem+json)", body = String),
(status = 503, description = "no swarm queue is configured on this host, or it is \
not connected (problem+json)", body = String),
(status = 500, description = "the record could not be written (problem+json)",
body = String),
),
tag = "agents"
)]
pub(crate) async fn delete_ssh_connection(
State(state): State<AppState>,
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 store = open_or_create(&client).await?;
for _ in 0..MAX_ATTEMPTS {
let (mut value, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
.await
.map_err(|e| queue_problem(&e))?;
let before = value.ssh_connections.len();
value.ssh_connections.retain(|c| c.name != conn);
if value.ssh_connections.len() == before {
return Ok(StatusCode::NO_CONTENT);
}
let written = if value == AgentPermissions::default() {
store
.delete(&agent)
.await
.map_err(|e| queue_problem(&anyhow::Error::new(e)))?;
true
} else {
write(&store, &agent, &value, revision)
.await
.map_err(|e| queue_problem(&e))?
};
if written {
tracing::info!(%agent, connection = %conn, "removed agent ssh connection");
return Ok(StatusCode::NO_CONTENT);
}
}
Err(queue_problem(&anyhow::anyhow!(
"gave up writing the ssh connections of {agent} after {MAX_ATTEMPTS} conflicting writes"
)))
}
#[cfg(test)]
mod tests {
use super::{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::{Path, State};
use axum::http::StatusCode;
use problem_details::ProblemDetails;
use swarm_controller_api::permissions::Permissions;
use swarm_controller_api::ssh_connections::{PutSshConnection, SshConnections};
fn body(user: &str, host: &str, port: u16) -> PutSshConnection {
PutSshConnection {
user: user.to_owned(),
host: host.to_owned(),
port,
}
}
async fn put(
state: &AppState,
agent: &str,
name: &str,
req: PutSshConnection,
) -> Result<SshConnections, ProblemDetails> {
put_ssh_connection(
State(state.clone()),
Path((agent.to_owned(), name.to_owned())),
Ok(Json(req)),
)
.await
.map(|Json(v)| v)
}
async fn get(state: &AppState, agent: &str) -> SshConnections {
get_ssh_connections(State(state.clone()), Path(agent.to_owned()))
.await
.expect("read")
.0
}
async fn remove(state: &AppState, agent: &str, name: &str) -> StatusCode {
delete_ssh_connection(
State(state.clone()),
Path((agent.to_owned(), name.to_owned())),
)
.await
.expect("delete")
}
async fn stored(
client: &async_nats::Client,
agent: &str,
) -> swarm_wire::agent_permissions::AgentPermissions {
match swarm_queue_client::agent_permissions::read(client, agent)
.await
.expect("read the record")
{
swarm_queue_client::agent_permissions::Read::Current(record) => record,
swarm_queue_client::agent_permissions::Read::NoBucket => panic!("no bucket"),
}
}
fn names(v: &SshConnections) -> Vec<&str> {
v.connections.iter().map(|c| c.name.as_str()).collect()
}
/// A connection is added, listed, replaced under its own name and removed;
/// another agent's list is not touched.
#[tokio::test]
async fn connections_are_added_replaced_and_removed_per_agent() {
use crate::wanted::WantedWriter;
use swarm_wire::wanted::AgentState;
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
WantedWriter::new(client.clone())
.set("pr1ma", "iris", AgentState::Up)
.await
.expect("declare iris");
assert!(get(&state, "atlas").await.connections.is_empty());
let one = put(
&state,
"atlas",
"k8s-lab-vm",
body("root", "muede-pc2", 2222),
)
.await
.expect("added");
assert_eq!(names(&one), ["k8s-lab-vm"]);
assert_eq!(one.connections[0].host, "muede-pc2");
let two = put(&state, "atlas", "db", body("deploy", "Db.Example.org", 22))
.await
.expect("added");
assert_eq!(names(&two), ["k8s-lab-vm", "db"]);
assert_eq!(
two.connections[1].host, "db.example.org",
"stored lower-case"
);
let replaced = put(
&state,
"atlas",
"k8s-lab-vm",
body("admin", "10.1.2.3", 2200),
)
.await
.expect("replaced");
assert_eq!(names(&replaced), ["k8s-lab-vm", "db"], "replaced in place");
assert_eq!(replaced.connections[0].user, "admin");
assert_eq!(get(&state, "atlas").await, replaced);
assert!(
get(&state, "iris").await.connections.is_empty(),
"another agent's list is not changed"
);
assert_eq!(
remove(&state, "atlas", "k8s-lab-vm").await,
StatusCode::NO_CONTENT
);
assert_eq!(names(&get(&state, "atlas").await), ["db"]);
assert_eq!(
remove(&state, "atlas", "k8s-lab-vm").await,
StatusCode::NO_CONTENT,
"removing again is not an error"
);
assert_eq!(names(&get(&state, "atlas").await), ["db"]);
}
/// The list rides in the permissions record: a `PUT` or `DELETE` of the
/// permissions keeps it, and a change of connections keeps the switch.
#[tokio::test]
async fn the_list_and_the_rest_of_the_record_survive_each_others_writes() {
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
let permissions = |ssh| Permissions {
ssh,
..Permissions::default()
};
let _ = put_agent_permissions(
State(state.clone()),
Path("atlas".to_owned()),
Ok(Json(permissions(Some(true)))),
)
.await
.expect("switch on");
let _ = put(&state, "atlas", "lab", body("root", "muede-pc2", 2222))
.await
.expect("added");
let _ = put_agent_permissions(
State(state.clone()),
Path("atlas".to_owned()),
Ok(Json(Permissions {
max_repos: Some(3),
..Permissions::default()
})),
)
.await
.expect("limit");
assert_eq!(
names(&get(&state, "atlas").await),
["lab"],
"PUT /permissions keeps it"
);
delete_agent_permissions(State(state.clone()), Path("atlas".to_owned()))
.await
.expect("cleared");
assert_eq!(
names(&get(&state, "atlas").await),
["lab"],
"DELETE /permissions keeps it"
);
let _ = put(&state, "atlas", "other", body("root", "h", 22))
.await
.expect("added");
let record = stored(&client, "atlas").await;
assert_eq!(
record.ssh,
Some(true),
"a connection write keeps the switch"
);
assert_eq!(record.max_repos, Some(3));
// Control: the permissions record the field left out of is still read.
remove(&state, "atlas", "lab").await;
remove(&state, "atlas", "other").await;
let record = stored(&client, "atlas").await;
assert_eq!(record.ssh, Some(true));
assert!(record.ssh_connections.is_empty());
}
/// Each refusal is a 400 and writes nothing; the control is the same
/// request with the one field fixed.
#[tokio::test]
async fn an_invalid_connection_is_refused_and_nothing_is_written() {
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
for (why, agent, name, req) in [
("alias", "atlas", "Bad Name", body("root", "h", 22)),
("user", "atlas", "a", body("-oProxyCommand=x", "h", 22)),
("loopback", "atlas", "a", body("root", "127.0.0.1", 22)),
("host", "atlas", "a", body("root", "h\nHost *", 22)),
("port 80", "atlas", "a", body("root", "h", 80)),
("port 443", "atlas", "a", body("root", "h", 443)),
("port 0", "atlas", "a", body("root", "h", 0)),
("undeclared agent", "ghost", "a", body("root", "h", 22)),
("agent name", "Not An Ident", "a", body("root", "h", 22)),
] {
let err = put(&state, agent, name, req).await.expect_err(why);
assert_eq!(err.status, Some(StatusCode::BAD_REQUEST), "{why}: {err:?}");
}
assert!(
get(&state, "atlas").await.connections.is_empty(),
"nothing was written"
);
let _ = put(&state, "atlas", "a", body("root", "h", 22))
.await
.expect("control: the valid one is stored");
}
/// The 33rd connection is refused; replacing one at the bound is not.
#[tokio::test]
async fn an_agent_has_at_most_the_bound() {
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
for i in 0..swarm_wire::ssh_connection::MAX_CONNECTIONS {
let _ = put(&state, "atlas", &format!("c{i}"), body("root", "h", 22))
.await
.expect("within the bound");
}
let err = put(&state, "atlas", "one-more", body("root", "h", 22))
.await
.expect_err("over the bound");
assert_eq!(err.status, Some(StatusCode::BAD_REQUEST));
let _ = put(&state, "atlas", "c0", body("root", "h", 2222))
.await
.expect("control: a replacement at the bound");
}
/// A record written before the field existed takes a connection and keeps
/// its other fields.
#[tokio::test]
async fn a_record_from_before_the_field_takes_a_connection() {
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
swarm_queue_client::agent_permissions::open_or_create(&client)
.await
.expect("bucket")
.put("atlas", br#"{"ssh":true,"max_repos":2}"#.to_vec().into())
.await
.expect("old record");
let _ = put(&state, "atlas", "lab", body("root", "muede-pc2", 2222))
.await
.expect("added");
let record = stored(&client, "atlas").await;
assert_eq!((record.ssh, record.max_repos), (Some(true), Some(2)));
assert_eq!(record.ssh_connections.len(), 1);
}
}