Skip to content

Commit

Permalink
backend: keep backend TLS config consistent with frontend TLS config (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Jun 6, 2023
1 parent e89da8f commit 95c9c3c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

var (
ErrCapabilityNegotiation = errors.New("capability negotiation failed")
ErrTLSConfigRequired = errors.New("require TLS config on TiProxy when require-backend-tls=true")
)

const unknownAuthPlugin = "auth_unknown_plugin"
Expand Down Expand Up @@ -300,7 +301,17 @@ func (auth *Authenticator) writeAuthHandshake(
}

var pkt []byte
if backendCapability&pnet.ClientSSL != 0 && backendTLSConfig != nil {
var enableTLS bool
if auth.requireBackendTLS {
if backendTLSConfig == nil {
return ErrTLSConfigRequired
}
enableTLS = true
} else {
// When client TLS is disabled, also disables proxy TLS.
enableTLS = pnet.Capability(auth.capability)&pnet.ClientSSL != 0 && backendCapability&pnet.ClientSSL != 0 && backendTLSConfig != nil
}
if enableTLS {
resp.Capability |= mysql.ClientSSL
pkt = pnet.MakeHandshakeResponse(resp)
// write SSL Packet
Expand Down
39 changes: 39 additions & 0 deletions pkg/proxy/backend/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,42 @@ func TestCustomAuth(t *testing.T) {
checker()
clean()
}

func TestEnableTLS(t *testing.T) {
tests := []struct {
cfg cfgOverrider
enabled bool
}{
{
cfg: func(cfg *testConfig) {
cfg.clientConfig.capability &= ^pnet.ClientSSL
cfg.backendConfig.capability |= pnet.ClientSSL
},
enabled: false,
},
{
cfg: func(cfg *testConfig) {
cfg.clientConfig.capability |= pnet.ClientSSL
cfg.backendConfig.capability |= pnet.ClientSSL
},
enabled: true,
},
{
// client enables TLS but backendTLSConfig is nil
cfg: func(cfg *testConfig) {
cfg.clientConfig.capability |= pnet.ClientSSL
cfg.proxyConfig.backendTLSConfig = nil
cfg.backendConfig.capability |= pnet.ClientSSL
},
enabled: false,
},
}
tc := newTCPConnSuite(t)
for _, test := range tests {
ts, clean := newTestSuite(t, tc, test.cfg)
ts.authenticateFirstTime(t, func(t *testing.T, _ *testSuite) {
require.Equal(t, test.enabled, ts.mb.capability&pnet.ClientSSL > 0)
})
clean()
}
}

0 comments on commit 95c9c3c

Please sign in to comment.