@@ -25,7 +25,7 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
25
25
if options == nil {
26
26
options = NewEnvironmentOptions ()
27
27
}
28
- client := newClient ("go-stream-locator" , nil , options .TCPParameters )
28
+ client := newClient ("go-stream-locator" , nil , options .TCPParameters , options . SaslConfiguration )
29
29
defer func (client * Client ) {
30
30
err := client .Close ()
31
31
if err != nil {
@@ -38,6 +38,12 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
38
38
return nil , fmt .Errorf (" MaxConsumersPerClient and MaxProducersPerClient must be between 1 and 254" )
39
39
}
40
40
41
+ if options .SaslConfiguration != nil {
42
+ if options .SaslConfiguration .Mechanism != SaslConfigurationPlain && options .SaslConfiguration .Mechanism != SaslConfigurationExternal {
43
+ return nil , fmt .Errorf ("SaslConfiguration mechanism must be PLAIN or EXTERNAL" )
44
+ }
45
+ }
46
+
41
47
if len (options .ConnectionParameters ) == 0 {
42
48
options .ConnectionParameters = []* Broker {newBrokerDefault ()}
43
49
}
@@ -76,7 +82,7 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
76
82
}
77
83
func (env * Environment ) newReconnectClient () (* Client , error ) {
78
84
broker := env .options .ConnectionParameters [0 ]
79
- client := newClient ("go-stream-locator" , broker , env .options .TCPParameters )
85
+ client := newClient ("go-stream-locator" , broker , env .options .TCPParameters , env . options . SaslConfiguration )
80
86
81
87
err := client .connect ()
82
88
tentatives := 1
@@ -86,7 +92,8 @@ func (env *Environment) newReconnectClient() (*Client, error) {
86
92
time .Sleep (time .Duration (tentatives ) * time .Second )
87
93
rand .Seed (time .Now ().UnixNano ())
88
94
n := rand .Intn (len (env .options .ConnectionParameters ))
89
- client = newClient ("stream-locator" , env .options .ConnectionParameters [n ], env .options .TCPParameters )
95
+ client = newClient ("stream-locator" , env .options .ConnectionParameters [n ], env .options .TCPParameters ,
96
+ env .options .SaslConfiguration )
90
97
tentatives = tentatives + 1
91
98
err = client .connect ()
92
99
@@ -261,6 +268,7 @@ func (env *Environment) IsClosed() bool {
261
268
type EnvironmentOptions struct {
262
269
ConnectionParameters []* Broker
263
270
TCPParameters * TCPParameters
271
+ SaslConfiguration * SaslConfiguration
264
272
MaxProducersPerClient int
265
273
MaxConsumersPerClient int
266
274
AddressResolver * AddressResolver
@@ -272,6 +280,7 @@ func NewEnvironmentOptions() *EnvironmentOptions {
272
280
MaxConsumersPerClient : 1 ,
273
281
ConnectionParameters : []* Broker {},
274
282
TCPParameters : newTCPParameterDefault (),
283
+ SaslConfiguration : newSaslConfigurationDefault (),
275
284
}
276
285
}
277
286
@@ -328,6 +337,14 @@ func (envOptions *EnvironmentOptions) SetVHost(vhost string) *EnvironmentOptions
328
337
return envOptions
329
338
}
330
339
340
+ func (envOptions * EnvironmentOptions ) SetSaslConfiguration (value string ) * EnvironmentOptions {
341
+ if envOptions .SaslConfiguration == nil {
342
+ envOptions .SaslConfiguration = newSaslConfigurationDefault ()
343
+ }
344
+ envOptions .SaslConfiguration .Mechanism = value
345
+ return envOptions
346
+ }
347
+
331
348
func (envOptions * EnvironmentOptions ) SetTLSConfig (config * tls.Config ) * EnvironmentOptions {
332
349
if envOptions .TCPParameters == nil {
333
350
envOptions .TCPParameters = newTCPParameterDefault ()
@@ -506,7 +523,7 @@ func (c *Client) maybeCleanConsumers(streamName string) {
506
523
}
507
524
}
508
525
509
- func (cc * environmentCoordinator ) newProducer (leader * Broker , tcpParameters * TCPParameters , streamName string ,
526
+ func (cc * environmentCoordinator ) newProducer (leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , streamName string ,
510
527
options * ProducerOptions ) (* Producer , error ) {
511
528
cc .mutex .Lock ()
512
529
defer cc .mutex .Unlock ()
@@ -521,7 +538,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
521
538
}
522
539
523
540
if clientResult == nil {
524
- clientResult = cc .newClientForProducer (leader , tcpParameters )
541
+ clientResult = cc .newClientForProducer (leader , tcpParameters , saslConfiguration )
525
542
}
526
543
527
544
err := clientResult .connect ()
@@ -538,7 +555,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
538
555
if err != nil {
539
556
return nil , err
540
557
}
541
- clientResult = cc .newClientForProducer (leader , tcpParameters )
558
+ clientResult = cc .newClientForProducer (leader , tcpParameters , saslConfiguration )
542
559
err = clientResult .connect ()
543
560
if err != nil {
544
561
return nil , err
@@ -555,8 +572,8 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
555
572
return producer , nil
556
573
}
557
574
558
- func (cc * environmentCoordinator ) newClientForProducer (leader * Broker , tcpParameters * TCPParameters ) * Client {
559
- clientResult := newClient ("go-stream-producer" , leader , tcpParameters )
575
+ func (cc * environmentCoordinator ) newClientForProducer (leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ) * Client {
576
+ clientResult := newClient ("go-stream-producer" , leader , tcpParameters , saslConfiguration )
560
577
chMeta := make (chan metaDataUpdateEvent , 1 )
561
578
clientResult .metadataListener = chMeta
562
579
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -575,7 +592,7 @@ func (cc *environmentCoordinator) newClientForProducer(leader *Broker, tcpParame
575
592
return clientResult
576
593
}
577
594
578
- func (cc * environmentCoordinator ) newConsumer (leader * Broker , tcpParameters * TCPParameters ,
595
+ func (cc * environmentCoordinator ) newConsumer (leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ,
579
596
streamName string , messagesHandler MessagesHandler ,
580
597
options * ConsumerOptions ) (* Consumer , error ) {
581
598
cc .mutex .Lock ()
@@ -591,7 +608,7 @@ func (cc *environmentCoordinator) newConsumer(leader *Broker, tcpParameters *TCP
591
608
}
592
609
593
610
if clientResult == nil {
594
- clientResult = newClient ("go-stream-consumer" , leader , tcpParameters )
611
+ clientResult = newClient ("go-stream-consumer" , leader , tcpParameters , saslConfiguration )
595
612
chMeta := make (chan metaDataUpdateEvent )
596
613
clientResult .metadataListener = chMeta
597
614
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -675,8 +692,8 @@ func (ps *producersEnvironment) newProducer(clientLocator *Client, streamName st
675
692
}
676
693
leader .cloneFrom (clientLocator .broker , resolver )
677
694
678
- producer , err := ps .producersCoordinator [coordinatorKey ].newProducer (leader , clientLocator .tcpParameters , streamName ,
679
- options )
695
+ producer , err := ps .producersCoordinator [coordinatorKey ].newProducer (leader , clientLocator .tcpParameters ,
696
+ clientLocator . saslConfiguration , streamName , options )
680
697
if err != nil {
681
698
return nil , err
682
699
}
@@ -740,7 +757,7 @@ func (ps *consumersEnvironment) NewSubscriber(clientLocator *Client, streamName
740
757
}
741
758
consumerBroker .cloneFrom (clientLocator .broker , resolver )
742
759
consumer , err := ps .consumersCoordinator [coordinatorKey ].
743
- newConsumer (consumerBroker , clientLocator .tcpParameters , streamName , messagesHandler , consumerOptions )
760
+ newConsumer (consumerBroker , clientLocator .tcpParameters , clientLocator . saslConfiguration , streamName , messagesHandler , consumerOptions )
744
761
if err != nil {
745
762
return nil , err
746
763
}
0 commit comments