2016-07-24 01:51:12 -04:00
|
|
|
/*
|
2017-03-18 14:28:41 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-07-24 01:51:12 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-07-24 01:51:12 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-05 01:01:58 -04:00
|
|
|
"encoding/json"
|
2016-07-24 01:51:12 -04:00
|
|
|
"encoding/xml"
|
2016-09-27 15:50:32 -04:00
|
|
|
"fmt"
|
2016-07-24 01:51:12 -04:00
|
|
|
"io"
|
2017-11-16 13:56:06 -05:00
|
|
|
"net"
|
2016-07-24 01:51:12 -04:00
|
|
|
"net/http"
|
2017-11-16 13:56:06 -05:00
|
|
|
"syscall"
|
2016-08-05 01:01:58 -04:00
|
|
|
"time"
|
2016-07-24 01:51:12 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-07-24 01:51:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketConfigPrefix = "buckets"
|
|
|
|
bucketNotificationConfig = "notification.xml"
|
2016-10-12 04:03:50 -04:00
|
|
|
bucketListenerConfig = "listener.json"
|
2016-07-24 01:51:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetBucketNotificationHandler - This implementation of the GET
|
|
|
|
// operation uses the notification subresource to return the
|
|
|
|
// notification configuration of a bucket. If notifications are
|
|
|
|
// not enabled on the bucket, the operation returns an empty
|
|
|
|
// NotificationConfiguration element.
|
|
|
|
func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-02-09 18:19:30 -05:00
|
|
|
|
2016-08-10 21:47:49 -04:00
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL)
|
2016-08-10 21:47:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
if !objAPI.IsNotificationSupported() {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
if s3Error := checkRequestAuthType(r, "", "", globalServerConfig.GetRegion()); s3Error != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, s3Error, r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
|
2016-07-24 01:51:12 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2016-11-14 18:45:00 -05:00
|
|
|
|
|
|
|
_, err := objAPI.GetBucketInfo(bucket)
|
|
|
|
if err != nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-11-14 18:45:00 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Attempt to successfully load notification config.
|
2016-07-31 17:11:14 -04:00
|
|
|
nConfig, err := loadNotificationConfig(bucket, objAPI)
|
2017-11-25 14:58:29 -05:00
|
|
|
if err != nil && errors.Cause(err) != errNoSuchNotifications {
|
2016-08-05 01:01:58 -04:00
|
|
|
errorIf(err, "Unable to read notification configuration.")
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
// For no notifications we write a dummy XML.
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.Cause(err) == errNoSuchNotifications {
|
2016-08-05 01:01:58 -04:00
|
|
|
// Complies with the s3 behavior in this regard.
|
|
|
|
nConfig = ¬ificationConfig{}
|
|
|
|
}
|
|
|
|
notificationBytes, err := xml.Marshal(nConfig)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2016-08-05 01:01:58 -04:00
|
|
|
// For any marshalling failure.
|
|
|
|
errorIf(err, "Unable to marshal notification configuration into XML.", err)
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2017-01-06 03:37:00 -05:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Success.
|
2017-01-06 03:37:00 -05:00
|
|
|
writeSuccessResponseXML(w, notificationBytes)
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBucketNotificationHandler - Minio notification feature enables
|
|
|
|
// you to receive notifications when certain events happen in your bucket.
|
|
|
|
// Using this API, you can replace an existing notification configuration.
|
|
|
|
// The configuration is an XML file that defines the event types that you
|
|
|
|
// want Minio to publish and the destination where you want Minio to publish
|
|
|
|
// an event notification when it detects an event of the specified type.
|
|
|
|
// By default, your bucket has no event notifications configured. That is,
|
|
|
|
// the notification configuration will be an empty NotificationConfiguration.
|
|
|
|
func (api objectAPIHandlers) PutBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-02-09 18:19:30 -05:00
|
|
|
|
2016-08-10 21:47:49 -04:00
|
|
|
objectAPI := api.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL)
|
2016-08-10 21:47:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
if !objectAPI.IsNotificationSupported() {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
if s3Error := checkRequestAuthType(r, "", "", globalServerConfig.GetRegion()); s3Error != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, s3Error, r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
|
2016-07-24 01:51:12 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
_, err := objectAPI.GetBucketInfo(bucket)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:33:01 -05:00
|
|
|
// If Content-Length is unknown or zero, deny the request.
|
|
|
|
// PutBucketNotification always needs a Content-Length.
|
|
|
|
if r.ContentLength == -1 || r.ContentLength == 0 {
|
|
|
|
writeErrorResponse(w, ErrMissingContentLength, r.URL)
|
|
|
|
return
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the incoming notification configuration.
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
if r.ContentLength >= 0 {
|
2016-10-12 04:03:50 -04:00
|
|
|
_, err = io.CopyN(&buffer, r.Body, r.ContentLength)
|
2016-07-24 01:51:12 -04:00
|
|
|
} else {
|
2016-10-12 04:03:50 -04:00
|
|
|
_, err = io.Copy(&buffer, r.Body)
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to read incoming body.")
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var notificationCfg notificationConfig
|
|
|
|
// Unmarshal notification bytes.
|
|
|
|
notificationConfigBytes := buffer.Bytes()
|
|
|
|
if err = xml.Unmarshal(notificationConfigBytes, ¬ificationCfg); err != nil {
|
|
|
|
errorIf(err, "Unable to parse notification configuration XML.")
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, ErrMalformedXML, r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
} // Successfully marshalled notification configuration.
|
|
|
|
|
|
|
|
// Validate unmarshalled bucket notification configuration.
|
|
|
|
if s3Error := validateNotificationConfig(notificationCfg); s3Error != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, s3Error, r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 18:09:55 -04:00
|
|
|
// Convert the incoming ARNs properly to the GetRegion().
|
|
|
|
for i, queueConfig := range notificationCfg.QueueConfigs {
|
|
|
|
queueConfig.QueueARN = unmarshalSqsARN(queueConfig.QueueARN).String()
|
|
|
|
notificationCfg.QueueConfigs[i] = queueConfig
|
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// Put bucket notification config.
|
|
|
|
err = PutBucketNotificationConfig(bucket, ¬ificationCfg, objectAPI)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
2017-01-06 03:37:00 -05:00
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
2016-08-05 01:01:58 -04:00
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// PutBucketNotificationConfig - Put a new notification config for a
|
|
|
|
// bucket (overwrites any previous config) persistently, updates
|
|
|
|
// global in-memory state, and notify other nodes in the cluster (if
|
|
|
|
// any)
|
|
|
|
func PutBucketNotificationConfig(bucket string, ncfg *notificationConfig, objAPI ObjectLayer) error {
|
|
|
|
if ncfg == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2016-10-24 22:52:24 -04:00
|
|
|
// Acquire a write lock on bucket before modifying its
|
|
|
|
// configuration.
|
2016-12-10 19:15:12 -05:00
|
|
|
bucketLock := globalNSMutex.NewNSLock(bucket, "")
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := bucketLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-24 22:52:24 -04:00
|
|
|
// Release lock after notifying peers
|
2016-11-09 13:58:41 -05:00
|
|
|
defer bucketLock.Unlock()
|
2016-10-24 22:52:24 -04:00
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// persist config to disk
|
|
|
|
err := persistNotificationConfig(bucket, ncfg, objAPI)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to persist Bucket notification config to object layer - config=%v errMsg=%v", *ncfg, err)
|
|
|
|
}
|
|
|
|
|
2016-11-14 18:45:00 -05:00
|
|
|
// All servers (including local) are told to update in-memory config
|
2016-10-12 04:03:50 -04:00
|
|
|
S3PeersUpdateBucketNotification(bucket, ncfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// writeNotification marshals notification message before writing to client.
|
|
|
|
func writeNotification(w http.ResponseWriter, notification map[string][]NotificationEvent) error {
|
|
|
|
// Invalid response writer.
|
|
|
|
if w == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
// Invalid notification input.
|
|
|
|
if notification == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
// Marshal notification data into JSON and write to client.
|
2016-08-05 01:01:58 -04:00
|
|
|
notificationBytes, err := json.Marshal(¬ification)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-03 22:59:41 -04:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Add additional CRLF characters for client to
|
|
|
|
// differentiate the individual events properly.
|
|
|
|
_, err = w.Write(append(notificationBytes, crlf...))
|
|
|
|
// Make sure we have flushed, this would set Transfer-Encoding: chunked.
|
|
|
|
w.(http.Flusher).Flush()
|
2017-03-08 13:00:47 -05:00
|
|
|
return err
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CRLF character used for chunked transfer in accordance with HTTP standards.
|
|
|
|
var crlf = []byte("\r\n")
|
|
|
|
|
2017-11-16 13:56:06 -05:00
|
|
|
// listenChan A `listenChan` provides a data channel to send event
|
|
|
|
// notifications on and `doneCh` to signal that events are no longer
|
|
|
|
// being received. It also sends empty events (whitespace) to keep the
|
|
|
|
// underlying connection alive.
|
|
|
|
type listenChan struct {
|
|
|
|
doneCh chan struct{}
|
|
|
|
dataCh chan []NotificationEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// newListenChan returns a listenChan with properly initialized
|
|
|
|
// unbuffered channels.
|
|
|
|
func newListenChan() *listenChan {
|
|
|
|
return &listenChan{
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
dataCh: make(chan []NotificationEvent),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendNotificationEvent sends notification events on the data channel
|
|
|
|
// unless doneCh is not closed
|
|
|
|
func (l *listenChan) sendNotificationEvent(events []NotificationEvent) {
|
|
|
|
select {
|
|
|
|
// Returns immediately if receiver has quit.
|
|
|
|
case <-l.doneCh:
|
|
|
|
// Blocks until receiver is available.
|
|
|
|
case l.dataCh <- events:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForListener writes event notification OR whitespaces on
|
|
|
|
// ResponseWriter until client closes connection
|
|
|
|
func (l *listenChan) waitForListener(w http.ResponseWriter) {
|
|
|
|
|
|
|
|
// Logs errors other than EPIPE and ECONNRESET.
|
|
|
|
// EPIPE and ECONNRESET indicate that the client stopped
|
|
|
|
// listening to notification events.
|
|
|
|
logClientError := func(err error, msg string) {
|
|
|
|
if oe, ok := err.(*net.OpError); ok && (oe.Err == syscall.EPIPE || oe.Err ==
|
|
|
|
syscall.ECONNRESET) {
|
|
|
|
errorIf(err, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyEvent := map[string][]NotificationEvent{"Records": nil}
|
|
|
|
defer close(l.doneCh)
|
2016-08-05 01:01:58 -04:00
|
|
|
for {
|
|
|
|
select {
|
2017-11-16 13:56:06 -05:00
|
|
|
case events := <-l.dataCh:
|
2016-08-05 01:01:58 -04:00
|
|
|
if err := writeNotification(w, map[string][]NotificationEvent{"Records": events}); err != nil {
|
2017-11-16 13:56:06 -05:00
|
|
|
logClientError(err, "Unable to write notification")
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
2017-11-16 13:56:06 -05:00
|
|
|
case <-time.After(globalSNSConnAlive):
|
|
|
|
if err := writeNotification(w, emptyEvent); err != nil {
|
|
|
|
logClientError(err, "Unable to write empty notification")
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenBucketNotificationHandler - list bucket notifications.
|
|
|
|
func (api objectAPIHandlers) ListenBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2016-08-10 21:47:49 -04:00
|
|
|
// Validate if bucket exists.
|
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL)
|
2016-08-10 21:47:49 -04:00
|
|
|
return
|
|
|
|
}
|
2018-02-09 18:19:30 -05:00
|
|
|
if !objAPI.IsNotificationSupported() {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
if s3Error := checkRequestAuthType(r, "", "", globalServerConfig.GetRegion()); s3Error != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, s3Error, r.URL)
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2016-09-27 15:50:32 -04:00
|
|
|
// Parse listen bucket notification resources.
|
2016-10-12 14:02:15 -04:00
|
|
|
prefixes, suffixes, events := getListenBucketNotificationResources(r.URL.Query())
|
|
|
|
|
|
|
|
if err := validateFilterValues(prefixes); err != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, err, r.URL)
|
2016-10-12 14:02:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateFilterValues(suffixes); err != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, err, r.URL)
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-27 15:50:32 -04:00
|
|
|
// Validate all the resource events.
|
|
|
|
for _, event := range events {
|
|
|
|
if errCode := checkEvent(event); errCode != ErrNone {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
2016-09-27 15:50:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
_, err := objAPI.GetBucketInfo(bucket)
|
2016-08-05 01:01:58 -04:00
|
|
|
if err != nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:23:42 -04:00
|
|
|
targetServer := GetLocalPeer(globalEndpoints)
|
2017-03-18 14:28:41 -04:00
|
|
|
accountID := fmt.Sprintf("%d", UTCNow().UnixNano())
|
2016-10-12 04:03:50 -04:00
|
|
|
accountARN := fmt.Sprintf(
|
2016-10-21 04:25:17 -04:00
|
|
|
"%s:%s:%s:%s-%s",
|
|
|
|
minioTopic,
|
2017-11-29 16:12:47 -05:00
|
|
|
globalServerConfig.GetRegion(),
|
2016-10-12 04:03:50 -04:00
|
|
|
accountID,
|
2016-10-21 04:25:17 -04:00
|
|
|
snsTypeMinio,
|
2017-10-08 23:23:42 -04:00
|
|
|
targetServer,
|
2016-10-12 04:03:50 -04:00
|
|
|
)
|
2017-10-08 23:23:42 -04:00
|
|
|
|
2016-09-27 15:50:32 -04:00
|
|
|
var filterRules []filterRule
|
2016-10-12 14:02:15 -04:00
|
|
|
|
|
|
|
for _, prefix := range prefixes {
|
2016-09-27 15:50:32 -04:00
|
|
|
filterRules = append(filterRules, filterRule{
|
|
|
|
Name: "prefix",
|
|
|
|
Value: prefix,
|
|
|
|
})
|
|
|
|
}
|
2016-10-12 14:02:15 -04:00
|
|
|
|
|
|
|
for _, suffix := range suffixes {
|
2016-09-27 15:50:32 -04:00
|
|
|
filterRules = append(filterRules, filterRule{
|
|
|
|
Name: "suffix",
|
|
|
|
Value: suffix,
|
|
|
|
})
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// Make topic configuration corresponding to this
|
|
|
|
// ListenBucketNotification request.
|
2016-09-29 01:46:19 -04:00
|
|
|
topicCfg := &topicConfig{
|
|
|
|
TopicARN: accountARN,
|
2016-10-12 04:03:50 -04:00
|
|
|
ServiceConfig: ServiceConfig{
|
2016-09-29 01:46:19 -04:00
|
|
|
Events: events,
|
|
|
|
Filter: struct {
|
2016-10-12 04:03:50 -04:00
|
|
|
Key keyFilter `xml:"S3Key,omitempty" json:"S3Key,omitempty"`
|
2016-09-29 01:46:19 -04:00
|
|
|
}{
|
|
|
|
Key: keyFilter{
|
|
|
|
FilterRules: filterRules,
|
2016-09-27 15:50:32 -04:00
|
|
|
},
|
|
|
|
},
|
2016-09-29 01:46:19 -04:00
|
|
|
ID: "sns-" + accountID,
|
|
|
|
},
|
2016-09-27 15:50:32 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 13:56:06 -05:00
|
|
|
// Setup a listen channel to receive notifications like
|
|
|
|
// s3:ObjectCreated, s3:ObjectDeleted etc.
|
|
|
|
nListenCh := newListenChan()
|
2016-10-12 04:03:50 -04:00
|
|
|
// Add channel for listener events
|
2017-11-16 13:56:06 -05:00
|
|
|
if err = globalEventNotifier.AddListenerChan(accountARN, nListenCh); err != nil {
|
2016-10-16 23:52:10 -04:00
|
|
|
errorIf(err, "Error adding a listener!")
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-10-16 23:52:10 -04:00
|
|
|
return
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
// Remove listener channel after the writer has closed or the
|
|
|
|
// client disconnected.
|
|
|
|
defer globalEventNotifier.RemoveListenerChan(accountARN)
|
|
|
|
|
|
|
|
// Update topic config to bucket config and persist - as soon
|
2017-11-16 13:56:06 -05:00
|
|
|
// as this call completes, events may start appearing in
|
|
|
|
// nListenCh
|
2016-10-12 04:03:50 -04:00
|
|
|
lc := listenerConfig{
|
|
|
|
TopicConfig: *topicCfg,
|
2017-10-08 23:23:42 -04:00
|
|
|
TargetServer: targetServer,
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2016-10-21 04:25:17 -04:00
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
err = AddBucketListenerConfig(bucket, &lc, objAPI)
|
|
|
|
if err != nil {
|
2017-01-06 03:37:00 -05:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(err), r.URL)
|
2016-08-05 01:01:58 -04:00
|
|
|
return
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
defer RemoveBucketListenerConfig(bucket, &lc, objAPI)
|
2016-08-05 01:01:58 -04:00
|
|
|
|
|
|
|
// Add all common headers.
|
|
|
|
setCommonHeaders(w)
|
|
|
|
|
2017-11-16 13:56:06 -05:00
|
|
|
// https://github.com/containous/traefik/issues/560
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
|
|
|
|
//
|
|
|
|
// Proxies might buffer the connection to avoid this we
|
|
|
|
// need the proper MIME type before writing to client.
|
|
|
|
// This MIME header tells the proxies to avoid buffering
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
|
|
|
// Start writing bucket notifications to ResponseWriter.
|
|
|
|
nListenCh.waitForListener(w)
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
2016-08-09 14:33:45 -04:00
|
|
|
|
2016-10-12 04:03:50 -04:00
|
|
|
// AddBucketListenerConfig - Updates on disk state of listeners, and
|
|
|
|
// updates all peers with the change in listener config.
|
|
|
|
func AddBucketListenerConfig(bucket string, lcfg *listenerConfig, objAPI ObjectLayer) error {
|
|
|
|
if lcfg == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
listenerCfgs := globalEventNotifier.GetBucketListenerConfig(bucket)
|
|
|
|
|
|
|
|
// add new lid to listeners and persist to object layer.
|
|
|
|
listenerCfgs = append(listenerCfgs, *lcfg)
|
|
|
|
|
2016-10-24 22:52:24 -04:00
|
|
|
// Acquire a write lock on bucket before modifying its
|
|
|
|
// configuration.
|
2016-12-10 19:15:12 -05:00
|
|
|
bucketLock := globalNSMutex.NewNSLock(bucket, "")
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := bucketLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-24 22:52:24 -04:00
|
|
|
// Release lock after notifying peers
|
2016-11-09 13:58:41 -05:00
|
|
|
defer bucketLock.Unlock()
|
2016-10-24 22:52:24 -04:00
|
|
|
|
2016-10-26 23:13:00 -04:00
|
|
|
// update persistent config if dist XL
|
2016-11-07 15:09:24 -05:00
|
|
|
if globalIsDistXL {
|
2016-10-26 23:13:00 -04:00
|
|
|
err := persistListenerConfig(bucket, listenerCfgs, objAPI)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Error persisting listener config when adding a listener.")
|
|
|
|
return err
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// persistence success - now update in-memory globals on all
|
|
|
|
// peers (including local)
|
|
|
|
S3PeersUpdateBucketListener(bucket, listenerCfgs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveBucketListenerConfig - removes a given bucket notification config
|
|
|
|
func RemoveBucketListenerConfig(bucket string, lcfg *listenerConfig, objAPI ObjectLayer) {
|
|
|
|
listenerCfgs := globalEventNotifier.GetBucketListenerConfig(bucket)
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// remove listener with matching ARN - if not found ignore and exit.
|
2016-10-12 04:03:50 -04:00
|
|
|
var updatedLcfgs []listenerConfig
|
|
|
|
found := false
|
|
|
|
for k, configuredLcfg := range listenerCfgs {
|
|
|
|
if configuredLcfg.TopicConfig.TopicARN == lcfg.TopicConfig.TopicARN {
|
|
|
|
updatedLcfgs = append(listenerCfgs[:k],
|
|
|
|
listenerCfgs[k+1:]...)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-24 22:52:24 -04:00
|
|
|
// Acquire a write lock on bucket before modifying its
|
|
|
|
// configuration.
|
2016-12-10 19:15:12 -05:00
|
|
|
bucketLock := globalNSMutex.NewNSLock(bucket, "")
|
2017-08-31 14:29:22 -04:00
|
|
|
if bucketLock.GetLock(globalOperationTimeout) != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-10-24 22:52:24 -04:00
|
|
|
// Release lock after notifying peers
|
2016-11-09 13:58:41 -05:00
|
|
|
defer bucketLock.Unlock()
|
2016-10-24 22:52:24 -04:00
|
|
|
|
2016-10-26 23:13:00 -04:00
|
|
|
// update persistent config if dist XL
|
2016-11-07 15:09:24 -05:00
|
|
|
if globalIsDistXL {
|
2016-10-26 23:13:00 -04:00
|
|
|
err := persistListenerConfig(bucket, updatedLcfgs, objAPI)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Error persisting listener config when removing a listener.")
|
|
|
|
return
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// persistence success - now update in-memory globals on all
|
|
|
|
// peers (including local)
|
|
|
|
S3PeersUpdateBucketListener(bucket, updatedLcfgs)
|
|
|
|
}
|