import { describe, expect, it } from "vitest"; import { bootstrapBefehl, bootstrapSkript, sshdBefundLesen } from "@/lib/ssh/bootstrap-skript"; import { shellQuote } from "@/lib/shell"; /** Das Bootstrap-Skript als Text (#010); ausgefuehrt wird es im Integrationstest. */ const KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl sshkv-deployment@x"; describe("Bootstrap-Skript", () => { it("quotiert Shell-Werte sicher", () => { expect(shellQuote("a'b")).toBe(`'a'\\''b'`); expect(shellQuote("$(id)")).toBe(`'$(id)'`); expect(() => shellQuote("a\0b")).toThrow(); }); it("weist ungueltige Benutzer und kaputte Keys ab", () => { expect(() => bootstrapSkript({ benutzer: "root;id", dienstbenutzerAnlegen: false, deploymentKey: KEY }), ).toThrow(); expect(() => bootstrapSkript({ benutzer: "sshkv", dienstbenutzerAnlegen: true, deploymentKey: "ssh-ed25519 kaputt\nid", }), ).toThrow(); }); it("setzt den Key quotiert ein und prueft sudoers vor dem Aktivieren", () => { const skript = bootstrapSkript({ benutzer: "sshkv", dienstbenutzerAnlegen: true, deploymentKey: KEY }); expect(skript).toContain(`KEY='${KEY}'`); expect(skript).toContain("visudo -cf"); expect(skript.indexOf("visudo -cf")).toBeLessThan(skript.indexOf('mv "$NEU_SUDO" /etc/sudoers.d/sshkv')); expect(skript).toContain("runuser -u"); }); it("ergibt einen Befehl mit Here-Document, das nichts expandiert", () => { const befehl = bootstrapBefehl({ benutzer: "root", dienstbenutzerAnlegen: false, deploymentKey: KEY }); expect(befehl.startsWith("sudo sh <<'SSHKV_BOOTSTRAP'\n")).toBe(true); expect(befehl.trimEnd().endsWith("SSHKV_BOOTSTRAP")).toBe(true); }); it("liest die sshd-Einstellungen aus der Ausgabe", () => { expect( sshdBefundLesen( "SSHKV-SSHD:permitrootlogin without-password\nSSHKV-SSHD:passwordauthentication no\nanderes\nSSHKV-SSHD:authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2\n", ), ).toEqual({ permitRootLogin: "without-password", passwordAuthentication: "no", authorizedKeysFile: ".ssh/authorized_keys .ssh/authorized_keys2", }); }); });