Taking the cluster's own login away and letting the proxy sign in with a certificate did not make the portal private. It made it public: the proxy sat outside the control plane's authentication on purpose, back when the cluster had a password of its own to fall back on. I found it by fetching c001's dashboard from the internet with no session and getting it. The proxy now asks before it opens a connection: a valid session, and the cluster has to belong to whoever is asking, or be reached by an administrator. A browser is sent to sign in, an API client gets 401, and nothing is proxied either way. The check runs before the registry is consulted, so a signed-out caller gets the same answer for a cluster that exists and one that does not. Also tagged the roles in site.yml. Changing the portal meant re-running everything, which rebuilds the node image - four minutes and a risk taken for nothing. Dynamic includes need "apply" for the tag to reach the role's own tasks; a tag on the include alone only decides whether the include runs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
207 lines
7.0 KiB
Go
207 lines
7.0 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/pem"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A registry entry only loads if its certificate does, so tests need a real
|
|
// one. The backend's own certificate is the trust anchor for that backend,
|
|
// which is exactly the arrangement in production.
|
|
func writeCert(t *testing.T, dir string, srv *httptest.Server) string {
|
|
t.Helper()
|
|
p := filepath.Join(dir, "tenant.crt")
|
|
pem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw})
|
|
if err := os.WriteFile(p, pem, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func writeRegistry(t *testing.T, dir, body string) string {
|
|
t.Helper()
|
|
p := filepath.Join(dir, "tenants.yaml")
|
|
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func TestLookupRoutesOnlyKnownTenants(t *testing.T) {
|
|
dir := t.TempDir()
|
|
srv := httptest.NewTLSServer(http.NotFoundHandler())
|
|
defer srv.Close()
|
|
crt := writeCert(t, dir, srv)
|
|
reg := NewClusterRegistry(writeRegistry(t, dir,
|
|
"clusters:\n - id: 2\n portal: 10.0.0.62:443\n cert: "+crt+"\n"),
|
|
"hpc.example.org")
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
host string
|
|
want string
|
|
ok bool
|
|
}{
|
|
{"known cluster", "c002.hpc.example.org", "10.0.0.62:443", true},
|
|
{"host with port", "c002.hpc.example.org:443", "10.0.0.62:443", true},
|
|
{"case is not significant", "C002.HPC.Example.Org", "10.0.0.62:443", true},
|
|
// The control plane's own name must fall through, or publishing
|
|
// tenants would take the portal itself offline.
|
|
{"control plane itself", "hpc.example.org", "", false},
|
|
{"cluster that does not exist", "c009.hpc.example.org", "", false},
|
|
// A label that is not a tenant id is a request for something we do
|
|
// not serve, whoever sent it.
|
|
{"label is not a cluster id", "admin.hpc.example.org", "", false},
|
|
{"deeper label", "a.c002.hpc.example.org", "", false},
|
|
{"another domain entirely", "c002.example.com", "", false},
|
|
// Suffix matching must not accept a domain that merely ends the same
|
|
// way: evil-hpc.example.org is not a subdomain of hpc.example.org.
|
|
{"suffix lookalike", "c002.evilhpc.example.org", "", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, ok := reg.lookup(tc.host)
|
|
if ok != tc.ok || got.addr != tc.want {
|
|
t.Fatalf("lookup(%q) = (%q, %v), want (%q, %v)", tc.host, got.addr, ok, tc.want, tc.ok)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnknownHostFallsThroughToControlPlane(t *testing.T) {
|
|
reg := NewClusterRegistry(writeRegistry(t, t.TempDir(), "clusters: []\n"), "hpc.example.org")
|
|
|
|
reached := false
|
|
h := clusterProxy(reg, nil, nil, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
reached = true
|
|
w.WriteHeader(http.StatusTeapot)
|
|
}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Host = "hpc.example.org"
|
|
h.ServeHTTP(rec, req)
|
|
|
|
if !reached || rec.Code != http.StatusTeapot {
|
|
t.Fatalf("control plane handler not reached: reached=%v code=%d", reached, rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestProxiesKnownTenantToItsPortal(t *testing.T) {
|
|
// Stand in for a tenant's OnDemand, and assert the proxy passes the
|
|
// public Host through: OnDemand builds absolute URLs from it, so a
|
|
// rewritten Host would send every link to an unreachable address.
|
|
var gotHost string
|
|
backend := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotHost = r.Host
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer backend.Close()
|
|
|
|
dir := t.TempDir()
|
|
reg := NewClusterRegistry(writeRegistry(t, dir,
|
|
"clusters:\n - id: 2\n portal: "+backend.Listener.Addr().String()+
|
|
"\n cert: "+writeCert(t, dir, backend)+"\n"),
|
|
"hpc.example.org")
|
|
|
|
h := clusterProxy(reg, nil, nil, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
t.Error("request for a known tenant reached the control plane")
|
|
}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Host = "c002.hpc.example.org"
|
|
h.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want 401 from the tenant portal", rec.Code)
|
|
}
|
|
if gotHost != "c002.hpc.example.org" {
|
|
t.Fatalf("backend saw Host %q, want the public name", gotHost)
|
|
}
|
|
}
|
|
|
|
func TestRegistryPicksUpANewTenantWithoutRestart(t *testing.T) {
|
|
dir := t.TempDir()
|
|
srv := httptest.NewTLSServer(http.NotFoundHandler())
|
|
defer srv.Close()
|
|
crt := writeCert(t, dir, srv)
|
|
p := writeRegistry(t, dir, "clusters: []\n")
|
|
reg := NewClusterRegistry(p, "hpc.example.org")
|
|
|
|
if _, ok := reg.lookup("c002.hpc.example.org"); ok {
|
|
t.Fatal("tenant resolved before it was published")
|
|
}
|
|
|
|
// A tenant provisioned after the control plane started must be reachable
|
|
// without restarting it.
|
|
if err := os.WriteFile(p, []byte("clusters:\n - id: 2\n portal: 10.0.0.62:443\n cert: "+crt+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Force a distinct mtime: the reload is gated on it, and a same-second
|
|
// write would otherwise look unchanged on a coarse filesystem clock.
|
|
if err := os.Chtimes(p, timeLater(), timeLater()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, ok := reg.lookup("c002.hpc.example.org")
|
|
if !ok || got.addr != "10.0.0.62:443" {
|
|
t.Fatalf("after publishing: (%q, %v), want (10.0.0.62:443, true)", got.addr, ok)
|
|
}
|
|
}
|
|
|
|
func timeLater() time.Time { return time.Now().Add(2 * time.Second) }
|
|
|
|
// A cluster that does not exist must say so. Falling through sent it to
|
|
// the control plane, which since it grew a public front page answered
|
|
// with that - so a destroyed cluster looked like a working site at the
|
|
// wrong address.
|
|
func TestAnUnknownClusterSaysSoRatherThanShowingTheFrontPage(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewClusterRegistry(filepath.Join(dir, "clusters.yaml"), "hpc.example")
|
|
|
|
fell := false
|
|
h := clusterProxy(reg, nil, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fell = true
|
|
_, _ = w.Write([]byte("the control plane's own page"))
|
|
}))
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.Host = "c042.hpc.example"
|
|
h.ServeHTTP(w, r)
|
|
|
|
if fell {
|
|
t.Error("an unknown cluster was handed to the control plane")
|
|
}
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("status = %d, want 404", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "c042") {
|
|
t.Errorf("the answer does not name the cluster: %s", w.Body)
|
|
}
|
|
}
|
|
|
|
// Everything that is not a cluster subdomain still belongs to the control
|
|
// plane - including its own hostname and anything else pointed at it.
|
|
func TestOtherHostsStillReachTheControlPlane(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewClusterRegistry(filepath.Join(dir, "clusters.yaml"), "hpc.example")
|
|
|
|
for _, host := range []string{"hpc.example", "www.example.com", "notacluster.hpc.example"} {
|
|
fell := false
|
|
h := clusterProxy(reg, nil, nil, http.HandlerFunc(func(http.ResponseWriter, *http.Request) { fell = true }))
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.Host = host
|
|
h.ServeHTTP(httptest.NewRecorder(), r)
|
|
if !fell {
|
|
t.Errorf("%s did not reach the control plane", host)
|
|
}
|
|
}
|
|
}
|