2022-02-17 18:58:51 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-04-29 13:08:22 +00:00
|
|
|
"context"
|
2022-02-17 18:58:51 +00:00
|
|
|
"encoding/xml"
|
2022-06-22 19:40:52 +00:00
|
|
|
"fmt"
|
2022-02-17 18:58:51 +00:00
|
|
|
"net/http"
|
2022-04-29 13:08:22 +00:00
|
|
|
"strings"
|
2022-11-08 09:12:55 +00:00
|
|
|
"time"
|
2022-02-17 18:58:51 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2022-04-29 13:08:22 +00:00
|
|
|
"github.com/google/uuid"
|
2022-02-17 18:58:51 +00:00
|
|
|
)
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
type (
|
|
|
|
SendNotificationParams struct {
|
2022-07-18 14:51:34 +00:00
|
|
|
Event string
|
|
|
|
NotificationInfo *data.NotificationInfo
|
|
|
|
BktInfo *data.BucketInfo
|
2023-07-05 14:05:45 +00:00
|
|
|
ReqInfo *middleware.ReqInfo
|
2022-07-18 14:51:34 +00:00
|
|
|
User string
|
2022-11-08 09:12:55 +00:00
|
|
|
Time time.Time
|
2022-04-29 13:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationConfiguration struct {
|
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ NotificationConfiguation"`
|
|
|
|
NotificationConfiguration data.NotificationConfiguration
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filterRuleSuffixName = "suffix"
|
|
|
|
filterRulePrefixName = "prefix"
|
|
|
|
|
|
|
|
EventObjectCreated = "s3:ObjectCreated:*"
|
|
|
|
EventObjectCreatedPut = "s3:ObjectCreated:Put"
|
|
|
|
EventObjectCreatedPost = "s3:ObjectCreated:Post"
|
|
|
|
EventObjectCreatedCopy = "s3:ObjectCreated:Copy"
|
|
|
|
EventReducedRedundancyLostObject = "s3:ReducedRedundancyLostObject"
|
|
|
|
EventObjectCreatedCompleteMultipartUpload = "s3:ObjectCreated:CompleteMultipartUpload"
|
|
|
|
EventObjectRemoved = "s3:ObjectRemoved:*"
|
|
|
|
EventObjectRemovedDelete = "s3:ObjectRemoved:Delete"
|
|
|
|
EventObjectRemovedDeleteMarkerCreated = "s3:ObjectRemoved:DeleteMarkerCreated"
|
|
|
|
EventObjectRestore = "s3:ObjectRestore:*"
|
|
|
|
EventObjectRestorePost = "s3:ObjectRestore:Post"
|
|
|
|
EventObjectRestoreCompleted = "s3:ObjectRestore:Completed"
|
|
|
|
EventReplication = "s3:Replication:*"
|
|
|
|
EventReplicationOperationFailedReplication = "s3:Replication:OperationFailedReplication"
|
|
|
|
EventReplicationOperationNotTracked = "s3:Replication:OperationNotTracked"
|
|
|
|
EventReplicationOperationMissedThreshold = "s3:Replication:OperationMissedThreshold"
|
|
|
|
EventReplicationOperationReplicatedAfterThreshold = "s3:Replication:OperationReplicatedAfterThreshold"
|
|
|
|
EventObjectRestoreDelete = "s3:ObjectRestore:Delete"
|
|
|
|
EventLifecycleTransition = "s3:LifecycleTransition"
|
|
|
|
EventIntelligentTiering = "s3:IntelligentTiering"
|
|
|
|
EventObjectACLPut = "s3:ObjectAcl:Put"
|
|
|
|
EventLifecycleExpiration = "s3:LifecycleExpiration:*"
|
|
|
|
EventLifecycleExpirationDelete = "s3:LifecycleExpiration:Delete"
|
|
|
|
EventLifecycleExpirationDeleteMarkerCreated = "s3:LifecycleExpiration:DeleteMarkerCreated"
|
|
|
|
EventObjectTagging = "s3:ObjectTagging:*"
|
|
|
|
EventObjectTaggingPut = "s3:ObjectTagging:Put"
|
|
|
|
EventObjectTaggingDelete = "s3:ObjectTagging:Delete"
|
|
|
|
)
|
|
|
|
|
|
|
|
var validEvents = map[string]struct{}{
|
|
|
|
EventReducedRedundancyLostObject: {},
|
|
|
|
EventObjectCreated: {},
|
|
|
|
EventObjectCreatedPut: {},
|
|
|
|
EventObjectCreatedPost: {},
|
|
|
|
EventObjectCreatedCopy: {},
|
|
|
|
EventObjectCreatedCompleteMultipartUpload: {},
|
|
|
|
EventObjectRemoved: {},
|
|
|
|
EventObjectRemovedDelete: {},
|
|
|
|
EventObjectRemovedDeleteMarkerCreated: {},
|
|
|
|
EventObjectRestore: {},
|
|
|
|
EventObjectRestorePost: {},
|
|
|
|
EventObjectRestoreCompleted: {},
|
|
|
|
EventReplication: {},
|
|
|
|
EventReplicationOperationFailedReplication: {},
|
|
|
|
EventReplicationOperationNotTracked: {},
|
|
|
|
EventReplicationOperationMissedThreshold: {},
|
|
|
|
EventReplicationOperationReplicatedAfterThreshold: {},
|
|
|
|
EventObjectRestoreDelete: {},
|
|
|
|
EventLifecycleTransition: {},
|
|
|
|
EventIntelligentTiering: {},
|
|
|
|
EventObjectACLPut: {},
|
|
|
|
EventLifecycleExpiration: {},
|
|
|
|
EventLifecycleExpirationDelete: {},
|
|
|
|
EventLifecycleExpirationDeleteMarkerCreated: {},
|
|
|
|
EventObjectTagging: {},
|
|
|
|
EventObjectTaggingPut: {},
|
|
|
|
EventObjectTaggingDelete: {},
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) PutBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-17 18:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
conf := &data.NotificationConfiguration{}
|
|
|
|
if err = xml.NewDecoder(r.Body).Decode(conf); err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't decode notification configuration", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
if _, err = h.checkBucketConfiguration(r.Context(), conf, reqInfo); err != nil {
|
2022-04-29 13:08:22 +00:00
|
|
|
h.logAndSendError(w, "couldn't check bucket configuration", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-17 18:58:51 +00:00
|
|
|
p := &layer.PutBucketNotificationConfigurationParams{
|
2022-04-29 13:08:22 +00:00
|
|
|
RequestInfo: reqInfo,
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Configuration: conf,
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.CopiesNumbers, err = h.pickCopiesNumbers(parseMetadata(r), bktInfo.LocationConstraint)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid copies number", reqInfo, err)
|
|
|
|
return
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
if err = h.obj.PutBucketNotificationConfiguration(r.Context(), p); err != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
h.logAndSendError(w, "couldn't put bucket configuration", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-02-17 18:58:51 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-17 18:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := h.obj.GetBucketNotificationConfiguration(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket notification configuration", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, conf); err != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
h.logAndSendError(w, "could not encode bucket notification configuration to response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 13:08:22 +00:00
|
|
|
|
|
|
|
func (h *handler) sendNotifications(ctx context.Context, p *SendNotificationParams) error {
|
|
|
|
if !h.cfg.NotificatorEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := h.obj.GetBucketNotificationConfiguration(ctx, p.BktInfo)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("failed to get notification configuration: %w", err)
|
2022-04-29 13:08:22 +00:00
|
|
|
}
|
|
|
|
if conf.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:34:41 +00:00
|
|
|
box, err := middleware.GetBoxData(ctx)
|
2022-06-01 14:00:30 +00:00
|
|
|
if err == nil && box.Gate.BearerToken != nil {
|
|
|
|
p.User = bearer.ResolveIssuer(*box.Gate.BearerToken).EncodeToString()
|
2022-04-29 13:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
p.Time = layer.TimeNow(ctx)
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
topics := filterSubjects(conf, p.Event, p.NotificationInfo.Name)
|
2022-04-29 13:08:22 +00:00
|
|
|
|
|
|
|
return h.notificator.SendNotifications(topics, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkBucketConfiguration checks notification configuration and generates an ID for configurations with empty ids.
|
2023-07-05 14:05:45 +00:00
|
|
|
func (h *handler) checkBucketConfiguration(ctx context.Context, conf *data.NotificationConfiguration, r *middleware.ReqInfo) (completed bool, err error) {
|
2022-04-29 13:08:22 +00:00
|
|
|
if conf == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.TopicConfigurations != nil || conf.LambdaFunctionConfigurations != nil {
|
|
|
|
return completed, errors.GetAPIError(errors.ErrNotificationTopicNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, q := range conf.QueueConfigurations {
|
|
|
|
if err = checkEvents(q.Events); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkRules(q.Filter.Key.FilterRules); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.cfg.NotificatorEnabled {
|
2022-11-08 09:12:55 +00:00
|
|
|
if err = h.notificator.SendTestNotification(q.QueueArn, r.BucketName, r.RequestID, r.Host, layer.TimeNow(ctx)); err != nil {
|
2022-04-29 13:08:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Warn(logs.FailedToSendTestEventBecauseNotificationsIsDisabled)
|
2022-04-29 13:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if q.ID == "" {
|
|
|
|
completed = true
|
|
|
|
conf.QueueConfigurations[i].ID = uuid.NewString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRules(rules []data.FilterRule) error {
|
|
|
|
names := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, r := range rules {
|
|
|
|
if r.Name != filterRuleSuffixName && r.Name != filterRulePrefixName {
|
|
|
|
return errors.GetAPIError(errors.ErrFilterNameInvalid)
|
|
|
|
}
|
|
|
|
if _, ok := names[r.Name]; ok {
|
|
|
|
if r.Name == filterRuleSuffixName {
|
|
|
|
return errors.GetAPIError(errors.ErrFilterNameSuffix)
|
|
|
|
}
|
|
|
|
return errors.GetAPIError(errors.ErrFilterNamePrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
names[r.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEvents(events []string) error {
|
|
|
|
for _, e := range events {
|
|
|
|
if _, ok := validEvents[e]; !ok {
|
|
|
|
return errors.GetAPIError(errors.ErrEventNotification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterSubjects(conf *data.NotificationConfiguration, eventType, objName string) map[string]string {
|
|
|
|
topics := make(map[string]string)
|
|
|
|
|
|
|
|
for _, t := range conf.QueueConfigurations {
|
|
|
|
event := false
|
|
|
|
for _, e := range t.Events {
|
|
|
|
// the second condition is comparison with the events ending with *:
|
|
|
|
// s3:ObjectCreated:*, s3:ObjectRemoved:* etc without the last char
|
|
|
|
if eventType == e || strings.HasPrefix(eventType, e[:len(e)-1]) {
|
|
|
|
event = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !event {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := true
|
|
|
|
for _, f := range t.Filter.Key.FilterRules {
|
|
|
|
if f.Name == filterRulePrefixName && !strings.HasPrefix(objName, f.Value) ||
|
|
|
|
f.Name == filterRuleSuffixName && !strings.HasSuffix(objName, f.Value) {
|
|
|
|
filter = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter {
|
|
|
|
topics[t.ID] = t.QueueArn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return topics
|
|
|
|
}
|