Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/agent/detail/ssh.ts

84 lines
3 KiB
TypeScript

// 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)}`;
}
}
/**
* The command that makes the agent forget a host's recorded key, run as root on
* the hive host after the operator has checked the new key out of band. The
* agent's pins live in its state directory.
*/
export function forgetHostKeyCommand(agent: string): string {
return `ssh-keygen -R <host> -f /agents/${agent}/state/ssh_known_hosts`;
}