2016-08-05 01:01:58 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
Add `access` format support for Elasticsearch notification target (#4006)
This change adds `access` format support for notifications to a
Elasticsearch server, and it refactors `namespace` format support.
In the case of `access` format, for each event in Minio, a JSON
document is inserted into Elasticsearch with its timestamp set to the
event's timestamp, and with the ID generated automatically by
elasticsearch. No events are modified or deleted in this mode.
In the case of `namespace` format, for each event in Minio, a JSON
document is keyed together by the bucket and object name is updated in
Elasticsearch. In the case of an object being created or over-written
in Minio, a new document or an existing document is inserted into the
Elasticsearch index. If an object is deleted in Minio, the
corresponding document is deleted from the Elasticsearch index.
Additionally, this change upgrades Elasticsearch support to the 5.x
series. This is a breaking change, and users of previous elasticsearch
versions should upgrade.
Also updates documentation on Elasticsearch notification target usage
and has a link to an elasticsearch upgrade guide.
This is the last patch that finally resolves #3928.
2017-03-31 17:11:27 -04:00
|
|
|
"fmt"
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/wildcard"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SQS type.
|
|
|
|
const (
|
|
|
|
// Minio sqs ARN prefix.
|
|
|
|
minioSqs = "arn:minio:sqs:"
|
|
|
|
|
|
|
|
// Static string indicating queue type 'amqp'.
|
|
|
|
queueTypeAMQP = "amqp"
|
2017-06-14 20:27:03 -04:00
|
|
|
// Static string indicating queue type 'mqtt'.
|
|
|
|
queueTypeMQTT = "mqtt"
|
2016-09-30 02:42:10 -04:00
|
|
|
// Static string indicating queue type 'nats'.
|
|
|
|
queueTypeNATS = "nats"
|
2016-08-05 01:01:58 -04:00
|
|
|
// Static string indicating queue type 'elasticsearch'.
|
|
|
|
queueTypeElastic = "elasticsearch"
|
|
|
|
// Static string indicating queue type 'redis'.
|
|
|
|
queueTypeRedis = "redis"
|
2016-10-03 20:29:55 -04:00
|
|
|
// Static string indicating queue type 'postgresql'.
|
|
|
|
queueTypePostgreSQL = "postgresql"
|
2017-03-17 12:29:17 -04:00
|
|
|
// Static string indicating queue type 'mysql'.
|
|
|
|
queueTypeMySQL = "mysql"
|
2016-12-15 11:23:48 -05:00
|
|
|
// Static string indicating queue type 'kafka'.
|
|
|
|
queueTypeKafka = "kafka"
|
2017-01-09 17:22:10 -05:00
|
|
|
// Static string for Webhooks
|
|
|
|
queueTypeWebhook = "webhook"
|
2017-03-27 14:27:25 -04:00
|
|
|
|
|
|
|
// Notifier format value constants
|
|
|
|
formatNamespace = "namespace"
|
|
|
|
formatAccess = "access"
|
2016-08-05 01:01:58 -04:00
|
|
|
)
|
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Topic type.
|
2016-08-05 01:01:58 -04:00
|
|
|
const (
|
2016-08-15 23:56:43 -04:00
|
|
|
// Minio topic ARN prefix.
|
|
|
|
minioTopic = "arn:minio:sns:"
|
2016-08-05 01:01:58 -04:00
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Static string indicating sns type 'listen'.
|
|
|
|
snsTypeMinio = "listen"
|
2016-08-05 01:01:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var errNotifyNotEnabled = errors.New("requested notifier not enabled")
|
|
|
|
|
|
|
|
// Returns true if queueArn is for an AMQP queue.
|
|
|
|
func isAMQPQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeAMQP {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
amqpL := globalServerConfig.Notify.GetAMQPByID(sqsArn.AccountID)
|
2016-08-05 01:01:58 -04:00
|
|
|
if !amqpL.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Connect to amqp server to validate.
|
|
|
|
amqpC, err := dialAMQP(amqpL)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to amqp service. %#v", amqpL)
|
|
|
|
return false
|
|
|
|
}
|
2017-09-04 20:45:30 -04:00
|
|
|
defer amqpC.conn.Close()
|
2016-08-05 01:01:58 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-06-14 20:27:03 -04:00
|
|
|
// Returns true if mqttARN is for an MQTT queue.
|
|
|
|
func isMQTTQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeMQTT {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
mqttL := globalServerConfig.Notify.GetMQTTByID(sqsArn.AccountID)
|
2017-06-14 20:27:03 -04:00
|
|
|
if !mqttL.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Connect to mqtt server to validate.
|
|
|
|
mqttC, err := dialMQTT(mqttL)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to mqtt service. %#v", mqttL)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer mqttC.Client.Disconnect(250)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-30 02:42:10 -04:00
|
|
|
// Returns true if natsArn is for an NATS queue.
|
|
|
|
func isNATSQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeNATS {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
natsL := globalServerConfig.Notify.GetNATSByID(sqsArn.AccountID)
|
2016-09-30 02:42:10 -04:00
|
|
|
if !natsL.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Connect to nats server to validate.
|
2017-01-11 19:41:05 -05:00
|
|
|
natsC, err := dialNATS(natsL, true)
|
2016-09-30 02:42:10 -04:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to nats service. %#v", natsL)
|
|
|
|
return false
|
|
|
|
}
|
2017-01-11 19:41:05 -05:00
|
|
|
closeNATS(natsC)
|
2016-09-30 02:42:10 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-09 17:22:10 -05:00
|
|
|
// Returns true if queueArn is for an Webhook queue
|
|
|
|
func isWebhookQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeWebhook {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
rNotify := globalServerConfig.Notify.GetWebhookByID(sqsArn.AccountID)
|
2017-03-08 13:00:47 -05:00
|
|
|
return rNotify.Enable
|
2017-01-09 17:22:10 -05:00
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Returns true if queueArn is for an Redis queue.
|
|
|
|
func isRedisQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeRedis {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
rNotify := globalServerConfig.Notify.GetRedisByID(sqsArn.AccountID)
|
2016-08-05 01:01:58 -04:00
|
|
|
if !rNotify.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Connect to redis server to validate.
|
|
|
|
rPool, err := dialRedis(rNotify)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to redis service. %#v", rNotify)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer rPool.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if queueArn is for an ElasticSearch queue.
|
|
|
|
func isElasticQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeElastic {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
esNotify := globalServerConfig.Notify.GetElasticSearchByID(sqsArn.AccountID)
|
2016-08-05 01:01:58 -04:00
|
|
|
if !esNotify.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
elasticC, err := dialElastic(esNotify)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to elasticsearch service %#v", esNotify)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer elasticC.Stop()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Returns true if queueArn is for PostgreSQL.
|
|
|
|
func isPostgreSQLQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypePostgreSQL {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
pgNotify := globalServerConfig.Notify.GetPostgreSQLByID(sqsArn.AccountID)
|
2016-10-03 20:29:55 -04:00
|
|
|
if !pgNotify.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
pgC, err := dialPostgreSQL(pgNotify)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to PostgreSQL server %#v", pgNotify)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer pgC.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-17 12:29:17 -04:00
|
|
|
// Returns true if queueArn is for MySQL.
|
|
|
|
func isMySQLQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeMySQL {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
msqlNotify := globalServerConfig.Notify.GetMySQLByID(sqsArn.AccountID)
|
2017-03-17 12:29:17 -04:00
|
|
|
if !msqlNotify.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
myC, err := dialMySQL(msqlNotify)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to connect to MySQL server %#v", msqlNotify)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer myC.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-15 11:23:48 -05:00
|
|
|
// Returns true if queueArn is for Kafka.
|
|
|
|
func isKafkaQueue(sqsArn arnSQS) bool {
|
|
|
|
if sqsArn.Type != queueTypeKafka {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
kafkaNotifyCfg := globalServerConfig.Notify.GetKafkaByID(sqsArn.AccountID)
|
2016-12-15 11:23:48 -05:00
|
|
|
if !kafkaNotifyCfg.Enable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
kafkaC, err := dialKafka(kafkaNotifyCfg)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to dial Kafka server %#v", kafkaNotifyCfg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer kafkaC.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Match function matches wild cards in 'pattern' for events.
|
|
|
|
func eventMatch(eventType string, events []string) (ok bool) {
|
|
|
|
for _, event := range events {
|
2016-08-27 03:27:17 -04:00
|
|
|
ok = wildcard.MatchSimple(event, eventType)
|
2016-08-05 01:01:58 -04:00
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter rule match, matches an object against the filter rules.
|
|
|
|
func filterRuleMatch(object string, frs []filterRule) bool {
|
|
|
|
var prefixMatch, suffixMatch = true, true
|
|
|
|
for _, fr := range frs {
|
|
|
|
if isValidFilterNamePrefix(fr.Name) {
|
2017-02-04 02:27:50 -05:00
|
|
|
prefixMatch = hasPrefix(object, fr.Value)
|
2016-08-05 01:01:58 -04:00
|
|
|
} else if isValidFilterNameSuffix(fr.Name) {
|
2017-02-04 02:27:50 -05:00
|
|
|
suffixMatch = hasSuffix(object, fr.Value)
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return prefixMatch && suffixMatch
|
|
|
|
}
|
Add `access` format support for Elasticsearch notification target (#4006)
This change adds `access` format support for notifications to a
Elasticsearch server, and it refactors `namespace` format support.
In the case of `access` format, for each event in Minio, a JSON
document is inserted into Elasticsearch with its timestamp set to the
event's timestamp, and with the ID generated automatically by
elasticsearch. No events are modified or deleted in this mode.
In the case of `namespace` format, for each event in Minio, a JSON
document is keyed together by the bucket and object name is updated in
Elasticsearch. In the case of an object being created or over-written
in Minio, a new document or an existing document is inserted into the
Elasticsearch index. If an object is deleted in Minio, the
corresponding document is deleted from the Elasticsearch index.
Additionally, this change upgrades Elasticsearch support to the 5.x
series. This is a breaking change, and users of previous elasticsearch
versions should upgrade.
Also updates documentation on Elasticsearch notification target usage
and has a link to an elasticsearch upgrade guide.
This is the last patch that finally resolves #3928.
2017-03-31 17:11:27 -04:00
|
|
|
|
|
|
|
// A type to represent dynamic error generation functions for
|
|
|
|
// notifications.
|
|
|
|
type notificationErrorFactoryFunc func(string, ...interface{}) error
|
|
|
|
|
|
|
|
// A function to build dynamic error generation functions for
|
|
|
|
// notifications by setting an error prefix string.
|
|
|
|
func newNotificationErrorFactory(prefix string) notificationErrorFactoryFunc {
|
|
|
|
return func(msg string, a ...interface{}) error {
|
|
|
|
s := fmt.Sprintf(msg, a...)
|
|
|
|
return fmt.Errorf("%s: %s", prefix, s)
|
|
|
|
}
|
|
|
|
}
|