Files
coredns-web-ui/delegation_store.go
T
bryanpedini 53444bec7a test(ui): avoid a literal em dash in the em dash check
- test(ui): avoid a literal em dash in the em dash check
- fix(cli): accept any positive int64 audit count and share the last active admin rule
- fix(api): limit audit before_id to 2^53-1
- fix(api): audit unmatched api requests, bound every request body and record audit write failures
- fix(ui): recheck the account in the login transaction and require existing zones on zone pages
- fix(api): recheck the acting admin and the stored password hash in change transactions
- fix(db): keep committed outcomes on lock release failure, narrow the records contract and trace sql statements
- fix(db): make the initial migration restartable and verify the application schema
- fix(auth): never move last_seen_at backwards on concurrent session touches
- test(api): cover lock release failure after commit and handler tracing without secrets
- fix(test): redact credentials from mysql setup errors
- fix(audit): retain addressed objects on admin refusals
- test(db): cover migration restarts and records contract ranges in mysql
- test(api): verify account races and audit rollback in mysql
- fix(auth): reject password changes after account deletion
- docs: document unknown api audit entries
- fix(audit): bound failure retries and redact sensitive sql parameters
- fix(test): isolate docker resources and cover image cli commands
- test(auth): expect unauthorized after an account deletion race
- fix(db): verify application column widths and foreign keys
2026-11-19 15:46:12 +01:00

152 lines
4.8 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
type delegationRow struct {
Zone string
UserID int64
Username string
CreatedAt time.Time
CreatedBy int64
}
type userZone struct {
Zone string
CreatedAt time.Time
ZoneExists bool
}
type delegationStore struct {
q dbtx
t tableNames
}
func newDelegationStore(q dbtx, t tableNames) delegationStore {
return delegationStore{q: q, t: t}
}
func (s delegationStore) GrantDelegation(ctx context.Context, zone string, userID, createdBy int64, now time.Time) (bool, error) {
defer traceStore(ctx, "delegationStore.GrantDelegation")()
_, err := s.q.ExecContext(ctx,
s.t.sql("INSERT INTO {delegations} (`zone`, `user_id`, `created_at`, `created_by`) VALUES (?, ?, ?, ?)"),
zone, userID, dbTime(now), createdBy)
if isDuplicateKey(err) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("grant delegation: %w", err)
}
return true, nil
}
func (s delegationStore) RevokeDelegation(ctx context.Context, zone string, userID int64) (bool, error) {
defer traceStore(ctx, "delegationStore.RevokeDelegation")()
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {delegations} WHERE `zone` = ? AND `user_id` = ?"), zone, userID)
if err != nil {
return false, fmt.Errorf("revoke delegation: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return false, fmt.Errorf("revoke delegation: %w", err)
}
return n > 0, nil
}
func (s delegationStore) ZoneUsers(ctx context.Context, zone string) ([]userRow, error) {
defer traceStore(ctx, "delegationStore.ZoneUsers")()
rows, err := s.q.QueryContext(ctx,
s.t.sql("SELECT "+userColumns+" FROM {delegations} d JOIN {users} u ON u.`id` = d.`user_id` "+
"WHERE d.`zone` = ? AND u.`role` = 'user' AND u.`deleted_at` IS NULL ORDER BY u.`username`"), zone)
if err != nil {
return nil, fmt.Errorf("zone users: %w", err)
}
return collectUsers(rows)
}
func (s delegationStore) UserZones(ctx context.Context, userID int64) ([]userZone, error) {
defer traceStore(ctx, "delegationStore.UserZones")()
rows, err := s.q.QueryContext(ctx,
s.t.sql("SELECT d.`zone`, d.`created_at`, EXISTS (SELECT 1 FROM {records} r WHERE r.`zone` = d.`zone`) "+
"FROM {delegations} d WHERE d.`user_id` = ? ORDER BY d.`zone`"), userID)
if err != nil {
return nil, fmt.Errorf("user zones: %w", err)
}
defer rows.Close()
var zones []userZone
for rows.Next() {
var z userZone
if err := rows.Scan(&z.Zone, &z.CreatedAt, &z.ZoneExists); err != nil {
return nil, fmt.Errorf("user zones: %w", err)
}
scanUTC(&z.CreatedAt)
zones = append(zones, z)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("user zones: %w", err)
}
return zones, nil
}
func (s delegationStore) DeleteZoneDelegations(ctx context.Context, zone string) ([]delegationRow, error) {
defer traceStore(ctx, "delegationStore.DeleteZoneDelegations")()
rows, err := s.q.QueryContext(ctx,
s.t.sql("SELECT d.`zone`, d.`user_id`, u.`username`, d.`created_at`, d.`created_by` FROM {delegations} d "+
"JOIN {users} u ON u.`id` = d.`user_id` WHERE d.`zone` = ? ORDER BY u.`username` FOR UPDATE OF d"), zone)
if err != nil {
return nil, fmt.Errorf("delete zone delegations: %w", err)
}
var deleted []delegationRow
for rows.Next() {
var d delegationRow
if err := rows.Scan(&d.Zone, &d.UserID, &d.Username, &d.CreatedAt, &d.CreatedBy); err != nil {
rows.Close()
return nil, fmt.Errorf("delete zone delegations: %w", err)
}
scanUTC(&d.CreatedAt)
deleted = append(deleted, d)
}
if err := rows.Err(); err != nil {
rows.Close()
return nil, fmt.Errorf("delete zone delegations: %w", err)
}
rows.Close()
if len(deleted) == 0 {
return nil, nil
}
if _, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {delegations} WHERE `zone` = ?"), zone); err != nil {
return nil, fmt.Errorf("delete zone delegations: %w", err)
}
return deleted, nil
}
func (s delegationStore) hasDelegation(ctx context.Context, query, zone string, userID int64) (bool, error) {
var one int
err := s.q.QueryRowContext(ctx, s.t.sql(query), zone, userID).Scan(&one)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("read delegation: %w", err)
}
return true, nil
}
func (s delegationStore) HasDelegation(ctx context.Context, zone string, userID int64) (bool, error) {
defer traceStore(ctx, "delegationStore.HasDelegation")()
return s.hasDelegation(ctx, "SELECT 1 FROM {delegations} WHERE `zone` = ? AND `user_id` = ?", zone, userID)
}
func (s delegationStore) HasDelegationForShare(ctx context.Context, zone string, userID int64) (bool, error) {
defer traceStore(ctx, "delegationStore.HasDelegationForShare")()
return s.hasDelegation(ctx, "SELECT 1 FROM {delegations} WHERE `zone` = ? AND `user_id` = ? FOR SHARE", zone, userID)
}