@@ -2,12 +2,10 @@ package mq
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"time"
7
6
8
7
mqtt "github.com/eclipse/paho.mqtt.golang"
9
8
"github.com/gravitl/netmaker/logger"
10
- "github.com/gravitl/netmaker/logic"
11
9
"github.com/gravitl/netmaker/netclient/ncutils"
12
10
"github.com/gravitl/netmaker/servercfg"
13
11
)
@@ -24,68 +22,56 @@ var peer_force_send = 0
24
22
25
23
var mqclient mqtt.Client
26
24
27
- func Configure () {
25
+ func SetUpAdminClient () {
28
26
opts := mqtt .NewClientOptions ()
29
- broker , _ := servercfg .GetMessageQueueEndpoint ()
30
- opts .AddBroker (broker )
31
- id := ncutils .MakeRandomString (23 )
32
- opts .ClientID = id
33
- opts .SetUsername (mqDynSecAdmin )
34
- opts .SetPassword (adminPassword )
35
- opts .SetAutoReconnect (true )
36
- opts .SetConnectRetry (true )
37
- opts .SetConnectRetryInterval (time .Second << 2 )
38
- opts .SetKeepAlive (time .Minute )
39
- opts .SetWriteTimeout (time .Minute )
40
- mqclient := mqtt .NewClient (opts )
27
+ setMqOptions (mqAdminUserName , servercfg .GetMqAdminPassword (), opts )
28
+ mqAdminClient = mqtt .NewClient (opts )
29
+ opts .SetOnConnectHandler (func (client mqtt.Client ) {
30
+ if token := client .Subscribe (DynamicSecSubTopic , 0 , mqtt .MessageHandler (watchDynSecTopic )); token .WaitTimeout (MQ_TIMEOUT * time .Second ) && token .Error () != nil {
31
+ client .Disconnect (240 )
32
+ logger .Log (0 , "Dynamic security client subscription failed" )
33
+ }
34
+
35
+ opts .SetOrderMatters (true )
36
+ opts .SetResumeSubs (true )
37
+ })
41
38
tperiod := time .Now ().Add (10 * time .Second )
42
39
for {
43
- if token := mqclient .Connect (); ! token .WaitTimeout (MQ_TIMEOUT * time .Second ) || token .Error () != nil {
44
- logger .Log (2 , "unable to connect to broker, retrying ..." )
40
+ if token := mqAdminClient .Connect (); ! token .WaitTimeout (MQ_TIMEOUT * time .Second ) || token .Error () != nil {
41
+ logger .Log (2 , "Admin: unable to connect to broker, retrying ..." )
45
42
if time .Now ().After (tperiod ) {
46
43
if token .Error () == nil {
47
- logger .FatalLog ("could not connect to broker, token timeout, exiting ..." )
44
+ logger .FatalLog ("Admin: could not connect to broker, token timeout, exiting ..." )
48
45
} else {
49
- logger .FatalLog ("could not connect to broker, exiting ..." , token .Error ().Error ())
46
+ logger .FatalLog ("Admin: could not connect to broker, exiting ..." , token .Error ().Error ())
50
47
}
51
48
}
52
49
} else {
53
50
break
54
51
}
55
52
time .Sleep (2 * time .Second )
56
53
}
57
- newAdminPassword := logic .GenKey ()
58
- payload := MqDynsecPayload {
59
- Commands : []MqDynSecCmd {
60
- {
61
- Command : ModifyClientCmd ,
62
- Username : mqDynSecAdmin ,
63
- Password : newAdminPassword ,
64
- },
65
- },
66
- }
67
- d , _ := json .Marshal (payload )
68
- if token := mqclient .Publish (DynamicSecPubTopic , 0 , true , d ); token .Error () != nil {
69
- logger .FatalLog ("failed to modify admin password: " , token .Error ().Error ())
70
- }
71
- mqclient .Disconnect (2 )
72
- adminPassword = newAdminPassword
54
+
73
55
}
74
56
75
- // SetupMQTT creates a connection to broker and return client
76
- func SetupMQTT () {
77
- opts := mqtt .NewClientOptions ()
57
+ func setMqOptions (user , password string , opts * mqtt.ClientOptions ) {
78
58
broker , _ := servercfg .GetMessageQueueEndpoint ()
79
59
opts .AddBroker (broker )
80
60
id := ncutils .MakeRandomString (23 )
81
61
opts .ClientID = id
82
- opts .SetUsername (mqDynSecAdmin )
83
- opts .SetPassword (adminPassword )
62
+ opts .SetUsername (user )
63
+ opts .SetPassword (password )
84
64
opts .SetAutoReconnect (true )
85
65
opts .SetConnectRetry (true )
86
66
opts .SetConnectRetryInterval (time .Second << 2 )
87
67
opts .SetKeepAlive (time .Minute )
88
68
opts .SetWriteTimeout (time .Minute )
69
+ }
70
+
71
+ // SetupMQTT creates a connection to broker and return client
72
+ func SetupMQTT () {
73
+ opts := mqtt .NewClientOptions ()
74
+ setMqOptions (mqNetmakerServerUserName , servercfg .GetMqAdminPassword (), opts )
89
75
opts .SetOnConnectHandler (func (client mqtt.Client ) {
90
76
if token := client .Subscribe ("ping/#" , 2 , mqtt .MessageHandler (Ping )); token .WaitTimeout (MQ_TIMEOUT * time .Second ) && token .Error () != nil {
91
77
client .Disconnect (240 )
0 commit comments