247 lines
9.3 KiB
Go
247 lines
9.3 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func (c *itCLI) run(args ...string) int {
|
|
c.stdout.Reset()
|
|
c.stderr.Reset()
|
|
return run(args, c.a)
|
|
}
|
|
|
|
func TestIntegrationUserCLI(t *testing.T) {
|
|
t.Parallel()
|
|
d := newITDatabase(t, itOptions{prefix: "coredns_"})
|
|
now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)
|
|
cli := d.cli(t, "first password here\n", now)
|
|
if code := cli.run("user", "add", "alice", "admin"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
cli = d.cli(t, "second password here\n", now)
|
|
if code := cli.run("user", "add", "bob", "user"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
cli = d.cli(t, "third password here\n", now)
|
|
if code := cli.run("user", "add", "alice", "user"); code != 1 || cli.stderr.String() != "coredns-web-ui: username already used\n" {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
var hash string
|
|
if err := d.root.QueryRow(d.sql("SELECT `password_hash` FROM {users} WHERE `username` = 'alice'")).Scan(&hash); err != nil || hash != "$test$first password here" {
|
|
t.Fatal(hash, err)
|
|
}
|
|
if code := cli.run("user", "list"); code != 0 {
|
|
t.Fatal(code)
|
|
}
|
|
want := "USERNAME ROLE ZONES CREATED_AT LAST_LOGIN_AT DELETED_AT\n" +
|
|
"alice admin 0 2026-11-18T12:00:00Z - -\n" +
|
|
"bob user 0 2026-11-18T12:00:00Z - -\n"
|
|
if cli.stdout.String() != want {
|
|
t.Fatalf("got\n%s", cli.stdout.String())
|
|
}
|
|
if code := cli.run("user", "role", "alice", "user"); code != 1 || !strings.Contains(cli.stderr.String(), "last active admin") {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
if code := cli.run("user", "delete", "alice"); code != 1 || !strings.Contains(cli.stderr.String(), "last active admin") {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
bobID := int64(d.count(t, "SELECT `id` FROM {users} WHERE `username` = 'bob'"))
|
|
d.rootExec(t, "INSERT INTO {sessions} VALUES (UNHEX(SHA2('a', 256)), ?, NOW(6), NOW(6), NOW(6), 'x', 'y')", bobID)
|
|
d.rootExec(t, "INSERT INTO {sessions} VALUES (UNHEX(SHA2('b', 256)), ?, NOW(6), NOW(6), NOW(6), 'x', 'y')", bobID)
|
|
cli = d.cli(t, "new bob password\n", now.Add(time.Minute))
|
|
if code := cli.run("user", "passwd", "bob"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
if d.count(t, "SELECT COUNT(*) FROM {sessions}") != 0 {
|
|
t.Fatal("sessions survived passwd")
|
|
}
|
|
if code := cli.run("user", "role", "bob", "admin"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
if code := cli.run("user", "role", "alice", "user"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
d.rootExec(t, "INSERT INTO {sessions} VALUES (UNHEX(SHA2('c', 256)), (SELECT `id` FROM {users} WHERE `username` = 'alice'), NOW(6), NOW(6), NOW(6), 'x', 'y')")
|
|
if code := cli.run("user", "delete", "alice"); code != 0 {
|
|
t.Fatalf("%d %q", code, cli.stderr.String())
|
|
}
|
|
if d.count(t, "SELECT COUNT(*) FROM {sessions}") != 0 {
|
|
t.Fatal("sessions survived delete")
|
|
}
|
|
if d.count(t, "SELECT COUNT(*) FROM {users} WHERE `username` = 'alice' AND `deleted_at` IS NOT NULL") != 1 {
|
|
t.Fatal("deleted user row missing")
|
|
}
|
|
for _, args := range [][]string{{"user", "passwd", "alice"}, {"user", "role", "alice", "admin"}, {"user", "delete", "alice"}, {"user", "passwd", "nobody"}} {
|
|
c := d.cli(t, "some password value\n", now)
|
|
if code := c.run(args...); code != 1 || c.stderr.String() != "coredns-web-ui: no active user has this username\n" {
|
|
t.Fatalf("%v: %d %q", args, code, c.stderr.String())
|
|
}
|
|
}
|
|
cli = d.cli(t, "some password value\n", now)
|
|
if code := cli.run("user", "add", "alice", "admin"); code != 1 {
|
|
t.Fatal("deleted username reused")
|
|
}
|
|
if code := cli.run("user", "list", "all"); code != 0 || !strings.Contains(cli.stdout.String(), "alice user 0 2026-11-18T12:00:00Z - 2026-11-18T12:01:00Z\n") {
|
|
t.Fatalf("%d\n%s", code, cli.stdout.String())
|
|
}
|
|
|
|
rows, err := d.root.Query(d.sql("SELECT `user_id`, `actor`, `origin`, `operation`, `status`, `error`, `zone`, `record_id`, `target_user_id`, `before_value`, `after_value` FROM {audit} ORDER BY `id`"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
var ops []string
|
|
for rows.Next() {
|
|
var userID, status, recordID, target sql.NullInt64
|
|
var actor, origin, op string
|
|
var errMsg, zone sql.NullString
|
|
var before, after []byte
|
|
if err := rows.Scan(&userID, &actor, &origin, &op, &status, &errMsg, &zone, &recordID, &target, &before, &after); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if userID.Valid || actor != "cli" || origin != "local" || status.Valid || errMsg.Valid || zone.Valid || recordID.Valid || !target.Valid || after == nil {
|
|
t.Fatalf("audit row %s: %v %s %s", op, userID, actor, origin)
|
|
}
|
|
if (op == "user_create") != (before == nil) {
|
|
t.Fatalf("%s before %s", op, before)
|
|
}
|
|
var v map[string]any
|
|
if err := json.Unmarshal(after, &v); err != nil || len(v) != 5 || v["username"] == nil {
|
|
t.Fatalf("%s after %s", op, after)
|
|
}
|
|
if strings.Contains(string(after), "password") || strings.Contains(string(after), "$test$") {
|
|
t.Fatal("password material in audit")
|
|
}
|
|
ops = append(ops, op)
|
|
}
|
|
if strings.Join(ops, ",") != "user_create,user_create,user_password,user_role,user_role,user_delete" {
|
|
t.Fatalf("audit operations %v", ops)
|
|
}
|
|
}
|
|
|
|
func TestIntegrationLastAdminRace(t *testing.T) {
|
|
t.Parallel()
|
|
for round := 0; round < 5; round++ {
|
|
d := newITDatabase(t, itOptions{prefix: "coredns_"})
|
|
now := time.Now()
|
|
for _, name := range []string{"alice", "bob"} {
|
|
c := d.cli(t, "initial password\n", now)
|
|
if code := c.run("user", "add", name, "admin"); code != 0 {
|
|
t.Fatalf("%d %q", code, c.stderr.String())
|
|
}
|
|
}
|
|
commands := [][]string{{"user", "role", "alice", "user"}, {"user", "role", "bob", "user"}}
|
|
if round%2 == 1 {
|
|
commands[1] = []string{"user", "delete", "bob"}
|
|
}
|
|
start := make(chan struct{})
|
|
codes := make([]int, len(commands))
|
|
var wg sync.WaitGroup
|
|
for i, args := range commands {
|
|
c := d.cli(t, "", now)
|
|
wg.Go(func() {
|
|
<-start
|
|
codes[i] = c.run(args...)
|
|
})
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
if codes[0]+codes[1] != 1 {
|
|
t.Fatalf("round %d: exit codes %v", round, codes)
|
|
}
|
|
if n := d.count(t, "SELECT COUNT(*) FROM {users} WHERE `role` = 'admin' AND `deleted_at` IS NULL"); n != 1 {
|
|
t.Fatalf("round %d: %d active admins", round, n)
|
|
}
|
|
if n := d.count(t, "SELECT COUNT(*) FROM {audit} WHERE `operation` IN ('user_role', 'user_delete')"); n != 1 {
|
|
t.Fatalf("round %d: %d change rows", round, n)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIntegrationCheckAndAuditCommands(t *testing.T) {
|
|
t.Parallel()
|
|
d := newITDatabase(t, itOptions{prefix: "dns_"})
|
|
c := d.cli(t, "", time.Now())
|
|
if code := c.run("check"); code != 0 || c.stdout.String() != "ZONE NAME TYPE ID SEVERITY PROBLEM\n" {
|
|
t.Fatalf("%d %q %q", code, c.stdout.String(), c.stderr.String())
|
|
}
|
|
if d.count(t, "SELECT `version` FROM {schema}") != 2 {
|
|
t.Fatal("check did not migrate")
|
|
}
|
|
d.rootExec(t, "INSERT INTO {records} (`zone`, `name`, `ttl`, `content`, `record_type`) VALUES ('example.org.', 'www', NULL, '{\"ip\": \"192.0.2.1\"}', 'A')")
|
|
c = d.cli(t, "", time.Now())
|
|
if code := c.run("check", "EXAMPLE.org"); code != 3 || !strings.Contains(c.stdout.String(), "ttl_null") || !strings.Contains(c.stdout.String(), "soa_missing") {
|
|
t.Fatalf("%d %q %q", code, c.stdout.String(), c.stderr.String())
|
|
}
|
|
c = d.cli(t, "", time.Now())
|
|
if code := c.run("check", "missing.example"); code != 1 || c.stderr.String() != "coredns-web-ui: the zone has no records\n" {
|
|
t.Fatalf("%d %q", code, c.stderr.String())
|
|
}
|
|
c = d.cli(t, "a-long-password\n", time.Date(2026, 11, 18, 9, 0, 0, 0, time.UTC))
|
|
if code := c.run("user", "add", "alice", "admin"); code != 0 {
|
|
t.Fatalf("%d %q", code, c.stderr.String())
|
|
}
|
|
c = d.cli(t, "", time.Now())
|
|
if code := c.run("audit", "5"); code != 0 || c.stdout.String() != "TIME ACTOR ORIGIN OPERATION STATUS ZONE RECORD TARGET_USER\n"+
|
|
"2026-11-18T09:00:00Z cli local user_create - - - alice\n" {
|
|
t.Fatalf("%d %q %q", code, c.stdout.String(), c.stderr.String())
|
|
}
|
|
if code := c.run("audit", "x"); code != 2 {
|
|
t.Fatal(code)
|
|
}
|
|
}
|
|
|
|
func TestIntegrationServer(t *testing.T) {
|
|
t.Parallel()
|
|
d := newITDatabase(t, itOptions{prefix: "coredns_"})
|
|
c := d.cli(t, "", time.Now())
|
|
c.a.stderr = io.Discard
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
var mu sync.Mutex
|
|
results := map[string]string{}
|
|
c.a.listen = func(string, string) (net.Listener, error) { return net.Listen("tcp", "127.0.0.1:0") }
|
|
c.a.serverStarted = func(ws *WebServer, ln net.Listener) {
|
|
go func() {
|
|
defer cancel()
|
|
for _, p := range []string{"/health", "/version"} {
|
|
resp, err := http.Get("http://" + ln.Addr().String() + p)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
mu.Lock()
|
|
results[p] = resp.Status + " " + string(body)
|
|
mu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
if code := c.a.exitCode(c.a.execute(ctx, nil)); code != 0 {
|
|
t.Fatalf("exit %d", code)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if results["/health"] != `200 OK {"status":"ok"}` || !strings.Contains(results["/version"], "CoreDNS Web UI") {
|
|
t.Fatalf("%v", results)
|
|
}
|
|
if d.count(t, "SELECT `version` FROM {schema}") != 2 {
|
|
t.Fatal("server did not migrate")
|
|
}
|
|
}
|