Skip to content

Commit e1f5945

Browse files
author
Achille
authored
add port to SASL metadata (#780)
1 parent f7dd036 commit e1f5945

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

address.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,9 @@ func makeNetAddr(network string, addresses []string) net.Addr {
2020
}
2121

2222
func makeAddr(network, address string) net.Addr {
23-
host, port, _ := net.SplitHostPort(address)
24-
if port == "" {
25-
port = "9092"
26-
}
27-
if host == "" {
28-
host = address
29-
}
3023
return &networkAddress{
3124
network: network,
32-
address: net.JoinHostPort(host, port),
25+
address: canonicalAddress(address),
3326
}
3427
}
3528

conn.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"path/filepath"
1212
"runtime"
13-
"strconv"
1413
"sync"
1514
"sync/atomic"
1615
"time"
@@ -241,11 +240,10 @@ func (c *Conn) loadVersions() (apiVersionMap, error) {
241240
// connection was established to.
242241
func (c *Conn) Broker() Broker {
243242
addr := c.conn.RemoteAddr()
244-
host, port, _ := net.SplitHostPort(addr.String())
245-
portNumber, _ := strconv.Atoi(port)
243+
host, port, _ := splitHostPortNumber(addr.String())
246244
return Broker{
247245
Host: host,
248-
Port: portNumber,
246+
Port: port,
249247
ID: int(c.broker),
250248
Rack: c.rack,
251249
}

dialer.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kafka
33
import (
44
"context"
55
"crypto/tls"
6+
"fmt"
67
"io"
78
"net"
89
"strconv"
@@ -281,8 +282,13 @@ func (d *Dialer) connect(ctx context.Context, network, address string, connCfg C
281282
conn := NewConnWith(c, connCfg)
282283

283284
if d.SASLMechanism != nil {
285+
host, port, err := splitHostPortNumber(address)
286+
if err != nil {
287+
return nil, err
288+
}
284289
metadata := &sasl.Metadata{
285-
Host: address,
290+
Host: host,
291+
Port: port,
286292
}
287293
if err := d.authenticateSASL(sasl.WithMetadata(ctx, metadata), conn); err != nil {
288294
_ = conn.Close()
@@ -435,14 +441,28 @@ func backoff(attempt int, min time.Duration, max time.Duration) time.Duration {
435441
return d
436442
}
437443

444+
func canonicalAddress(s string) string {
445+
return net.JoinHostPort(splitHostPort(s))
446+
}
447+
438448
func splitHostPort(s string) (host string, port string) {
439449
host, port, _ = net.SplitHostPort(s)
440450
if len(host) == 0 && len(port) == 0 {
441451
host = s
452+
port = "9092"
442453
}
443454
return
444455
}
445456

457+
func splitHostPortNumber(s string) (host string, portNumber int, err error) {
458+
host, port := splitHostPort(s)
459+
portNumber, err = strconv.Atoi(port)
460+
if err != nil {
461+
return host, 0, fmt.Errorf("%s: %w", s, err)
462+
}
463+
return host, portNumber, nil
464+
}
465+
446466
func lookupHost(ctx context.Context, address string, resolver Resolver) (string, error) {
447467
host, port := splitHostPort(address)
448468

@@ -468,9 +488,5 @@ func lookupHost(ctx context.Context, address string, resolver Resolver) (string,
468488
}
469489
}
470490

471-
if port == "" {
472-
port = "9092"
473-
}
474-
475491
return net.JoinHostPort(host, port), nil
476492
}

sasl/sasl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Metadata struct {
5050
// Host is the address of the broker the authentication will be
5151
// performed on.
5252
Host string
53+
Port int
5354
}
5455

5556
// WithMetadata returns a copy of the context with associated Metadata.

transport.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -972,16 +972,12 @@ func (g *connGroup) grabConnOrConnect(ctx context.Context) (*conn, error) {
972972
broker := g.broker
973973

974974
if broker.ID < 0 {
975-
host, port, err := net.SplitHostPort(addr.String())
975+
host, port, err := splitHostPortNumber(addr.String())
976976
if err != nil {
977-
return nil, fmt.Errorf("%s: %w", addr, err)
978-
}
979-
portNumber, err := strconv.Atoi(port)
980-
if err != nil {
981-
return nil, fmt.Errorf("%s: %w", addr, err)
977+
return nil, err
982978
}
983979
broker.Host = host
984-
broker.Port = portNumber
980+
broker.Port = port
985981
}
986982

987983
ipAddrs, err := rslv.LookupBrokerIPAddr(ctx, broker)
@@ -1167,7 +1163,7 @@ func (g *connGroup) connect(ctx context.Context, addr net.Addr) (*conn, error) {
11671163

11681164
if tlsConfig := g.pool.tls; tlsConfig != nil {
11691165
if tlsConfig.ServerName == "" && !tlsConfig.InsecureSkipVerify {
1170-
host, _, _ := net.SplitHostPort(netAddr.String())
1166+
host, _ := splitHostPort(netAddr.String())
11711167
tlsConfig = tlsConfig.Clone()
11721168
tlsConfig.ServerName = host
11731169
}
@@ -1197,8 +1193,13 @@ func (g *connGroup) connect(ctx context.Context, addr net.Addr) (*conn, error) {
11971193
pc.SetDeadline(time.Time{})
11981194

11991195
if g.pool.sasl != nil {
1196+
host, port, err := splitHostPortNumber(netAddr.String())
1197+
if err != nil {
1198+
return nil, err
1199+
}
12001200
metadata := &sasl.Metadata{
1201-
Host: netAddr.String(),
1201+
Host: host,
1202+
Port: port,
12021203
}
12031204
if err := authenticateSASL(sasl.WithMetadata(ctx, metadata), pc, g.pool.sasl); err != nil {
12041205
return nil, err

0 commit comments

Comments
 (0)