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
+62
-30
@@ -61,34 +61,66 @@ type pinger interface {
|
||||
}
|
||||
|
||||
type tableNames struct {
|
||||
prefix string
|
||||
records string
|
||||
schema string
|
||||
users string
|
||||
sessions string
|
||||
delegations string
|
||||
audit string
|
||||
locks string
|
||||
recordsSchema string
|
||||
recordsTable string
|
||||
webuiSchema string
|
||||
webuiPrefix string
|
||||
records string
|
||||
schema string
|
||||
users string
|
||||
sessions string
|
||||
delegations string
|
||||
audit string
|
||||
locks string
|
||||
}
|
||||
|
||||
func newTableNames(prefix string) (tableNames, error) {
|
||||
if !validTablePrefix(prefix) {
|
||||
type tableLayout struct {
|
||||
recordsSchema string
|
||||
recordsPrefix string
|
||||
webuiSchema string
|
||||
webuiPrefix string
|
||||
}
|
||||
|
||||
func tableNamesFor(cfg Config) (tableNames, error) {
|
||||
return newTableNames(tableLayout{
|
||||
recordsSchema: cfg.DBName,
|
||||
recordsPrefix: cfg.DBTablePrefix,
|
||||
webuiSchema: cfg.DBWebUIName,
|
||||
webuiPrefix: cfg.DBWebUIPrefix,
|
||||
})
|
||||
}
|
||||
|
||||
func newTableNames(l tableLayout) (tableNames, error) {
|
||||
if !validTablePrefix(l.recordsPrefix) || !validTablePrefix(l.webuiPrefix) {
|
||||
return tableNames{}, errors.New("invalid table prefix")
|
||||
}
|
||||
if !validDBIdentifier(l.recordsSchema) || !validDBIdentifier(l.webuiSchema) {
|
||||
return tableNames{}, errors.New("invalid database name")
|
||||
}
|
||||
webui := func(name string) string {
|
||||
return qualifiedIdent(l.webuiSchema, l.webuiPrefix+"webui_"+name)
|
||||
}
|
||||
return tableNames{
|
||||
prefix: prefix,
|
||||
records: quoteIdent(prefix + "records"),
|
||||
schema: quoteIdent(prefix + "webui_schema"),
|
||||
users: quoteIdent(prefix + "webui_users"),
|
||||
sessions: quoteIdent(prefix + "webui_sessions"),
|
||||
delegations: quoteIdent(prefix + "webui_zone_delegations"),
|
||||
audit: quoteIdent(prefix + "webui_audit"),
|
||||
locks: quoteIdent(prefix + "webui_locks"),
|
||||
recordsSchema: l.recordsSchema,
|
||||
recordsTable: l.recordsPrefix + "records",
|
||||
webuiSchema: l.webuiSchema,
|
||||
webuiPrefix: l.webuiPrefix,
|
||||
records: qualifiedIdent(l.recordsSchema, l.recordsPrefix+"records"),
|
||||
schema: webui("schema"),
|
||||
users: webui("users"),
|
||||
sessions: webui("sessions"),
|
||||
delegations: webui("zone_delegations"),
|
||||
audit: webui("audit"),
|
||||
locks: webui("locks"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func qualifiedIdent(schema, name string) string {
|
||||
return quoteIdent(schema) + "." + quoteIdent(name)
|
||||
}
|
||||
|
||||
func quoteIdent(name string) string {
|
||||
return "`" + name + "`"
|
||||
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
|
||||
}
|
||||
|
||||
func (t tableNames) sql(query string) string {
|
||||
@@ -104,12 +136,12 @@ func (t tableNames) sql(query string) string {
|
||||
}
|
||||
|
||||
func (t tableNames) migrateLockName() string {
|
||||
return t.prefix + "webui_migrate"
|
||||
return t.webuiPrefix + "webui_migrate"
|
||||
}
|
||||
|
||||
func (t tableNames) zoneLockName(zone string) string {
|
||||
sum := sha256.Sum256([]byte(zone))
|
||||
return t.prefix + "webui:" + hex.EncodeToString(sum[:])[:16]
|
||||
return t.webuiPrefix + "webui:" + hex.EncodeToString(sum[:])[:16]
|
||||
}
|
||||
|
||||
func mysqlConfig(cfg Config) *mysql.Config {
|
||||
@@ -118,7 +150,7 @@ func mysqlConfig(cfg Config) *mysql.Config {
|
||||
mc.Passwd = cfg.DBPassword
|
||||
mc.Net = "tcp"
|
||||
mc.Addr = cfg.DBAddress()
|
||||
mc.DBName = cfg.DBName
|
||||
mc.DBName = cfg.DBWebUIName
|
||||
mc.ParseTime = true
|
||||
mc.Loc = time.UTC
|
||||
_ = mc.Apply(mysql.Charset("utf8mb4", ""))
|
||||
@@ -144,7 +176,7 @@ func openDatabase(cfg Config, log *slog.Logger) (*sql.DB, error) {
|
||||
db.SetMaxOpenConns(dbMaxOpenConns)
|
||||
db.SetMaxIdleConns(dbMaxIdleConns)
|
||||
db.SetConnMaxLifetime(dbConnMaxLifetime)
|
||||
log.Debug("database pool opened", "host", cfg.DBHost, "port", cfg.DBPort, "user", cfg.DBUser, "database", cfg.DBName, "tls", cfg.DBTLS)
|
||||
log.Debug("database pool opened", "host", cfg.DBHost, "port", cfg.DBPort, "user", cfg.DBUser, "database", cfg.DBName, "webui_database", cfg.DBWebUIName, "tls", cfg.DBTLS)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
@@ -246,10 +278,10 @@ var recordsContract = []contractColumn{
|
||||
}
|
||||
|
||||
func verifyRecordsContract(ctx context.Context, q dbtx, t tableNames, log *slog.Logger) error {
|
||||
table := t.prefix + "records"
|
||||
table := t.recordsTable
|
||||
rows, err := q.QueryContext(ctx,
|
||||
"SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?",
|
||||
table)
|
||||
"SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?",
|
||||
t.recordsSchema, table)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read records table columns: %w", err)
|
||||
}
|
||||
@@ -266,16 +298,16 @@ func verifyRecordsContract(ctx context.Context, q dbtx, t tableNames, log *slog.
|
||||
if err := rows.Err(); err != nil {
|
||||
return fmt.Errorf("read records table columns: %w", err)
|
||||
}
|
||||
if err := checkRecordsColumns(table, columns); err != nil {
|
||||
if err := checkRecordsColumns(t.recordsSchema, table, columns); err != nil {
|
||||
return err
|
||||
}
|
||||
log.DebugContext(ctx, "records table contract verified", "table", table)
|
||||
log.DebugContext(ctx, "records table contract verified", "database", t.recordsSchema, "table", table)
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkRecordsColumns(table string, columns map[string]columnInfo) error {
|
||||
func checkRecordsColumns(schema, table string, columns map[string]columnInfo) error {
|
||||
if len(columns) == 0 {
|
||||
return fmt.Errorf("%w: table %s does not exist in the configured database", errContract, table)
|
||||
return fmt.Errorf("%w: table %s does not exist in database %s or is not readable", errContract, table, schema)
|
||||
}
|
||||
for _, c := range recordsContract {
|
||||
info, ok := columns[c.name]
|
||||
|
||||
Reference in new issue
Block a user