adding ssh host signing
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"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 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)
|
||||
}
|
||||
Reference in New Issue
Block a user