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
2 changed files with 1420 additions and 12 deletions

View file

@ -14,3 +14,4 @@ pub mod messages;
pub mod permissions;
pub mod schedules;
pub mod ssh;
pub mod ssh_connections;

View file

@ -0,0 +1,77 @@
//! `/api/agents/{name}/ssh-connections`: the named SSH connections declared for
//! one agent. The agent's ssh config gets one `Host` block for each while its
//! `ssh` switch is on.
use serde::{Deserialize, Serialize};
/// One named connection: `ssh <name>` connects as `user` to `host` on `port`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
pub struct SshConnection {
/// The alias the agent types: lower-case letters, digits and hyphens, at
/// most 63, not starting with a hyphen.
#[cfg_attr(feature = "schema", schema(example = "k8s-lab-vm"))]
pub name: String,
/// The account to log in as.
#[cfg_attr(feature = "schema", schema(example = "root"))]
pub user: String,
/// A DNS name or an IPv4 address.
#[cfg_attr(feature = "schema", schema(example = "muede-pc2"))]
pub host: String,
/// The TCP port, 1 to 65535 except 80 and 443.
#[cfg_attr(feature = "schema", schema(example = 2222))]
pub port: u16,
}
/// The body of `PUT /api/agents/{name}/ssh-connections/{conn}`; the name is the
/// path's.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
#[serde(deny_unknown_fields)]
pub struct PutSshConnection {
/// The account to log in as.
#[cfg_attr(feature = "schema", schema(example = "root"))]
pub user: String,
/// A DNS name or an IPv4 address.
#[cfg_attr(feature = "schema", schema(example = "muede-pc2"))]
pub host: String,
/// The TCP port, 1 to 65535 except 80 and 443.
#[cfg_attr(feature = "schema", schema(example = 2222))]
pub port: u16,
}
/// The answer to `GET /api/agents/{name}/ssh-connections`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
pub struct SshConnections {
/// The agent's connections, in the order they were added.
pub connections: Vec<SshConnection>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bodies_round_trip_and_the_put_refuses_other_fields() {
let put = PutSshConnection {
user: "root".to_owned(),
host: "muede-pc2".to_owned(),
port: 2222,
};
let wire = r#"{"user":"root","host":"muede-pc2","port":2222}"#;
assert_eq!(serde_json::to_string(&put).unwrap(), wire);
assert_eq!(serde_json::from_str::<PutSshConnection>(wire).unwrap(), put);
assert!(
serde_json::from_str::<PutSshConnection>(
r#"{"name":"x","user":"root","host":"h","port":22}"#
)
.is_err(),
"the name is the path's"
);
assert_eq!(
serde_json::to_string(&SshConnections::default()).unwrap(),
r#"{"connections":[]}"#
);
}
}