swarm ui ssh row with target snippets, docs for the per-agent switch
This commit is contained in:
parent
624d5f24f8
commit
e621397252
1 changed files with 417 additions and 94 deletions
75
frontend/packages/swarm-ui/src/agent/detail/ssh.ts
Normal file
75
frontend/packages/swarm-ui/src/agent/detail/ssh.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// An opened agent card's SSH row: what the badge says, the body of the PUT that
|
||||
// flips the switch, and the text an operator puts on a target machine. Kept out
|
||||
// of the component so `npm test` can run it.
|
||||
|
||||
/** The three ways a target can be set up to trust the swarm's SSH CA. */
|
||||
export type TargetForm = "nixos" | "home-manager" | "ssh";
|
||||
|
||||
/** The forms in the order the card offers them, with their tab labels. */
|
||||
export const TARGET_FORMS: [TargetForm, string][] = [
|
||||
["nixos", "NixOS"],
|
||||
["home-manager", "home-manager"],
|
||||
["ssh", "ssh"],
|
||||
];
|
||||
|
||||
/** What the badge shows for a stored `ssh` flag: absent is off. */
|
||||
export function sshSummary(stored: boolean | undefined): string {
|
||||
return stored === true ? "on" : "off";
|
||||
}
|
||||
|
||||
/**
|
||||
* The `PUT` body that sets the switch. A `PUT` without `permissions` resets a
|
||||
* stored list to the default, so the stored list goes along, as in
|
||||
* `internetBody`; the other fields are left out, which keeps them.
|
||||
*/
|
||||
export function sshBody(
|
||||
stored: string[] | undefined,
|
||||
on: boolean,
|
||||
): { permissions?: string[]; ssh: boolean } {
|
||||
return {
|
||||
...(stored === undefined ? {} : { permissions: stored }),
|
||||
ssh: on,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The single `authorized_keys` line that makes a machine accept `agent`'s
|
||||
* certificates, whatever its sshd config: it trusts the CA for that one
|
||||
* principal and no other.
|
||||
*/
|
||||
export function authorizedKeysLine(agent: string, caKey: string): string {
|
||||
return `cert-authority,principals="${agent}" ${caKey.trim()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The text for one form of target. `<user>` stays a placeholder the operator
|
||||
* edits: it is the account on the target the agent logs in as.
|
||||
*/
|
||||
export function targetSnippet(
|
||||
form: TargetForm,
|
||||
agent: string,
|
||||
caKey: string,
|
||||
): string {
|
||||
const ca = caKey.trim();
|
||||
switch (form) {
|
||||
case "nixos":
|
||||
return [
|
||||
"# The CA key is public. sshd trusts certificates it signed, and the",
|
||||
`# principal ${agent} may log in as <user>.`,
|
||||
`environment.etc."ssh/hyperhive-agent-ssh-ca.pub".text = "${ca}\\n";`,
|
||||
'services.openssh.settings.TrustedUserCAKeys = "/etc/ssh/hyperhive-agent-ssh-ca.pub";',
|
||||
`users.users.<user>.openssh.authorizedPrincipals = [ "${agent}" ];`,
|
||||
"",
|
||||
"# The same, as one module: imports = [ hyperhive.nixosModules.agent-ssh-target ];",
|
||||
"# services.hyperhive.agentSshTarget.enable = true, with caPublicKey and principals.",
|
||||
].join("\n");
|
||||
case "home-manager":
|
||||
return [
|
||||
"home.file.\".ssh/authorized_keys\".text = ''",
|
||||
` ${authorizedKeysLine(agent, ca)}`,
|
||||
"'';",
|
||||
].join("\n");
|
||||
case "ssh":
|
||||
return `# in ~<user>/.ssh/authorized_keys on the target\n${authorizedKeysLine(agent, ca)}`;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue