2020-08-19 23:36:00 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-08-22 02:45:15 +00:00
|
|
|
"encoding/xml"
|
2020-08-19 23:36:00 +00:00
|
|
|
"net/http"
|
2021-09-07 06:17:12 +00:00
|
|
|
"strconv"
|
2022-05-26 13:11:14 +00:00
|
|
|
"strings"
|
2020-08-19 23:36:00 +00:00
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-03-31 06:24:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-06-23 20:25:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2022-04-21 08:49:56 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-31 06:24:29 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-06-22 13:22:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-08-19 23:36:00 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-10 12:08:15 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2020-08-19 23:36:00 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DeleteObjectsRequest -- xml carrying the object key names which should be deleted.
|
2020-08-22 02:45:15 +00:00
|
|
|
type DeleteObjectsRequest struct {
|
|
|
|
// Element to enable quiet mode for the request
|
|
|
|
Quiet bool
|
|
|
|
// List of objects to be deleted
|
|
|
|
Objects []ObjectIdentifier `xml:"Object"`
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// ObjectIdentifier carries the key name for the object to delete.
|
2020-08-22 02:45:15 +00:00
|
|
|
type ObjectIdentifier struct {
|
|
|
|
ObjectName string `xml:"Key"`
|
2021-08-10 12:08:15 +00:00
|
|
|
VersionID string `xml:"VersionId,omitempty"`
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DeletedObject carries the key name for the object to delete.
|
2021-09-07 06:17:12 +00:00
|
|
|
type DeletedObject struct {
|
|
|
|
ObjectIdentifier
|
|
|
|
DeleteMarker bool `xml:"DeleteMarker,omitempty"`
|
|
|
|
DeleteMarkerVersionID string `xml:"DeleteMarkerVersionId,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-22 02:45:15 +00:00
|
|
|
// DeleteError structure.
|
|
|
|
type DeleteError struct {
|
2021-08-19 06:55:22 +00:00
|
|
|
Code string
|
|
|
|
Message string
|
|
|
|
Key string
|
|
|
|
VersionID string `xml:"versionId,omitempty"`
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectsResponse container for multiple object deletes.
|
|
|
|
type DeleteObjectsResponse struct {
|
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ DeleteResult" json:"-"`
|
|
|
|
|
|
|
|
// Collection of all deleted objects
|
2021-09-07 06:17:12 +00:00
|
|
|
DeletedObjects []DeletedObject `xml:"Deleted,omitempty"`
|
2020-08-22 02:45:15 +00:00
|
|
|
|
|
|
|
// Collection of errors deleting certain objects.
|
|
|
|
Errors []DeleteError `xml:"Error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:00 +00:00
|
|
|
func (h *handler) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2022-03-31 06:24:57 +00:00
|
|
|
versionID := reqInfo.URL.Query().Get(api.QueryVersionID)
|
2021-08-10 12:08:15 +00:00
|
|
|
versionedObject := []*layer.VersionedObject{{
|
|
|
|
Name: reqInfo.ObjectName,
|
2022-03-31 06:24:57 +00:00
|
|
|
VersionID: versionID,
|
2021-08-10 12:08:15 +00:00
|
|
|
}}
|
2021-08-05 09:18:52 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-08-23 08:19:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:24:29 +00:00
|
|
|
bktSettings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.DeleteObjectParams{
|
2022-06-24 12:39:30 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Objects: versionedObject,
|
|
|
|
Settings: bktSettings,
|
2022-03-31 06:24:29 +00:00
|
|
|
}
|
2022-06-24 12:39:30 +00:00
|
|
|
deletedObjects := h.obj.DeleteObjects(r.Context(), p)
|
2021-09-07 06:17:12 +00:00
|
|
|
deletedObject := deletedObjects[0]
|
2022-06-24 12:39:30 +00:00
|
|
|
if deletedObject.Error != nil {
|
|
|
|
if isErrObjectLocked(deletedObject.Error) {
|
2022-03-09 10:23:33 +00:00
|
|
|
h.logAndSendError(w, "object is locked", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
|
|
|
|
return
|
|
|
|
}
|
2020-08-19 23:36:00 +00:00
|
|
|
h.log.Error("could not delete object",
|
2021-08-05 09:18:52 +00:00
|
|
|
zap.String("request_id", reqInfo.RequestID),
|
|
|
|
zap.String("bucket_name", reqInfo.BucketName),
|
|
|
|
zap.String("object_name", reqInfo.ObjectName),
|
2022-06-24 12:39:30 +00:00
|
|
|
zap.Error(deletedObject.Error))
|
2021-09-07 06:17:12 +00:00
|
|
|
}
|
2020-08-19 23:36:00 +00:00
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
var m *SendNotificationParams
|
2022-03-31 06:24:57 +00:00
|
|
|
|
2022-07-19 14:58:18 +00:00
|
|
|
if bktSettings.VersioningEnabled() && len(versionID) == 0 {
|
2022-04-29 13:08:22 +00:00
|
|
|
m = &SendNotificationParams{
|
|
|
|
Event: EventObjectRemovedDeleteMarkerCreated,
|
2022-03-31 06:24:57 +00:00
|
|
|
ObjInfo: &data.ObjectInfo{
|
|
|
|
Name: reqInfo.ObjectName,
|
|
|
|
HashSum: deletedObject.DeleteMarkerEtag,
|
|
|
|
},
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-25 09:57:58 +00:00
|
|
|
var objID oid.ID
|
2022-03-31 06:24:57 +00:00
|
|
|
if len(versionID) != 0 {
|
2022-04-25 09:57:58 +00:00
|
|
|
if err = objID.DecodeString(versionID); err != nil {
|
2022-03-31 06:24:57 +00:00
|
|
|
h.log.Error("couldn't send notification: %w", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
m = &SendNotificationParams{
|
|
|
|
Event: EventObjectRemovedDelete,
|
2022-03-31 06:24:57 +00:00
|
|
|
ObjInfo: &data.ObjectInfo{
|
|
|
|
Name: reqInfo.ObjectName,
|
2022-05-25 17:25:43 +00:00
|
|
|
ID: objID,
|
2022-03-31 06:24:57 +00:00
|
|
|
},
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
if err = h.sendNotifications(r.Context(), m); err != nil {
|
2022-03-31 06:24:57 +00:00
|
|
|
h.log.Error("couldn't send notification: %w", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
if deletedObject.DeleteMarkVersion != "" {
|
|
|
|
w.Header().Set(api.AmzDeleteMarker, strconv.FormatBool(true))
|
|
|
|
w.Header().Set(api.AmzVersionID, deletedObject.DeleteMarkVersion)
|
2020-08-19 23:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
2020-08-20 19:35:39 +00:00
|
|
|
|
2022-04-21 08:49:56 +00:00
|
|
|
func isErrObjectLocked(err error) bool {
|
|
|
|
switch err.(type) {
|
|
|
|
default:
|
2022-05-26 13:11:14 +00:00
|
|
|
return strings.Contains(err.Error(), "object is locked")
|
2022-04-21 08:49:56 +00:00
|
|
|
case apistatus.ObjectLocked,
|
|
|
|
*apistatus.ObjectLocked:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// DeleteMultipleObjectsHandler handles multiple delete requests.
|
2020-08-20 19:35:39 +00:00
|
|
|
func (h *handler) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-22 02:45:15 +00:00
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Content-Md5 is required and should be set
|
2020-08-22 02:45:15 +00:00
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
|
|
|
|
if _, ok := r.Header[api.ContentMD5]; !ok {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "missing Content-MD5", reqInfo, errors.GetAPIError(errors.ErrMissingContentMD5))
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content-Length is required and should be non-zero
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
|
|
|
|
if r.ContentLength <= 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "missing Content-Length", reqInfo, errors.GetAPIError(errors.ErrMissingContentLength))
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal list of keys to be deleted.
|
|
|
|
requested := &DeleteObjectsRequest{}
|
|
|
|
if err := xml.NewDecoder(r.Body).Decode(requested); err != nil {
|
2022-01-19 20:20:07 +00:00
|
|
|
h.logAndSendError(w, "couldn't decode body", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
removed := make(map[string]*layer.VersionedObject)
|
2021-08-10 12:08:15 +00:00
|
|
|
toRemove := make([]*layer.VersionedObject, 0, len(requested.Objects))
|
2020-08-22 02:45:15 +00:00
|
|
|
for _, obj := range requested.Objects {
|
2021-08-10 12:08:15 +00:00
|
|
|
versionedObj := &layer.VersionedObject{
|
|
|
|
Name: obj.ObjectName,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
}
|
|
|
|
toRemove = append(toRemove, versionedObj)
|
2021-08-19 06:55:22 +00:00
|
|
|
removed[versionedObj.String()] = versionedObj
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &DeleteObjectsResponse{
|
|
|
|
Errors: make([]DeleteError, 0, len(toRemove)),
|
2021-09-07 06:17:12 +00:00
|
|
|
DeletedObjects: make([]DeletedObject, 0, len(toRemove)),
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-08-23 08:19:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-24 12:39:30 +00:00
|
|
|
bktSettings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:08:15 +00:00
|
|
|
marshaler := zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
|
|
|
|
for _, obj := range toRemove {
|
|
|
|
encoder.AppendString(obj.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-31 06:24:29 +00:00
|
|
|
p := &layer.DeleteObjectParams{
|
2022-06-24 12:39:30 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Objects: toRemove,
|
|
|
|
Settings: bktSettings,
|
2021-09-07 06:17:12 +00:00
|
|
|
}
|
2022-06-24 12:39:30 +00:00
|
|
|
deletedObjects := h.obj.DeleteObjects(r.Context(), p)
|
2020-08-22 02:45:15 +00:00
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
var errs []error
|
|
|
|
for _, obj := range deletedObjects {
|
2022-04-04 11:38:27 +00:00
|
|
|
if obj.Error != nil {
|
2021-09-07 06:17:12 +00:00
|
|
|
code := "BadRequest"
|
|
|
|
if s3err, ok := obj.Error.(errors.Error); ok {
|
|
|
|
code = s3err.Code
|
|
|
|
}
|
|
|
|
response.Errors = append(response.Errors, DeleteError{
|
|
|
|
Code: code,
|
|
|
|
Message: obj.Error.Error(),
|
|
|
|
Key: obj.Name,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
})
|
|
|
|
errs = append(errs, obj.Error)
|
2022-04-04 11:38:27 +00:00
|
|
|
} else if !requested.Quiet {
|
|
|
|
deletedObj := DeletedObject{
|
|
|
|
ObjectIdentifier: ObjectIdentifier{
|
|
|
|
ObjectName: obj.Name,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
},
|
|
|
|
DeleteMarkerVersionID: obj.DeleteMarkVersion,
|
|
|
|
}
|
|
|
|
if deletedObj.DeleteMarkerVersionID != "" {
|
|
|
|
deletedObj.DeleteMarker = true
|
|
|
|
}
|
|
|
|
response.DeletedObjects = append(response.DeletedObjects, deletedObj)
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-04 11:38:27 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
fields := []zap.Field{
|
2021-09-07 06:17:12 +00:00
|
|
|
zap.Array("objects", marshaler),
|
|
|
|
zap.Errors("errors", errs),
|
|
|
|
}
|
2022-04-04 11:38:27 +00:00
|
|
|
h.log.Error("couldn't delete objects", fields...)
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:40:52 +00:00
|
|
|
if err = api.EncodeToResponse(w, response); err != nil {
|
2021-08-10 12:08:15 +00:00
|
|
|
h.logAndSendError(w, "could not write response", reqInfo, err, zap.Array("objects", marshaler))
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
2020-08-20 19:35:39 +00:00
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
|
|
|
func (h *handler) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-08-23 08:19:41 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-18 13:04:09 +00:00
|
|
|
|
2022-06-22 13:22:28 +00:00
|
|
|
var sessionToken *session.Container
|
|
|
|
|
|
|
|
boxData, err := layer.GetBoxData(r.Context())
|
|
|
|
if err == nil {
|
|
|
|
sessionToken = boxData.Gate.SessionTokenForDelete()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.DeleteBucket(r.Context(), &layer.DeleteBucketParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
}); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "couldn't delete bucket", reqInfo, err)
|
2021-06-23 20:25:00 +00:00
|
|
|
}
|
2021-09-21 09:31:23 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2021-06-23 20:25:00 +00:00
|
|
|
}
|