Skip to content

Commit

Permalink
privilege: add min TLS version for LDAP (pingcap#50527)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 committed Jan 31, 2024
1 parent 38dd336 commit 0c1b094
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/privilege/privileges/ldap/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ go_test(
"test/ldap.key",
],
flaky = True,
shard_count = 3,
deps = ["@com_github_stretchr_testify//require"],
)
2 changes: 2 additions & 0 deletions pkg/privilege/privileges/ldap/ldap_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (impl *ldapAuthImpl) tryConnectLDAPThroughStartTLS(address string) (*ldap.C
err = ldapConnection.StartTLS(&tls.Config{
RootCAs: impl.caPool,
ServerName: impl.ldapServerHost,
MinVersion: tls.VersionTLS12,
})
if err != nil {
ldapConnection.Close()
Expand All @@ -136,6 +137,7 @@ func (impl *ldapAuthImpl) tryConnectLDAPThroughTLS(address string) (*ldap.Conn,
ldapConnection, err := ldap.DialTLS("tcp", address, &tls.Config{
RootCAs: impl.caPool,
ServerName: impl.ldapServerHost,
MinVersion: tls.VersionTLS12,
})
if err != nil {
return nil, err
Expand Down
64 changes: 64 additions & 0 deletions pkg/privilege/privileges/ldap/ldap_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,67 @@ func TestConnectThrough636(t *testing.T) {
require.NoError(t, err)
defer conn.Close()
}

func TestConnectWithTLS11(t *testing.T) {
var ln net.Listener

startListen := make(chan struct{})

// this test only tests whether the LDAP with LTS enabled will fallback from StartTLS
randomTLSServicePort := rand.Int()%10000 + 10000
serverWg := &sync.WaitGroup{}
serverWg.Add(1)
go func() {
defer close(startListen)
defer serverWg.Done()

cert, err := tls.X509KeyPair(tlsCrtStr, tlsKeyStr)
require.NoError(t, err)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
MaxVersion: tls.VersionTLS11,
}
ln, err = tls.Listen("tcp", fmt.Sprintf("localhost:%d", randomTLSServicePort), tlsConfig)
require.NoError(t, err)
startListen <- struct{}{}

for {
conn, err := ln.Accept()
if err != nil {
break
}

// handling one connection at a time is enough for test
func() {
defer func() {
require.NoError(t, conn.Close())
}()

r := bufio.NewReader(conn)
for {
_, err := r.ReadByte()
if err != nil {
break
}
}
}()
}
}()

<-startListen
defer func() {
require.NoError(t, ln.Close())
serverWg.Wait()
}()

impl := &ldapAuthImpl{}
impl.SetEnableTLS(true)
impl.SetLDAPServerHost("localhost")
impl.SetLDAPServerPort(randomTLSServicePort)

impl.caPool = x509.NewCertPool()
require.True(t, impl.caPool.AppendCertsFromPEM(tlsCAStr))

_, err := impl.connectionFactory()
require.ErrorContains(t, err, "protocol version not supported")
}

0 comments on commit 0c1b094

Please sign in to comment.