hub (unreleased): the hub keeps and shows which boxes were accepted on a small system disk (D83, R-1398 part 2)
gates / gates (push) Successful in 3m40s

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 16:01:46 +01:00
1 parent 8378dc446b
commit c3fa43a486
10 files changed
+407 -1

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
func enrollBody(h http.Handler, body, pass string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/api/v1/host-enroll", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
if pass != "" {
req.Header.Set("X-Retrieval-Password", pass)
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
return rr
}
// D83 (R-1398 part 2): an install that went on with --accept-small-disk says so at its enrolment, and the hub keeps
// it for the box's page — at the first enrolment and at a reinstall over the same record. An ordinary enrolment, and
// an older installer, mark nothing.
func TestD83_EnrolmentStoresTheSmallDiskMark(t *testing.T) {
h, st, _ := newTestHandler(t)
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "k1", RetrievalPassword: "pass-phrase"})
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c2", APIKey: "k2", RetrievalPassword: "pass-phrase"})
rr := enrollBody(h, `{"customer_id":"c1","small_disk":true}`, "pass-phrase") // what installer 1.38.0 sends
if rr.Code != http.StatusCreated {
t.Fatalf("status %d: %s", rr.Code, rr.Body.String())
}
var got enrollResp
_ = json.Unmarshal(rr.Body.Bytes(), &got)
sd := st.SmallDiskOf(got.HostID)
if sd == nil || sd.Bytes != 0 || sd.NotedAt.IsZero() {
t.Fatalf("the hub did not keep the mark of %s: %+v", got.HostID, sd)
}
// a reinstall over the same record, now with the size (a later installer): the size is kept
rr = enrollBody(h, `{"customer_id":"c1","small_disk":true,"system_disk_bytes":128035676160}`, "pass-phrase")
if rr.Code != http.StatusOK {
t.Fatalf("reuse status %d", rr.Code)
}
if sd := st.SmallDiskOf(got.HostID); sd == nil || sd.Bytes != 128035676160 {
t.Fatalf("the size of the second enrolment was not kept: %+v", sd)
}
// the control: an ordinary enrolment marks nothing
rr = enrollBody(h, `{"customer_id":"c2"}`, "pass-phrase")
var other enrollResp
_ = json.Unmarshal(rr.Body.Bytes(), &other)
if rr.Code != http.StatusCreated || st.SmallDiskOf(other.HostID) != nil {
t.Fatalf("an ordinary enrolment (status %d) was marked small: %+v", rr.Code, st.SmallDiskOf(other.HostID))
}
}
// What a box sends is a box's word: only the JSON `true` is the flag, a size that is not a whole number is „not
// told”, and a request that is REFUSED marks nothing.
func TestD83_OnlyTrueIsTheFlag_AndARefusedRequestMarksNothing(t *testing.T) {
h, st, _ := newTestHandler(t)
for i, body := range []string{
`{"customer_id":"c%d","small_disk":"true"}`, `{"customer_id":"c%d","small_disk":1}`,
`{"customer_id":"c%d","small_disk":false}`, `{"customer_id":"c%d","small_disk":null}`,
`{"customer_id":"c%d","small_disk":{"a":true}}`,
} {
cid := "c" + string(rune('a'+i))
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: cid, APIKey: "k" + cid, RetrievalPassword: "pass-phrase"})
rr := enrollBody(h, strings.Replace(body, "c%d", cid, 1), "pass-phrase")
var got enrollResp
_ = json.Unmarshal(rr.Body.Bytes(), &got)
if rr.Code != http.StatusCreated {
t.Fatalf("%s: status %d — the enrolment itself must not fail on a strange flag", body, rr.Code)
}
if st.SmallDiskOf(got.HostID) != nil {
t.Fatalf("%s was read as the flag", body)
}
}
// a size that is not a whole number: the flag stands, the size is „not told”
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "cz", APIKey: "kz", RetrievalPassword: "pass-phrase"})
rr := enrollBody(h, `{"customer_id":"cz","small_disk":true,"system_disk_bytes":"<b>big</b>"}`, "pass-phrase")
var got enrollResp
_ = json.Unmarshal(rr.Body.Bytes(), &got)
if sd := st.SmallDiskOf(got.HostID); rr.Code != http.StatusCreated || sd == nil || sd.Bytes != 0 {
t.Fatalf("status %d, mark %+v", rr.Code, sd)
}
// a wrong passphrase: refused, and nothing is marked for any box of that household
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "cw", APIKey: "kw", RetrievalPassword: "pass-phrase"})
if rr := enrollBody(h, `{"customer_id":"cw","small_disk":true}`, "wrong"); rr.Code != http.StatusUnauthorized {
t.Fatalf("status %d, want 401", rr.Code)
}
if n := len(st.OSAlarmKeys("small_disk:cw")); n != 0 {
t.Fatalf("a refused enrolment left %d mark(s)", n)
}
}
+34
View File
@@ -1264,11 +1264,29 @@ func (h *Handler) handleHostEnroll(w http.ResponseWriter, r *http.Request) {
}
var req struct {
CustomerID string `json:"customer_id"`
// D83 (R-1398 part 2): an install that went on with --accept-small-disk says so; a later installer may add
// the size. An older installer sends neither. Anything but the JSON `true` is „not said”.
SmallDisk json.RawMessage `json:"small_disk"`
SystemDiskBytes json.RawMessage `json:"system_disk_bytes"`
}
if err := json.Unmarshal(body, &req); err != nil || req.CustomerID == "" {
http.Error(w, "Invalid payload: customer_id required", http.StatusBadRequest)
return
}
// noteSmallDisk is called only where the enrolment is ANSWERED with a host id (after the passphrase check): a
// request that is refused marks nothing.
noteSmallDisk := func(hostID string) {
if string(req.SmallDisk) != "true" {
return
}
var n int64
_ = json.Unmarshal(req.SystemDiskBytes, &n) // not a whole number → 0 = „not told”
if err := h.store.NoteSmallDisk(hostID, n, time.Now().UTC()); err != nil {
h.logger.Printf("[WARN] host-enroll: the small-disk mark of %s was not stored: %v", hostID, err)
return
}
h.logger.Printf("[WARN] host-enroll: %s was installed with --accept-small-disk (a system disk below 200 GB); shown on the box's page", hostID)
}
// Passphrase auth — mirrors handleConfigRetrieve exactly (header, 404-then-401 order,
// constant-time compare). Happens BEFORE any mint.
@@ -1322,6 +1340,7 @@ func (h *Handler) handleHostEnroll(w http.ResponseWriter, r *http.Request) {
h.refusePassphraseKeys(w, r, req.CustomerID, "POST /host-enroll", why)
return
}
noteSmallDisk(existing.HostID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"host_id": existing.HostID, "api_key": existing.APIKey})
@@ -1356,6 +1375,7 @@ func (h *Handler) handleHostEnroll(w http.ResponseWriter, r *http.Request) {
// customer (first provision). Also re-issue the off-site login (F3) — since hub v0.127.0 that is the
// HUB's own Storage Box login, not something the fresh box receives (R-1158; see offsiteReissuer).
h.reissueOnReenroll(cc)
noteSmallDisk(hostID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
@@ -3682,6 +3702,9 @@ func (h *Handler) noteAppList(customerID string, body []byte) {
Deployed []string `json:"deployed"`
} `json:"stacks"`
Offsite *struct {
// Enabled: the report shows the off-site tier switched on. Only then is an app that is missing from
// ended_apps a take-back (store.NoteEndNotices).
Enabled bool `json:"enabled"`
EndedApps []struct {
App string `json:"app"`
// The box's own dates. Carried on the wire for the operator's reading of a stored report; the
@@ -3695,11 +3718,22 @@ func (h *Handler) noteAppList(customerID string, body []byte) {
return
}
var claimed []string
var presses []store.EndClaim
if p.Offsite != nil {
for _, e := range p.Offsite.EndedApps {
claimed = append(claimed, e.App)
presses = append(presses, store.EndClaim{App: e.App, Press: e.RemovedAt}) // the press's KEY, never a date
}
}
// The notice lock (`09` §3 decision 315): the hub's own record of who must be told, and the sends (end_notice.go).
// After the app record below, whatever it returns — a notice must not wait on another table's write.
defer func() {
if nerr := h.store.NoteEndNotices(customerID, p.Stacks.Deployed, presses, p.Offsite != nil && p.Offsite.Enabled, time.Now()); nerr != nil {
h.logger.Printf("[WARN] [api] report from %s: the hub's own notice record could not be updated: %v", customerID, nerr)
return
}
h.kickEndNotices(customerID)
}()
unseen, err := h.store.NoteAppListClaims(customerID, p.Stacks.Deployed, claimed, time.Now())
if err != nil {
h.logger.Printf("[WARN] [api] report from %s: the hub's own app record could not be updated: %v", customerID, err)