From c1412dffb23aa39623a9aa948e2c6abc9441302f Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Wed, 31 Mar 2021 20:57:30 +0200 Subject: [PATCH] mqtt authentication --- config/config.go | 29 ++++++++++++++++++++++++----- main.go | 18 ++++++++++++++---- run_default.ps1 | 3 +++ weathersource/mqtt-source.go | 19 ++++++++++++++++--- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 31ae553..029ed64 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,9 @@ const influxURL = "https://influx.default-address.com" const mqttURL = "tcp://default-address.com:1883" const mqttTopic = "sensor/#" const defaultLocation = "default-location" +const mqttUser = "weather-api" +const mqttPassword = "weather-api" +const useAnonymousMqttAuthentication = false const mqttPublishInterval = time.Second const mqttMinDistToLastValue = 250 * time.Millisecond @@ -48,6 +51,18 @@ func GetMqttTopic() string { return getVariableWithDefault("WEATHER-API-MQTT_TOPIC", mqttTopic) } +func GetMqttUser() string { + return getVariableWithDefault("WEATHER-API-MQTT_USER", mqttTopic) +} + +func GetMqttPassword() string { + return getVariableWithDefault("WEATHER-API-MQTT_PASSWORD", mqttTopic) +} + +func UseAnonymousMqttAuthentication() bool { + return getVariableWithDefaultBool("WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION", useAnonymousMqttAuthentication) +} + func MqttPublishInterval() time.Duration { interval, err := strconv.ParseInt(os.Getenv("WEATHER-API-MQTT_PUBLISH_INTERVAL"), 10, 64) if err != nil { @@ -66,11 +81,7 @@ func MqttMinDistToLastValue() time.Duration { //common config func AllowUnregisteredSensors() bool { - allow, err := strconv.ParseBool(os.Getenv("WEATHER-API-ALLOW_UNREGISTERED_SENSORS")) - if err != nil { - return allowUnregisteredSensors - } - return allow + return getVariableWithDefaultBool("WEATHER-API-ALLOW_UNREGISTERED_SENSORS", allowUnregisteredSensors) } //helper @@ -81,3 +92,11 @@ func getVariableWithDefault(variableKey, defaultValue string) string { } return variable } + +func getVariableWithDefaultBool(variableKey string, defaultValue bool) bool { + ok, err := strconv.ParseBool(os.Getenv(variableKey)) + if err != nil { + return defaultValue + } + return ok +} diff --git a/main.go b/main.go index dac6aa8..c5312b3 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "os" "weather-data/api" "weather-data/config" @@ -33,13 +34,22 @@ func main() { defer weatherStorage.Close() //setup new weatherData source -> mqtt - weatherSource, err = weathersource.NewMqttSource( - config.GetMqttUrl(), - config.GetMqttTopic()) - + if config.UseAnonymousMqttAuthentication() { + weatherSource, err = weathersource.NewAnonymousMqttSource( + config.GetMqttUrl(), + config.GetMqttTopic()) + } else { + weatherSource, err = weathersource.NewMqttSource( + config.GetMqttUrl(), + config.GetMqttTopic(), + config.GetMqttUser(), + config.GetMqttPassword()) + } if err != nil { + fmt.Println("Could not connect to mqtt:", err.Error()) os.Exit(1) } + defer weatherSource.Close() weatherSource.AddNewWeatherDataCallback(handleNewWeatherData) diff --git a/run_default.ps1 b/run_default.ps1 index 184314b..7ddd90b 100644 --- a/run_default.ps1 +++ b/run_default.ps1 @@ -9,6 +9,9 @@ Set-Item -Path "Env:WEATHER-API-INFLUX_BUCKET" -Value "default-bucket" Set-Item -Path "Env:WEATHER-API-MQTT_URL" -Value "tcp://default-address.com:1883" Set-Item -Path "Env:WEATHER-API-MQTT_TOPIC" -Value "sensor/#" +Set-Item -Path "Env:WEATHER-API-MQTT_USER" -Value "weather-api" +Set-Item -Path "Env:WEATHER-API-MQTT_PASSWORD" -Value "weather-api" +Set-Item -Path "Env:WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION" -Value "false" Set-Item -Path "Env:WEATHER-API-MQTT_PUBLISH_INTERVAL" -Value "2500" Set-Item -Path "Env:WEATHER-API-MQTT_MIN_DIST_TO_LAST_VALUE" -Value "250" diff --git a/weathersource/mqtt-source.go b/weathersource/mqtt-source.go index 23fa130..477a7df 100644 --- a/weathersource/mqtt-source.go +++ b/weathersource/mqtt-source.go @@ -1,7 +1,6 @@ package weathersource import ( - "fmt" "regexp" "strconv" "strings" @@ -30,8 +29,18 @@ func (source *mqttWeatherSource) Close() { source.mqttClient.Disconnect(2) } +//NewAnonymousMqttSource Factory function for mqttWeatherSource with anonymous authentication +func NewAnonymousMqttSource(url, topic string) (*mqttWeatherSource, error) { + return newMqttSource(url, topic, "", "", true) +} + +//NewMqttSource Factory function for mqttWeatherSource with authentication +func NewMqttSource(url, topic, user, password string) (*mqttWeatherSource, error) { + return newMqttSource(url, topic, user, password, false) +} + //NewMqttSource Factory function for mqttWeatherSource -func NewMqttSource(url, topic string) (*mqttWeatherSource, error) { +func newMqttSource(url, topic, user, password string, anonymous bool) (*mqttWeatherSource, error) { source := new(mqttWeatherSource) source.url = url @@ -42,6 +51,11 @@ func NewMqttSource(url, topic string) (*mqttWeatherSource, error) { opts.SetDefaultPublishHandler(source.mqttMessageHandler()) opts.SetPingTimeout(1 * time.Second) + if !anonymous { + opts.Username = user + opts.Password = password + } + source.mqttClient = mqtt.NewClient(opts) if token := source.mqttClient.Connect(); token.Wait() && token.Error() != nil { @@ -69,7 +83,6 @@ func (source *mqttWeatherSource) mqttMessageHandler() mqtt.MessageHandler { if err != nil { return } - fmt.Println(sensorId) lastWeatherData, found := source.getUnwrittenDatapoints(sensorId)