2016-07-24 01:51:12 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016, 2017, 2018 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 (
|
|
|
|
"encoding/xml"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2019-12-13 15:36:45 -05:00
|
|
|
"reflect"
|
2016-07-24 01:51:12 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-01-27 17:12:34 -05:00
|
|
|
"github.com/minio/minio/pkg/bucket/policy"
|
2018-03-15 16:03:41 -04:00
|
|
|
"github.com/minio/minio/pkg/event"
|
2016-07-24 01:51:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketConfigPrefix = "buckets"
|
|
|
|
bucketNotificationConfig = "notification.xml"
|
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// 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 01:51:12 -04:00
|
|
|
func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-20 21:46:32 -04:00
|
|
|
ctx := newContext(r, w, "GetBucketNotification")
|
2018-02-09 18:19:30 -05:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2018-10-12 15:25:59 -04:00
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucketName := vars["bucket"]
|
|
|
|
|
2016-08-10 21:47:49 -04:00
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL, guessIsBrowserReq(r))
|
2016-08-10 21:47:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
if !objAPI.IsNotificationSupported() {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
2018-02-09 18:19:30 -05:00
|
|
|
return
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.GetBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL, guessIsBrowserReq(r))
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
|
2018-03-14 15:01:47 -04:00
|
|
|
_, err := objAPI.GetBucketInfo(ctx, bucketName)
|
2016-11-14 18:45:00 -05:00
|
|
|
if err != nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-11-14 18:45:00 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
config, err := globalBucketMetadataSys.GetNotificationConfig(bucketName)
|
2018-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
2020-05-20 13:18:15 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2020-05-21 14:03:59 -04:00
|
|
|
config.SetRegion(globalServerRegion)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err = config.Validate(globalServerRegion, globalNotificationSys.targetList); err != nil {
|
|
|
|
arnErr, ok := err.(*event.ErrARNNotFound)
|
|
|
|
if ok {
|
|
|
|
for i, queue := range config.QueueList {
|
|
|
|
// Remove ARN not found queues, because we previously allowed
|
|
|
|
// adding unexpected entries into the config.
|
|
|
|
//
|
|
|
|
// With newer config disallowing changing / turning off
|
|
|
|
// notification targets without removing ARN in notification
|
|
|
|
// configuration we won't see this problem anymore.
|
|
|
|
if reflect.DeepEqual(queue.ARN, arnErr.ARN) && i < len(config.QueueList) {
|
|
|
|
config.QueueList = append(config.QueueList[:i],
|
|
|
|
config.QueueList[i+1:]...)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
// This is a one time activity we shall do this
|
|
|
|
// here and allow stale ARN to be removed. We shall
|
|
|
|
// never reach a stage where we will have stale
|
|
|
|
// notification configs.
|
2019-12-13 15:36:45 -05:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
} else {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
2019-12-13 15:36:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
configData, err := xml.Marshal(config)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
2017-01-06 03:37:00 -05:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
writeSuccessResponseXML(w, configData)
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// PutBucketNotificationHandler - This HTTP handler stores given notification configuration as per
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html.
|
2016-07-24 01:51:12 -04:00
|
|
|
func (api objectAPIHandlers) PutBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-20 21:46:32 -04:00
|
|
|
ctx := newContext(r, w, "PutBucketNotification")
|
2018-02-09 18:19:30 -05:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2018-10-12 15:25:59 -04:00
|
|
|
|
2016-08-10 21:47:49 -04:00
|
|
|
objectAPI := api.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL, guessIsBrowserReq(r))
|
2016-08-10 21:47:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
if !objectAPI.IsNotificationSupported() {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
2018-02-09 18:19:30 -05:00
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
|
2016-07-24 01:51:12 -04:00
|
|
|
vars := mux.Vars(r)
|
2018-03-15 16:03:41 -04:00
|
|
|
bucketName := vars["bucket"]
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.PutBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL, guessIsBrowserReq(r))
|
2018-04-24 18:53:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-14 15:01:47 -04:00
|
|
|
_, err := objectAPI.GetBucketInfo(ctx, bucketName)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
2016-07-24 01:51:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:33:01 -05:00
|
|
|
// PutBucketNotification always needs a Content-Length.
|
2018-03-15 16:03:41 -04:00
|
|
|
if r.ContentLength <= 0 {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrMissingContentLength), r.URL, guessIsBrowserReq(r))
|
2017-01-20 19:33:01 -05:00
|
|
|
return
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
config, err := event.ParseConfig(io.LimitReader(r.Body, r.ContentLength), globalServerRegion, globalNotificationSys.targetList)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2019-02-12 04:25:52 -05:00
|
|
|
apiErr := errorCodes.ToAPIErr(ErrMalformedXML)
|
2018-03-15 16:03:41 -04:00
|
|
|
if event.IsEventError(err) {
|
2019-02-12 04:25:52 -05:00
|
|
|
apiErr = toAPIError(ctx, err)
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-12-13 15:36:45 -05:00
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = globalBucketMetadataSys.Update(bucketName, bucketNotificationConfig, configData); err != nil {
|
2019-02-13 19:07:21 -05:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
2018-03-15 16:03:41 -04:00
|
|
|
return
|
2017-11-16 13:56:06 -05:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
rulesMap := config.ToRulesMap()
|
|
|
|
globalNotificationSys.AddRulesMap(bucketName, rulesMap)
|
2017-11-16 13:56:06 -05:00
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|