feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders

- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders
- feat(cli): add check and audit commands
- feat(api): add api framework, login, page routes and account, user, delegation and audit handlers
- test(auth): cover passwords, sessions, rate limiter, authorization and audit builders
- test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal
- test(api): cover account, user, delegation and audit handlers and the authorization matrix
- test(ui): cover login, logout, page routes and the page authorization matrix
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 23bec143e3
commit 99fa5de747
28 files changed
+6591 -243

No files matched your search

+177 -7
View File
@@ -665,21 +665,173 @@ func TestUserDelete(t *testing.T) {
}
}
func TestCheckAndAuditCommands(t *testing.T) {
type fakeReadBackend struct {
zones []zoneSummary
rows map[string][]recordRow
audit []auditRow
failOn string
auditN int
rowCalls []string
}
func (f *fakeReadBackend) ListZones(ctx context.Context) ([]zoneSummary, error) {
if f.failOn == "ListZones" {
return nil, errFakeStore
}
return f.zones, nil
}
func (f *fakeReadBackend) ZoneRows(ctx context.Context, zone string) ([]recordRow, error) {
f.rowCalls = append(f.rowCalls, zone)
if f.failOn == "ZoneRows" {
return nil, errFakeStore
}
return f.rows[zone], nil
}
func (f *fakeReadBackend) LatestAudit(ctx context.Context, n int) ([]auditRow, error) {
f.auditN = n
if f.failOn == "LatestAudit" {
return nil, errFakeStore
}
return f.audit, nil
}
func cliRecord(id int64, zone, name, recordType string, ttl any, content any) recordRow {
r := recordRow{ID: id, Zone: zone, Name: name, RecordType: recordType}
if v, ok := ttl.(int); ok {
r.TTL = sql.NullInt64{Int64: int64(v), Valid: true}
}
if v, ok := content.(string); ok {
r.Content = sql.NullString{String: v, Valid: true}
}
return r
}
func cleanZoneRows(zone string) []recordRow {
return []recordRow{
cliRecord(1, zone, "", "SOA", 3600, `{"ns":"ns1.`+zone+`","MBox":"hostmaster.`+zone+`","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111800}`),
cliRecord(2, zone, "", "NS", 3600, `{"host":"ns1.`+zone+`"}`),
cliRecord(3, zone, "ns1", "A", 3600, `{"ip":"192.0.2.1"}`),
}
}
func (h *cliHarness) withReadBackend(b *fakeReadBackend) {
h.a.newReadBackend = func(*sql.DB, tableNames) cliReadBackend { return b }
}
func TestCheckCommand(t *testing.T) {
var zone string
b := &fakeReadBackend{rows: map[string][]recordRow{"example.org.": cleanZoneRows("example.org.")}}
h := newCLIHarness(t, "")
h.a.normalizeZone = func(z string) (string, error) { zone = z; return "example.org.", nil }
if code := h.run("check", "Example.Org"); code != 1 || h.stderr.String() != "coredns-web-ui: check: not implemented\n" || h.prepared != 1 || zone != "Example.Org" {
h.withReadBackend(b)
h.a.normalizeZone = func(z string) (string, error) { zone = z; return normalizeZone(z) }
if code := h.run("check", "Example.Org"); code != 0 || h.stderr.Len() != 0 || h.prepared != 1 || zone != "Example.Org" {
t.Fatalf("%d %q", code, h.stderr.String())
}
if h.stdout.String() != "ZONE NAME TYPE ID SEVERITY PROBLEM\n" || len(b.rowCalls) != 1 || b.rowCalls[0] != "example.org." {
t.Fatalf("%q %v", h.stdout.String(), b.rowCalls)
}
broken := []recordRow{
cliRecord(7, "broken.test.", "www", "A", nil, `{"ip":"192.0.2.7"}`),
cliRecord(8, "broken.test.", "", "TXT", 300, "x\ty"),
}
b = &fakeReadBackend{
zones: []zoneSummary{{Zone: "broken.test."}, {Zone: "example.org."}},
rows: map[string][]recordRow{"example.org.": cleanZoneRows("example.org."), "broken.test.": broken},
}
h = newCLIHarness(t, "")
if code := h.run("check"); code != 1 || h.prepared != 1 {
h.withReadBackend(b)
if code := h.run("check"); code != 3 || h.stderr.Len() != 0 {
t.Fatalf("%d %q", code, h.stderr.String())
}
lines := strings.Split(strings.TrimSuffix(h.stdout.String(), "\n"), "\n")
if !strings.HasPrefix(lines[0], "ZONE ") || !strings.Contains(lines[0], "PROBLEM") {
t.Fatal(lines[0])
}
out := h.stdout.String()
for _, want := range []string{"ttl_null: stored TTL is NULL", "soa_missing", "content_json", " www ", " @ ", " 7 ", " 8 ", "error"} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in\n%s", want, out)
}
}
if strings.Contains(out, "example.org.") || strings.Contains(out, "\t") {
t.Fatalf("unexpected output\n%s", out)
}
for _, line := range lines[1:] {
if !strings.HasPrefix(line, "broken.test.") {
t.Fatalf("line %q", line)
}
}
if !strings.Contains(out, "broken.test. - - - error") {
t.Fatalf("zone level problem row missing\n%s", out)
}
h = newCLIHarness(t, "")
h.withReadBackend(&fakeReadBackend{rows: map[string][]recordRow{}})
if code := h.run("check", "missing.test"); code != 1 || h.stderr.String() != "coredns-web-ui: the zone has no records\n" || h.stdout.Len() != 0 {
t.Fatalf("%d %q", code, h.stderr.String())
}
for _, op := range []string{"ListZones", "ZoneRows"} {
h = newCLIHarness(t, "")
h.withReadBackend(&fakeReadBackend{zones: []zoneSummary{{Zone: "a.test."}}, failOn: op})
if code := h.run("check"); code != 1 || h.stdout.Len() != 0 {
t.Fatalf("%s: %d", op, code)
}
}
h = newCLIHarness(t, "")
h.withReadBackend(&fakeReadBackend{})
if code := h.run("check"); code != 0 || h.stdout.String() != "ZONE NAME TYPE ID SEVERITY PROBLEM\n" {
t.Fatalf("no zones: %d %q", code, h.stdout.String())
}
h = newCLIHarness(t, "")
if code := h.run("check", "a", "b"); code != 2 || h.prepared != 0 {
t.Fatal(code)
}
for _, args := range [][]string{{"audit"}, {"audit", "5"}, {"audit", "0001"}, {"audit", "1000000"}} {
h = newCLIHarness(t, "")
if code := h.run("check", "bad zone"); code != 2 || h.prepared != 0 || strings.Contains(h.stderr.String(), "bad zone") {
t.Fatal(code, h.stderr.String())
}
}
func TestAuditCommand(t *testing.T) {
at := time.Date(2026, 11, 18, 10, 11, 12, 345678000, time.UTC)
b := &fakeReadBackend{audit: []auditRow{
{auditEntry: auditEntry{ID: 1, Time: at, Actor: "cli", Origin: "local", Operation: "user_create", TargetUserID: sql.NullInt64{Int64: 3, Valid: true}}, TargetUsername: sql.NullString{String: "bob", Valid: true}},
{auditEntry: auditEntry{ID: 2, Time: at.Add(time.Second), UserID: sql.NullInt64{Int64: 3, Valid: true}, Actor: "bad\tactor\n", Origin: "192.0.2.1", Operation: "record_update",
Status: sql.NullInt64{Int64: 200, Valid: true}, Zone: sql.NullString{String: "example.org.", Valid: true}, RecordID: sql.NullInt64{Int64: 42, Valid: true}}},
{auditEntry: auditEntry{ID: 3, Time: at.Add(2 * time.Second), Actor: "", Origin: "2001:db8::1", Operation: "login", Status: sql.NullInt64{Int64: 401, Valid: true}}},
}}
h := newCLIHarness(t, "")
h.withReadBackend(b)
if code := h.run("audit"); code != 0 || h.stderr.Len() != 0 || b.auditN != 20 || h.prepared != 1 {
t.Fatalf("%d %q %d", code, h.stderr.String(), b.auditN)
}
want := "TIME ACTOR ORIGIN OPERATION STATUS ZONE RECORD TARGET_USER\n" +
"2026-11-18T10:11:12Z cli local user_create - - - bob\n" +
"2026-11-18T10:11:13Z bad actor 192.0.2.1 record_update 200 example.org. 42 -\n" +
"2026-11-18T10:11:14Z - 2001:db8::1 login 401 - - -\n"
if h.stdout.String() != want {
t.Fatalf("got\n%s\nwant\n%s", h.stdout.String(), want)
}
for args, n := range map[string]int{"5": 5, "0001": 1, "1000000": 1000000} {
b = &fakeReadBackend{}
h = newCLIHarness(t, "")
if code := h.run(args...); code != 1 || h.stderr.String() != "coredns-web-ui: audit: not implemented\n" || h.prepared != 1 {
t.Fatalf("%v: %d %q", args, code, h.stderr.String())
h.withReadBackend(b)
if code := h.run("audit", args); code != 0 || b.auditN != n || h.stdout.String() != "TIME ACTOR ORIGIN OPERATION STATUS ZONE RECORD TARGET_USER\n" {
t.Fatalf("%s: %d %d %q", args, code, b.auditN, h.stdout.String())
}
}
h = newCLIHarness(t, "")
h.withReadBackend(&fakeReadBackend{failOn: "LatestAudit"})
if code := h.run("audit"); code != 1 || h.stdout.Len() != 0 {
t.Fatal(code)
}
for _, args := range [][]string{{"audit", "0"}, {"audit", "-1"}, {"audit", "x"}, {"audit", "1000001"}, {"audit", "1", "2"}, {"audit", "+5"}} {
h = newCLIHarness(t, "")
if code := h.run(args...); code != 2 || h.prepared != 0 {
t.Fatalf("%v: %d", args, code)
}
}
if n, err := parseAuditCount("20"); n != 20 || err != nil {
@@ -687,6 +839,24 @@ func TestCheckAndAuditCommands(t *testing.T) {
}
}
func TestSQLReadBackend(t *testing.T) {
db, state := newFakeDB(t, fdbRules())
b := newSQLReadBackend(db, fdbTables(t))
if _, err := b.ListZones(context.Background()); err != nil {
t.Fatal(err)
}
if _, err := b.ZoneRows(context.Background(), "example.org."); err != nil {
t.Fatal(err)
}
if _, err := b.LatestAudit(context.Background(), 3); err != nil {
t.Fatal(err)
}
q := state.Queries()
if len(q) != 3 || !strings.Contains(q[0], "GROUP BY") || !strings.Contains(q[1], "`coredns_records`") || !strings.Contains(q[2], "`coredns_webui_audit`") {
t.Fatal(q)
}
}
func TestExitCodes(t *testing.T) {
h := newCLIHarness(t, "")
if h.a.exitCode(nil) != 0 {