Support a separate database for Web UI tables

This commit is contained in:
bryanpedini committed 2026-11-21 20:36:03 +01:00
1 parent a2f8e59d9b
commit f464dc0af9
38 files changed
+880 -302

No files matched your search

+12 -6
View File
@@ -86,7 +86,7 @@ CREATE TABLE `coredns_records` (
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;
```
The table belongs to the operator and is shared with CoreDNS and possibly with other writers. The application only runs `SELECT`, `INSERT`, `UPDATE` and `DELETE` on it. At startup it verifies, through `information_schema.COLUMNS` of `DATABASE()`, that the table exists and has the six columns with compatible types; otherwise it exits with a clear error naming the missing table or column. It never creates or changes the table, its indexes or its collation.
The table belongs to the operator and is shared with CoreDNS and possibly with other writers. The application only runs `SELECT`, `INSERT`, `UPDATE` and `DELETE` on it. At startup it queries `information_schema.COLUMNS` with `TABLE_SCHEMA` set to `DB_NAME` and verifies that the table has the six columns with compatible types. If the check fails, the application exits with an error naming the missing table or column. It never creates or changes the table, its indexes or its collation.
### Column semantics, as implemented by the plugin
@@ -138,6 +138,8 @@ Configuration comes from environment variables only. No configuration file is re
| `DB_NAME` | yes | none | Database holding the records table |
| `DB_TLS` | no | `false` | `false`, `true`, `skip-verify` or `preferred` |
| `DB_TABLE_PREFIX` | no | `coredns_` | Must equal the plugin's `table_prefix` |
| `DB_WEBUI_NAME` | no | `DB_NAME` | Existing database for Web UI tables on the same server; `^[A-Za-z0-9_$-]{1,64}$` |
| `DB_WEBUI_TABLE_PREFIX` | no | `DB_TABLE_PREFIX` | Optional prefix for Web UI tables; an explicit empty value is valid |
| `HTTP_ADDR` | no | `0.0.0.0` | HTTP listen address |
| `HTTP_PORT` | no | `8080` | HTTP listen port |
| `LOG_LEVEL` | no | `INFO` | `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`, case-insensitive |
@@ -147,7 +149,7 @@ Configuration comes from environment variables only. No configuration file is re
| `SESSION_MAX_AGE` | no | `12h` | Go duration, at least `SESSION_IDLE_TIMEOUT` |
| `TRUSTED_PROXIES` | no | empty | Comma-separated CIDRs whose `X-Forwarded-For` is trusted |
Validation happens before any other work. A missing required variable, an empty value, an invalid port, address, duration, boolean, CIDR, log level or TLS mode, or a table prefix not matching `^[A-Za-z0-9_]{0,32}$` produces a message naming the variable (never its value for `DB_PASSWORD`) and exit code `2`.
Validation happens before any other work. Missing required variables and invalid values produce a message naming the variable and exit code `2`. Both table prefixes must match `^[A-Za-z0-9_]{0,32}$`. Empty values are accepted for the two table prefixes and `TRUSTED_PROXIES`.
The connection is built with `mysql.Config` from `github.com/go-sql-driver/mysql`, never by string concatenation: `Net=tcp`, `ParseTime=true`, `Loc=UTC`, charset `utf8mb4` via `SET NAMES`, connect timeout 5 s, read and write timeouts 30 s, `MultiStatements=false`, `InterpolateParams=false`, TLS from `DB_TLS`. The pool uses the plugin's defaults: at most 10 open and 10 idle connections, connection lifetime 1 minute. The DSN and the password are never logged; logs carry host, port, user and database only.
@@ -211,7 +213,7 @@ Usage errors are fixed messages prefixed by `coredns-web-ui: usage error: ` that
## 6. Application schema
The application keeps its own tables in the same database, prefixed with `<prefix>webui_`, so the plugin never sees them. Migrations are embedded SQL files (`migrations/001_initial.sql`, ...) applied in order inside the migration lock; the current version is the single row of `<prefix>webui_schema`, as in `gestionale-inquilini`. The table prefix is substituted into the SQL after validation; it is the only value ever interpolated into SQL, and it comes from configuration, not from users.
The application keeps its own tables in `DB_WEBUI_NAME`, using `DB_WEBUI_TABLE_PREFIX` before `webui_`. Both settings default to the records settings for existing installations. The single pool selects `DB_WEBUI_NAME` and uses database-qualified table names for both schemas. Migrations use validated identifiers and run in `DB_WEBUI_NAME` under the migration row lock in that database. They apply the embedded SQL files (`migrations/001_initial.sql`, ...) in order and store the version in the single row of `<prefix>webui_schema`.
```sql
CREATE TABLE IF NOT EXISTS `coredns_webui_schema` (
@@ -301,7 +303,7 @@ GRANT SELECT, INSERT, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `coredns`.`cor
GRANT SELECT, INSERT, CREATE, ALTER, INDEX, REFERENCES ON `coredns`.`coredns_webui_audit` TO 'coredns_webui'@'%';
```
Without `DELETE` on the users table and without `UPDATE` and `DELETE` on the audit table, MySQL itself guarantees that users are never removed and audit rows are never changed or removed, even through a bug in the application.
With a separate Web UI database, apply the six `webui_` grants there using its configured prefix. Without `DELETE` on the users table and without `UPDATE` and `DELETE` on the audit table, MySQL itself guarantees that users are never removed and audit rows are never changed or removed, even through a bug in the application.
The CoreDNS user keeps `SELECT` on `coredns_records` only, so it cannot read password hashes, sessions, delegations or the audit.
@@ -352,6 +354,8 @@ Warnings:
### Concurrency
When the two databases differ, each DNS write uses one InnoDB transaction spanning both schemas on the same server.
Every write starts a transaction and updates its zone key in `<prefix>webui_locks` (10 s acquisition timeout, HTTP 503 "zone busy" on timeout or deadlock). Changing this row supplies a Galera certification key. The application then checks again that the actor may operate on the zone (the user row and, for role `user`, the delegation row read with `LOCK IN SHARE MODE`, so a concurrent revocation, demotion or deletion waits for the write or wins before it), reads the zone's rows, applies the rules, writes the records, SOA serial and audit row, and commits, releasing the lock. The lock serializes writers of this application; other writers of the table are detected through ETags.
Every record carries an ETag: the hex SHA-256 of `id`, `zone`, `name`, `ttl`, `record_type` and the raw `content`, with explicit markers for `NULL`. Updates and deletes require `If-Match`; inside the transaction the row is read with `SELECT ... FOR UPDATE` and compared. A missing `If-Match` is HTTP 428, a mismatch or a missing row is HTTP 412 and the UI offers to reload.
@@ -756,10 +760,12 @@ Every function and every meaningful branch has automated coverage. Tests clean u
### Integration tests
Build tag `integration`, run by `scripts/integration.sh` against a disposable `mysql:8.4` container with the records table created as the plugin documents it:
Build tag `integration`, run by `scripts/integration.sh` in shared and separate database layouts against a disposable `mysql:8.4` container with the plugin's records table:
- Contract check: missing table, missing column, incompatible type, non-default prefix.
- Migrations: fresh database, idempotent rerun, two processes migrating at the same time.
- Moving an existing installation with `RENAME TABLE` preserves audit rows, delegations and foreign keys.
- A Web UI account with only table-level DML on the records database and full grants on its own database can migrate, run CLI commands and commit or roll back writes across both databases.
- The least-privilege grants of section 6 are sufficient for every operation, and the CoreDNS user cannot read the application tables.
- The application user cannot `DELETE` users nor `UPDATE` or `DELETE` audit rows, and the foreign keys of sessions, delegations and audit rows hold.
- Two admins demoting each other at the same time leave one active admin; a revocation, demotion or user deletion racing a write either blocks the write or happens after it, never in between.
@@ -768,7 +774,7 @@ Build tag `integration`, run by `scripts/integration.sh` against a disposable `m
### End-to-end tests
Playwright Test, running in a container built from the pinned Playwright image with `dnsutils` added, against a compose stack on an isolated network: MySQL, CoreDNS from `git.bjphoster.com/docker/coredns-mysql` with a Corefile using the `mysql` plugin, `zone_update_interval 1s` and no cache, and the application image under test. Each test group gets its own database and users.
Playwright Test, running in a container built from the pinned Playwright image with `dnsutils` added, against a compose stack on an isolated network: MySQL, CoreDNS from `git.bjphoster.com/docker/coredns-mysql` with a Corefile using the `mysql` plugin, `zone_update_interval 1s` and no cache, and the application image under test. Each test group gets its own records database and users. The suite exercises shared and separate Web UI databases, an independent Web UI prefix, and database-level grants confined to the Web UI database.
The central invariant: every change made through the UI is verified three ways, through the UI, through the rows in MySQL (exact `zone`, `name`, `ttl`, `record_type` and canonical `content`) and through `dig` against CoreDNS (rcode, answer section, TTL, authority SOA serial). Every row the application writes is served by CoreDNS without SERVFAIL or NOTIMP and with exactly the entered data.