2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2018-07-18 14:22:29 -04:00
|
|
|
"errors"
|
2019-10-11 20:46:03 -04:00
|
|
|
"fmt"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net/url"
|
2019-04-10 08:46:01 -04:00
|
|
|
"os"
|
2019-01-14 02:09:00 -05:00
|
|
|
"path/filepath"
|
2018-03-15 16:03:41 -04:00
|
|
|
"time"
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/event"
|
2022-07-27 12:44:59 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2023-04-04 13:52:24 -04:00
|
|
|
"github.com/minio/minio/internal/store"
|
2021-06-14 17:54:37 -04:00
|
|
|
xnet "github.com/minio/pkg/net"
|
2018-03-15 16:03:41 -04:00
|
|
|
)
|
|
|
|
|
2019-02-25 21:01:13 -05:00
|
|
|
const (
|
2022-09-27 20:23:28 -04:00
|
|
|
reconnectInterval = 5 * time.Second
|
2019-04-10 08:46:01 -04:00
|
|
|
storePrefix = "minio"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// MQTT input constants
|
|
|
|
const (
|
|
|
|
MqttBroker = "broker"
|
|
|
|
MqttTopic = "topic"
|
|
|
|
MqttQoS = "qos"
|
|
|
|
MqttUsername = "username"
|
|
|
|
MqttPassword = "password"
|
|
|
|
MqttReconnectInterval = "reconnect_interval"
|
|
|
|
MqttKeepAliveInterval = "keep_alive_interval"
|
|
|
|
MqttQueueDir = "queue_dir"
|
|
|
|
MqttQueueLimit = "queue_limit"
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvMQTTEnable = "MINIO_NOTIFY_MQTT_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvMQTTBroker = "MINIO_NOTIFY_MQTT_BROKER"
|
|
|
|
EnvMQTTTopic = "MINIO_NOTIFY_MQTT_TOPIC"
|
|
|
|
EnvMQTTQoS = "MINIO_NOTIFY_MQTT_QOS"
|
|
|
|
EnvMQTTUsername = "MINIO_NOTIFY_MQTT_USERNAME"
|
|
|
|
EnvMQTTPassword = "MINIO_NOTIFY_MQTT_PASSWORD"
|
|
|
|
EnvMQTTReconnectInterval = "MINIO_NOTIFY_MQTT_RECONNECT_INTERVAL"
|
|
|
|
EnvMQTTKeepAliveInterval = "MINIO_NOTIFY_MQTT_KEEP_ALIVE_INTERVAL"
|
|
|
|
EnvMQTTQueueDir = "MINIO_NOTIFY_MQTT_QUEUE_DIR"
|
|
|
|
EnvMQTTQueueLimit = "MINIO_NOTIFY_MQTT_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// MQTTArgs - MQTT target arguments.
|
|
|
|
type MQTTArgs struct {
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Broker xnet.URL `json:"broker"`
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
QoS byte `json:"qos"`
|
|
|
|
User string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
MaxReconnectInterval time.Duration `json:"reconnectInterval"`
|
|
|
|
KeepAlive time.Duration `json:"keepAliveInterval"`
|
|
|
|
RootCAs *x509.CertPool `json:"-"`
|
2019-01-14 02:09:00 -05:00
|
|
|
QueueDir string `json:"queueDir"`
|
2019-07-11 22:53:20 -04:00
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate MQTTArgs fields
|
|
|
|
func (m MQTTArgs) Validate() error {
|
|
|
|
if !m.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
u, err := xnet.ParseURL(m.Broker.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch u.Scheme {
|
|
|
|
case "ws", "wss", "tcp", "ssl", "tls", "tcps":
|
|
|
|
default:
|
|
|
|
return errors.New("unknown protocol in broker address")
|
|
|
|
}
|
2019-01-29 04:30:15 -05:00
|
|
|
if m.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(m.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
if m.QoS == 0 {
|
|
|
|
return errors.New("qos should be set to 1 or 2 if queueDir is set")
|
|
|
|
}
|
2019-01-14 02:09:00 -05:00
|
|
|
}
|
2019-01-29 04:30:15 -05:00
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// MQTTTarget - MQTT target.
|
|
|
|
type MQTTTarget struct {
|
2022-09-27 20:23:28 -04:00
|
|
|
lazyInit lazyInit
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
id event.TargetID
|
|
|
|
args MQTTArgs
|
|
|
|
client mqtt.Client
|
2023-04-04 13:52:24 -04:00
|
|
|
store store.Store[event.Event]
|
2019-10-15 22:39:14 -04:00
|
|
|
quitCh chan struct{}
|
2022-07-27 12:44:59 -04:00
|
|
|
loggerOnce logger.LogOnce
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *MQTTTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Name - returns the Name of the target.
|
|
|
|
func (target *MQTTTarget) Name() string {
|
|
|
|
return target.ID().String()
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Store returns any underlying store if set.
|
|
|
|
func (target *MQTTTarget) Store() event.TargetStore {
|
|
|
|
return target.store
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *MQTTTarget) IsActive() (bool, error) {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return target.isActive()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (target *MQTTTarget) isActive() (bool, error) {
|
2019-12-11 17:27:03 -05:00
|
|
|
if !target.client.IsConnectionOpen() {
|
2023-04-04 13:52:24 -04:00
|
|
|
return false, store.ErrNotConnected
|
2019-12-11 17:27:03 -05:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 16:34:48 -04:00
|
|
|
// send - sends an event to the mqtt.
|
|
|
|
func (target *MQTTTarget) send(eventData event.Event) error {
|
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
|
|
|
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
token := target.client.Publish(target.args.Topic, target.args.QoS, false, string(data))
|
2022-09-27 20:23:28 -04:00
|
|
|
if !token.WaitTimeout(reconnectInterval) {
|
2023-04-04 13:52:24 -04:00
|
|
|
return store.ErrNotConnected
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
2019-10-15 22:39:14 -04:00
|
|
|
return token.Error()
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send - reads an event from store and sends it to MQTT.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *MQTTTarget) Send(eventKey string) error {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-15 22:39:14 -04:00
|
|
|
// Do not send if the connection is not active.
|
2022-09-27 20:23:28 -04:00
|
|
|
_, err := target.isActive()
|
2019-12-11 17:27:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
eventData, err := target.store.Get(eventKey)
|
|
|
|
if err != nil {
|
2019-04-10 08:46:01 -04:00
|
|
|
// The last event key in a successful batch will be sent in the channel atmost once by the replayEvents()
|
|
|
|
// Such events will not exist and wouldve been already been sent successfully.
|
2019-10-11 20:46:03 -04:00
|
|
|
if os.IsNotExist(err) {
|
2019-04-10 08:46:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-11 20:46:03 -04:00
|
|
|
return err
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
if err = target.send(eventData); err != nil {
|
2018-03-15 16:03:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:46:01 -04:00
|
|
|
// Delete the event from store.
|
|
|
|
return target.store.Del(eventKey)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
// Save - saves the events to the store if queuestore is configured, which will
|
|
|
|
// be replayed when the mqtt connection is active.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *MQTTTarget) Save(eventData event.Event) error {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-22 16:34:48 -04:00
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not send if the connection is not active.
|
2022-09-27 20:23:28 -04:00
|
|
|
_, err := target.isActive()
|
2019-12-11 17:27:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return target.send(eventData)
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close - does nothing and available for interface compatibility.
|
|
|
|
func (target *MQTTTarget) Close() error {
|
2019-10-15 22:39:14 -04:00
|
|
|
target.client.Disconnect(100)
|
|
|
|
close(target.quitCh)
|
2018-03-15 16:03:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
func (target *MQTTTarget) init() error {
|
|
|
|
return target.lazyInit.Do(target.initMQTT)
|
|
|
|
}
|
2019-10-15 22:39:14 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
func (target *MQTTTarget) initMQTT() error {
|
|
|
|
args := target.args
|
2022-01-06 14:25:39 -05:00
|
|
|
|
2022-02-23 23:19:59 -05:00
|
|
|
// Using hex here, to make sure we avoid 23
|
|
|
|
// character limit on client_id according to
|
|
|
|
// MQTT spec.
|
|
|
|
clientID := fmt.Sprintf("%x", time.Now().UnixNano())
|
|
|
|
|
2018-03-26 19:45:21 -04:00
|
|
|
options := mqtt.NewClientOptions().
|
2022-02-23 23:19:59 -05:00
|
|
|
SetClientID(clientID).
|
2018-03-26 19:45:21 -04:00
|
|
|
SetCleanSession(true).
|
|
|
|
SetUsername(args.User).
|
|
|
|
SetPassword(args.Password).
|
|
|
|
SetMaxReconnectInterval(args.MaxReconnectInterval).
|
|
|
|
SetKeepAlive(args.KeepAlive).
|
|
|
|
SetTLSConfig(&tls.Config{RootCAs: args.RootCAs}).
|
|
|
|
AddBroker(args.Broker.String())
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
target.client = mqtt.NewClient(options)
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
token := target.client.Connect()
|
|
|
|
ok := token.WaitTimeout(reconnectInterval)
|
|
|
|
if !ok {
|
2023-04-04 13:52:24 -04:00
|
|
|
return store.ErrNotConnected
|
2022-09-27 20:23:28 -04:00
|
|
|
}
|
|
|
|
if token.Error() != nil {
|
|
|
|
return token.Error()
|
2019-10-11 20:46:03 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
yes, err := target.isActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !yes {
|
2023-04-04 13:52:24 -04:00
|
|
|
return store.ErrNotConnected
|
2022-09-27 20:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMQTTTarget - creates new MQTT target.
|
|
|
|
func NewMQTTTarget(id string, args MQTTArgs, loggerOnce logger.LogOnce) (*MQTTTarget, error) {
|
|
|
|
if args.MaxReconnectInterval == 0 {
|
|
|
|
// Default interval
|
|
|
|
// https://github.com/eclipse/paho.mqtt.golang/blob/master/options.go#L115
|
|
|
|
args.MaxReconnectInterval = 10 * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.KeepAlive == 0 {
|
|
|
|
args.KeepAlive = 10 * time.Second
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
var queueStore store.Store[event.Event]
|
2019-10-11 20:46:03 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-mqtt-"+id)
|
2023-04-04 13:52:24 -04:00
|
|
|
queueStore = store.NewQueueStore[event.Event](queueDir, args.QueueLimit, event.StoreExtension)
|
|
|
|
if err := queueStore.Open(); err != nil {
|
2022-09-27 20:23:28 -04:00
|
|
|
return nil, fmt.Errorf("unable to initialize the queue store of MQTT `%s`: %w", id, err)
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 20:23:28 -04:00
|
|
|
|
2022-11-07 11:01:24 -05:00
|
|
|
target := &MQTTTarget{
|
2022-09-27 20:23:28 -04:00
|
|
|
id: event.TargetID{ID: id, Name: "mqtt"},
|
|
|
|
args: args,
|
2023-04-04 13:52:24 -04:00
|
|
|
store: queueStore,
|
2022-09-27 20:23:28 -04:00
|
|
|
quitCh: make(chan struct{}),
|
|
|
|
loggerOnce: loggerOnce,
|
2022-11-07 11:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if target.store != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
store.StreamItems(target.store, target, target.quitCh, target.loggerOnce)
|
2022-11-07 11:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|