Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privilege: add min TLS version for LDAP #50527

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make it as a config such as grpc?

https://github.com/dfawley/grpc-go/blob/aa13a323eeff39203a2e20ab534d675e2360e425/credentials/tls.go#L176

GRPC makes it as a config. if you don't set it, it will use tls1.2

Copy link
Member Author

@bb7133 bb7133 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! We do have a config related to TLS version(https://docs-archive.pingcap.com/tidb/v7.2/tidb-configuration-file#tls-version), maybe we should control it using the config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us update this in the next PR.

})
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")
}