From f4a459c330e389ec228ef2e11358ad6bc78b55f5 Mon Sep 17 00:00:00 2001 From: Fritz Larco Date: Wed, 18 Dec 2024 11:03:21 -0300 Subject: [PATCH] add mysql tls config - Added a unique connection ID to the MySQL connection to improve TLS configuration management. - The `Init()` function now registers the TLS configuration using the unique connection ID. - Added error handling for TLS configuration registration. - Modified `GetURL` to use the unique connection ID as the key for custom TLS configurations. - This prevents issues with multiple connections potentially using the same TLS configuration. --- core/dbio/database/database.go | 3 +-- core/dbio/database/database_mysql.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/dbio/database/database.go b/core/dbio/database/database.go index 40ae88d5..1a9df9ef 100755 --- a/core/dbio/database/database.go +++ b/core/dbio/database/database.go @@ -311,6 +311,7 @@ func NewConnContext(ctx context.Context, URL string, props ...string) (Connectio } // Init + conn.SetProp("sling_conn_id", g.RandSuffix(g.F("conn-%s-", conn.GetType()), 3)) conn.SetProp("orig_url", OrigURL) conn.SetProp("orig_prop_keys", g.Marshal(lo.Keys(conn.Base().properties))) // used when caching conn @@ -320,8 +321,6 @@ func NewConnContext(ctx context.Context, URL string, props ...string) (Connectio } err = conn.Init() - - conn.SetProp("sling_conn_id", g.RandSuffix(g.F("conn-%s-", conn.GetType()), 3)) return conn, err } diff --git a/core/dbio/database/database_mysql.go b/core/dbio/database/database_mysql.go index e6420e13..df15499d 100755 --- a/core/dbio/database/database_mysql.go +++ b/core/dbio/database/database_mysql.go @@ -8,6 +8,7 @@ import ( "runtime" "strings" + "github.com/go-sql-driver/mysql" "github.com/slingdata-io/sling-cli/core/dbio" "github.com/flarco/g" @@ -41,6 +42,14 @@ func (conn *MySQLConn) Init() error { conn.BaseConn.SetProp("allow_bulk_import", "false") } + // register tls + tlsConfig, err := conn.makeTlsConfig() + if err != nil { + return g.Error(err, "could not register tls") + } else if tlsConfig != nil { + mysql.RegisterTLSConfig(conn.GetProp("sling_conn_id"), tlsConfig) + } + instance := Connection(conn) conn.BaseConn.instance = &instance @@ -100,6 +109,11 @@ func (conn *MySQLConn) GetURL(newURL ...string) string { } } + // make tls + if query.Get("tls") == "custom" { + query.Set("tls", conn.GetProp("sling_conn_id")) + } + // reconstruct the url u.RawQuery = query.Encode() u, err = dburl.Parse(u.String())