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 (
|
|
|
|
"bytes"
|
2016-10-12 04:03:50 -04:00
|
|
|
"encoding/json"
|
2016-08-05 01:01:58 -04:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2016-08-25 02:04:34 -04:00
|
|
|
"net"
|
2016-08-05 01:01:58 -04:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
type externalNotifier struct {
|
|
|
|
// Per-bucket notification config. This is updated via
|
|
|
|
// PutBucketNotification API.
|
|
|
|
notificationConfigs map[string]*notificationConfig
|
|
|
|
|
|
|
|
// An external target keeps a connection to an external
|
|
|
|
// service to which events are to be sent. It is a mapping
|
|
|
|
// from an ARN to a log object
|
|
|
|
targets map[string]*logrus.Logger
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
rwMutex *sync.RWMutex
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
type internalNotifier struct {
|
|
|
|
// per-bucket listener configuration. This is updated
|
|
|
|
// when listeners connect or disconnect.
|
|
|
|
listenerConfigs map[string][]listenerConfig
|
|
|
|
|
|
|
|
// An internal target is a peer Minio server, that is
|
|
|
|
// connected to a listening client. Here, targets is a map of
|
|
|
|
// listener ARN to log object.
|
|
|
|
targets map[string]*listenerLogger
|
|
|
|
|
|
|
|
// Connected listeners is a map of listener ARNs to channels
|
|
|
|
// on which the ListenBucket API handler go routine is waiting
|
|
|
|
// for events to send to a client.
|
|
|
|
connectedListeners map[string]chan []NotificationEvent
|
|
|
|
|
|
|
|
rwMutex *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global event notification configuration. This structure has state
|
|
|
|
// about configured external notifications, and run-time configuration
|
|
|
|
// for listener notifications.
|
|
|
|
type eventNotifier struct {
|
|
|
|
|
|
|
|
// `external` here refers to notification configuration to
|
|
|
|
// send events to supported external systems
|
|
|
|
external externalNotifier
|
|
|
|
|
|
|
|
// `internal` refers to notification configuration for live
|
|
|
|
// listening clients. Events for a client are send from all
|
|
|
|
// servers, internally to a particular server that is
|
|
|
|
// connected to the client.
|
|
|
|
internal internalNotifier
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents data to be sent with notification event.
|
|
|
|
type eventData struct {
|
|
|
|
Type EventName
|
|
|
|
Bucket string
|
|
|
|
ObjInfo ObjectInfo
|
|
|
|
ReqParams map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New notification event constructs a new notification event message from
|
|
|
|
// input request metadata which completed successfully.
|
|
|
|
func newNotificationEvent(event eventData) NotificationEvent {
|
|
|
|
/// Construct a new object created event.
|
|
|
|
region := serverConfig.GetRegion()
|
|
|
|
tnow := time.Now().UTC()
|
|
|
|
sequencer := fmt.Sprintf("%X", tnow.UnixNano())
|
2016-10-12 04:03:50 -04:00
|
|
|
// Following blocks fills in all the necessary details of s3
|
|
|
|
// event message structure.
|
2016-08-05 01:01:58 -04:00
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
|
|
|
|
nEvent := NotificationEvent{
|
|
|
|
EventVersion: "2.0",
|
|
|
|
EventSource: "aws:s3",
|
|
|
|
AwsRegion: region,
|
2016-08-15 04:50:07 -04:00
|
|
|
EventTime: tnow.Format(timeFormatAMZ),
|
2016-08-05 01:01:58 -04:00
|
|
|
EventName: event.Type.String(),
|
|
|
|
UserIdentity: defaultIdentity(),
|
|
|
|
RequestParameters: event.ReqParams,
|
|
|
|
ResponseElements: map[string]string{},
|
|
|
|
S3: eventMeta{
|
|
|
|
SchemaVersion: "1.0",
|
|
|
|
ConfigurationID: "Config",
|
|
|
|
Bucket: bucketMeta{
|
|
|
|
Name: event.Bucket,
|
|
|
|
OwnerIdentity: defaultIdentity(),
|
|
|
|
ARN: "arn:aws:s3:::" + event.Bucket,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
escapedObj := url.QueryEscape(event.ObjInfo.Name)
|
|
|
|
// For delete object event type, we do not need to set ETag and Size.
|
|
|
|
if event.Type == ObjectRemovedDelete {
|
|
|
|
nEvent.S3.Object = objectMeta{
|
|
|
|
Key: escapedObj,
|
|
|
|
Sequencer: sequencer,
|
|
|
|
}
|
|
|
|
return nEvent
|
|
|
|
}
|
|
|
|
// For all other events we should set ETag and Size.
|
|
|
|
nEvent.S3.Object = objectMeta{
|
|
|
|
Key: escapedObj,
|
|
|
|
ETag: event.ObjInfo.MD5Sum,
|
|
|
|
Size: event.ObjInfo.Size,
|
|
|
|
Sequencer: sequencer,
|
|
|
|
}
|
|
|
|
// Success.
|
|
|
|
return nEvent
|
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// Fetch the external target. No locking needed here since this map is
|
|
|
|
// never written after initial startup.
|
|
|
|
func (en eventNotifier) GetExternalTarget(queueARN string) *logrus.Logger {
|
|
|
|
return en.external.targets[queueARN]
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
func (en eventNotifier) GetInternalTarget(arn string) *listenerLogger {
|
|
|
|
en.internal.rwMutex.RLock()
|
|
|
|
defer en.internal.rwMutex.RUnlock()
|
|
|
|
return en.internal.targets[arn]
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Set a new sns target for an input sns ARN.
|
2016-10-12 04:03:50 -04:00
|
|
|
func (en *eventNotifier) AddListenerChan(snsARN string, listenerCh chan []NotificationEvent) error {
|
2016-08-05 01:01:58 -04:00
|
|
|
if listenerCh == nil {
|
2016-09-10 05:23:28 -04:00
|
|
|
return errInvalidArgument
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
en.internal.rwMutex.Lock()
|
|
|
|
defer en.internal.rwMutex.Unlock()
|
|
|
|
en.internal.connectedListeners[snsARN] = listenerCh
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Remove sns target for an input sns ARN.
|
2016-10-12 04:03:50 -04:00
|
|
|
func (en *eventNotifier) RemoveListenerChan(snsARN string) {
|
|
|
|
en.internal.rwMutex.Lock()
|
|
|
|
defer en.internal.rwMutex.Unlock()
|
|
|
|
if en.internal.connectedListeners != nil {
|
|
|
|
delete(en.internal.connectedListeners, snsARN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (en *eventNotifier) SendListenerEvent(arn string, event []NotificationEvent) error {
|
|
|
|
en.internal.rwMutex.Lock()
|
|
|
|
defer en.internal.rwMutex.Unlock()
|
|
|
|
|
|
|
|
ch, ok := en.internal.connectedListeners[arn]
|
2016-08-05 01:01:58 -04:00
|
|
|
if ok {
|
2016-10-12 04:03:50 -04:00
|
|
|
ch <- event
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
// If the channel is not present we ignore the event.
|
|
|
|
return nil
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch bucket notification config for an input bucket.
|
|
|
|
func (en eventNotifier) GetBucketNotificationConfig(bucket string) *notificationConfig {
|
2016-10-12 04:03:50 -04:00
|
|
|
en.external.rwMutex.RLock()
|
|
|
|
defer en.external.rwMutex.RUnlock()
|
|
|
|
return en.external.notificationConfigs[bucket]
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
func (en *eventNotifier) SetBucketNotificationConfig(bucket string, ncfg *notificationConfig) {
|
|
|
|
en.external.rwMutex.Lock()
|
|
|
|
if ncfg == nil {
|
|
|
|
delete(en.external.notificationConfigs, bucket)
|
|
|
|
} else {
|
|
|
|
en.external.notificationConfigs[bucket] = ncfg
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
en.external.rwMutex.Unlock()
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
func (en *eventNotifier) GetBucketListenerConfig(bucket string) []listenerConfig {
|
|
|
|
en.internal.rwMutex.RLock()
|
|
|
|
defer en.internal.rwMutex.RUnlock()
|
|
|
|
return en.internal.listenerConfigs[bucket]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (en *eventNotifier) SetBucketListenerConfig(bucket string, lcfg []listenerConfig) error {
|
|
|
|
en.internal.rwMutex.Lock()
|
|
|
|
defer en.internal.rwMutex.Unlock()
|
2016-10-20 19:09:19 -04:00
|
|
|
if len(lcfg) == 0 {
|
2016-10-12 04:03:50 -04:00
|
|
|
delete(en.internal.listenerConfigs, bucket)
|
|
|
|
} else {
|
|
|
|
en.internal.listenerConfigs[bucket] = lcfg
|
|
|
|
}
|
|
|
|
en.internal.targets = make(map[string]*listenerLogger)
|
2016-10-20 19:09:19 -04:00
|
|
|
for _, elcArr := range en.internal.listenerConfigs {
|
|
|
|
for _, elcElem := range elcArr {
|
|
|
|
currArn := elcElem.TopicConfig.TopicARN
|
|
|
|
logger, err := newListenerLogger(currArn, elcElem.TargetServer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
en.internal.targets[currArn] = logger
|
2016-09-29 01:46:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
func eventNotifyForBucketNotifications(eventType, objectName, bucketName string,
|
|
|
|
nEvent []NotificationEvent) {
|
|
|
|
nConfig := globalEventNotifier.GetBucketNotificationConfig(bucketName)
|
2016-09-10 05:23:28 -04:00
|
|
|
if nConfig == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
// Validate if the event and object match the queue configs.
|
|
|
|
for _, qConfig := range nConfig.QueueConfigs {
|
|
|
|
eventMatch := eventMatch(eventType, qConfig.Events)
|
|
|
|
ruleMatch := filterRuleMatch(objectName, qConfig.Filter.Key.FilterRules)
|
|
|
|
if eventMatch && ruleMatch {
|
2016-10-12 04:03:50 -04:00
|
|
|
targetLog := globalEventNotifier.GetExternalTarget(qConfig.QueueARN)
|
2016-08-05 01:01:58 -04:00
|
|
|
if targetLog != nil {
|
|
|
|
targetLog.WithFields(logrus.Fields{
|
2016-10-12 04:03:50 -04:00
|
|
|
"Key": path.Join(bucketName, objectName),
|
2016-09-20 05:11:17 -04:00
|
|
|
"EventType": eventType,
|
2016-10-12 04:03:50 -04:00
|
|
|
"Records": nEvent,
|
2016-08-05 01:01:58 -04:00
|
|
|
}).Info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func eventNotifyForBucketListeners(eventType, objectName, bucketName string,
|
|
|
|
nEvent []NotificationEvent) {
|
|
|
|
lCfgs := globalEventNotifier.GetBucketListenerConfig(bucketName)
|
|
|
|
if lCfgs == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Validate if the event and object match listener configs
|
|
|
|
for _, lcfg := range lCfgs {
|
|
|
|
ruleMatch := filterRuleMatch(objectName, lcfg.TopicConfig.Filter.Key.FilterRules)
|
|
|
|
eventMatch := eventMatch(eventType, lcfg.TopicConfig.Events)
|
2016-08-05 01:01:58 -04:00
|
|
|
if eventMatch && ruleMatch {
|
2016-10-12 04:03:50 -04:00
|
|
|
targetLog := globalEventNotifier.GetInternalTarget(
|
|
|
|
lcfg.TopicConfig.TopicARN)
|
|
|
|
if targetLog != nil && targetLog.log != nil {
|
|
|
|
targetLog.log.WithFields(logrus.Fields{
|
|
|
|
"Key": path.Join(bucketName, objectName),
|
|
|
|
"EventType": eventType,
|
|
|
|
"Records": nEvent,
|
|
|
|
}).Info()
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventNotify notifies an event to relevant targets based on their
|
|
|
|
// bucket configuration (notifications and listeners).
|
|
|
|
func eventNotify(event eventData) {
|
|
|
|
// Notifies a new event.
|
|
|
|
// List of events reported through this function are
|
|
|
|
// - s3:ObjectCreated:Put
|
|
|
|
// - s3:ObjectCreated:Post
|
|
|
|
// - s3:ObjectCreated:Copy
|
|
|
|
// - s3:ObjectCreated:CompleteMultipartUpload
|
|
|
|
// - s3:ObjectRemoved:Delete
|
|
|
|
|
|
|
|
// Event type.
|
|
|
|
eventType := event.Type.String()
|
|
|
|
|
|
|
|
// Object name.
|
|
|
|
objectName := event.ObjInfo.Name
|
|
|
|
|
|
|
|
// Save the notification event to be sent.
|
|
|
|
notificationEvent := []NotificationEvent{newNotificationEvent(event)}
|
|
|
|
|
|
|
|
// Notify external targets.
|
|
|
|
eventNotifyForBucketNotifications(eventType, objectName, event.Bucket,
|
|
|
|
notificationEvent)
|
|
|
|
|
|
|
|
// Notify internal targets.
|
|
|
|
eventNotifyForBucketListeners(eventType, objectName, event.Bucket,
|
|
|
|
notificationEvent)
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// loads notification config if any for a given bucket, returns
|
|
|
|
// structured notification config.
|
2016-08-05 01:01:58 -04:00
|
|
|
func loadNotificationConfig(bucket string, objAPI ObjectLayer) (*notificationConfig, error) {
|
|
|
|
// Construct the notification config path.
|
|
|
|
notificationConfigPath := path.Join(bucketConfigPrefix, bucket, bucketNotificationConfig)
|
|
|
|
objInfo, err := objAPI.GetObjectInfo(minioMetaBucket, notificationConfigPath)
|
2016-08-25 12:39:01 -04:00
|
|
|
err = errorCause(err)
|
2016-08-05 01:01:58 -04:00
|
|
|
if err != nil {
|
2016-10-12 04:03:50 -04:00
|
|
|
// 'notification.xml' not found return
|
|
|
|
// 'errNoSuchNotifications'. This is default when no
|
|
|
|
// bucket notifications are found on the bucket.
|
2016-08-05 01:01:58 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
return nil, errNoSuchNotifications
|
|
|
|
}
|
2016-09-06 16:30:05 -04:00
|
|
|
errorIf(err, "Unable to load bucket-notification for bucket %s", bucket)
|
2016-08-05 01:01:58 -04:00
|
|
|
// Returns error for other errors.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err = objAPI.GetObject(minioMetaBucket, notificationConfigPath, 0, objInfo.Size, &buffer)
|
2016-08-25 12:39:01 -04:00
|
|
|
err = errorCause(err)
|
2016-08-05 01:01:58 -04:00
|
|
|
if err != nil {
|
2016-10-12 04:03:50 -04:00
|
|
|
// 'notification.xml' not found return
|
|
|
|
// 'errNoSuchNotifications'. This is default when no
|
|
|
|
// bucket notifications are found on the bucket.
|
2016-08-05 01:01:58 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
return nil, errNoSuchNotifications
|
|
|
|
}
|
2016-09-06 16:30:05 -04:00
|
|
|
errorIf(err, "Unable to load bucket-notification for bucket %s", bucket)
|
2016-08-05 01:01:58 -04:00
|
|
|
// Returns error for other errors.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal notification bytes.
|
|
|
|
notificationConfigBytes := buffer.Bytes()
|
|
|
|
notificationCfg := ¬ificationConfig{}
|
|
|
|
if err = xml.Unmarshal(notificationConfigBytes, ¬ificationCfg); err != nil {
|
|
|
|
return nil, err
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
// Return success.
|
|
|
|
return notificationCfg, nil
|
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// loads notification config if any for a given bucket, returns
|
|
|
|
// structured notification config.
|
|
|
|
func loadListenerConfig(bucket string, objAPI ObjectLayer) ([]listenerConfig, error) {
|
|
|
|
// Construct the notification config path.
|
|
|
|
listenerConfigPath := path.Join(bucketConfigPrefix, bucket, bucketListenerConfig)
|
|
|
|
objInfo, err := objAPI.GetObjectInfo(minioMetaBucket, listenerConfigPath)
|
|
|
|
err = errorCause(err)
|
|
|
|
if err != nil {
|
|
|
|
// 'listener.json' not found return
|
|
|
|
// 'errNoSuchNotifications'. This is default when no
|
|
|
|
// bucket notifications are found on the bucket.
|
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
return nil, errNoSuchNotifications
|
|
|
|
}
|
|
|
|
errorIf(err, "Unable to load bucket-listeners for bucket %s", bucket)
|
|
|
|
// Returns error for other errors.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err = objAPI.GetObject(minioMetaBucket, listenerConfigPath, 0, objInfo.Size, &buffer)
|
|
|
|
err = errorCause(err)
|
|
|
|
if err != nil {
|
|
|
|
// 'notification.xml' not found return
|
|
|
|
// 'errNoSuchNotifications'. This is default when no
|
|
|
|
// bucket listners are found on the bucket.
|
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
return nil, errNoSuchNotifications
|
|
|
|
}
|
|
|
|
errorIf(err, "Unable to load bucket-listeners for bucket %s", bucket)
|
|
|
|
// Returns error for other errors.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal notification bytes.
|
|
|
|
var lCfg []listenerConfig
|
|
|
|
lConfigBytes := buffer.Bytes()
|
|
|
|
if err = json.Unmarshal(lConfigBytes, &lCfg); err != nil {
|
|
|
|
errorIf(err, "Unable to unmarshal listener config from JSON.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success.
|
|
|
|
return lCfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func persistNotificationConfig(bucket string, ncfg *notificationConfig, obj ObjectLayer) error {
|
|
|
|
// marshal to xml
|
|
|
|
buf, err := xml.Marshal(ncfg)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to marshal notification configuration into XML")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify bucket exists
|
|
|
|
// FIXME: There is a race between this check and PutObject
|
|
|
|
if err = isBucketExist(bucket, obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// build path
|
|
|
|
ncPath := path.Join(bucketConfigPrefix, bucket, bucketNotificationConfig)
|
|
|
|
// write object to path
|
|
|
|
_, err = obj.PutObject(minioMetaBucket, ncPath, int64(len(buf)),
|
|
|
|
bytes.NewReader(buf), nil, "")
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to write bucket notification configuration.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persists validated listener config to object layer.
|
|
|
|
func persistListenerConfig(bucket string, lcfg []listenerConfig, obj ObjectLayer) error {
|
|
|
|
buf, err := json.Marshal(lcfg)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to marshal listener config to JSON.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify bucket exists
|
|
|
|
// FIXME: There is a race between this check and PutObject
|
|
|
|
if err = isBucketExist(bucket, obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// build path
|
|
|
|
lcPath := path.Join(bucketConfigPrefix, bucket, bucketListenerConfig)
|
|
|
|
// write object to path
|
|
|
|
_, err = obj.PutObject(minioMetaBucket, lcPath, int64(len(buf)),
|
|
|
|
bytes.NewReader(buf), nil, "")
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to write bucket listener configuration to object layer.")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:09:19 -04:00
|
|
|
// Remove listener configuration from storage layer. Used when a
|
|
|
|
// bucket is deleted.
|
|
|
|
func removeListenerConfig(bucket string, obj ObjectLayer) error {
|
|
|
|
// make the path
|
|
|
|
lcPath := path.Join(bucketConfigPrefix, bucket, bucketListenerConfig)
|
|
|
|
// remove it
|
|
|
|
return obj.DeleteObject(minioMetaBucket, lcPath)
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// loads all bucket notifications if present.
|
2016-10-12 04:03:50 -04:00
|
|
|
func loadAllBucketNotifications(objAPI ObjectLayer) (map[string]*notificationConfig, map[string][]listenerConfig, error) {
|
2016-08-05 01:01:58 -04:00
|
|
|
// List buckets to proceed loading all notification configuration.
|
|
|
|
buckets, err := objAPI.ListBuckets()
|
|
|
|
if err != nil {
|
2016-10-12 04:03:50 -04:00
|
|
|
return nil, nil, err
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
nConfigs := make(map[string]*notificationConfig)
|
|
|
|
lConfigs := make(map[string][]listenerConfig)
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
// Loads all bucket notifications.
|
|
|
|
for _, bucket := range buckets {
|
2016-09-06 16:30:05 -04:00
|
|
|
nCfg, nErr := loadNotificationConfig(bucket.Name, objAPI)
|
|
|
|
if nErr != nil {
|
2016-10-12 04:03:50 -04:00
|
|
|
if nErr != errNoSuchNotifications {
|
|
|
|
return nil, nil, nErr
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
} else {
|
|
|
|
nConfigs[bucket.Name] = nCfg
|
|
|
|
}
|
|
|
|
|
|
|
|
lCfg, lErr := loadListenerConfig(bucket.Name, objAPI)
|
|
|
|
if lErr != nil {
|
|
|
|
if lErr != errNoSuchNotifications {
|
|
|
|
return nil, nil, lErr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lConfigs[bucket.Name] = lCfg
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
2016-10-12 04:03:50 -04:00
|
|
|
return nConfigs, lConfigs, nil
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loads all queue targets, initializes each queueARNs depending on their config.
|
|
|
|
// Each instance of queueARN registers its own logrus to communicate with the
|
|
|
|
// queue service. QueueARN once initialized is not initialized again for the
|
|
|
|
// same queueARN, instead previous connection is used.
|
|
|
|
func loadAllQueueTargets() (map[string]*logrus.Logger, error) {
|
|
|
|
queueTargets := make(map[string]*logrus.Logger)
|
|
|
|
// Load all amqp targets, initialize their respective loggers.
|
|
|
|
for accountID, amqpN := range serverConfig.GetAMQP() {
|
|
|
|
if !amqpN.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct the queue ARN for AMQP.
|
|
|
|
queueARN := minioSqs + serverConfig.GetRegion() + ":" + accountID + ":" + queueTypeAMQP
|
|
|
|
// Queue target if already initialized we move to the next ARN.
|
|
|
|
_, ok := queueTargets[queueARN]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Using accountID we can now initialize a new AMQP logrus instance.
|
|
|
|
amqpLog, err := newAMQPNotify(accountID)
|
|
|
|
if err != nil {
|
2016-08-25 02:04:34 -04:00
|
|
|
// Encapsulate network error to be more informative.
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "Connecting to " + queueARN,
|
|
|
|
Net: "tcp",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queueTargets[queueARN] = amqpLog
|
|
|
|
}
|
2016-09-30 02:42:10 -04:00
|
|
|
// Load all nats targets, initialize their respective loggers.
|
|
|
|
for accountID, natsN := range serverConfig.GetNATS() {
|
|
|
|
if !natsN.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct the queue ARN for NATS.
|
|
|
|
queueARN := minioSqs + serverConfig.GetRegion() + ":" + accountID + ":" + queueTypeNATS
|
|
|
|
// Queue target if already initialized we move to the next ARN.
|
|
|
|
_, ok := queueTargets[queueARN]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Using accountID we can now initialize a new NATS logrus instance.
|
|
|
|
natsLog, err := newNATSNotify(accountID)
|
|
|
|
if err != nil {
|
|
|
|
// Encapsulate network error to be more informative.
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "Connecting to " + queueARN,
|
|
|
|
Net: "tcp",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queueTargets[queueARN] = natsLog
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Load redis targets, initialize their respective loggers.
|
|
|
|
for accountID, redisN := range serverConfig.GetRedis() {
|
|
|
|
if !redisN.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct the queue ARN for Redis.
|
|
|
|
queueARN := minioSqs + serverConfig.GetRegion() + ":" + accountID + ":" + queueTypeRedis
|
|
|
|
// Queue target if already initialized we move to the next ARN.
|
|
|
|
_, ok := queueTargets[queueARN]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Using accountID we can now initialize a new Redis logrus instance.
|
|
|
|
redisLog, err := newRedisNotify(accountID)
|
|
|
|
if err != nil {
|
2016-08-25 02:04:34 -04:00
|
|
|
// Encapsulate network error to be more informative.
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "Connecting to " + queueARN,
|
|
|
|
Net: "tcp",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queueTargets[queueARN] = redisLog
|
|
|
|
}
|
|
|
|
// Load elastic targets, initialize their respective loggers.
|
|
|
|
for accountID, elasticN := range serverConfig.GetElasticSearch() {
|
|
|
|
if !elasticN.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct the queue ARN for Elastic.
|
|
|
|
queueARN := minioSqs + serverConfig.GetRegion() + ":" + accountID + ":" + queueTypeElastic
|
|
|
|
_, ok := queueTargets[queueARN]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Using accountID we can now initialize a new ElasticSearch logrus instance.
|
|
|
|
elasticLog, err := newElasticNotify(accountID)
|
|
|
|
if err != nil {
|
2016-08-25 02:04:34 -04:00
|
|
|
// Encapsulate network error to be more informative.
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "Connecting to " + queueARN, Net: "tcp",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queueTargets[queueARN] = elasticLog
|
|
|
|
}
|
2016-10-03 20:29:55 -04:00
|
|
|
// Load PostgreSQL targets, initialize their respective loggers.
|
|
|
|
for accountID, pgN := range serverConfig.GetPostgreSQL() {
|
|
|
|
if !pgN.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct the queue ARN for Postgres.
|
|
|
|
queueARN := minioSqs + serverConfig.GetRegion() + ":" + accountID + ":" + queueTypePostgreSQL
|
|
|
|
_, ok := queueTargets[queueARN]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Using accountID initialize a new Postgresql logrus instance.
|
|
|
|
pgLog, err := newPostgreSQLNotify(accountID)
|
|
|
|
if err != nil {
|
|
|
|
// Encapsulate network error to be more informative.
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "Connecting to " + queueARN, Net: "tcp",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queueTargets[queueARN] = pgLog
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Successfully initialized queue targets.
|
|
|
|
return queueTargets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global instance of event notification queue.
|
2016-09-10 05:23:28 -04:00
|
|
|
var globalEventNotifier *eventNotifier
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
// Initialize event notifier.
|
|
|
|
func initEventNotifier(objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all saved bucket notifications.
|
2016-10-12 04:03:50 -04:00
|
|
|
nConfigs, lConfigs, err := loadAllBucketNotifications(objAPI)
|
2016-08-05 01:01:58 -04:00
|
|
|
if err != nil {
|
2016-10-12 04:03:50 -04:00
|
|
|
errorIf(err, "Error loading bucket notifications - %v", err)
|
2016-08-05 01:01:58 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes all queue targets.
|
|
|
|
queueTargets, err := loadAllQueueTargets()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// Initialize internal listener targets
|
|
|
|
listenTargets := make(map[string]*listenerLogger)
|
|
|
|
for _, listeners := range lConfigs {
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ln, err := newListenerLogger(
|
|
|
|
listener.TopicConfig.TopicARN,
|
|
|
|
listener.TargetServer,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to initialize listener target logger.")
|
|
|
|
//TODO: improve error
|
|
|
|
return fmt.Errorf("Error initializing listner target logger - %v", err)
|
|
|
|
}
|
|
|
|
listenTargets[listener.TopicConfig.TopicARN] = ln
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize event notifier queue.
|
2016-09-10 05:23:28 -04:00
|
|
|
globalEventNotifier = &eventNotifier{
|
2016-10-12 04:03:50 -04:00
|
|
|
external: externalNotifier{
|
|
|
|
notificationConfigs: nConfigs,
|
|
|
|
targets: queueTargets,
|
|
|
|
rwMutex: &sync.RWMutex{},
|
|
|
|
},
|
|
|
|
internal: internalNotifier{
|
|
|
|
rwMutex: &sync.RWMutex{},
|
|
|
|
targets: listenTargets,
|
|
|
|
listenerConfigs: lConfigs,
|
|
|
|
connectedListeners: make(map[string]chan []NotificationEvent),
|
|
|
|
},
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|