Skip to content
Open
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
18 changes: 18 additions & 0 deletions pulsar/client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ func TestTLSInsecureConnection(t *testing.T) {
client.Close()
}

func TestTLSInsecureConnectionWithCerts(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test doesn't seem to do anything substantive. I'd expect it to exercise the resultant tls.Config in some way, maybe by making an actual TLS connection.

client, err := NewClient(ClientOptions{
URL: serviceURLTLS,
TLSAllowInsecureConnection: true,
TLSTrustCertsFilePath: caCertsPath,
})
assert.NoError(t, err)

producer, err := client.CreateProducer(ProducerOptions{
Topic: newTopicName(),
})

assert.NoError(t, err)
assert.NotNil(t, producer)

client.Close()
}

func TestTLSConnection(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: serviceURLTLS,
Expand Down
42 changes: 40 additions & 2 deletions pulsar/internal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,46 @@ func (c *connection) getTLSConfig() (*tls.Config, error) {
}
}

if c.tlsOptions.ValidateHostname {
tlsConfig.ServerName = c.physicalAddr.Hostname()
tlsConfig.ServerName = c.physicalAddr.Hostname()

if tlsConfig.InsecureSkipVerify {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why isn't c.tlsOptions.ValidateHostname used anywhere? I was thinking:

tlsConfig := &tls.Config{
    // disable built-in verification if hostname verification is to be skipped
		InsecureSkipVerify: c.tlsOptions.AllowInsecureConnection || !c.tlsOptions.ValidateHostname
	}

if !c.tlsOptions.AllowInsecureConnection && tlsConfig.InsecureSkipVerify {
  // implement a limited verification routine that checks the certificate but skips hostname verification 
  tlsConfig.VerifyPeerCertificate = func() {...}
}

// Solution is credited to https://github.com/golang/go/issues/21971
// Code is adapted from the original implementation of handshake_client.go at
// https://github.com/golang/go/blob/master/src/crypto/tls/handshake_client.go#L804
// disable the default verification; use customized VerifyPeerCertificate
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, certChain [][]*x509.Certificate) error {
// If this is the first handshake on a connection, process and
// (optionally) verify the server's certificates.
certs := make([]*x509.Certificate, len(rawCerts))
for i, asn1Data := range rawCerts {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return fmt.Errorf("tls: failed to parse server certificate error: %s", err.Error())
}
certs[i] = cert
}

if tlsConfig.RootCAs == nil {
return nil
}
Comment on lines +733 to +735
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How would this deal with the common scenario where the client is using the system's bundle? I am unsure of whether RootCAs is nil in that scenario.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just ran into this. As-is verification will return success when no roots are loaded.


// Verify certs if they exist but skip ServerName validation
opts := x509.VerifyOptions{
Roots: tlsConfig.RootCAs,
CurrentTime: time.Now(),
DNSName: "", // skip hostname verification
Intermediates: x509.NewCertPool(),
}

for i, cert := range certs {
if i == 0 {
continue
}
opts.Intermediates.AddCert(cert)
}
_, err := certs[0].Verify(opts)
return err
}
}

cert, err := c.auth.GetTLSCertificate()
Expand Down