Skip to content

Commit 273a3f2

Browse files
authored
Merge pull request #1692 from Shopify/diego_fix-tls-set-server-name
Set ServerName using tls.DialWithDialer approach
2 parents 8fe9db2 + cb29302 commit 273a3f2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

broker.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,22 @@ func (b *Broker) Open(conf *Config) error {
165165

166166
if conf.Net.TLS.Enable {
167167
Logger.Printf("Using tls")
168-
b.conn = tls.Client(b.conn, conf.Net.TLS.Config)
168+
cfg := conf.Net.TLS.Config
169+
if cfg == nil {
170+
cfg = &tls.Config{}
171+
}
172+
// If no ServerName is set, infer the ServerName
173+
// from the hostname we're connecting to.
174+
// Gets the hostname as tls.DialWithDialer does it.
175+
if cfg.ServerName == "" {
176+
colonPos := strings.LastIndex(b.addr, ":")
177+
if colonPos == -1 {
178+
colonPos = len(b.addr)
179+
}
180+
hostname := b.addr[:colonPos]
181+
cfg.ServerName = hostname
182+
}
183+
b.conn = tls.Client(b.conn, cfg)
169184
}
170185

171186
b.conn = newBufConn(b.conn)

client_tls_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ func TestTLS(t *testing.T) {
158158
Succeed: true,
159159
Server: serverTLSConfig,
160160
Client: &tls.Config{
161-
RootCAs: pool,
162-
ServerName: "127.0.0.1",
161+
RootCAs: pool,
163162
Certificates: []tls.Certificate{{
164163
Certificate: [][]byte{clientDer},
165164
PrivateKey: clientkey,

0 commit comments

Comments
 (0)