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>
130 lines
4.2 KiB
Go
130 lines
4.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A certificate that belongs to nobody in this test, used to prove the pin
|
|
// rejects. It cannot come from another httptest server: those all present
|
|
// the same built-in certificate, so pinning one and connecting to another
|
|
// would pass for the wrong reason.
|
|
func unrelatedCertPEM(t *testing.T) []byte {
|
|
t.Helper()
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{CommonName: "not-this-tenant"},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().Add(time.Hour),
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
|
|
}
|
|
|
|
func TestWrongCertificateIsRejected(t *testing.T) {
|
|
dir := t.TempDir()
|
|
backend := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer backend.Close()
|
|
|
|
crt := filepath.Join(dir, "wrong.crt")
|
|
if err := os.WriteFile(crt, unrelatedCertPEM(t), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
reg := NewClusterRegistry(writeRegistry(t, dir,
|
|
"clusters:\n - id: 2\n portal: "+backend.Listener.Addr().String()+
|
|
"\n cert: "+crt+"\n"), "hpc.example.org")
|
|
|
|
h := clusterProxy(reg, nil, nil, http.NotFoundHandler())
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Host = "c002.hpc.example.org"
|
|
h.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusBadGateway {
|
|
t.Fatalf("status = %d, want 502: a portal presenting an unpinned certificate must not be proxied", rec.Code)
|
|
}
|
|
}
|
|
|
|
// A tenant whose certificate file is missing must not be published at all -
|
|
// proxying to an address we cannot authenticate is worse than not serving it.
|
|
func TestTenantWithoutCertificateIsNotPublished(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewClusterRegistry(writeRegistry(t, dir,
|
|
"clusters:\n - id: 2\n portal: 10.0.0.62:443\n cert: /nonexistent\n"),
|
|
"hpc.example.org")
|
|
|
|
if _, ok := reg.lookup("c002.hpc.example.org"); ok {
|
|
t.Fatal("tenant published despite having no usable certificate")
|
|
}
|
|
}
|
|
|
|
// A tenant that rotates its certificate must become reachable again without
|
|
// restarting the control plane. The first version cached transports on the
|
|
// address alone, so the old trust pool outlived the certificate and the
|
|
// tenant stayed unreachable until a restart - found in production, not here.
|
|
func TestRotatedCertificateIsPickedUpWithoutRestart(t *testing.T) {
|
|
dir := t.TempDir()
|
|
backend := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer backend.Close()
|
|
|
|
crt := filepath.Join(dir, "tenant.crt")
|
|
// Start with the wrong certificate: the proxy must refuse.
|
|
if err := os.WriteFile(crt, unrelatedCertPEM(t), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg := NewClusterRegistry(writeRegistry(t, dir,
|
|
"clusters:\n - id: 2\n portal: "+backend.Listener.Addr().String()+
|
|
"\n cert: "+crt+"\n"), "hpc.example.org")
|
|
h := clusterProxy(reg, nil, nil, http.NotFoundHandler())
|
|
|
|
call := func() int {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Host = "c002.hpc.example.org"
|
|
h.ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
|
|
if got := call(); got != http.StatusBadGateway {
|
|
t.Fatalf("with the wrong certificate: %d, want 502", got)
|
|
}
|
|
|
|
// Now publish the right one, as a rotation would.
|
|
real := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: backend.Certificate().Raw})
|
|
if err := os.WriteFile(crt, real, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reload := filepath.Join(dir, "tenants.yaml")
|
|
later := time.Now().Add(2 * time.Second)
|
|
if err := os.Chtimes(reload, later, later); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if got := call(); got != http.StatusUnauthorized {
|
|
t.Fatalf("after rotation: %d, want 401 - the new certificate was not picked up", got)
|
|
}
|
|
}
|