swarm ui: an agent's named ssh connections show in its ssh popover, with add, replace and remove
The ssh popover of an opened agent card gets a folded connections section: a table of the agent's named connections (name, user@host:port) with edit and remove, and a form that saves one. The list is read on the first open; a save is a PUT that replaces a connection of the same name, so the button reads replace and a line names the connection when the name exists; remove asks first. The controller's refusal shows under the form as it comes, and the section says the connections are inactive while ssh is off. The request building is in ssh-connections.ts with a test; swarm-ui.md describes the section.
This commit is contained in:
parent
60af939514
commit
62551e004e
5 changed files with 462 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
// <AgentSsh> — one agent's SSH access in an opened agent card: a badge that
|
||||
// opens an explainer, the switch, and a folded "on the target" section with the
|
||||
// text a machine needs to trust the agent's certificates.
|
||||
// opens an explainer, the switch, a folded "on the target" section with the text
|
||||
// a machine needs to trust the agent's certificates, and a folded "connections"
|
||||
// section for the agent's named connections (`AgentSshConnections`).
|
||||
//
|
||||
// The switch is the `ssh` field of `GET/PUT /api/agents/{name}/permissions`,
|
||||
// shared with the other grant controls through `useGrant`. The CA key comes
|
||||
|
|
@ -17,6 +18,7 @@ import { Button } from "../../ui/button/Button.js";
|
|||
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
|
||||
import { Hint } from "../../ui/hint/Hint.js";
|
||||
import { Preformatted } from "../../ui/preformatted/Preformatted.js";
|
||||
import { AgentSshConnections } from "./AgentSshConnections.js";
|
||||
import type { GrantState } from "./AgentPermissions.js";
|
||||
import {
|
||||
forgetHostKeyCommand,
|
||||
|
|
@ -160,6 +162,7 @@ export function AgentSsh({ agent, g }: { agent: string; g: GrantState }) {
|
|||
</>
|
||||
)}
|
||||
</details>
|
||||
<AgentSshConnections agent={agent} on={on} />
|
||||
</BadgePopover>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
/* The named SSH connections of an agent card: a table that scrolls on its own on
|
||||
a narrow screen rather than breaking the popover. */
|
||||
.ui-agent-ssh-connections {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75em;
|
||||
margin-block-start: 0.5em;
|
||||
}
|
||||
.ui-agent-ssh-connections-scroll {
|
||||
overflow-x: auto;
|
||||
}
|
||||
.ui-agent-ssh-connections-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.ui-agent-ssh-connections-table th,
|
||||
.ui-agent-ssh-connections-table td {
|
||||
padding: 0.25em 0.75em 0.25em 0;
|
||||
text-align: start;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.ui-agent-ssh-connections-table thead th {
|
||||
color: var(--muted);
|
||||
font-weight: normal;
|
||||
}
|
||||
.ui-agent-ssh-connections-table tbody th {
|
||||
font-weight: 600;
|
||||
}
|
||||
.ui-agent-ssh-connections-actions {
|
||||
text-align: end;
|
||||
}
|
||||
|
|
@ -0,0 +1,276 @@
|
|||
// <AgentSshConnections> — the named SSH connections of one agent, a folded
|
||||
// section of its SSH popover: a table of them with a remove button each, and a
|
||||
// form to add one.
|
||||
//
|
||||
// A connection is `name; user; host; port`. It becomes an `ssh <name>` entry in
|
||||
// the agent's ssh config, and the agent's hive lets it reach that host and port.
|
||||
// It only takes effect while the agent's ssh switch is on, so the section says
|
||||
// so when it is off, and stays usable then: the list is what the operator set.
|
||||
//
|
||||
// Reads `GET /api/agents/{name}/ssh-connections` when the section is first
|
||||
// opened. A save is a `PUT` of one connection, which replaces one of the same
|
||||
// name, so the form says "replace" and names it when the name is taken. A save
|
||||
// answers the whole list; a remove is a `DELETE`. The controller does all the
|
||||
// checking, and the form shows its refusal as it arrives.
|
||||
//
|
||||
// Its state belongs to one agent: callers key it by agent name.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { ActionRow } from "../../ui/action-row/ActionRow.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
||||
import { FormStack } from "../../ui/form-stack/FormStack.js";
|
||||
import { Hint } from "../../ui/hint/Hint.js";
|
||||
import { TextField } from "../../ui/text-field/TextField.js";
|
||||
import {
|
||||
connectionBody,
|
||||
connectionUrl,
|
||||
connectionsUrl,
|
||||
replaces,
|
||||
sortedConnections,
|
||||
type SshConnection,
|
||||
type SshConnections,
|
||||
} from "./ssh-connections.js";
|
||||
import "./AgentSshConnections.css";
|
||||
|
||||
export function AgentSshConnections({
|
||||
agent,
|
||||
on,
|
||||
}: {
|
||||
agent: string;
|
||||
on: boolean;
|
||||
}) {
|
||||
// Read on the first open, so a card nobody asks about reads nothing.
|
||||
const [opened, setOpened] = useState(false);
|
||||
return (
|
||||
<details
|
||||
class="ui-agent-ssh-target"
|
||||
onToggle={(e) => {
|
||||
if ((e.currentTarget as HTMLDetailsElement).open) setOpened(true);
|
||||
}}
|
||||
>
|
||||
<summary>connections</summary>
|
||||
{opened ? <Connections agent={agent} on={on} /> : null}
|
||||
</details>
|
||||
);
|
||||
}
|
||||
|
||||
function Connections({ agent, on }: { agent: string; on: boolean }) {
|
||||
const [list, setList] = useState<SshConnection[] | null>(null);
|
||||
const [loadError, setLoadError] = useState<ProblemDetails | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [user, setUser] = useState("");
|
||||
const [host, setHost] = useState("");
|
||||
const [port, setPort] = useState("22");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [problem, setProblem] = useState<ProblemDetails | null>(null);
|
||||
const [confirm, confirmDialog] = useConfirm();
|
||||
|
||||
async function load() {
|
||||
setLoadError(null);
|
||||
try {
|
||||
const r = await fetch(connectionsUrl(agent));
|
||||
if (!r.ok) {
|
||||
setLoadError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
setList(((await r.json()) as SshConnections).connections);
|
||||
} catch (e: unknown) {
|
||||
setLoadError({ detail: String(e) });
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [agent]);
|
||||
|
||||
async function save(e: Event) {
|
||||
e.preventDefault();
|
||||
const body = connectionBody(user, host, port);
|
||||
if (body === null || name.trim() === "") return;
|
||||
setBusy(true);
|
||||
setProblem(null);
|
||||
try {
|
||||
const r = await fetch(connectionUrl(agent, name.trim()), {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!r.ok) {
|
||||
// The form stays as typed, so the operator can fix it and save again.
|
||||
setProblem(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
setList(((await r.json()) as SshConnections).connections);
|
||||
setName("");
|
||||
setUser("");
|
||||
setHost("");
|
||||
setPort("22");
|
||||
} catch (err: unknown) {
|
||||
setProblem({ detail: String(err) });
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(c: SshConnection) {
|
||||
if (
|
||||
!(await confirm({
|
||||
label: `remove the connection ${c.name}`,
|
||||
confirmLabel: "remove",
|
||||
danger: true,
|
||||
body: (
|
||||
<p>
|
||||
Remove <code>{c.name}</code> ({c.user}@{c.host}:{c.port})? {agent}{" "}
|
||||
can no longer <code>ssh {c.name}</code>, and its hive stops letting
|
||||
it reach that host.
|
||||
</p>
|
||||
),
|
||||
}))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
setProblem(null);
|
||||
try {
|
||||
const r = await fetch(connectionUrl(agent, c.name), { method: "DELETE" });
|
||||
if (!r.ok) {
|
||||
setProblem(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
setList((l) => (l ?? []).filter((x) => x.name !== c.name));
|
||||
} catch (err: unknown) {
|
||||
setProblem({ detail: String(err) });
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
function edit(c: SshConnection) {
|
||||
setName(c.name);
|
||||
setUser(c.user);
|
||||
setHost(c.host);
|
||||
setPort(String(c.port));
|
||||
setProblem(null);
|
||||
}
|
||||
|
||||
const replacing = list !== null && replaces(list, name);
|
||||
const complete =
|
||||
name.trim() !== "" && connectionBody(user, host, port) !== null;
|
||||
return (
|
||||
<div class="ui-agent-ssh-connections">
|
||||
<p class="ui-badge-popover-help">
|
||||
A named connection is an <code>ssh <name></code> entry in {agent}
|
||||
's ssh config, and its hive lets {agent} reach that host and port.
|
||||
{on ? null : " Inactive while ssh is off."}
|
||||
</p>
|
||||
{loadError ? (
|
||||
<>
|
||||
<ApiErrorPanel
|
||||
context="the connections were not read"
|
||||
problem={loadError}
|
||||
/>
|
||||
<Button onClick={() => void load()}>retry</Button>
|
||||
</>
|
||||
) : list === null ? (
|
||||
<Hint>reading the connections…</Hint>
|
||||
) : list.length === 0 ? (
|
||||
<Hint>no connections</Hint>
|
||||
) : (
|
||||
<div class="ui-agent-ssh-connections-scroll">
|
||||
<table class="ui-agent-ssh-connections-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">name</th>
|
||||
<th scope="col">connects to</th>
|
||||
<th scope="col">
|
||||
<span class="ui-visually-hidden">actions</span>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{sortedConnections(list).map((c) => (
|
||||
<tr key={c.name}>
|
||||
<th scope="row">{c.name}</th>
|
||||
<td>
|
||||
{c.user}@{c.host}:{c.port}
|
||||
</td>
|
||||
<td class="ui-agent-ssh-connections-actions">
|
||||
<Badge
|
||||
variant="quiet"
|
||||
value="edit"
|
||||
ariaLabel={`edit ${c.name}`}
|
||||
title={`fill the form with ${c.name}`}
|
||||
disabled={busy}
|
||||
onClick={() => edit(c)}
|
||||
/>{" "}
|
||||
<Badge
|
||||
variant="quiet"
|
||||
value="remove"
|
||||
ariaLabel={`remove ${c.name}`}
|
||||
title={`remove the connection ${c.name}`}
|
||||
disabled={busy}
|
||||
onClick={() => void remove(c)}
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
<FormStack onSubmit={(e) => void save(e)}>
|
||||
<TextField
|
||||
id={`ssh-conn-name-${agent}`}
|
||||
label="name"
|
||||
value={name}
|
||||
placeholder="e.g. k8s-lab-vm"
|
||||
onInput={setName}
|
||||
/>
|
||||
<TextField
|
||||
id={`ssh-conn-user-${agent}`}
|
||||
label="user"
|
||||
value={user}
|
||||
placeholder="e.g. root"
|
||||
onInput={setUser}
|
||||
/>
|
||||
<TextField
|
||||
id={`ssh-conn-host-${agent}`}
|
||||
label="host"
|
||||
value={host}
|
||||
placeholder="e.g. muede-pc2"
|
||||
onInput={setHost}
|
||||
/>
|
||||
<TextField
|
||||
id={`ssh-conn-port-${agent}`}
|
||||
label="port"
|
||||
value={port}
|
||||
onInput={setPort}
|
||||
/>
|
||||
{replacing ? (
|
||||
<Hint>
|
||||
There is already a connection named <code>{name.trim()}</code>;
|
||||
saving replaces it.
|
||||
</Hint>
|
||||
) : null}
|
||||
{problem ? (
|
||||
<ApiErrorPanel
|
||||
context="the connections were not changed"
|
||||
problem={problem}
|
||||
/>
|
||||
) : null}
|
||||
<ActionRow>
|
||||
<Button
|
||||
variant="primary"
|
||||
type="submit"
|
||||
disabled={busy || list === null || !complete}
|
||||
>
|
||||
{busy ? "saving…" : replacing ? "replace" : "add"}
|
||||
</Button>
|
||||
</ActionRow>
|
||||
</FormStack>
|
||||
{confirmDialog}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
// The named SSH connections of an opened agent card: the routes, the body a
|
||||
// save sends and what the add form says about a name that is already there.
|
||||
// Kept out of the component so `npm test` can run it. The controller checks
|
||||
// every field (the name, user and host rules, the ports it refuses, the limit of
|
||||
// 32), so nothing here repeats those rules: a refusal shows as the controller
|
||||
// words it.
|
||||
|
||||
/** One connection as `GET /api/agents/{agent}/ssh-connections` carries it. */
|
||||
export interface SshConnection {
|
||||
name: string;
|
||||
user: string;
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
/** The answer of the list route and of a save: the agent's whole list. */
|
||||
export interface SshConnections {
|
||||
connections: SshConnection[];
|
||||
}
|
||||
|
||||
export function connectionsUrl(agent: string): string {
|
||||
return `/api/agents/${encodeURIComponent(agent)}/ssh-connections`;
|
||||
}
|
||||
|
||||
export function connectionUrl(agent: string, name: string): string {
|
||||
return `${connectionsUrl(agent)}/${encodeURIComponent(name)}`;
|
||||
}
|
||||
|
||||
/** The connections in name order, as the table lists them. */
|
||||
export function sortedConnections(list: SshConnection[]): SshConnection[] {
|
||||
return [...list].sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
/** Whether saving `name` replaces a connection the list already has. */
|
||||
export function replaces(list: SshConnection[], name: string): boolean {
|
||||
const wanted = name.trim();
|
||||
return list.some((c) => c.name === wanted);
|
||||
}
|
||||
|
||||
/**
|
||||
* The port as a whole number from what was typed, or null when it is not one.
|
||||
* The range and the ports the swarm refuses are the controller's to judge.
|
||||
*/
|
||||
export function parsePort(text: string): number | null {
|
||||
const t = text.trim();
|
||||
return /^\d+$/.test(t) ? Number(t) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The body of a save, or null while a field is empty or the port is not a
|
||||
* number. The name goes in the URL.
|
||||
*/
|
||||
export function connectionBody(
|
||||
user: string,
|
||||
host: string,
|
||||
port: string,
|
||||
): { user: string; host: string; port: number } | null {
|
||||
const n = parsePort(port);
|
||||
if (user.trim() === "" || host.trim() === "" || n === null) return null;
|
||||
return { user: user.trim(), host: host.trim(), port: n };
|
||||
}
|
||||
75
frontend/packages/swarm-ui/test/ssh-connections.test.mjs
Normal file
75
frontend/packages/swarm-ui/test/ssh-connections.test.mjs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// The named SSH connections of an agent card: URLs, the save body and the
|
||||
// replace check. Run by `npm test`.
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import {
|
||||
connectionBody,
|
||||
connectionUrl,
|
||||
connectionsUrl,
|
||||
parsePort,
|
||||
replaces,
|
||||
sortedConnections,
|
||||
} from "../src/agent/detail/ssh-connections.ts";
|
||||
|
||||
const conn = (name, host = "box", port = 22, user = "root") => ({
|
||||
name,
|
||||
user,
|
||||
host,
|
||||
port,
|
||||
});
|
||||
|
||||
test("the routes carry the agent and the connection name", () => {
|
||||
assert.equal(connectionsUrl("iris"), "/api/agents/iris/ssh-connections");
|
||||
assert.equal(
|
||||
connectionUrl("iris", "k8s-lab-vm"),
|
||||
"/api/agents/iris/ssh-connections/k8s-lab-vm",
|
||||
);
|
||||
});
|
||||
|
||||
test("a name is escaped in the URL", () => {
|
||||
assert.equal(
|
||||
connectionUrl("iris", "a/b c"),
|
||||
"/api/agents/iris/ssh-connections/a%2Fb%20c",
|
||||
);
|
||||
});
|
||||
|
||||
test("the table lists connections by name", () => {
|
||||
const list = [conn("zeta"), conn("alpha"), conn("mid")];
|
||||
assert.deepEqual(
|
||||
sortedConnections(list).map((c) => c.name),
|
||||
["alpha", "mid", "zeta"],
|
||||
);
|
||||
assert.deepEqual(
|
||||
list.map((c) => c.name),
|
||||
["zeta", "alpha", "mid"],
|
||||
"the list given is not reordered",
|
||||
);
|
||||
});
|
||||
|
||||
test("a save replaces a connection with the same name, ignoring spaces typed", () => {
|
||||
const list = [conn("lab"), conn("prod")];
|
||||
assert.equal(replaces(list, "lab"), true);
|
||||
assert.equal(replaces(list, " lab "), true);
|
||||
assert.equal(replaces(list, "other"), false);
|
||||
assert.equal(replaces([], "lab"), false);
|
||||
});
|
||||
|
||||
test("a port is a whole number or nothing", () => {
|
||||
assert.equal(parsePort("2222"), 2222);
|
||||
assert.equal(parsePort(" 22 "), 22);
|
||||
assert.equal(parsePort(""), null);
|
||||
assert.equal(parsePort("22a"), null);
|
||||
assert.equal(parsePort("-1"), null);
|
||||
assert.equal(parsePort("2.5"), null);
|
||||
});
|
||||
|
||||
test("the body trims the fields and needs all of them", () => {
|
||||
assert.deepEqual(connectionBody(" root ", " muede-pc2 ", "2222"), {
|
||||
user: "root",
|
||||
host: "muede-pc2",
|
||||
port: 2222,
|
||||
});
|
||||
assert.equal(connectionBody("", "host", "22"), null);
|
||||
assert.equal(connectionBody("root", " ", "22"), null);
|
||||
assert.equal(connectionBody("root", "host", "ssh"), null);
|
||||
});
|
||||
Loading…
Reference in a new issue