2018-03-15 16:03:41 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-03-15 16:03:41 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2018-07-18 14:22:29 -04:00
|
|
|
"errors"
|
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"
|
|
|
|
|
|
|
|
"github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
2019-02-25 21:01:13 -05:00
|
|
|
const (
|
|
|
|
reconnectInterval = 5 // In Seconds
|
2019-04-10 08:46:01 -04:00
|
|
|
storePrefix = "minio"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
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-02-25 21:01:13 -05:00
|
|
|
QueueLimit uint16 `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-05-22 16:34:48 -04:00
|
|
|
if m.QueueLimit > 10000 {
|
|
|
|
return errors.New("queueLimit should not exceed 10000")
|
|
|
|
}
|
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 {
|
|
|
|
id event.TargetID
|
|
|
|
args MQTTArgs
|
|
|
|
client mqtt.Client
|
2019-02-25 21:01:13 -05:00
|
|
|
store Store
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *MQTTTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
|
|
|
return token.Error()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
|
|
|
if !target.client.IsConnectionOpen() {
|
|
|
|
return errNotConnected
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2019-04-10 08:46:01 -04:00
|
|
|
eventData, eErr := target.store.Get(eventKey)
|
|
|
|
if eErr != nil {
|
|
|
|
// 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.
|
|
|
|
if os.IsNotExist(eErr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eErr
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-05-22 16:34:48 -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-05-22 16:34:48 -04:00
|
|
|
// Save - saves the events to the store if questore 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 {
|
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.
|
|
|
|
if !target.client.IsConnectionOpen() {
|
|
|
|
return errNotConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMQTTTarget - creates new MQTT target.
|
2019-04-10 08:46:01 -04:00
|
|
|
func NewMQTTTarget(id string, args MQTTArgs, doneCh <-chan struct{}) (*MQTTTarget, error) {
|
2018-03-26 19:45:21 -04:00
|
|
|
options := mqtt.NewClientOptions().
|
2019-01-29 04:30:15 -05:00
|
|
|
SetClientID("").
|
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
|
|
|
|
2019-02-25 21:01:13 -05:00
|
|
|
var store Store
|
|
|
|
|
2019-01-14 02:09:00 -05:00
|
|
|
if args.QueueDir != "" {
|
2019-04-10 08:46:01 -04:00
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-mqtt-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
2019-02-25 21:01:13 -05:00
|
|
|
if oErr := store.Open(); oErr != nil {
|
|
|
|
return nil, oErr
|
|
|
|
}
|
2019-01-14 02:09:00 -05:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
client := mqtt.NewClient(options)
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
// The client should establish a first time connection.
|
|
|
|
// Connect() should be successful atleast once to publish events.
|
2018-03-15 16:03:41 -04:00
|
|
|
token := client.Connect()
|
|
|
|
|
2019-05-22 16:34:48 -04:00
|
|
|
// Retries until the clientID gets registered.
|
|
|
|
retryRegister := func() {
|
2019-02-25 21:01:13 -05:00
|
|
|
// Repeat the pings until the client registers the clientId and receives a token.
|
|
|
|
for {
|
|
|
|
if token.Wait() && token.Error() == nil {
|
|
|
|
// Connected
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Reconnecting
|
|
|
|
time.Sleep(reconnectInterval * time.Second)
|
|
|
|
token = client.Connect()
|
|
|
|
}
|
2019-05-22 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if store == nil {
|
|
|
|
if token.Wait() && token.Error() != nil {
|
|
|
|
return nil, token.Error()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
go retryRegister()
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
target := &MQTTTarget{
|
2018-11-14 13:23:44 -05:00
|
|
|
id: event.TargetID{ID: id, Name: "mqtt"},
|
2018-03-15 16:03:41 -04:00
|
|
|
args: args,
|
|
|
|
client: client,
|
2019-02-25 21:01:13 -05:00
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
|
2019-05-22 16:34:48 -04:00
|
|
|
if target.store != nil {
|
|
|
|
// Replays the events from the store.
|
|
|
|
eventKeyCh := replayEvents(target.store, doneCh)
|
|
|
|
// Start replaying events from the store.
|
|
|
|
go sendEvents(target, eventKeyCh, doneCh)
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|