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
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
@@ -13,8 +14,10 @@ import (
|
||||
"git.mchus.pro/mchus/quoteforge/internal/appmeta"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/db"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
|
||||
"github.com/gin-gonic/gin"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SetupHandler struct {
|
||||
@@ -83,7 +86,7 @@ func (h *SetupHandler) TestConnection(c *gin.Context) {
|
||||
}
|
||||
|
||||
settings, err := h.localDB.GetSettings()
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_ = c.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read settings"})
|
||||
return
|
||||
@@ -94,7 +97,12 @@ func (h *SetupHandler) TestConnection(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dsn := buildMySQLDSN(host, port, database, user, password, 5*time.Second)
|
||||
tlsMode, caPEM := c.DefaultPostForm("tls_mode", "preferred"), c.PostForm("tls_ca_pem")
|
||||
dsn, err := buildMySQLDSNWithTLS(host, port, database, user, password, 5*time.Second, tlsMode, caPEM)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
lotCount, canWrite, err := db.ValidateMariaDBConnection(dsn)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
@@ -115,7 +123,12 @@ func (h *SetupHandler) TestConnection(c *gin.Context) {
|
||||
|
||||
// SaveConnection saves the connection settings and signals restart
|
||||
func (h *SetupHandler) SaveConnection(c *gin.Context) {
|
||||
existingSettings, _ := h.localDB.GetSettings()
|
||||
existingSettings, settingsErr := h.localDB.GetSettings()
|
||||
if settingsErr != nil && !errors.Is(settingsErr, gorm.ErrRecordNotFound) {
|
||||
_ = c.Error(settingsErr)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read settings"})
|
||||
return
|
||||
}
|
||||
|
||||
host := c.PostForm("host")
|
||||
portStr := c.PostForm("port")
|
||||
@@ -135,7 +148,12 @@ func (h *SetupHandler) SaveConnection(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Test connection first
|
||||
dsn := buildMySQLDSN(host, port, database, user, password, 5*time.Second)
|
||||
tlsMode, caPEM := c.DefaultPostForm("tls_mode", "preferred"), c.PostForm("tls_ca_pem")
|
||||
dsn, err := buildMySQLDSNWithTLS(host, port, database, user, password, 5*time.Second, tlsMode, caPEM)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if _, _, err := db.ValidateMariaDBConnection(dsn); err != nil {
|
||||
_ = c.Error(err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
@@ -146,7 +164,7 @@ func (h *SetupHandler) SaveConnection(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Save settings
|
||||
if err := h.localDB.SaveSettings(host, port, database, user, password); err != nil {
|
||||
if err := h.localDB.SaveSettingsWithTLS(host, port, database, user, password, tlsMode, caPEM); err != nil {
|
||||
_ = c.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"success": false,
|
||||
@@ -169,7 +187,8 @@ func (h *SetupHandler) SaveConnection(c *gin.Context) {
|
||||
existingSettings.Port != port ||
|
||||
existingSettings.Database != database ||
|
||||
existingSettings.User != user ||
|
||||
existingSettings.PasswordEncrypted != password
|
||||
existingSettings.PasswordEncrypted != password ||
|
||||
existingSettings.TLSMode != tlsMode || existingSettings.TLSCAPEM != caPEM
|
||||
|
||||
restartQueued := settingsChanged && h.restartSig != nil
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -199,7 +218,7 @@ func (h *SetupHandler) GetStatus(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func buildMySQLDSN(host string, port int, database, user, password string, timeout time.Duration) string {
|
||||
func buildMySQLDSNWithTLS(host string, port int, database, user, password string, timeout time.Duration, tlsMode, caPEM string) (string, error) {
|
||||
cfg := mysqlDriver.NewConfig()
|
||||
cfg.User = user
|
||||
cfg.Passwd = password
|
||||
@@ -212,7 +231,10 @@ func buildMySQLDSN(host string, port int, database, user, password string, timeo
|
||||
cfg.Params = map[string]string{
|
||||
"charset": "utf8mb4",
|
||||
}
|
||||
return cfg.FormatDSN()
|
||||
if err := mariadb.ConfigureTLS(cfg, tlsMode, caPEM); err != nil {
|
||||
return "", fmt.Errorf("configure TLS: %w", err)
|
||||
}
|
||||
return cfg.FormatDSN(), nil
|
||||
}
|
||||
|
||||
// setupPassword reuses a credential only for its saved destination and account.
|
||||
|
||||
Reference in new issue
Block a user