fix: replace named locks with Galera-compatible row locks
Test / test (push) Successful in 25m39s
Build and publish image / image (push) Successful in 16m16s

This commit is contained in:
bryanpedini committed 2026-11-20 16:27:15 +01:00
1 parent ee07866d1c
commit 65d16c2fc6
22 files changed
+314 -365

No files matched your search

+7 -5
View File
@@ -94,7 +94,7 @@ Rows with errors are shown in the UI with their raw content and problems; editin
### Writes, locking and ETags
Every write takes a MySQL named lock per zone (`<prefix>webui:<first 16 hex characters of sha256(zone)>`, 10 s timeout, HTTP `503` "zone busy" on timeout), runs in one transaction and writes the records, the SOA serial and the audit row together. Every record carries an ETag (the SHA-256 of its columns). Updates and deletes require `If-Match`: a missing header is HTTP `428`, a mismatch or a missing row is HTTP `412`, so concurrent changes by other writers are detected instead of overwritten.
Every zone write acquires its row in `<prefix>webui_locks` within the transaction that updates the records, SOA serial and audit row. Acquisition waits up to 10 seconds. Lock timeouts and deadlocks, including Galera certification conflicts, return HTTP `503` "zone busy". Every record carries an ETag (the SHA-256 of its columns). Updates and deletes require `If-Match`: a missing header is HTTP `428`, a mismatch or a missing row is HTTP `412`, so concurrent changes by other writers are detected instead of overwritten.
### Zone files
@@ -107,6 +107,7 @@ The application user needs `SELECT`, `INSERT`, `UPDATE` and `DELETE` on the reco
```sql
CREATE USER 'coredns_webui'@'%' IDENTIFIED BY '<password>';
GRANT SELECT, INSERT, UPDATE, DELETE ON `coredns`.`coredns_records` TO 'coredns_webui'@'%';
GRANT SELECT, INSERT, UPDATE, CREATE ON `coredns`.`coredns_webui_locks` TO 'coredns_webui'@'%';
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `coredns`.`coredns_webui_schema` TO 'coredns_webui'@'%';
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `coredns`.`coredns_webui_users` TO 'coredns_webui'@'%';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `coredns`.`coredns_webui_sessions` TO 'coredns_webui'@'%';
@@ -125,10 +126,11 @@ GRANT SELECT ON `coredns`.`coredns_records` TO 'coredns'@'%';
### Application schema and migrations
Migrations are embedded SQL files (`migrations/001_initial.sql`, ...) applied in order. The current version is the single row of `<prefix>webui_schema`. The table prefix is substituted into the SQL after validation; it is the only value ever interpolated into SQL. Migrations run while holding the MySQL named lock `<prefix>webui_migrate` (`GET_LOCK`, 30 s timeout), so several instances starting together never race.
Migrations are embedded SQL files (`migrations/001_initial.sql`, ...) applied in order. The current version is the single row of `<prefix>webui_schema`. The table prefix is substituted into the SQL after validation; it is the only value ever interpolated into SQL. Startup creates `<prefix>webui_locks` if absent and locks the migration row with a 30-second acquisition budget. The migration transaction holds that row while a separate connection runs the DDL, which commits implicitly. This serializes startup on a single database writer. For Galera, route all application connections to the same writer during migrations, for example through a single-writer ProxySQL hostgroup.
| Table | Content |
|---|---|
| `<prefix>webui_locks` | the migration row and one row per zone written by the application |
| `<prefix>webui_schema` | the schema version |
| `<prefix>webui_users` | `id`, `username`, `role`, `password_hash`, `created_at`, `updated_at`, `last_login_at`, `deleted_at` |
| `<prefix>webui_sessions` | SHA-256 of the session token, user, creation, last seen and expiry times, origin, user agent |
@@ -163,7 +165,7 @@ Validation happens before any other work. The first problem is reported as `core
The database connection is built with `mysql.Config` from `github.com/go-sql-driver/mysql` (`ParseTime`, UTC, charset `utf8mb4` via `SET NAMES`, connect timeout 5 s, read and write timeouts 30 s, no multi statements, no client-side interpolation). The pool holds at most 10 open and 10 idle connections with a connection lifetime of 1 minute. The DSN and the password are never logged.
Use a database with the `utf8mb4` character set and InnoDB. The database may use any `utf8mb4` collation. Usernames and delegation keys use exact matching, and zone names are normalized before DNS comparisons. Existing application tables keep their collations and data. On new databases the audit tables store JSON payloads as text; existing native JSON columns still work. CI tests MySQL 5.7, MySQL 8.4 and MariaDB 10.11.
Use a database with the `utf8mb4` character set and InnoDB. The database may use any `utf8mb4` collation. Usernames and delegation keys use exact matching, and zone names are normalized before DNS comparisons. Existing application tables keep their collations and data. On new databases the audit tables store JSON payloads as text; existing native JSON columns still work. CI tests MySQL 5.7, MySQL 8.4, MariaDB 10.11 standalone and MariaDB 10.11 Galera.
`TRUSTED_PROXIES` controls the client origin stored in sessions and audit rows. By default it is the TCP peer address. When the peer is inside a trusted CIDR, it is the rightmost `X-Forwarded-For` entry that is not itself trusted; forwarding headers from other peers are ignored.
@@ -174,7 +176,7 @@ Use a database with the `utf8mb4` character set and InnoDB. The database may use
1. Loads and validates the configuration and initializes the logger.
2. Opens the pool and pings MySQL, retrying with backoff for up to 30 seconds.
3. Verifies the records table contract.
4. Applies the migrations under the named lock.
4. Bootstraps the lock table and applies migrations under its migration row lock.
5. Starts a background task that deletes expired sessions every 10 minutes.
6. Listens on `${HTTP_ADDR}:${HTTP_PORT}` with `ReadHeaderTimeout` 10 s, `ReadTimeout` 30 s, `WriteTimeout` 60 s, `IdleTimeout` 120 s and `MaxHeaderBytes` 64 KiB.
7. On `SIGINT` or `SIGTERM`, stops accepting connections, waits up to 25 seconds for running requests, stops the background task and closes the pool.
@@ -419,7 +421,7 @@ docker run --rm -v "$PWD":/src -w /src -v coredns-web-ui-gomod:/go/pkg/mod -e GO
### Integration tests
`scripts/integration.sh` builds the dockerfile's `integration` stage and runs the tests with the build tag `integration` against a disposable MySQL or MariaDB container (`MYSQL_IMAGE`, default `mysql:8.4`) on an internal Docker network with random credentials. Nothing is published on the host. It covers the records table contract, migrations (including two processes migrating at the same time), the least-privilege grants, the foreign keys, the stores and transactions, the zone lock and the races between role, delegation and user changes and writes. Run with `MYSQL_IMAGE=mysql:5.7` or `MYSQL_IMAGE=mariadb:10.11` to use the other databases in CI. It needs only Docker. Containers, network, volume and image are named `cdwu-it-<id>` (`IT_ID`, default the shell's process id) and are removed at exit, also after failures.
`scripts/integration.sh` builds the dockerfile's `integration` stage and runs the tests with the build tag `integration` against a disposable MySQL or MariaDB container (`MYSQL_IMAGE`, default `mysql:8.4`) on an internal Docker network with random credentials. Nothing is published on the host. It covers the records table contract, migrations (including two processes migrating at the same time), the least-privilege grants, the foreign keys, the stores and transactions, the zone lock and the races between role, delegation and user changes and writes. Run with `MYSQL_IMAGE=mysql:5.7` or `MYSQL_IMAGE=mariadb:10.11` to use the other standalone databases in CI. Run `IT_GALERA=1 MYSQL_IMAGE=mariadb:10.11.3 scripts/integration.sh` for the single-node Galera suite with a loaded wsrep provider. It needs only Docker. Containers, network, volume and image are named `cdwu-it-<id>` (`IT_ID`, default the shell's process id) and are removed at exit, also after failures.
### Image tests