2016-07-24 07:51:12 +02:00
|
|
|
/*
|
2018-03-15 21:03:41 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
2016-07-24 07:51:12 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-07-24 07:51:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2018-03-15 21:03:41 +01:00
|
|
|
"errors"
|
2016-07-24 07:51:12 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-04-06 00:04:40 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-03-15 21:03:41 +01:00
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
"github.com/minio/minio/pkg/event/target"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2018-04-25 00:53:30 +02:00
|
|
|
"github.com/minio/minio/pkg/policy"
|
2016-07-24 07:51:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketConfigPrefix = "buckets"
|
|
|
|
bucketNotificationConfig = "notification.xml"
|
2016-10-12 10:03:50 +02:00
|
|
|
bucketListenerConfig = "listener.json"
|
2016-07-24 07:51:12 +02:00
|
|
|
)
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
var errNoSuchNotifications = errors.New("The specified bucket does not have bucket notifications")
|
|
|
|
|
|
|
|
// GetBucketNotificationHandler - This HTTP handler returns event notification configuration
|
|
|
|
// as per http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html.
|
|
|
|
// It returns empty configuration if its not set.
|
2016-07-24 07:51:12 +02:00
|
|
|
func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-21 03:46:32 +02:00
|
|
|
ctx := newContext(r, w, "GetBucketNotification")
|
2018-02-10 00:19:30 +01:00
|
|
|
|
2018-11-22 05:03:24 +01:00
|
|
|
defer logger.AuditLog(w, r, "GetBucketNotification", mustGetClaimsFromToken(r))
|
2018-10-12 21:25:59 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucketName := vars["bucket"]
|
|
|
|
|
2016-08-11 03:47:49 +02:00
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL, guessIsBrowserReq(r))
|
2016-08-11 03:47:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:19:30 +01:00
|
|
|
if !objAPI.IsNotificationSupported() {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL, guessIsBrowserReq(r))
|
2018-02-10 00:19:30 +01:00
|
|
|
return
|
|
|
|
}
|
2018-04-25 00:53:30 +02:00
|
|
|
|
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.GetBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, s3Error, r.URL, guessIsBrowserReq(r))
|
2016-07-24 07:51:12 +02:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 22:51:05 +01:00
|
|
|
|
2018-03-14 20:01:47 +01:00
|
|
|
_, err := objAPI.GetBucketInfo(ctx, bucketName)
|
2016-11-15 00:45:00 +01:00
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-11-15 00:45:00 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:01:58 +02:00
|
|
|
// Attempt to successfully load notification config.
|
2018-04-06 00:04:40 +02:00
|
|
|
nConfig, err := readNotificationConfig(ctx, objAPI, bucketName)
|
2018-03-15 21:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
// Ignore errNoSuchNotifications to comply with AWS S3.
|
2018-04-10 18:36:37 +02:00
|
|
|
if err != errNoSuchNotifications {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nConfig = &event.Config{}
|
2016-08-05 07:01:58 +02:00
|
|
|
}
|
2018-03-15 21:03:41 +01:00
|
|
|
|
2018-11-09 19:40:03 +01:00
|
|
|
// If xml namespace is empty, set a default value before returning.
|
|
|
|
if nConfig.XMLNS == "" {
|
|
|
|
nConfig.XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/"
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:01:58 +02:00
|
|
|
notificationBytes, err := xml.Marshal(nConfig)
|
2016-07-24 07:51:12 +02:00
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-07-24 07:51:12 +02:00
|
|
|
return
|
|
|
|
}
|
2017-01-06 09:37:00 +01:00
|
|
|
|
|
|
|
writeSuccessResponseXML(w, notificationBytes)
|
2016-07-24 07:51:12 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
// PutBucketNotificationHandler - This HTTP handler stores given notification configuration as per
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html.
|
2016-07-24 07:51:12 +02:00
|
|
|
func (api objectAPIHandlers) PutBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-21 03:46:32 +02:00
|
|
|
ctx := newContext(r, w, "PutBucketNotification")
|
2018-02-10 00:19:30 +01:00
|
|
|
|
2018-11-22 05:03:24 +01:00
|
|
|
defer logger.AuditLog(w, r, "PutBucketNotification", mustGetClaimsFromToken(r))
|
2018-10-12 21:25:59 +02:00
|
|
|
|
2016-08-11 03:47:49 +02:00
|
|
|
objectAPI := api.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL, guessIsBrowserReq(r))
|
2016-08-11 03:47:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:19:30 +01:00
|
|
|
if !objectAPI.IsNotificationSupported() {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL, guessIsBrowserReq(r))
|
2018-02-10 00:19:30 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 22:51:05 +01:00
|
|
|
|
2016-07-24 07:51:12 +02:00
|
|
|
vars := mux.Vars(r)
|
2018-03-15 21:03:41 +01:00
|
|
|
bucketName := vars["bucket"]
|
2016-07-24 07:51:12 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.PutBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, s3Error, r.URL, guessIsBrowserReq(r))
|
2018-04-25 00:53:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:01:47 +01:00
|
|
|
_, err := objectAPI.GetBucketInfo(ctx, bucketName)
|
2016-07-24 07:51:12 +02:00
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-07-24 07:51:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-21 01:33:01 +01:00
|
|
|
// PutBucketNotification always needs a Content-Length.
|
2018-03-15 21:03:41 +01:00
|
|
|
if r.ContentLength <= 0 {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrMissingContentLength, r.URL, guessIsBrowserReq(r))
|
2017-01-21 01:33:01 +01:00
|
|
|
return
|
2016-07-24 07:51:12 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
var config *event.Config
|
|
|
|
config, err = event.ParseConfig(io.LimitReader(r.Body, r.ContentLength), globalServerConfig.GetRegion(), globalNotificationSys.targetList)
|
2016-07-24 07:51:12 +02:00
|
|
|
if err != nil {
|
2018-03-15 21:03:41 +01:00
|
|
|
apiErr := ErrMalformedXML
|
|
|
|
if event.IsEventError(err) {
|
2018-11-12 20:07:43 +01:00
|
|
|
apiErr = toAPIErrorCode(ctx, err)
|
2018-03-15 21:03:41 +01:00
|
|
|
}
|
2016-07-24 07:51:12 +02:00
|
|
|
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, apiErr, r.URL, guessIsBrowserReq(r))
|
2016-07-24 07:51:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 23:00:01 +02:00
|
|
|
if err = saveNotificationConfig(ctx, objectAPI, bucketName, config); err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
return
|
2017-11-16 19:56:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
rulesMap := config.ToRulesMap()
|
|
|
|
globalNotificationSys.AddRulesMap(bucketName, rulesMap)
|
2018-07-03 20:09:36 +02:00
|
|
|
globalNotificationSys.PutBucketNotification(ctx, bucketName, rulesMap)
|
2017-11-16 19:56:06 +01:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2016-08-05 07:01:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
// ListenBucketNotificationHandler - This HTTP handler sends events to the connected HTTP client.
|
|
|
|
// Client should send prefix/suffix object name to match and events to watch as query parameters.
|
2016-08-05 07:01:58 +02:00
|
|
|
func (api objectAPIHandlers) ListenBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-21 03:46:32 +02:00
|
|
|
ctx := newContext(r, w, "ListenBucketNotification")
|
2018-03-14 20:01:47 +01:00
|
|
|
|
2018-11-22 05:03:24 +01:00
|
|
|
defer logger.AuditLog(w, r, "ListenBucketNotification", mustGetClaimsFromToken(r))
|
2018-10-12 21:25:59 +02:00
|
|
|
|
2016-08-11 03:47:49 +02:00
|
|
|
// Validate if bucket exists.
|
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrServerNotInitialized, r.URL, guessIsBrowserReq(r))
|
2016-08-11 03:47:49 +02:00
|
|
|
return
|
|
|
|
}
|
2018-02-10 00:19:30 +01:00
|
|
|
if !objAPI.IsNotificationSupported() {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL, guessIsBrowserReq(r))
|
2018-02-10 00:19:30 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 22:51:05 +01:00
|
|
|
|
2018-12-05 23:03:42 +01:00
|
|
|
if !objAPI.IsListenBucketSupported() {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2016-08-05 07:01:58 +02:00
|
|
|
vars := mux.Vars(r)
|
2018-03-15 21:03:41 +01:00
|
|
|
bucketName := vars["bucket"]
|
2016-08-05 07:01:58 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.ListenBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, s3Error, r.URL, guessIsBrowserReq(r))
|
2018-04-25 00:53:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
values := r.URL.Query()
|
2016-10-12 20:02:15 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
var prefix string
|
|
|
|
if len(values["prefix"]) > 1 {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrFilterNamePrefix, r.URL, guessIsBrowserReq(r))
|
2016-10-12 20:02:15 +02:00
|
|
|
}
|
2018-03-15 21:03:41 +01:00
|
|
|
if len(values["prefix"]) == 1 {
|
|
|
|
if err := event.ValidateFilterRuleValue(values["prefix"][0]); err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
return
|
|
|
|
}
|
2016-10-12 20:02:15 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
prefix = values["prefix"][0]
|
2016-08-05 07:01:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
var suffix string
|
|
|
|
if len(values["suffix"]) > 1 {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, ErrFilterNameSuffix, r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
}
|
|
|
|
if len(values["suffix"]) == 1 {
|
|
|
|
if err := event.ValidateFilterRuleValue(values["suffix"][0]); err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-09-27 21:50:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
suffix = values["suffix"][0]
|
2016-08-05 07:01:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
pattern := event.NewPattern(prefix, suffix)
|
2016-10-12 20:02:15 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
eventNames := []event.Name{}
|
|
|
|
for _, s := range values["events"] {
|
|
|
|
eventName, err := event.ParseName(s)
|
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
return
|
|
|
|
}
|
2016-08-05 07:01:58 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
eventNames = append(eventNames, eventName)
|
2016-09-27 21:50:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 20:01:47 +01:00
|
|
|
if _, err := objAPI.GetBucketInfo(ctx, bucketName); err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-10-17 05:52:10 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-21 10:25:17 +02:00
|
|
|
|
2018-06-14 19:17:07 +02:00
|
|
|
host, err := xnet.ParseHost(r.RemoteAddr)
|
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-06-14 19:17:07 +02:00
|
|
|
return
|
|
|
|
}
|
2018-04-20 02:24:43 +02:00
|
|
|
|
2018-06-14 19:17:07 +02:00
|
|
|
target, err := target.NewHTTPClientTarget(*host, w)
|
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-06-14 19:17:07 +02:00
|
|
|
return
|
|
|
|
}
|
2018-04-20 02:24:43 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
rulesMap := event.NewRulesMap(eventNames, pattern, target.ID())
|
|
|
|
|
2018-07-21 03:46:32 +02:00
|
|
|
if err = globalNotificationSys.AddRemoteTarget(bucketName, target, rulesMap); err != nil {
|
2018-04-06 00:04:40 +02:00
|
|
|
logger.GetReqInfo(ctx).AppendTags("target", target.ID().Name)
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-08-05 07:01:58 +02:00
|
|
|
return
|
|
|
|
}
|
2018-03-15 21:03:41 +01:00
|
|
|
defer globalNotificationSys.RemoveRemoteTarget(bucketName, target.ID())
|
|
|
|
defer globalNotificationSys.RemoveRulesMap(bucketName, rulesMap)
|
2016-10-12 10:03:50 +02:00
|
|
|
|
2018-06-14 19:17:07 +02:00
|
|
|
thisAddr, err := xnet.ParseHost(GetLocalPeer(globalEndpoints))
|
|
|
|
if err != nil {
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-06-14 19:17:07 +02:00
|
|
|
return
|
|
|
|
}
|
2018-04-20 02:24:43 +02:00
|
|
|
|
2018-06-14 19:17:07 +02:00
|
|
|
if err = SaveListener(objAPI, bucketName, eventNames, pattern, target.ID(), *thisAddr); err != nil {
|
2018-04-06 00:04:40 +02:00
|
|
|
logger.GetReqInfo(ctx).AppendTags("target", target.ID().Name)
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 21:03:41 +01:00
|
|
|
return
|
2017-08-31 20:29:22 +02:00
|
|
|
}
|
2016-10-25 04:52:24 +02:00
|
|
|
|
2018-07-03 20:09:36 +02:00
|
|
|
globalNotificationSys.ListenBucketNotification(ctx, bucketName, eventNames, pattern, target.ID(), *thisAddr)
|
2016-10-12 10:03:50 +02:00
|
|
|
|
2018-03-15 21:03:41 +01:00
|
|
|
<-target.DoneCh
|
2016-10-12 10:03:50 +02:00
|
|
|
|
2018-06-14 19:17:07 +02:00
|
|
|
if err = RemoveListener(objAPI, bucketName, target.ID(), *thisAddr); err != nil {
|
2018-04-06 00:04:40 +02:00
|
|
|
logger.GetReqInfo(ctx).AppendTags("target", target.ID().Name)
|
2018-11-26 21:15:12 +01:00
|
|
|
writeErrorResponse(w, toAPIErrorCode(ctx, err), r.URL, guessIsBrowserReq(r))
|
2017-08-31 20:29:22 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-12 10:03:50 +02:00
|
|
|
}
|