Skip to content

Commit 5b87693

Browse files
committed
stomp: override default behavior for "host" header
If not provided the go-stomp package looks at the address reported by the TCP socket's remote side. This is not useful if the server doesn't run at a fixed address. The connect function now attempts to parse and use the specified host name. Signed-off-by: Hank Donnay <[email protected]>
1 parent e2f264f commit 5b87693

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

notifier/amqp/deliverer_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func TestDeliverer(t *testing.T) {
4747
if uri == "" {
4848
uri = defaultRabbitMQURI
4949
}
50+
t.Logf("using uri: %q", uri)
5051

5152
conf.URIs = []string{
5253
// give a few bogus URIs to confirm failover mechanisms are working

notifier/amqp/directdeliverer_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestDirectDeliverer(t *testing.T) {
7979
if uri == "" {
8080
uri = defaultRabbitMQURI
8181
}
82+
t.Logf("using uri: %q", uri)
8283
conn, err := samqp.Dial(uri)
8384
if err != nil {
8485
t.Fatalf("failed to connect to broker at %v: %v", uri, err)

notifier/stomp/deliverer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/url"
8+
"time"
89

910
gostomp "github.com/go-stomp/stomp/v3"
1011
"github.com/google/uuid"
@@ -32,6 +33,10 @@ func New(conf *config.STOMP) (*Deliverer, error) {
3233
}
3334

3435
func (d *Deliverer) load(cfg *config.STOMP) error {
36+
d.fo.timeout = 30 * time.Second
37+
// TODO(hank) Wire up the "host" and "timeout" config somehow -- probably
38+
// just make the config URIs strings actual URIs and parse them out with
39+
// query parameters.
3540
var err error
3641
if cfg.TLS != nil {
3742
d.fo.tls, err = cfg.TLS.Config()
@@ -46,8 +51,8 @@ func (d *Deliverer) load(cfg *config.STOMP) error {
4651
}
4752
}
4853

49-
d.fo.uris = make([]string, len(cfg.URIs))
50-
copy(d.fo.uris, cfg.URIs)
54+
d.fo.addrs = make([]string, len(cfg.URIs))
55+
copy(d.fo.addrs, cfg.URIs)
5156
d.destination = cfg.Destination
5257
d.rollup = cfg.Rollup
5358
return nil

notifier/stomp/failover.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,48 @@ import (
1717
//
1818
// failOver is safe for concurrent usage.
1919
type failOver struct {
20-
tls *tls.Config
21-
login *config.Login
22-
uris []string
20+
tls *tls.Config
21+
login *config.Login
22+
addrs []string
23+
timeout time.Duration
2324
}
2425

25-
// Dial will dial the provided URI in accordance with the provided Config.
26+
// Dial will dial the provided address in accordance with the provided Config.
2627
//
2728
// Note: the STOMP protocol does not support multiplexing operations over a
2829
// single TCP connection. A TCP connection must be made for each STOMP
2930
// connection.
30-
func (f *failOver) Dial(ctx context.Context, uri string) (*gostomp.Conn, error) {
31+
func (f *failOver) Dial(ctx context.Context, addr string) (*gostomp.Conn, error) {
3132
var opts []func(*gostomp.Conn) error
3233
if f.login != nil {
3334
opts = append(opts, gostomp.ConnOpt.Login(f.login.Login, f.login.Passcode))
3435
}
36+
if host, _, err := net.SplitHostPort(addr); err == nil {
37+
opts = append(opts, gostomp.ConnOpt.Host(host))
38+
}
3539

3640
var d interface {
3741
DialContext(context.Context, string, string) (net.Conn, error)
3842
} = &net.Dialer{
39-
Timeout: 2 * time.Second,
43+
Timeout: f.timeout,
4044
}
4145
if f.tls != nil {
4246
d = &tls.Dialer{
4347
NetDialer: d.(*net.Dialer),
4448
Config: f.tls,
4549
}
4650
}
47-
conn, err := d.DialContext(ctx, "tcp", uri)
51+
conn, err := d.DialContext(ctx, "tcp", addr)
4852
if err != nil {
49-
return nil, fmt.Errorf("failed to connect to broker @ %v: %w", uri, err)
53+
return nil, fmt.Errorf("failed to connect to broker @ %v: %w", addr, err)
5054
}
5155

5256
stompConn, err := gostomp.Connect(conn, opts...)
5357
if err != nil {
5458
if conn != nil {
5559
conn.Close()
5660
}
57-
return nil, fmt.Errorf("stomp connect handshake to broker @ %v failed: %w", uri, err)
61+
return nil, fmt.Errorf("stomp connect handshake to broker @ %v failed: %w", addr, err)
5862
}
5963

6064
return stompConn, err
@@ -68,13 +72,13 @@ func (f *failOver) Dial(ctx context.Context, uri string) (*gostomp.Conn, error)
6872
func (f *failOver) Connection(ctx context.Context) (*gostomp.Conn, error) {
6973
ctx = zlog.ContextWithValues(ctx, "component", "notifier/stomp/failOver.Connection")
7074

71-
for _, uri := range f.uris {
72-
conn, err := f.Dial(ctx, uri)
75+
for _, addr := range f.addrs {
76+
conn, err := f.Dial(ctx, addr)
7377
if err != nil {
7478
zlog.Debug(ctx).
75-
Str("broker", uri).
79+
Str("broker", addr).
7680
Err(err).
77-
Msg("failed to dial broker. attempting next")
81+
Msg("failed to dial broker, attempting next")
7882
continue
7983
}
8084
return conn, nil

notifier/stomp/integration_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/quay/clair/v4/notifier"
2222
)
2323

24-
func setURI(t *testing.T, cfg *config.STOMP, uri string) (dial string, opt []func(*stomp.Conn) error) {
24+
func setURI(t *testing.T, cfg config.STOMP, uri string) (next config.STOMP, dial string, opt []func(*stomp.Conn) error) {
2525
const (
2626
defaultStompBrokerURI = "localhost:61613"
2727
)
@@ -30,28 +30,30 @@ func setURI(t *testing.T, cfg *config.STOMP, uri string) (dial string, opt []fun
3030
case uri == "":
3131
t.Logf("using default broker URI: %q", defaultStompBrokerURI)
3232
cfg.URIs = append(cfg.URIs, defaultStompBrokerURI)
33-
return defaultStompBrokerURI, nil
33+
return cfg, defaultStompBrokerURI, nil
3434
case strings.Contains(uri, "://"): // probably a URL
3535
u, err := url.Parse(uri)
3636
if err != nil {
3737
t.Logf("weird test URI: %q: %v", uri, err)
3838
return setURI(t, cfg, "")
3939
}
40-
t.Logf("using broker URI: %q", u.Host)
40+
t.Logf("using broker address: %q", u.Host)
4141
cfg.URIs = append(cfg.URIs, u.Host)
42+
t.Logf("using broker vhost: %q", u.Hostname())
43+
opt = append(opt, stomp.ConnOpt.Host(u.Hostname()))
4244
if u := u.User; u != nil {
4345
t.Logf("using login: %q", u.String())
4446
cfg.Login = &config.Login{
4547
Login: u.Username(),
4648
}
4749
cfg.Login.Passcode, _ = u.Password()
48-
opt = append(opt, stomp.ConnOpt.Login(u.Username(), cfg.Login.Passcode))
50+
opt = append(opt, stomp.ConnOpt.Login(cfg.Login.Login, cfg.Login.Passcode))
4951
}
50-
return u.Host, opt
52+
return cfg, u.Host, opt
5153
default:
5254
t.Logf("using broker URI: %q", uri)
5355
cfg.URIs = append(cfg.URIs, uri)
54-
return uri, nil
56+
return cfg, uri, nil
5557
}
5658
}
5759

@@ -125,7 +127,7 @@ func TestDeliverer(t *testing.T) {
125127
},
126128
}
127129
)
128-
dial, opt := setURI(t, &conf, os.Getenv("STOMP_CONNECTION_STRING"))
130+
conf, dial, opt := setURI(t, conf, os.Getenv("STOMP_CONNECTION_STRING"))
129131
opt = append(opt, stomp.ConnOpt.Logger(logAdapter{t}))
130132

131133
// test parallel usage
@@ -205,7 +207,7 @@ func TestDirectDeliverer(t *testing.T) {
205207
Rollup: tt.rollup,
206208
Destination: queue,
207209
}
208-
dial, opt := setURI(t, &conf, os.Getenv("STOMP_CONNECTION_STRING"))
210+
conf, dial, opt := setURI(t, conf, os.Getenv("STOMP_CONNECTION_STRING"))
209211

210212
noteID := uuid.New()
211213
notes := make([]notifier.Notification, 0, tt.notes)

0 commit comments

Comments
 (0)