feat: configure MariaDB TLS for setup and runtime connections (#5)
This commit is contained in:
1 parent
15f81e053c
commit
370407d3ba
9 files changed
+325
-9
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/appmeta"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/appstate"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
|
||||
"github.com/glebarez/sqlite"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
uuidpkg "github.com/google/uuid"
|
||||
@@ -24,6 +25,8 @@ import (
|
||||
|
||||
// ConnectionSettings stores MariaDB connection credentials
|
||||
type ConnectionSettings struct {
|
||||
TLSMode string `gorm:"default:preferred"`
|
||||
TLSCAPEM string
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Host string `gorm:"not null"`
|
||||
Port int `gorm:"not null;default:3306"`
|
||||
@@ -507,6 +510,14 @@ func (l *LocalDB) GetSettings() (*ConnectionSettings, error) {
|
||||
|
||||
// SaveSettings saves connection settings with encrypted password
|
||||
func (l *LocalDB) SaveSettings(host string, port int, database, user, password string) error {
|
||||
return l.SaveSettingsWithTLS(host, port, database, user, password, "preferred", "")
|
||||
}
|
||||
|
||||
func (l *LocalDB) SaveSettingsWithTLS(host string, port int, database, user, password, tlsMode, caPEM string) error {
|
||||
if err := mariadb.ConfigureTLS(mysqlDriver.NewConfig(), tlsMode, caPEM); err != nil {
|
||||
return fmt.Errorf("validate connection TLS: %w", err)
|
||||
}
|
||||
|
||||
// Encrypt password
|
||||
encrypted, err := Encrypt(password)
|
||||
if err != nil {
|
||||
@@ -515,6 +526,8 @@ func (l *LocalDB) SaveSettings(host string, port int, database, user, password s
|
||||
|
||||
settings := ConnectionSettings{
|
||||
ID: 1, // Always use ID=1 for single settings row
|
||||
TLSMode: tlsMode,
|
||||
TLSCAPEM: caPEM,
|
||||
Host: host,
|
||||
Port: port,
|
||||
Database: database,
|
||||
@@ -560,6 +573,9 @@ func (l *LocalDB) GetDSN() (string, error) {
|
||||
"charset": "utf8mb4",
|
||||
}
|
||||
|
||||
if err := mariadb.ConfigureTLS(cfg, settings.TLSMode, settings.TLSCAPEM); err != nil {
|
||||
return "", fmt.Errorf("configure connection TLS: %w", err)
|
||||
}
|
||||
return cfg.FormatDSN(), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package localdb
|
||||
|
||||
import (
|
||||
mysql "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSettingsTLSRoundTrip(t *testing.T) {
|
||||
t.Setenv("QFS_STATE_DIR", t.TempDir())
|
||||
local := newModeDB(t)
|
||||
for _, mode := range []string{"preferred", "required", "verify-full"} {
|
||||
if err := local.SaveSettingsWithTLS("db.example", 3306, "example", "example", "example", mode, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings, err := local.GetSettings()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if settings.TLSMode != mode {
|
||||
t.Fatalf("mode=%s, want %s", settings.TLSMode, mode)
|
||||
}
|
||||
dsn, err := local.GetDSN()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.TLS == nil || cfg.AllowFallbackToPlaintext != (mode == "preferred") {
|
||||
t.Fatalf("wrong TLS policy for %s", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user