-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(service): add mqtt service #149
feat(service): add mqtt service #149
Conversation
Co-authored-by: Mexazonic <[email protected]>
Codecov Report
@@ Coverage Diff @@
## main #149 +/- ##
==========================================
+ Coverage 63.08% 64.17% +1.09%
==========================================
Files 79 70 -9
Lines 2487 2010 -477
==========================================
- Hits 1569 1290 -279
+ Misses 780 606 -174
+ Partials 138 114 -24
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there are some things that needs addressing. I know that this isn't the easiest API to implement, so don't be too disheartened😄
As a global comment, you probably need to run go fmt
on the code, since it's not formatted.
Also, you do still have a lot of code copied from the telegram service that probably shouldn't be used (I added a comment about it as well, since you are now sending telegram API messages to MQTT)
Co-authored-by: Mexazonic <[email protected]>
Co-authored-by: Mexazonic <[email protected]>
Co-authored-by: Mexazonic <[email protected]>
@piksel , in this update, we solved the issues that you pointed out. Also, we added the TLS option and some docs. |
Co-authored-by: Mexazonic <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please complement your implementation with some tests verifying the intended behavior? Thanks
When generating a config object with/without optional arguments
I have nothing more to add. @piksel, if you feel that your feedback has been addressed, feel free to merge. Thank you @Mexazonic and @Eugeniosales for your contributions! 🔥 |
Is there anything more to be addressed, @piksel? If so, we'd like to get a feedback and keep working on it. |
Sorry, I have just been busy with other projects. Will get to it soon, I promise! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! The username and password ought to be specified using URL user:pass
fields, both for conistency and some planned logging features (hiding credentials by default in logging). I have marked these suggestions with (URL creds) so that they can be batch-merged if you want.
There is also some concern regarding the TLS configuration, but it should be fairly easy to fix!
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { | ||
|
||
return &url.URL{ | ||
Host: fmt.Sprintf("%s:%d", config.Host, config.Port), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(URL creds)
Host: fmt.Sprintf("%s:%d", config.Host, config.Port), | |
User: util.URLUserPassword(config.Username, config.Password), | |
Host: fmt.Sprintf("%s:%d", config.Host, config.Port), |
"strconv" | ||
|
||
"github.com/containrrr/shoutrrr/pkg/format" | ||
"github.com/containrrr/shoutrrr/pkg/types" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(URL creds)
"github.com/containrrr/shoutrrr/pkg/types" | |
"github.com/containrrr/shoutrrr/pkg/types" | |
"github.com/containrrr/shoutrrr/pkg/util" |
Username string `key:"username" default:"" desc:"username for auth"` | ||
Password string `key:"password" default:"" desc:"password for auth"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(URL creds)
Username string `key:"username" default:"" desc:"username for auth"` | |
Password string `key:"password" default:"" desc:"password for auth"` | |
Username string `key:"username" default:"" desc:"username for auth" url:"User"` | |
Password string `key:"password" default:"" desc:"password for auth" url:"Pass"` |
// GetTLSConfig returns the configuration with the certificates for TLS | ||
func (config *Config) GetTLSConfig() *tls.Config { | ||
certpool := x509.NewCertPool() | ||
ca, err := ioutil.ReadFile("ca.crt") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requiring the files to have this specific name and be in the working directory of the consuming app makes this really hard to use. Ideally, we should add some kind of generic way to add files/blobs to services (mostly for TLS, so perhaps even a specific TLS-cert interface...)
Right now, this should at least be configurable in the config
struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in #185
RootCAs: certpool, | ||
ClientAuth: tls.NoClientCert, | ||
ClientCAs: nil, | ||
InsecureSkipVerify: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of loading custom certs when verification is disabled? This should absolutely not be the default.
## Parameters Description | ||
|
||
- **Host** - MQTT broker server hostname or IP address (**Required**) | ||
Default: _empty_ | ||
Aliases: `host` | ||
|
||
- **Port** - MQTT server port, common ones are 8883 for TCP/TLS and 1883 for TCP (**Required**) | ||
Default: `8883` | ||
|
||
- **Topic** - Topic where the message is sent (**Required**) | ||
Default: _empty_ | ||
Aliases: `Topic` | ||
|
||
- **DisableTLS** - disable TLS/SSL Configurations | ||
Default: `false` | ||
|
||
- **ClientID** - The client identifier (ClientID) identifies each MQTT client that connects to an MQTT | ||
Default: _empty_ | ||
Aliases: `clientID` | ||
|
||
- **Username** - name of the sender to auth | ||
Default: _empty_ | ||
Aliases: `clientID` | ||
|
||
- **Password** - authentication password or hash | ||
Default: _empty_ | ||
Aliases: `password` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can auto-generated from the config tags
## Parameters Description | |
- **Host** - MQTT broker server hostname or IP address (**Required**) | |
Default: _empty_ | |
Aliases: `host` | |
- **Port** - MQTT server port, common ones are 8883 for TCP/TLS and 1883 for TCP (**Required**) | |
Default: `8883` | |
- **Topic** - Topic where the message is sent (**Required**) | |
Default: _empty_ | |
Aliases: `Topic` | |
- **DisableTLS** - disable TLS/SSL Configurations | |
Default: `false` | |
- **ClientID** - The client identifier (ClientID) identifies each MQTT client that connects to an MQTT | |
Default: _empty_ | |
Aliases: `clientID` | |
- **Username** - name of the sender to auth | |
Default: _empty_ | |
Aliases: `clientID` | |
- **Password** - authentication password or hash | |
Default: _empty_ | |
Aliases: `password` | |
--8<-- "docs/services/mqtt/config.md" |
## Optional parameters | ||
|
||
You can optionally specify the **`disableTLS`**, **`clientID`**, **`username`** and **`password`** parameters in the URL: | ||
_mqtt://**`host`**:**`port`**?topic=**`topic`**&disableTLS=true&clientID=**`clientID`**&username=**`username`**&password:**`password`**_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(URL creds)
_mqtt://**`host`**:**`port`**?topic=**`topic`**&disableTLS=true&clientID=**`clientID`**&username=**`username`**&password:**`password`**_ | |
_mqtt://**`username`**:**`password`**@**`host`**:**`port`**?topic=**`topic`**&disableTLS=true&clientID=**`clientID`**&username=**`username`**&password:**`password`**_ |
|
||
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error { | ||
|
||
config.Host = url.Hostname() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(URL creds)
config.Host = url.Hostname() | |
config.Host = url.Hostname() | |
config.Username = url.User.Username() | |
password, _ := url.User.Password() | |
config.Password = password |
Any progress on this? |
This has been untouched for so long that I wouldn't want to merge it. If anyone wants to pick it up again, or provide a completely different implementation, feel free to open a new PR. Closing. |
This adds a custom mqtt service as notification options that just publishes the message to a mqtt server such as mosquitto. This feature aims to contribute with the Issue 86 and Issue 710
The TLS connection option still needs to be added and some tests, but we would like to get a feedback and keep working on it. Thanks!