Publishing worked; everything after it did not. Four things were wrong and each was silent in its own way. Versions were compared as text, so asking for "gromacs" with 2024.9 and 2024.10 published returned 2024.9. The whole meaning of an unversioned reference is "the newest one", and it was a wrong answer rather than a missing one - invisible until somebody noticed they had spent a week on last year's build. A recipe could hold one version while the catalogue could hold many, so an update either destroyed what it replaced or grew a parallel directory sharing nothing with the first. Recipes now carry a list, and the single-version form is read into the same shape, so nothing downstream knows which was written. An image was named by a mutable tag. Whoever repushes gromacs:2024.4 changed what every rebuilt cluster received, silently, and the record meant to freeze the cluster froze only its opinion of it. Two hashes are now recorded because they are checkable in different places: the manifest digest answers "has this moved" from the control plane, and the SIF hash is the only one a head can check with no network and no credential - which matters, because the head is where a wrong image does harm. Pulling by digest is deliberately not done: whether this Apptainer accepts an @sha256: reference is not established, and a provision failing at its last task over untested syntax would be worse than the problem. Module and binaries had been in the catalogue since it was written and were read by nothing - Module was referenced nowhere in the program. So a tenant was handed /opt/apps/gromacs.sif and left to work out Apptainer. They now become a wrapper per binary and an Lmod modulefile, and the answer is `module load gromacs`. Whether to pass --nv is decided on the node at run time: passing it without a driver fails outright, and omitting it where there is one leaves the job on the CPU saying nothing at all, which is the dangerous one. Four states replace `published:`. A withdrawn entry stays in the catalogue file, because dropping it would make it indistinguishable from one that never existed - and that difference is what an operator needs when a cluster is found still holding it. A pinned reference to one is refused with the recorded reason instead of "no application", which would send the user hunting for a typo in a correct name. The portal re-reads the catalogue when it changes, so publishing an application no longer needs a restart - the step that is invisible when forgotten. C20 ships the catalogue, which nothing did. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
227 lines
7.3 KiB
Go
227 lines
7.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/catalog"
|
|
"github.com/ilkermanap/hpcod/internal/model"
|
|
)
|
|
|
|
func authedServer(t *testing.T) (*Server, *authStore) {
|
|
t.Helper()
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("hunter2"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file := filepath.Join(t.TempDir(), "users")
|
|
if err := os.WriteFile(file, []byte("op:"+string(hash)+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a, err := newAuthStore(file, time.Hour, 12*time.Hour)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := &Server{cache: &cache{}, catalog: catalog.Static(nil), store: &fakeStore{}, auth: a}
|
|
s.cache.set([]model.Site{{ID: "hv01", Reachable: true, Healthy: true}})
|
|
tmpl, err := parseTemplates()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.tmpl = tmpl
|
|
return s, a
|
|
}
|
|
|
|
func (s *Server) testHandler() http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/healthz", s.healthz)
|
|
mux.HandleFunc("/v1/sites", s.jsonSites)
|
|
mux.HandleFunc("/v1/clusters", s.jsonClusters)
|
|
mux.HandleFunc("/clusters/new", s.pagePlan)
|
|
mux.HandleFunc("/capmap", s.pageCapMap)
|
|
mux.HandleFunc("/clusters", s.pageClusters)
|
|
mux.HandleFunc("/sites", s.pageSites)
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
s.pageFront(w, r)
|
|
})
|
|
return s.auth.middleware(mux, []byte("<!doctype html><form></form>"), "")
|
|
}
|
|
|
|
// The front door is the only public route. This is the test that matters:
|
|
// if a later change makes something else public, it fails here rather than
|
|
// in the open.
|
|
func TestOnlyTheFrontDoorIsPublic(t *testing.T) {
|
|
s, _ := authedServer(t)
|
|
h := s.testHandler()
|
|
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("/ = %d, want 200 for a visitor", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "A cluster of your own") {
|
|
t.Errorf("/ did not serve the front door to a visitor")
|
|
}
|
|
|
|
for _, path := range []string{"/clusters", "/sites", "/clusters/new", "/capmap", "/no-such-page"} {
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if w.Code != http.StatusSeeOther {
|
|
t.Errorf("%s = %d without a session, want 303 to the login", path, w.Code)
|
|
}
|
|
}
|
|
for _, path := range []string{"/v1/sites", "/v1/clusters"} {
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s = %d without a session, want 401", path, w.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Whatever else is true of the public page, it must not describe the
|
|
// estate. Anyone on the internet can read it.
|
|
func TestTheFrontDoorLeaksNothing(t *testing.T) {
|
|
s, _ := authedServer(t)
|
|
s.cache.set([]model.Site{{
|
|
ID: "hv01", Name: "Lab cluster", Reachable: true, Healthy: true,
|
|
Nodes: []model.Node{{
|
|
Name: "hv01", Online: true, Cores: 168, MemBytes: 655 << 30,
|
|
CPUModel: "Intel(R) Xeon(R) CPU E5-2680 v4",
|
|
Devices: []model.Device{{
|
|
NativeID: "0000:01:00.0", Model: "Quadro K620",
|
|
Capability: model.CapGPU, Confidence: model.ConfClass, Passthrough: true,
|
|
}},
|
|
}},
|
|
}})
|
|
|
|
w := httptest.NewRecorder()
|
|
s.testHandler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
body := w.Body.String()
|
|
|
|
// Matched on word boundaries. A bare substring search fails on
|
|
// innocent words - "label" in a stylesheet contains "lab" - and a test
|
|
// that cries wolf gets weakened until it stops catching anything.
|
|
for _, secret := range []string{
|
|
"hv01", "Quadro", "0000:01:00\\.0", "Xeon", "168", "655",
|
|
"c001", "lab", "10\\.0\\.0", "oversubscribed",
|
|
} {
|
|
re := regexp.MustCompile(`\b` + secret + `\b`)
|
|
if loc := re.FindStringIndex(body); loc != nil {
|
|
from := max(0, loc[0]-40)
|
|
to := min(len(body), loc[1]+40)
|
|
t.Errorf("the public page mentions %q: ...%s...", secret, body[from:to])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Someone already inside should not be shown a page inviting them in.
|
|
func TestASignedInOperatorGetsTheOverview(t *testing.T) {
|
|
s, a := authedServer(t)
|
|
tok := a.newSession("op", false, "127.0.0.1")
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: tok})
|
|
w := httptest.NewRecorder()
|
|
s.testHandler().ServeHTTP(w, r)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("/ = %d for a signed-in operator", w.Code)
|
|
}
|
|
body := w.Body.String()
|
|
if strings.Contains(body, "A cluster of your own") {
|
|
t.Error("an operator was served the visitor's page")
|
|
}
|
|
if !strings.Contains(body, "sites healthy") {
|
|
t.Error("the overview did not render")
|
|
}
|
|
}
|
|
|
|
// With no login configured there is nobody to be signed in, so the
|
|
// operator's own view has to be what "/" shows - otherwise a loopback
|
|
// portal would offer nothing but an invitation.
|
|
func TestWithoutAuthTheFrontDoorIsTheOverview(t *testing.T) {
|
|
s := testServerWithPages(t, &fakeStore{})
|
|
w := httptest.NewRecorder()
|
|
s.pageFront(w, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
if strings.Contains(w.Body.String(), "A cluster of your own") {
|
|
t.Error("a portal with no login served the visitor's page")
|
|
}
|
|
if !strings.Contains(w.Body.String(), "sites healthy") {
|
|
t.Error("the overview did not render")
|
|
}
|
|
}
|
|
|
|
// The session has to reach the cluster subdomains, because that is where
|
|
// the cluster portals now get their authentication from. A host-only
|
|
// cookie is never sent to c001.hpc.example, so clicking through from the
|
|
// clusters page lands on the login screen - which is exactly what
|
|
// happened, and what curl with a hand-set header does not reproduce.
|
|
func TestTheSessionCookieCoversClusterSubdomains(t *testing.T) {
|
|
s, a := authedServer(t)
|
|
a.cookieDomain = "hpc.example"
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader("username=op&password=hunter2"))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
a.handleLogin(w, r, []byte("login"), "")
|
|
|
|
var session *http.Cookie
|
|
for _, c := range w.Result().Cookies() {
|
|
if c.Name == sessionCookie {
|
|
session = c
|
|
}
|
|
}
|
|
if session == nil {
|
|
t.Fatalf("no session cookie was set; status %d", w.Code)
|
|
}
|
|
if session.Domain != "hpc.example" {
|
|
t.Errorf("cookie domain = %q, want hpc.example - a host-only cookie never reaches a cluster", session.Domain)
|
|
}
|
|
if !session.HttpOnly || session.SameSite != http.SameSiteStrictMode {
|
|
t.Errorf("widening the domain must not have loosened anything else: %+v", session)
|
|
}
|
|
_ = s
|
|
}
|
|
|
|
// Signing out has to name the same domain, or the browser keeps the
|
|
// original cookie and the session survives the logout.
|
|
func TestSigningOutClearsTheSameCookie(t *testing.T) {
|
|
_, a := authedServer(t)
|
|
a.cookieDomain = "hpc.example"
|
|
tok := a.newSession("op", false, "127.0.0.1")
|
|
|
|
h := a.middleware(http.NotFoundHandler(), []byte("login"), "")
|
|
r := httptest.NewRequest(http.MethodGet, "/logout", nil)
|
|
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: tok})
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
|
|
for _, c := range w.Result().Cookies() {
|
|
if c.Name == sessionCookie {
|
|
if c.Domain != "hpc.example" {
|
|
t.Errorf("the cleared cookie names %q; the browser keeps the original", c.Domain)
|
|
}
|
|
if c.MaxAge >= 0 {
|
|
t.Errorf("the cookie was not expired: MaxAge=%d", c.MaxAge)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
t.Error("signing out set no cookie at all")
|
|
}
|