Support a separate database for Web UI tables
This commit is contained in:
1 parent
a2f8e59d9b
commit
f464dc0af9
38 files changed
+880
-302
No files matched your search
@@ -115,7 +115,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'@'%';
|
||||
```
|
||||
|
||||
Replace `coredns` with the database and `coredns_` with the table 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.
|
||||
Use `DB_NAME` and `DB_TABLE_PREFIX` for the records grant. Use `DB_WEBUI_NAME` and `DB_WEBUI_TABLE_PREFIX` for the six `webui_` grants. A database-level grant confined to a dedicated Web UI database also works. The table grants shown here enforce additional limits on the application account, including keeping users and audit entries from being deleted and keeping audit entries from being updated.
|
||||
|
||||
The CoreDNS user keeps `SELECT` on `coredns_records` only, so it cannot read password hashes, sessions, delegations or the audit:
|
||||
|
||||
@@ -126,7 +126,7 @@ 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. 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.
|
||||
Migrations are embedded SQL files (`migrations/001_initial.sql`, ...) applied in order in `DB_WEBUI_NAME`. The current version is the single row of `<prefix>webui_schema`. The migration SQL uses the validated Web UI database and table prefix. Startup creates `<prefix>webui_locks` in that database if absent and locks its migration row with a 30-second acquisition budget. The migration transaction holds that row while a separate connection from the same pool 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 |
|
||||
|---|---|
|
||||
@@ -139,9 +139,28 @@ Migrations are embedded SQL files (`migrations/001_initial.sql`, ...) applied in
|
||||
|
||||
All times are UTC. Rows of the users table are never deleted, so the foreign keys from sessions, delegations and audit rows always resolve. Audit rows have no foreign key to the records table, so deleted records keep their history. The audit `record_id` column is `BIGINT` to preserve IDs across the full range of an `INT UNSIGNED` records table.
|
||||
|
||||
### Move an existing installation to a separate database
|
||||
|
||||
1. Stop all Web UI instances and pause its database-writing CLI commands. Back up the six application tables. Use the database writer for the following operations.
|
||||
2. As a database administrator, create an empty database on the same server and move the six tables in one `RENAME TABLE` statement. This example keeps the `coredns_` prefix and moves the application tables from `coredns` to `coredns_webui`.
|
||||
|
||||
```sql
|
||||
CREATE DATABASE `coredns_webui` DEFAULT CHARACTER SET utf8mb4;
|
||||
RENAME TABLE
|
||||
`coredns`.`coredns_webui_schema` TO `coredns_webui`.`coredns_webui_schema`,
|
||||
`coredns`.`coredns_webui_locks` TO `coredns_webui`.`coredns_webui_locks`,
|
||||
`coredns`.`coredns_webui_users` TO `coredns_webui`.`coredns_webui_users`,
|
||||
`coredns`.`coredns_webui_sessions` TO `coredns_webui`.`coredns_webui_sessions`,
|
||||
`coredns`.`coredns_webui_zone_delegations` TO `coredns_webui`.`coredns_webui_zone_delegations`,
|
||||
`coredns`.`coredns_webui_audit` TO `coredns_webui`.`coredns_webui_audit`;
|
||||
```
|
||||
|
||||
3. Apply the six Web UI table grants shown above to the destination database. Table-specific grants stay on the old names after a rename. Revoke those old grants and keep the records-table grants for the application and CoreDNS accounts.
|
||||
4. Set `DB_WEBUI_NAME=coredns_webui` on every Web UI instance and for the CLI. Leave `DB_WEBUI_TABLE_PREFIX` unset to retain `DB_TABLE_PREFIX`. If you also change the prefix, change all six destination names and grants accordingly. Restart the Web UI and verify that `/health` reports ready and an existing administrator can sign in.
|
||||
|
||||
## Configuration
|
||||
|
||||
Configuration comes only from environment variables. No configuration file is read. A variable that is set but empty is an error, except `DB_TABLE_PREFIX` (an empty prefix is valid) and `TRUSTED_PROXIES` (empty means no trusted proxy).
|
||||
Configuration comes only from environment variables. No configuration file is read. Empty values are accepted for `DB_TABLE_PREFIX`, `DB_WEBUI_TABLE_PREFIX` and `TRUSTED_PROXIES`.
|
||||
|
||||
| Variable | Required | Default | Accepted values |
|
||||
|---|---|---|---|
|
||||
@@ -152,6 +171,8 @@ Configuration comes only from environment variables. No configuration file is re
|
||||
| `DB_NAME` | yes | none | Database holding the records table; at most 64 characters, no control characters |
|
||||
| `DB_TLS` | no | `false` | `false`, `true`, `skip-verify` or `preferred` |
|
||||
| `DB_TABLE_PREFIX` | no | `coredns_` | `^[A-Za-z0-9_]{0,32}$`; must equal the plugin's `table_prefix` |
|
||||
| `DB_WEBUI_NAME` | no | `DB_NAME` | Existing database for the Web UI tables on the same server; `^[A-Za-z0-9_$-]{1,64}$` |
|
||||
| `DB_WEBUI_TABLE_PREFIX` | no | `DB_TABLE_PREFIX` | `^[A-Za-z0-9_]{0,32}$`; an explicit empty value uses names such as `webui_users` |
|
||||
| `HTTP_ADDR` | no | `0.0.0.0` | A literal IPv4 or IPv6 address (no host names, ports or zones) |
|
||||
| `HTTP_PORT` | no | `8080` | Integer from `1` to `65535`, ASCII digits only |
|
||||
| `LOG_LEVEL` | no | `INFO` | `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`, case-insensitive |
|
||||
@@ -165,6 +186,8 @@ 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.
|
||||
|
||||
The connection selects `DB_WEBUI_NAME` as its default database and qualifies tables with their configured database. The records contract checks `DB_NAME`. The same pool and credentials serve both databases, and each DNS write commits the records, SOA serial and audit together under the Web UI's zone lock.
|
||||
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user