Skip to content

Commit

Permalink
add mysql tls config
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
flarco committed Dec 18, 2024
1 parent 27c75cf commit f4a459c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions core/dbio/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions core/dbio/database/database_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit f4a459c

Please sign in to comment.