fix: close record repair and verification gaps

This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 9251c85fd8
commit adff599e08
27 files changed
+787 -153

No files matched your search

+148
View File
@@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
@@ -92,6 +93,94 @@ func (it itZoneAPI) audit(t *testing.T, operation string, want int) []auditRow {
return rows
}
func TestIntegrationUnsignedRecordIDs(t *testing.T) {
const firstID, lastID = int64(2147483648), int64(4294967295)
d := newITDatabase(t, itOptions{prefix: "uint_ids_", recordDDL: strings.Replace(itRecordsDDL, "`id` INT", "`id` INT UNSIGNED", 1)})
db, tables := d.migrated(t)
server := newWebServer(d.config(t), discardLogger(), db, tables, func() time.Time { return webNow })
it := itHTTP{database: d, server: server, handler: server.Handler()}
admin := it.createUser(t, "alice", roleAdmin)
token := it.token(t, admin)
do := func(method, path, etag, body string, status int) *httptest.ResponseRecorder {
t.Helper()
res := it.do(t.Context(), webRequest{method: method, path: apiPrefix + path, token: token, body: body,
header: map[string]string{"If-Match": etag}})
if res.Code != status {
t.Fatalf("%s %s: got %d, want %d, body %s", method, path, res.Code, status, res.Body)
}
return res
}
assertRecord := func(res *httptest.ResponseRecorder, id int64) string {
t.Helper()
var body struct {
Record zoneAPIRecord `json:"record"`
}
if err := json.Unmarshal(res.Body.Bytes(), &body); err != nil || body.Record.ID != id || body.Record.ETag == "" || body.Record.ETag != res.Header().Get("ETag") {
t.Fatalf("record ID or ETag mismatch, want ID %d, got %s, error %v", id, res.Body, err)
}
return body.Record.ETag
}
do(http.MethodPost, "/zones", "", zoneAPITestCreate, http.StatusCreated)
store := newRecordStore(db, tables)
rows, err := store.ZoneRows(t.Context(), zoneAPITestName)
if err != nil || len(rows) != 3 {
t.Fatalf("zone creation: %d rows, error %v", len(rows), err)
}
soaID := zoneAPIFindSOA(rows).ID
path := "/zones/" + zoneAPITestName + "/records"
firstPath := path + "/" + strconv.FormatInt(firstID, 10)
d.rootExec(t, tables.sql("ALTER TABLE {records} AUTO_INCREMENT = 2147483648"))
etag := assertRecord(do(http.MethodPost, path, "", zoneAPITestRecord, http.StatusCreated), firstID)
newRecord := strings.Replace(zoneAPITestRecord, `"ttl":60`, `"ttl":120`, 1)
newETag := assertRecord(do(http.MethodPut, firstPath, etag, newRecord, http.StatusOK), firstID)
if newETag == etag {
t.Fatal("record update kept the original ETag")
}
do(http.MethodDelete, firstPath, newETag, "", http.StatusOK)
d.rootExec(t, tables.sql("ALTER TABLE {records} AUTO_INCREMENT = 4294967295"))
etag = assertRecord(do(http.MethodPost, path, "", zoneAPITestRecord, http.StatusCreated), lastID)
bulk := fmt.Sprintf(`{"records":[{"id":%d,"etag":%q}]}`, lastID, etag)
do(http.MethodPost, path+"/delete", "", bulk, http.StatusOK)
do(http.MethodPut, path+"/4294967296", etag, zoneAPITestRecord, http.StatusBadRequest)
do(http.MethodPost, path+"/delete", "", strings.Replace(bulk, "4294967295", "4294967296", 1), http.StatusUnprocessableEntity)
for _, id := range []int64{firstID, lastID} {
if _, err := store.RecordByID(t.Context(), id); !errors.Is(err, errRowNotFound) {
t.Errorf("deleted record %d: %v", id, err)
}
}
audit, err := newAuditStore(db, tables).LatestAudit(t.Context(), 9)
want := []struct {
op string
status int
id int64
}{
{opZoneCreate, 201, 0}, {opRecordCreate, 201, firstID}, {opRecordUpdate, 200, firstID}, {opRecordDelete, 200, firstID},
{opRecordCreate, 201, lastID}, {opRecordBulkDelete, 200, 0}, {opRecordUpdate, 400, 0}, {opRecordBulkDelete, 422, 0},
}
if err != nil || len(audit) != len(want) {
t.Fatalf("audit: got %d rows, want %d, error %v", len(audit), len(want), err)
}
for i, entry := range want {
row := audit[i]
assertITHTTPAudit(t, row, entry.op, entry.status, admin)
if row.RecordID != (sql.NullInt64{Int64: entry.id, Valid: entry.id != 0}) {
t.Fatalf("%s audit: got ID %+v, want %d", entry.op, row.RecordID, entry.id)
}
}
zoneAPITestAuditIDs(t, audit[5].Before, []int64{soaID, lastID})
zoneAPITestAuditIDs(t, audit[5].After, []int64{soaID})
var apiAudit auditResponse
res := do(http.MethodGet, "/audit?operation=record_create", "", "", http.StatusOK)
if err := json.Unmarshal(res.Body.Bytes(), &apiAudit); err != nil || len(apiAudit.Entries) != 2 {
t.Fatalf("API audit: %s, error %v", res.Body, err)
}
for i, id := range []int64{lastID, firstID} {
if got := apiAudit.Entries[i].RecordID; got == nil || *got != id {
t.Fatalf("API audit record ID at %d: %v, want %d", i, got, id)
}
}
}
func TestIntegrationZoneAPIHandlers(t *testing.T) {
it := newITZoneAPI(t)
admin, user := it.admin, it.user
@@ -396,6 +485,65 @@ func TestIntegrationZoneAPIRepairs(t *testing.T) {
}
}
func TestIntegrationUndottedZoneDelegation(t *testing.T) {
it := newITHTTP(t)
admin := it.createUser(t, "admin", roleAdmin)
user := it.createUser(t, "member", roleUser)
adminToken, userToken := it.token(t, admin), it.token(t, user)
zonePath := "/zones/" + zoneAPITestName
request := func(token, method, path, body, etag string, status int) []byte {
t.Helper()
r := it.do(t.Context(), webRequest{token: token, method: method, path: apiPrefix + path, body: body,
header: map[string]string{"If-Match": etag}})
if r.Code != status {
t.Fatalf("%s %s: status %d, want %d, body %s", method, path, r.Code, status, r.Body)
}
return r.Body.Bytes()
}
request(adminToken, http.MethodPost, "/zones", zoneAPITestCreate, "", http.StatusCreated)
request(adminToken, http.MethodPost, zonePath+"/records", zoneAPITestRecord, "", http.StatusCreated)
request(adminToken, http.MethodPut, zonePath+"/users/member", "", "", http.StatusNoContent)
ctx, tables := t.Context(), it.server.tables
const missing = "missing.example.net."
if _, err := newDelegationStore(it.server.db, tables).GrantDelegation(ctx, missing, user.ID, admin.ID, webNow); err != nil {
t.Fatal(err)
}
undotted := strings.TrimSuffix(zoneAPITestName, ".")
it.database.rootExec(t, tables.sql("UPDATE {records} SET `zone` = ? WHERE `zone` = ?"), undotted, zoneAPITestName)
if n := it.database.count(t, tables.sql("SELECT COUNT(*) FROM {records} WHERE `zone` = ?"), zoneAPITestName); n != 0 {
t.Fatalf("fixture still has %d canonical rows", n)
}
var list struct {
Zones []zoneAPIListItem `json:"zones"`
}
if err := json.Unmarshal(request(userToken, http.MethodGet, "/zones", "", "", http.StatusOK), &list); err != nil ||
len(list.Zones) != 1 || list.Zones[0].Name != zoneAPITestName {
t.Fatalf("delegated zone list = %+v, error %v", list.Zones, err)
}
var account accountResponse
if err := json.Unmarshal(request(userToken, http.MethodGet, "/account", "", "", http.StatusOK), &account); err != nil ||
len(account.Zones) != 1 || account.Zones[0] != zoneAPITestName {
t.Fatalf("delegated account zones = %v, error %v", account.Zones, err)
}
var detail userDetail
if err := json.Unmarshal(request(adminToken, http.MethodGet, "/users/member", "", "", http.StatusOK), &detail); err != nil ||
len(detail.Zones) != 2 || detail.Zones[0] != (userZoneItem{Zone: zoneAPITestName, Exists: true}) ||
detail.Zones[1] != (userZoneItem{Zone: missing}) {
t.Fatalf("admin's view of delegated zones = %+v, error %v", detail.Zones, err)
}
records := newRecordStore(it.server.db, tables)
rows, err := records.ZoneRows(ctx, zoneAPITestName)
if err != nil || len(rows) != 4 {
t.Fatalf("zone rows = %+v, error %v", rows, err)
}
record := rows[len(rows)-1]
request(userToken, http.MethodPut, zonePath+"/records/"+strconv.FormatInt(record.ID, 10), zoneAPITestRecord, recordETag(record), http.StatusOK)
repaired, err := records.RecordByID(ctx, record.ID)
if err != nil || repaired.Zone != zoneAPITestName {
t.Fatalf("record not repaired: %+v, error %v", repaired, err)
}
}
func TestIntegrationZoneAPIAuditRollback(t *testing.T) {
it := newITZoneAPI(t)
it.call(t, it.admin, "zone_create", "", "", zoneAPITestCreate, 201)