fix: serialize delegations and repair verification

This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 99fa5de747
commit 5f17aab6ca
8 files changed
+88 -32

No files matched your search

+2 -2
View File
@@ -183,7 +183,7 @@ func TestPasswordRules(t *testing.T) {
"long-enough-password\n": "password must not contain control characters",
"long-enough-password\u007f": "password must not contain control characters",
"shortä": "password must be 12 to 256 bytes",
"​zero-width-is-printable-ok": "",
"\u200bzero-width-is-printable-ok": "",
}
for p, want := range cases {
err := checkPasswordRules(p)
@@ -300,7 +300,7 @@ func TestPrintableText(t *testing.T) {
{"a\x00b\nc\td", 64, "a b c d"},
{"bad\xffbyte", 64, "bad byte"},
{"keep�replacement", 64, "keep�replacement"},
{"‮evil", 64, " evil"},
{"\u202eevil", 64, " evil"},
{strings.Repeat("é", 70), 64, strings.Repeat("é", 64)},
{"abc", 0, ""},
{"abc", -1, "abc"},
+2 -2
View File
@@ -241,7 +241,7 @@ func handleDelegationGrant(ctx context.Context, c *apiContext) (apiResponse, err
if err != nil {
return apiResponse{}, err
}
err = c.Server.store.InTx(ctx, func(tx webTx) error {
err = c.Server.store.InZoneTx(ctx, c.Zone, c.Server.log, func(tx webTx) error {
if err := requireZone(ctx, tx.ZoneExists, c.Zone); err != nil {
return err
}
@@ -274,7 +274,7 @@ func handleDelegationRevoke(ctx context.Context, c *apiContext) (apiResponse, er
if err != nil {
return apiResponse{}, err
}
err = c.Server.store.InTx(ctx, func(tx webTx) error {
err = c.Server.store.InZoneTx(ctx, c.Zone, c.Server.log, func(tx webTx) error {
u, err := activeTargetForUpdate(ctx, c, tx, username)
if err != nil {
return err
+6
View File
@@ -192,6 +192,9 @@ func TestDelegationGrant(t *testing.T) {
if res.status != 204 || !h.store.hasDelegation(testZone, bob.ID) {
t.Fatalf("%d %s", res.status, res.body)
}
if len(h.store.zoneLocks) != 1 || h.store.zoneLocks[0] != testZone {
t.Fatalf("grant locks = %v", h.store.zoneLocks)
}
e := expectChange(t, res, opDelegationGrant, 204, bob.ID, "", `{"zone":"example.org.","username":"bob"}`)
if e.Zone.String != testZone {
t.Fatal(e.Zone)
@@ -237,6 +240,9 @@ func TestDelegationRevoke(t *testing.T) {
if res.status != 204 || h.store.hasDelegation(testZone, carol.ID) {
t.Fatalf("%d %s", res.status, res.body)
}
if len(h.store.zoneLocks) != 1 || h.store.zoneLocks[0] != testZone {
t.Fatalf("revoke locks = %v", h.store.zoneLocks)
}
expectChange(t, res, opDelegationRevoke, 204, carol.ID, `{"zone":"example.org.","username":"carol"}`, "")
res = h.api("DELETE", "/zones/example.org/users/carol", token, "")
expectError(t, res, 404, `{"error":"delegation not found"}`)
+1
View File
@@ -260,6 +260,7 @@ func (d *itDatabase) cli(t *testing.T, stdin string, now time.Time) *itCLI {
listen: nil,
openDB: openDatabase,
newUserBackend: newSQLUserBackend,
newReadBackend: newSQLReadBackend,
pingBackoff: defaultPingBackoff(),
healthClient: newHealthClient(),
}
+49
View File
@@ -443,3 +443,52 @@ func TestIntegrationZoneAPIAuthorizationRace(t *testing.T) {
}
}
}
func TestIntegrationDelegationZoneLock(t *testing.T) {
for _, operation := range []string{opDelegationGrant, opDelegationRevoke} {
t.Run(operation, func(t *testing.T) {
it := newITZoneAPI(t)
it.server.store = newSQLWebStore(it.server.db, it.server.tables)
it.call(t, it.admin, "zone_create", "", "", zoneAPITestCreate, 201)
if operation == opDelegationRevoke {
it.grant(t, zoneAPITestName)
}
c := it.request(it.admin, operation, "", "", "")
c.Vars["username"] = it.user.Username
result := make(chan error, 1)
err := withZoneLock(t.Context(), it.server.db, it.server.tables, zoneAPITestName, it.server.log, func(conn *sql.Conn) error {
go func() {
var err error
if operation == opDelegationGrant {
_, err = handleDelegationGrant(t.Context(), c)
} else {
_, err = handleDelegationRevoke(t.Context(), c)
}
result <- err
}()
select {
case err := <-result:
return fmt.Errorf("delegation did not wait for zone lock: %v", err)
case <-time.After(200 * time.Millisecond):
}
return runInTx(t.Context(), conn, func(tx *sql.Tx) error {
if _, err := newDelegationStore(tx, it.server.tables).DeleteZoneDelegations(t.Context(), zoneAPITestName); err != nil {
return err
}
_, err := newRecordStore(tx, it.server.tables).DeleteZoneRecords(t.Context(), zoneAPITestName)
return err
})
})
if err != nil {
t.Fatal(err)
}
var apiErr *apiError
if err := <-result; !errors.As(err, &apiErr) || apiErr.Status != http.StatusNotFound {
t.Fatalf("delegation after delete: %v", err)
}
if exists, err := it.delegations.HasDelegation(t.Context(), zoneAPITestName, it.user.ID); err != nil || exists {
t.Fatalf("orphan delegation: %v, %v", exists, err)
}
})
}
}
+9
View File
@@ -130,6 +130,7 @@ type webStore interface {
ZoneExists(ctx context.Context, zone string) (bool, error)
ListAudit(ctx context.Context, f auditFilter) ([]auditRow, error)
InTx(ctx context.Context, fn func(tx webTx) error) error
InZoneTx(ctx context.Context, zone string, log *slog.Logger, fn func(tx webTx) error) error
}
type sqlWebTx struct {
@@ -166,6 +167,14 @@ func (s *sqlWebStore) InTx(ctx context.Context, fn func(tx webTx) error) error {
})
}
func (s *sqlWebStore) InZoneTx(ctx context.Context, zone string, log *slog.Logger, fn func(tx webTx) error) error {
return withZoneLock(ctx, s.db, s.tables, zone, log, func(conn *sql.Conn) error {
return runInTx(ctx, conn, func(tx *sql.Tx) error {
return fn(newSQLWebTx(tx, s.tables))
})
})
}
func (c *apiContext) writeChangeAudit(ctx context.Context, tx *sql.Tx, status int) error {
return c.writeChangeAuditWith(ctx, newAuditStore(tx, c.Server.tables), status)
}
+10 -10
View File
@@ -13,7 +13,7 @@ version=$2
commit=$3
probe=${PROBE_IMAGE:-golang:1.27-alpine}
mysql_image=${MYSQL_IMAGE:-mysql:8.4}
name="cdwu-img-${IMG_ID:-$$}"
name="${IMG_NAME:-cdwu-img-${IMG_ID:-$$}}"
network="$name"
mysql="${name}-mysql"
server="${name}-app"
@@ -34,7 +34,7 @@ cleanup() {
fi
done
fi
docker rm --force --volumes "$server" "$mysql" "$exported" >/dev/null 2>&1 || true
docker rm --force --volumes "$server" "$mysql" "$exported" "${name}-probe" >/dev/null 2>&1 || true
docker network rm "$network" >/dev/null 2>&1 || true
rm -rf "$tmp"
exit "$status"
@@ -113,7 +113,7 @@ ok "OCI labels"
[ "$(docker image inspect --format '{{json .Config.Healthcheck.Test}}' "$image")" = '["CMD","/app","healthcheck"]' ] || fail "image healthcheck is not /app healthcheck"
ok "image user, entrypoint and healthcheck"
capture docker run --rm --entrypoint /bin/sh "$image" -c true
capture docker run --rm --name "${name}-probe" --entrypoint /bin/sh "$image" -c true
[ "$rc" -ne 0 ] || fail "the image runs /bin/sh"
docker create --name "$exported" "$image" >/dev/null
docker export "$exported" | tar -t >"$tmp/files"
@@ -129,23 +129,23 @@ grep -qx 'app' "$tmp/files" || fail "/app missing from the image filesystem"
grep -qx 'etc/ssl/cert.pem' "$tmp/files" || fail "CA bundle missing from the image filesystem"
ok "image filesystem has no shell"
capture docker run --rm "$image"
capture docker run --rm --name "${name}-probe" "$image"
expect_rc "start without configuration" 2
ok "exit 2 without configuration"
capture docker run --rm "$image" version
capture docker run --rm --name "${name}-probe" "$image" version
expect_rc "version" 0
[ "$(cat "$tmp/out")" = "coredns-web-ui $version $commit" ] || fail "version output is not 'coredns-web-ui $version $commit'"
ok "version output"
capture docker run --rm "$image" help
capture docker run --rm --name "${name}-probe" "$image" help
expect_rc "help" 0
cmp -s "$tmp/out" "$tmp/usage" || fail "help output differs from the usage text"
[ ! -s "$tmp/err" ] || fail "help wrote to stderr"
ok "help output"
for args in "bogus" "help extra"; do
capture docker run --rm "$image" $args
capture docker run --rm --name "${name}-probe" "$image" $args
expect_rc "usage error ($args)" 2
[ ! -s "$tmp/out" ] || fail "usage error ($args) wrote to stdout"
head -n 1 "$tmp/err" | grep -q '^coredns-web-ui: usage error: ' || fail "usage error ($args) message has no usage error prefix"
@@ -153,7 +153,7 @@ for args in "bogus" "help extra"; do
done
ok "usage errors exit 2 with the usage text on stderr"
capture docker run --rm "$image" healthcheck
capture docker run --rm --name "${name}-probe" "$image" healthcheck
expect_rc "healthcheck without a server" 1
ok "healthcheck fails without a server"
@@ -200,9 +200,9 @@ capture docker exec "$server" /app healthcheck
expect_rc "docker exec healthcheck" 0
ok "docker exec healthcheck"
health=$(docker run --rm --network "$network" "$probe" wget -qO- http://app:8080/health) || fail "GET /health failed"
health=$(docker run --rm --name "${name}-probe" --network "$network" "$probe" wget -qO- http://app:8080/health) || fail "GET /health failed"
[ "$health" = '{"status":"ok"}' ] || fail "unexpected /health body: $health"
page=$(docker run --rm --network "$network" "$probe" wget -qO- http://app:8080/version) || fail "GET /version failed"
page=$(docker run --rm --name "${name}-probe" --network "$network" "$probe" wget -qO- http://app:8080/version) || fail "GET /version failed"
printf '%s' "$page" | grep -qF "$version" || fail "/version does not render the version"
printf '%s' "$page" | grep -qF "$commit" || fail "/version does not render the commit"
ok "/health and /version"
+9 -18
View File
@@ -10,10 +10,10 @@ import (
"database/sql"
"encoding/json"
"io"
"log/slog"
"maps"
"net/http"
"net/http/httptest"
"net/netip"
"slices"
"strings"
"sync"
@@ -63,6 +63,7 @@ type fakeWebStore struct {
failOn map[string]error
onLock func(state *fakeWebState)
calls []string
zoneLocks []string
commits int
rollbacks int
}
@@ -354,6 +355,13 @@ func (f *fakeWebStore) InTx(ctx context.Context, fn func(tx webTx) error) error
return nil
}
func (f *fakeWebStore) InZoneTx(ctx context.Context, zone string, log *slog.Logger, fn func(tx webTx) error) error {
f.mu.Lock()
f.zoneLocks = append(f.zoneLocks, zone)
f.mu.Unlock()
return f.InTx(ctx, fn)
}
func (f *fakeWebStore) InsertAudit(ctx context.Context, e auditEntry) (int64, error) {
f.mu.Lock()
defer f.mu.Unlock()
@@ -700,15 +708,6 @@ type webResult struct {
audit []fakeAuditInsert
}
func (r webResult) json(t *testing.T) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal([]byte(r.body), &m); err != nil {
t.Fatalf("invalid JSON %q: %v", r.body, err)
}
return m
}
func (h *webHarness) do(req webRequest) webResult {
h.t.Helper()
var body io.Reader = strings.NewReader(req.body)
@@ -748,14 +747,6 @@ func (h *webHarness) api(method, path, token, body string) webResult {
return h.do(webRequest{method: method, path: apiPrefix + path, token: token, body: body})
}
func trustProxies(prefixes ...string) webOption {
return func(cfg *Config) {
for _, p := range prefixes {
cfg.TrustedProxies = append(cfg.TrustedProxies, netip.MustParsePrefix(p))
}
}
}
func expectOneAudit(t *testing.T, res webResult, operation string, status int) auditEntry {
t.Helper()
if len(res.audit) != 1 {