Repository: crazy-max/diun
Pull request #292 (2021-03-07 15:06:06)

allow to configure scheme for MQTT broker
## Use case
With MQTT server behind a reverse proxy, I want to enable TLS connection between diun and MQTT

## Manual tests
I tested the four kind of connection manually and it works as expected

## Note about naming
The [paho documentation](https://pkg.go.dev/github.com/eclipse/paho.mqtt.golang#ClientOptions.AddBroker) only mention `tcp`/`ssl`/`ws`
But [the library support](https://github.com/eclipse/paho.mqtt.golang/blob/54b5153cb955d512bc82f56455769e61ca3d21e9/netconn.go#L34):
* `ws`
* `wss`
* `mqtt`/`tcp`
* `ssl`/`tls`/`mqtts`/`mqtt+ssl`/`tcps`
* `unix`

Moreover, the only *official* mention of scheme I found, it's [on the mqtt.org wiki](https://github.com/mqtt/mqtt.org/wiki/URI-Scheme#scheme) and seem to prefer `mqtt`/`mqtts` over `tcp`/`ssl`

IMO I prefer `mqtt` over `tcp` since it's less technical and more explicit for the user :man_shrugging: 

codecov[bot]: # [Codecov](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=h1) Report
> Merging [#292](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=desc) (0c82eba) into [master](https://codecov.io/gh/crazy-max/diun/commit/456b1f5e5c545f4ff78384d53853c73c636159d5?el=desc) (456b1f5) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/crazy-max/diun/pull/292/graphs/tree.svg?width=650&height=150&src=pr&token=phc5KYIjsV)](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #292   +/-   ##
=======================================
  Coverage   70.95%   70.95%           
=======================================
  Files           8        8           
  Lines         241      241           
=======================================
  Hits          171      171           
  Misses         44       44           
  Partials       26       26           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=footer). Last update [456b1f5...5a470d5](https://codecov.io/gh/crazy-max/diun/pull/292?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/notif/mqtt.md b/docs/notif/mqtt.md
index d4ae4ffc..8f082d5e 100644
--- a/docs/notif/mqtt.md
+++ b/docs/notif/mqtt.md
@@ -8,6 +8,7 @@ You can send notifications to any MQTT compatible server with the following sett
     ```yaml
     notif:
       mqtt:
+        scheme: mqtt
         host: localhost
         port: 1883
         username: guest
@@ -19,6 +20,7 @@ You can send notifications to any MQTT compatible server with the following sett
 
 | Name               | Default       | Description   |
 |--------------------|---------------|---------------|
+| `scheme`[^1]       | `mqtt`        | MQTT server scheme (`mqtt`, `mqtts`, `ws` or `wss`) |
 | `host`[^1]         | `localhost`   | MQTT server host |
 | `port`[^1]         | `1883`        | MQTT server port |
 | `username`         |               | MQTT username |
@@ -30,6 +32,7 @@ You can send notifications to any MQTT compatible server with the following sett
 | `qos`              | `0`           | Ensured message delivery at specified Quality of Service (QoS) |
 
 !!! abstract "Environment variables"
+    * `DIUN_NOTIF_MQTT_SCHEME`
     * `DIUN_NOTIF_MQTT_HOST`
     * `DIUN_NOTIF_MQTT_PORT`
     * `DIUN_NOTIF_MQTT_USERNAME`
diff --git a/internal/config/fixtures/config.validate.yml b/internal/config/fixtures/config.validate.yml
index 20e7ecca..c631d0b9 100644
--- a/internal/config/fixtures/config.validate.yml
+++ b/internal/config/fixtures/config.validate.yml
@@ -44,6 +44,7 @@ notif:
     password: bar
     roomID: "!abcdefGHIjklmno:matrix.org"
   mqtt:
+    scheme: "mqtt"
     host: "localhost"
     port: 1883
     username: "guest"
diff --git a/internal/model/notif_mqtt.go b/internal/model/notif_mqtt.go
index 65a460c3..ea10efaf 100644
--- a/internal/model/notif_mqtt.go
+++ b/internal/model/notif_mqtt.go
@@ -1,6 +1,7 @@
 package model
 
 type NotifMqtt struct {
+	Scheme       string `yaml:"scheme,omitempty" json:"scheme,omitempty" validate:"required,oneof=mqtt mqtts ws wss"`
 	Host         string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"`
 	Port         int    `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"`
 	Username     string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"`
@@ -21,6 +22,7 @@ func (s *NotifMqtt) GetDefaults() *NotifMqtt {
 
 // SetDefaults sets the default values
 func (s *NotifMqtt) SetDefaults() {
+	s.Scheme = "mqtt"
 	s.Host = "localhost"
 	s.Port = 1883
 	s.QoS = 0
diff --git a/internal/notif/mqtt/client.go b/internal/notif/mqtt/client.go
index 7b16397d..1257f346 100644
--- a/internal/notif/mqtt/client.go
+++ b/internal/notif/mqtt/client.go
@@ -49,7 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
 		return err
 	}
 
-	broker := fmt.Sprintf("tcp://%s:%d", c.cfg.Host, c.cfg.Port)
+	broker := fmt.Sprintf("%s://%s:%d", c.cfg.Scheme, c.cfg.Host, c.cfg.Port)
 	opts := MQTT.NewClientOptions().AddBroker(broker).SetClientID(c.cfg.Client)
 	opts.Username = username
 	opts.Password = password


Test patch:
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index d9bf694c..365b37ae 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -123,6 +123,7 @@ func TestLoadFile(t *testing.T) {
 						MsgType:       model.NotifMatrixMsgTypeNotice,
 					},
 					Mqtt: &model.NotifMqtt{
+						Scheme:   "mqtt",
 						Host:     "localhost",
 						Port:     1883,
 						Username: "guest",
diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml
index 6940ee69..17fb777b 100644
--- a/internal/config/fixtures/config.test.yml
+++ b/internal/config/fixtures/config.test.yml
@@ -44,6 +44,7 @@ notif:
     password: bar
     roomID: "!abcdefGHIjklmno:matrix.org"
   mqtt:
+    scheme: "mqtt"
     host: "localhost"
     port: 1883
     username: "guest"