TLS support for planetmint (#381)

* added TSL support for mqtt
* added configuration value mqtt-tls (bool) to support tls and non-tls connections (testing)


Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-04-24 14:59:17 +02:00 committed by GitHub
parent 43d152fcf6
commit b45c381b3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 0 deletions

View File

@ -23,6 +23,7 @@ mqtt-domain = "{{ .PlmntConfig.MqttDomain }}"
mqtt-port = {{ .PlmntConfig.MqttPort }}
mqtt-user = "{{ .PlmntConfig.MqttUser }}"
mqtt-password = "{{ .PlmntConfig.MqttPassword }}"
mqtt-tls = "{{ .PlmntConfig.MqttTLS }}"
`
@ -40,6 +41,7 @@ type Config struct {
MqttPort int `json:"mqtt-port" mapstructure:"mqtt-port"`
MqttUser string `json:"mqtt-user" mapstructure:"mqtt-user"`
MqttPassword string `json:"mqtt-password" mapstructure:"mqtt-password"`
MqttTLS bool `json:"mqtt-tls" mapstructure:"mqtt-tls"`
}
// cosmos-sdk wide global singleton
@ -63,6 +65,7 @@ func DefaultConfig() *Config {
MqttPort: 1885,
MqttUser: "user",
MqttPassword: "password",
MqttTLS: true,
}
}

View File

@ -1,6 +1,7 @@
package monitor
import (
"crypto/tls"
"math/rand"
"net"
"strconv"
@ -51,11 +52,19 @@ func LazyLoadMonitorMQTTClient() {
conf := config.GetConfig()
hostPort := net.JoinHostPort(conf.MqttDomain, strconv.FormatInt(int64(conf.MqttPort), 10))
uri := "tcp://" + hostPort
if conf.MqttTLS {
uri = "ssl://" + hostPort
}
opts := mqtt.NewClientOptions().AddBroker(uri)
opts.SetClientID(conf.ValidatorAddress + "-monitor")
opts.SetUsername(conf.MqttUser)
opts.SetPassword(conf.MqttPassword)
if conf.MqttTLS {
tlsConfig := &tls.Config{}
opts.SetTLSConfig(tlsConfig)
}
MonitorMQTTClient = mqtt.NewClient(opts)
}

View File

@ -1,6 +1,7 @@
package util
import (
"crypto/tls"
"encoding/json"
"net"
"strconv"
@ -42,11 +43,19 @@ func LazyLoadMQTTClient() {
conf := config.GetConfig()
hostPort := net.JoinHostPort(conf.MqttDomain, strconv.FormatInt(int64(conf.MqttPort), 10))
uri := "tcp://" + hostPort
if conf.MqttTLS {
uri = "ssl://" + hostPort
}
opts := mqtt.NewClientOptions().AddBroker(uri)
opts.SetClientID(conf.ValidatorAddress)
opts.SetUsername(conf.MqttUser)
opts.SetPassword(conf.MqttPassword)
if conf.MqttTLS {
tlsConfig := &tls.Config{}
opts.SetTLSConfig(tlsConfig)
}
MQTTClient = mqtt.NewClient(opts)
}