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 (
|
2019-10-11 20:46:03 -04:00
|
|
|
"context"
|
2019-12-06 16:53:51 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-03-15 16:03:41 -04:00
|
|
|
"encoding/json"
|
2018-07-18 14:22:29 -04:00
|
|
|
"errors"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net/url"
|
2019-07-23 13:37:25 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2019-06-03 20:49:49 -04:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"github.com/nats-io/stan.go"
|
2018-03-15 16:03:41 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// NATS related constants
|
|
|
|
const (
|
2019-11-15 12:13:23 -05:00
|
|
|
NATSAddress = "address"
|
|
|
|
NATSSubject = "subject"
|
|
|
|
NATSUsername = "username"
|
|
|
|
NATSPassword = "password"
|
|
|
|
NATSToken = "token"
|
2019-12-06 16:53:51 -05:00
|
|
|
NATSTLS = "tls"
|
|
|
|
NATSTLSSkipVerify = "tls_skip_verify"
|
2019-11-15 12:13:23 -05:00
|
|
|
NATSPingInterval = "ping_interval"
|
|
|
|
NATSQueueDir = "queue_dir"
|
|
|
|
NATSQueueLimit = "queue_limit"
|
|
|
|
NATSCertAuthority = "cert_authority"
|
|
|
|
NATSClientCert = "client_cert"
|
|
|
|
NATSClientKey = "client_key"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
// Streaming constants
|
2019-11-13 20:38:05 -05:00
|
|
|
NATSStreaming = "streaming"
|
2019-10-23 01:59:13 -04:00
|
|
|
NATSStreamingClusterID = "streaming_cluster_id"
|
|
|
|
NATSStreamingAsync = "streaming_async"
|
|
|
|
NATSStreamingMaxPubAcksInFlight = "streaming_max_pub_acks_in_flight"
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvNATSEnable = "MINIO_NOTIFY_NATS_ENABLE"
|
2019-11-15 12:13:23 -05:00
|
|
|
EnvNATSAddress = "MINIO_NOTIFY_NATS_ADDRESS"
|
|
|
|
EnvNATSSubject = "MINIO_NOTIFY_NATS_SUBJECT"
|
|
|
|
EnvNATSUsername = "MINIO_NOTIFY_NATS_USERNAME"
|
|
|
|
EnvNATSPassword = "MINIO_NOTIFY_NATS_PASSWORD"
|
|
|
|
EnvNATSToken = "MINIO_NOTIFY_NATS_TOKEN"
|
2019-12-06 16:53:51 -05:00
|
|
|
EnvNATSTLS = "MINIO_NOTIFY_NATS_TLS"
|
|
|
|
EnvNATSTLSSkipVerify = "MINIO_NOTIFY_NATS_TLS_SKIP_VERIFY"
|
2019-11-15 12:13:23 -05:00
|
|
|
EnvNATSPingInterval = "MINIO_NOTIFY_NATS_PING_INTERVAL"
|
|
|
|
EnvNATSQueueDir = "MINIO_NOTIFY_NATS_QUEUE_DIR"
|
|
|
|
EnvNATSQueueLimit = "MINIO_NOTIFY_NATS_QUEUE_LIMIT"
|
|
|
|
EnvNATSCertAuthority = "MINIO_NOTIFY_NATS_CERT_AUTHORITY"
|
|
|
|
EnvNATSClientCert = "MINIO_NOTIFY_NATS_CLIENT_CERT"
|
|
|
|
EnvNATSClientKey = "MINIO_NOTIFY_NATS_CLIENT_KEY"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
// Streaming constants
|
2019-11-13 20:38:05 -05:00
|
|
|
EnvNATSStreaming = "MINIO_NOTIFY_NATS_STREAMING"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvNATSStreamingClusterID = "MINIO_NOTIFY_NATS_STREAMING_CLUSTER_ID"
|
|
|
|
EnvNATSStreamingAsync = "MINIO_NOTIFY_NATS_STREAMING_ASYNC"
|
|
|
|
EnvNATSStreamingMaxPubAcksInFlight = "MINIO_NOTIFY_NATS_STREAMING_MAX_PUB_ACKS_IN_FLIGHT"
|
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// NATSArgs - NATS target arguments.
|
|
|
|
type NATSArgs struct {
|
2019-11-15 12:13:23 -05:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Address xnet.Host `json:"address"`
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Token string `json:"token"`
|
2019-12-06 16:53:51 -05:00
|
|
|
TLS bool `json:"tls"`
|
|
|
|
TLSSkipVerify bool `json:"tlsSkipVerify"`
|
2019-11-15 12:13:23 -05:00
|
|
|
Secure bool `json:"secure"`
|
|
|
|
CertAuthority string `json:"certAuthority"`
|
|
|
|
ClientCert string `json:"clientCert"`
|
|
|
|
ClientKey string `json:"clientKey"`
|
|
|
|
PingInterval int64 `json:"pingInterval"`
|
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
|
|
|
Streaming struct {
|
2018-03-15 16:03:41 -04:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
ClusterID string `json:"clusterID"`
|
|
|
|
Async bool `json:"async"`
|
|
|
|
MaxPubAcksInflight int `json:"maxPubAcksInflight"`
|
|
|
|
} `json:"streaming"`
|
2019-12-06 16:53:51 -05:00
|
|
|
|
|
|
|
RootCAs *x509.CertPool `json:"-"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate NATSArgs fields
|
|
|
|
func (n NATSArgs) Validate() error {
|
|
|
|
if !n.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Address.IsEmpty() {
|
|
|
|
return errors.New("empty address")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Subject == "" {
|
|
|
|
return errors.New("empty subject")
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:13:23 -05:00
|
|
|
if n.ClientCert != "" && n.ClientKey == "" || n.ClientCert == "" && n.ClientKey != "" {
|
|
|
|
return errors.New("cert and key must be specified as a pair")
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:45:24 -04:00
|
|
|
if n.Username != "" && n.Password == "" || n.Username == "" && n.Password != "" {
|
|
|
|
return errors.New("username and password must be specified as a pair")
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
if n.Streaming.Enable {
|
|
|
|
if n.Streaming.ClusterID == "" {
|
|
|
|
return errors.New("empty cluster id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
if n.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(n.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
// To obtain a nats connection from args.
|
|
|
|
func (n NATSArgs) connectNats() (*nats.Conn, error) {
|
2019-11-15 12:13:23 -05:00
|
|
|
connOpts := []nats.Option{nats.Name("Minio Notification")}
|
|
|
|
if n.Username != "" && n.Password != "" {
|
|
|
|
connOpts = append(connOpts, nats.UserInfo(n.Username, n.Password))
|
|
|
|
}
|
|
|
|
if n.Token != "" {
|
|
|
|
connOpts = append(connOpts, nats.Token(n.Token))
|
|
|
|
}
|
2019-12-06 16:53:51 -05:00
|
|
|
if n.Secure || n.TLS && n.TLSSkipVerify {
|
2019-11-15 12:13:23 -05:00
|
|
|
connOpts = append(connOpts, nats.Secure(nil))
|
2019-12-06 16:53:51 -05:00
|
|
|
} else if n.TLS {
|
|
|
|
connOpts = append(connOpts, nats.Secure(&tls.Config{RootCAs: n.RootCAs}))
|
2019-11-15 12:13:23 -05:00
|
|
|
}
|
|
|
|
if n.CertAuthority != "" {
|
|
|
|
connOpts = append(connOpts, nats.RootCAs(n.CertAuthority))
|
|
|
|
}
|
|
|
|
if n.ClientCert != "" && n.ClientKey != "" {
|
|
|
|
connOpts = append(connOpts, nats.ClientCert(n.ClientCert, n.ClientKey))
|
|
|
|
}
|
|
|
|
return nats.Connect(n.Address.String(), connOpts...)
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// To obtain a streaming connection from args.
|
|
|
|
func (n NATSArgs) connectStan() (stan.Conn, error) {
|
|
|
|
scheme := "nats"
|
|
|
|
if n.Secure {
|
|
|
|
scheme = "tls"
|
|
|
|
}
|
2020-04-08 15:45:24 -04:00
|
|
|
|
|
|
|
var addressURL string
|
|
|
|
if n.Username != "" && n.Password != "" {
|
|
|
|
addressURL = scheme + "://" + n.Username + ":" + n.Password + "@" + n.Address.String()
|
|
|
|
} else if n.Token != "" {
|
|
|
|
addressURL = scheme + "://" + n.Token + "@" + n.Address.String()
|
|
|
|
} else {
|
|
|
|
addressURL = scheme + "://" + n.Address.String()
|
|
|
|
}
|
2019-07-23 13:37:25 -04:00
|
|
|
|
|
|
|
clientID, err := getNewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
connOpts := []stan.Option{stan.NatsURL(addressURL)}
|
|
|
|
if n.Streaming.MaxPubAcksInflight > 0 {
|
|
|
|
connOpts = append(connOpts, stan.MaxPubAcksInflight(n.Streaming.MaxPubAcksInflight))
|
|
|
|
}
|
|
|
|
|
|
|
|
return stan.Connect(n.Streaming.ClusterID, clientID, connOpts...)
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// NATSTarget - NATS target.
|
|
|
|
type NATSTarget struct {
|
2020-04-14 14:19:25 -04:00
|
|
|
id event.TargetID
|
|
|
|
args NATSArgs
|
|
|
|
natsConn *nats.Conn
|
|
|
|
stanConn stan.Conn
|
|
|
|
store Store
|
|
|
|
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *NATSTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2020-04-21 12:38:32 -04:00
|
|
|
// HasQueueStore - Checks if the queueStore has been configured for the target
|
|
|
|
func (target *NATSTarget) HasQueueStore() bool {
|
|
|
|
return target.store != nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *NATSTarget) IsActive() (bool, error) {
|
2020-04-14 14:19:25 -04:00
|
|
|
var connErr error
|
2019-07-23 13:37:25 -04:00
|
|
|
if target.args.Streaming.Enable {
|
2020-04-14 14:19:25 -04:00
|
|
|
if target.stanConn == nil || target.stanConn.NatsConn() == nil {
|
|
|
|
target.stanConn, connErr = target.args.connectStan()
|
|
|
|
} else {
|
|
|
|
if !target.stanConn.NatsConn().IsConnected() {
|
|
|
|
return false, errNotConnected
|
|
|
|
}
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-14 14:19:25 -04:00
|
|
|
if target.natsConn == nil {
|
|
|
|
target.natsConn, connErr = target.args.connectNats()
|
|
|
|
} else {
|
|
|
|
if !target.natsConn.IsConnected() {
|
|
|
|
return false, errNotConnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if connErr != nil {
|
|
|
|
if connErr.Error() == nats.ErrNoServers.Error() {
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, errNotConnected
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
return false, connErr
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save - saves the events to the store which will be replayed when the Nats connection is active.
|
|
|
|
func (target *NATSTarget) Save(eventData event.Event) error {
|
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-10 08:46:01 -04:00
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
// send - sends an event to the Nats.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *NATSTarget) send(eventData event.Event) error {
|
2018-03-15 16:03:41 -04:00
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
2018-11-14 13:23:44 -05:00
|
|
|
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
2018-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.stanConn != nil {
|
|
|
|
if target.args.Streaming.Async {
|
|
|
|
_, err = target.stanConn.PublishAsync(target.args.Subject, data, nil)
|
|
|
|
} else {
|
|
|
|
err = target.stanConn.Publish(target.args.Subject, data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = target.natsConn.Publish(target.args.Subject, data)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
// Send - sends event to Nats.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *NATSTarget) Send(eventKey string) error {
|
2020-04-14 14:19:25 -04:00
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-23 13:37:25 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := target.send(eventData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return target.store.Del(eventKey)
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// Close - closes underneath connections to NATS server.
|
|
|
|
func (target *NATSTarget) Close() (err error) {
|
|
|
|
if target.stanConn != nil {
|
2019-07-23 13:37:25 -04:00
|
|
|
// closing the streaming connection does not close the provided NATS connection.
|
|
|
|
if target.stanConn.NatsConn() != nil {
|
|
|
|
target.stanConn.NatsConn().Close()
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
err = target.stanConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.natsConn != nil {
|
|
|
|
target.natsConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNATSTarget - creates new NATS target.
|
2020-01-09 09:15:44 -05:00
|
|
|
func NewNATSTarget(id string, args NATSArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*NATSTarget, error) {
|
2018-03-15 16:03:41 -04:00
|
|
|
var natsConn *nats.Conn
|
|
|
|
var stanConn stan.Conn
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
var err error
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
var store Store
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2020-04-14 14:19:25 -04:00
|
|
|
target := &NATSTarget{
|
|
|
|
id: event.TargetID{ID: id, Name: "nats"},
|
|
|
|
args: args,
|
|
|
|
loggerOnce: loggerOnce,
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nats-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
|
|
|
if oErr := store.Open(); oErr != nil {
|
2020-04-14 14:19:25 -04:00
|
|
|
target.loggerOnce(context.Background(), oErr, target.ID())
|
|
|
|
return target, oErr
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
target.store = store
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-07-23 13:37:25 -04:00
|
|
|
if args.Streaming.Enable {
|
|
|
|
stanConn, err = args.connectStan()
|
2020-04-14 14:19:25 -04:00
|
|
|
target.stanConn = stanConn
|
2018-03-15 16:03:41 -04:00
|
|
|
} else {
|
2019-07-23 13:37:25 -04:00
|
|
|
natsConn, err = args.connectNats()
|
2020-04-14 14:19:25 -04:00
|
|
|
target.natsConn = natsConn
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-07-23 13:37:25 -04:00
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
2019-07-23 13:37:25 -04:00
|
|
|
if store == nil || err.Error() != nats.ErrNoServers.Error() {
|
2020-04-14 14:19:25 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
|
|
|
return target, err
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.store != nil && !test {
|
2019-07-23 13:37:25 -04:00
|
|
|
// Replays the events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
2019-07-23 13:37:25 -04:00
|
|
|
// Start replaying events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
2019-07-23 13:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|