Skip to content

Commit

Permalink
use net.SplitHostPort and don't separate conf var
Browse files Browse the repository at this point in the history
  • Loading branch information
d1egoaz committed May 14, 2020
1 parent d871f33 commit 1628dc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 6 additions & 12 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,23 +1423,17 @@ func (b *Broker) registerCounter(name string) metrics.Counter {
return metrics.GetOrRegisterCounter(nameForBroker, b.conf.MetricRegistry)
}

func validServerNameTLS(addr string, conf *tls.Config) *tls.Config {
cfg := conf
func validServerNameTLS(addr string, cfg *tls.Config) *tls.Config {
if cfg == nil {
cfg = &tls.Config{}
}
// If no ServerName is set, infer the ServerName
// from the hostname we're connecting to.
// Gets the hostname as tls.DialWithDialer does it.
if cfg.ServerName == "" {
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname := addr[:colonPos]
// Make a copy to avoid polluting argument or default.
c := cfg.Clone()
c.ServerName = hostname
sn, _, err := net.SplitHostPort(addr)
if err != nil {
Logger.Println(fmt.Errorf("failed to get ServerName from addr %w", err))
}
c.ServerName = sn
cfg = c
}
return cfg
Expand Down
8 changes: 6 additions & 2 deletions client_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,20 @@ func doListenerTLSTest(t *testing.T, expectSuccess bool, serverConfig, clientCon
}

func TestSetServerName(t *testing.T) {
if validServerNameTLS("kafka-server.domain.com", nil).ServerName != "kafka-server.domain.com" {
if validServerNameTLS("kafka-server.domain.com:9093", nil).ServerName != "kafka-server.domain.com" {
t.Fatal("Expected kafka-server.domain.com as tls.ServerName when tls config is nil")
}

if validServerNameTLS("kafka-server.domain.com", &tls.Config{}).ServerName != "kafka-server.domain.com" {
if validServerNameTLS("kafka-server.domain.com:9093", &tls.Config{}).ServerName != "kafka-server.domain.com" {
t.Fatal("Expected kafka-server.domain.com as tls.ServerName when tls config ServerName is not provided")
}

c := &tls.Config{ServerName: "kafka-server-other.domain.com"}
if validServerNameTLS("", c).ServerName != "kafka-server-other.domain.com" {
t.Fatal("Expected kafka-server-other.domain.com as tls.ServerName when tls config ServerName is provided")
}

if validServerNameTLS("host-no-port", nil).ServerName != "" {
t.Fatal("Expected empty ServerName as the broker addr is missing the port")
}
}

0 comments on commit 1628dc1

Please sign in to comment.