Skip to content

Commit d8672f7

Browse files
Fix TLS verify error when gh-ost discovers the replication master (#1487)
* Update connection.DuplicateCredentials function to set correct ServerName property Ensure the ServerName TLS property matches the new connection instance key hostname to avoid TLS verify errors like the following: 2025-01-02 02:07:26 FATAL tls: failed to verify certificate: x509: certificate is valid for [old host], not [new host] This is only one part of the fix for this issue. The second part, registering TLS Config with the mysql driver, will come in subsequent commits. * Use connection.DuplicateCredentials in cases where the connection key changes * Extract TLS config key name generation to GetDBTLSConfigKey function * Extract function to register a connection's TLS config with the mysql driver This allows us to register TLS configuration is the various places where connection configs are created and before they're used. * Register TLS config when setting up master connection info This ensures that the master's TLS config has been registered with the mysql driver before any connections are attempted. This is the second part of resolving the following TLS verify error: 2025-01-02 02:07:26 FATAL tls: failed to verify certificate: x509: certificate is valid for [old host], not [new host] * Register TLS config when setting up the throttler's connection info This ensures that the throttler's TLS config has been registered with the mysql driver before any connections are attempted. This is the second part of resolving the following TLS verify error: 2025-01-02 02:07:26 FATAL tls: failed to verify certificate: x509: certificate is valid for [old host], not [new host] --------- Co-authored-by: meiji163 <meiji163@github.com>
1 parent 2ea0e60 commit d8672f7

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

Diff for: go/logic/migrator.go

+3
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ func (this *Migrator) initiateInspector() (err error) {
791791
if this.migrationContext.CliMasterPassword != "" {
792792
this.migrationContext.ApplierConnectionConfig.Password = this.migrationContext.CliMasterPassword
793793
}
794+
if err := this.migrationContext.ApplierConnectionConfig.RegisterTLSConfig(); err != nil {
795+
return err
796+
}
794797
this.migrationContext.Log.Infof("Master forced to be %+v", *this.migrationContext.ApplierConnectionConfig.ImpliedKey)
795798
}
796799
// validate configs

Diff for: go/logic/throttler.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ func (this *Throttler) collectControlReplicasLag() {
215215
}
216216
lagResults := make(chan *mysql.ReplicationLagResult, instanceKeyMap.Len())
217217
for replicaKey := range *instanceKeyMap {
218-
connectionConfig := this.migrationContext.InspectorConnectionConfig.Duplicate()
219-
connectionConfig.Key = replicaKey
218+
connectionConfig := this.migrationContext.InspectorConnectionConfig.DuplicateCredentials(replicaKey)
219+
if err := connectionConfig.RegisterTLSConfig(); err != nil {
220+
return &mysql.ReplicationLagResult{Err: err}
221+
}
220222

221223
lagResult := &mysql.ReplicationLagResult{Key: connectionConfig.Key}
222224
go func() {

Diff for: go/mysql/connection.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (this *ConnectionConfig) DuplicateCredentials(key InstanceKey) *ConnectionC
5252
TransactionIsolation: this.TransactionIsolation,
5353
Charset: this.Charset,
5454
}
55+
56+
if this.tlsConfig != nil {
57+
config.tlsConfig = &tls.Config{
58+
ServerName: key.Hostname,
59+
Certificates: this.tlsConfig.Certificates,
60+
RootCAs: this.tlsConfig.RootCAs,
61+
InsecureSkipVerify: this.tlsConfig.InsecureSkipVerify,
62+
}
63+
}
64+
5565
config.ImpliedKey = &config.Key
5666
return config
5767
}
@@ -103,7 +113,20 @@ func (this *ConnectionConfig) UseTLS(caCertificatePath, clientCertificate, clien
103113
InsecureSkipVerify: allowInsecure,
104114
}
105115

106-
return mysql.RegisterTLSConfig(TLS_CONFIG_KEY, this.tlsConfig)
116+
return this.RegisterTLSConfig()
117+
}
118+
119+
func (this *ConnectionConfig) RegisterTLSConfig() error {
120+
if this.tlsConfig == nil {
121+
return nil
122+
}
123+
if this.tlsConfig.ServerName == "" {
124+
return errors.New("tlsConfig.ServerName cannot be empty")
125+
}
126+
127+
var tlsOption = GetDBTLSConfigKey(this.tlsConfig.ServerName)
128+
129+
return mysql.RegisterTLSConfig(tlsOption, this.tlsConfig)
107130
}
108131

109132
func (this *ConnectionConfig) TLSConfig() *tls.Config {
@@ -122,7 +145,7 @@ func (this *ConnectionConfig) GetDBUri(databaseName string) string {
122145
// simplify construction of the DSN below.
123146
tlsOption := "false"
124147
if this.tlsConfig != nil {
125-
tlsOption = TLS_CONFIG_KEY
148+
tlsOption = GetDBTLSConfigKey(this.tlsConfig.ServerName)
126149
}
127150

128151
if this.Charset == "" {
@@ -142,3 +165,7 @@ func (this *ConnectionConfig) GetDBUri(databaseName string) string {
142165

143166
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s", this.User, this.Password, hostname, this.Key.Port, databaseName, strings.Join(connectionParams, "&"))
144167
}
168+
169+
func GetDBTLSConfigKey(tlsServerName string) string {
170+
return fmt.Sprintf("%s-%s", TLS_CONFIG_KEY, tlsServerName)
171+
}

Diff for: go/mysql/connection_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func TestDuplicateCredentials(t *testing.T) {
5252
require.Equal(t, 3310, dup.ImpliedKey.Port)
5353
require.Equal(t, "gromit", dup.User)
5454
require.Equal(t, "penguin", dup.Password)
55-
require.Equal(t, c.tlsConfig, dup.tlsConfig)
55+
require.Equal(t, "otherhost", dup.tlsConfig.ServerName)
56+
require.Equal(t, c.tlsConfig.Certificates, dup.tlsConfig.Certificates)
57+
require.Equal(t, c.tlsConfig.RootCAs, dup.tlsConfig.RootCAs)
58+
require.Equal(t, c.tlsConfig.InsecureSkipVerify, dup.tlsConfig.InsecureSkipVerify)
5659
require.Equal(t, c.TransactionIsolation, dup.TransactionIsolation)
5760
require.Equal(t, c.Charset, dup.Charset)
5861
}
@@ -72,6 +75,7 @@ func TestDuplicate(t *testing.T) {
7275
require.Equal(t, 3306, dup.ImpliedKey.Port)
7376
require.Equal(t, "gromit", dup.User)
7477
require.Equal(t, "penguin", dup.Password)
78+
require.Equal(t, c.tlsConfig, dup.tlsConfig)
7579
require.Equal(t, transactionIsolation, dup.TransactionIsolation)
7680
require.Equal(t, "utf8mb4", dup.Charset)
7781
}
@@ -95,10 +99,17 @@ func TestGetDBUriWithTLSSetup(t *testing.T) {
9599
c.User = "gromit"
96100
c.Password = "penguin"
97101
c.Timeout = 1.2345
98-
c.tlsConfig = &tls.Config{}
102+
c.tlsConfig = &tls.Config{
103+
ServerName: c.Key.Hostname,
104+
}
99105
c.TransactionIsolation = transactionIsolation
100106
c.Charset = "utf8mb4_general_ci,utf8_general_ci,latin1"
101107

102108
uri := c.GetDBUri("test")
103-
require.Equal(t, `gromit:penguin@tcp(myhost:3306)/test?autocommit=true&interpolateParams=true&charset=utf8mb4_general_ci,utf8_general_ci,latin1&tls=ghost&transaction_isolation="REPEATABLE-READ"&timeout=1.234500s&readTimeout=1.234500s&writeTimeout=1.234500s`, uri)
109+
require.Equal(t, `gromit:penguin@tcp(myhost:3306)/test?autocommit=true&interpolateParams=true&charset=utf8mb4_general_ci,utf8_general_ci,latin1&tls=ghost-myhost&transaction_isolation="REPEATABLE-READ"&timeout=1.234500s&readTimeout=1.234500s&writeTimeout=1.234500s`, uri)
110+
}
111+
112+
func TestGetDBTLSConfigKey(t *testing.T) {
113+
configKey := GetDBTLSConfigKey("myhost")
114+
require.Equal(t, "ghost-myhost", configKey)
104115
}

Diff for: go/mysql/utils.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ func GetMasterConnectionConfigSafe(dbVersion string, connectionConfig *Connectio
136136
if !masterKey.IsValid() {
137137
return connectionConfig, nil
138138
}
139-
masterConfig = connectionConfig.Duplicate()
140-
masterConfig.Key = *masterKey
139+
140+
masterConfig = connectionConfig.DuplicateCredentials(*masterKey)
141+
if err := masterConfig.RegisterTLSConfig(); err != nil {
142+
return nil, err
143+
}
141144

142145
log.Debugf("%s of %+v is %+v", ReplicaTermFor(dbVersion, "master"), connectionConfig.Key, masterConfig.Key)
143146
if visitedKeys.HasKey(masterConfig.Key) {

0 commit comments

Comments
 (0)