Files
Audiobuzz 28d31e436c ssh-hostcert + enrol-broker: fix defects surfaced by the F-02 bench
F-02/P-17 SSH host-cert flow was proven end-to-end on comet-beacon-54
(2026-07-11); these fix the correctness/scoping/cleanup defects the bench
surfaced:

- ssh-hostcert: derive the FQDN principal <hostname>.<domain> from the DHCP
  search domain at runtime (resolv.conf), alongside the short hostname, so an
  admin `@cert-authority *.<domain>` line accepts by-FQDN logins instead of
  "name is not a listed principal". Never baked (invariant #1). + tests.
- ssh-ca-bootstrap.sh: export the SSH host CA public key in OpenSSH wire format
  (ssh-keygen -i -m PKCS8), not the PEM/PKCS8 that `step crypto keypair` writes
  -- a PEM @cert-authority anchor is unparseable and silently falls back to TOFU.
- ssh-ca-bootstrap.sh: fail loudly (and verify enableSSHCA=true) if the
  provisioner SSH-enable step fails, instead of swallowing it and shipping a CA
  that 502s every runtime sign.
- dot1x-bench: point the co-located broker at https://localhost:8443, not the
  fleet dnsmasq name ca.fleet.lab (unresolvable from the broker's host netns ->
  "no such host" at sign time). localhost is in the CA cert SAN.
- serving: remove the mock-signer enrol-broker copy -- two host-networked
  brokers collide on 9090/9443 and the mock issued untrusted certs. The real
  broker lives with step-ca (dot1x-bench); nginx /enrol/ still proxies to it on
  loopback.
- enrol-broker: retire the HTML admin console; the fleet-manager passkey SPA now
  drives the mTLS JSON API (/pending, /approve|deny). Removes a weaker,
  client-cert-only parallel human-approve path.

Design-doc bench note deferred: docs/ssh-host-certificate-design.md lives on the
secure-ntp branch (fbb8100), not here -- to be added there / on reconcile.
2026-07-11 09:44:33 +00:00

151 lines
4.2 KiB
Go

package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestReadHostPubLine(t *testing.T) {
dir := t.TempDir()
p := dir + "/k.pub"
if err := writeFileAtomic(p, []byte("ecdsa-sha2-nistp256 AAAAB3Nz root@fleet\n"), 0o644); err != nil {
t.Fatal(err)
}
line, err := readHostPubLine(p)
if err != nil {
t.Fatal(err)
}
if line != "ecdsa-sha2-nistp256 AAAAB3Nz root@fleet" {
t.Errorf("got %q", line)
}
if err := writeFileAtomic(p, []byte("garbage\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, err := readHostPubLine(p); err == nil {
t.Error("expected error on malformed key")
}
}
func TestSplitPrincipals(t *testing.T) {
got := splitPrincipals(" a, b ,,c ")
if len(got) != 3 || got[0] != "a" || got[1] != "b" || got[2] != "c" {
t.Errorf("got %v", got)
}
if len(splitPrincipals("")) != 0 {
t.Error("empty string should yield no principals")
}
}
func TestDhcpDomain(t *testing.T) {
dir := t.TempDir()
rc := dir + "/resolv.conf"
old := resolvConf
resolvConf = rc
defer func() { resolvConf = old }()
if err := os.WriteFile(rc, []byte("domain corp.example\n"), 0o644); err != nil {
t.Fatal(err)
}
if d := dhcpDomain(); d != "corp.example" {
t.Errorf("domain line: got %q", d)
}
if err := os.WriteFile(rc, []byte("nameserver 1.1.1.1\nsearch a.lab b.lab\n"), 0o644); err != nil {
t.Fatal(err)
}
if d := dhcpDomain(); d != "a.lab" {
t.Errorf("search line: got %q", d)
}
os.Remove(rc)
if d := dhcpDomain(); d != "" {
t.Errorf("missing file: got %q", d)
}
}
func TestDefaultPrincipalsFQDN(t *testing.T) {
h, _ := os.Hostname()
if h == "" || strings.Contains(h, ".") {
t.Skip("host name unsuitable for this test")
}
dir := t.TempDir()
rc := dir + "/resolv.conf"
if err := os.WriteFile(rc, []byte("nameserver 10.0.0.1\nsearch fleet.lab example.org\n"), 0o644); err != nil {
t.Fatal(err)
}
old := resolvConf
resolvConf = rc
defer func() { resolvConf = old }()
got := defaultPrincipals()
if len(got) != 2 || got[0] != h || got[1] != h+".fleet.lab" {
t.Errorf("defaultPrincipals = %v, want [%s %s.fleet.lab]", got, h, h)
}
}
func TestAppendUnique(t *testing.T) {
got := appendUnique([]string{"a", "b"}, "b", "c", "", "a", "d")
want := []string{"a", "b", "c", "d"}
if len(got) != len(want) {
t.Fatalf("got %v want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("got %v want %v", got, want)
}
}
}
func TestParseHandle(t *testing.T) {
h, err := parseHandle("0x81000100")
if err != nil || uint32(h) != 0x81000100 {
t.Fatalf("h=%#x err=%v", uint32(h), err)
}
if _, err := parseHandle("nothex"); err == nil {
t.Error("expected error")
}
}
func TestRequestHostCert(t *testing.T) {
const certLine = "ecdsa-sha2-nistp256-cert-v01@openssh.com AAAAcert host.fleet.lab"
var gotReq hostCertRequest
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &gotReq)
writeJSON(w, http.StatusOK, hostCertResponse{Cert: certLine})
}))
defer srv.Close()
got, err := requestHostCert(srv.Client(), srv.URL, "ecdsa-sha2-nistp256 AAAA host", []string{"host", "host.fleet.lab"}, "host.fleet.lab")
if err != nil {
t.Fatalf("requestHostCert: %v", err)
}
if got != certLine {
t.Errorf("cert = %q", got)
}
if gotReq.PublicKey != "ecdsa-sha2-nistp256 AAAA host" || gotReq.KeyID != "host.fleet.lab" || len(gotReq.Principals) != 2 {
t.Errorf("broker received unexpected request: %+v", gotReq)
}
}
func TestRequestHostCertErrorBody(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusForbidden, hostCertResponse{Error: "not enrolled"})
}))
defer srv.Close()
if _, err := requestHostCert(srv.Client(), srv.URL, "ecdsa-sha2-nistp256 AAAA host", []string{"host"}, "host"); err == nil || !strings.Contains(err.Error(), "not enrolled") {
t.Errorf("want error carrying broker message, got %v", err)
}
}
// writeJSON mirrors the broker's small JSON helper for the test server.
func writeJSON(w http.ResponseWriter, code int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(v)
}