2020-08-19 23:35:51 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-07-18 14:51:34 +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-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2022-06-03 13:37:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-08-19 23:35:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
type copyObjectArgs struct {
|
2021-08-06 15:08:09 +00:00
|
|
|
Conditional *conditionalArgs
|
|
|
|
MetadataDirective string
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
const replaceMetadataDirective = "REPLACE"
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// path2BucketObject returns a bucket and an object.
|
2020-08-19 23:35:51 +00:00
|
|
|
func path2BucketObject(path string) (bucket, prefix string) {
|
|
|
|
path = strings.TrimPrefix(path, api.SlashSeparator)
|
|
|
|
m := strings.Index(path, api.SlashSeparator)
|
|
|
|
if m < 0 {
|
|
|
|
return path, ""
|
|
|
|
}
|
|
|
|
return path[:m], path[m+len(api.SlashSeparator):]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2022-06-03 13:37:38 +00:00
|
|
|
versionID string
|
|
|
|
metadata map[string]string
|
|
|
|
sessionTokenEACL *session.Container
|
2020-08-19 23:35:51 +00:00
|
|
|
|
2022-06-03 13:37:38 +00:00
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
containsACL = containsACLHeaders(r)
|
2020-08-19 23:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
src := r.Header.Get("X-Amz-Copy-Source")
|
|
|
|
// Check https://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectVersioning.html
|
|
|
|
// Regardless of whether you have enabled versioning, each object in your bucket
|
|
|
|
// has a version ID. If you have not enabled versioning, Amazon S3 sets the value
|
|
|
|
// of the version ID to null. If you have enabled versioning, Amazon S3 assigns a
|
|
|
|
// unique version ID value for the object.
|
|
|
|
if u, err := url.Parse(src); err == nil {
|
2021-08-19 06:55:22 +00:00
|
|
|
versionID = u.Query().Get(api.QueryVersionID)
|
2020-08-19 23:35:51 +00:00
|
|
|
src = u.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
srcBucket, srcObject := path2BucketObject(src)
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
args, err := parseCopyObjectArgs(r.Header)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse request params", reqInfo, err)
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-10 10:03:09 +00:00
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
Object: srcObject,
|
2021-08-19 06:55:22 +00:00
|
|
|
VersionID: versionID,
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
if args.MetadataDirective == replaceMetadataDirective {
|
|
|
|
metadata = parseMetadata(r)
|
|
|
|
} else if srcBucket == reqInfo.BucketName && srcObject == reqInfo.ObjectName {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "could not copy to itself", reqInfo, errors.GetAPIError(errors.ErrInvalidRequest))
|
2021-08-06 15:08:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
if p.BktInfo, err = h.getBucketAndCheckOwner(r, srcBucket, api.AmzSourceExpectedBucketOwner); err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get source bucket", reqInfo, err)
|
2021-08-23 08:19:41 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
dstBktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get target bucket", reqInfo, err)
|
2021-08-23 08:19:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:37:38 +00:00
|
|
|
if containsACL {
|
|
|
|
if sessionTokenEACL, err = getSessionTokenSetEACL(r.Context()); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get eacl session token from a box", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 00:54:21 +00:00
|
|
|
objInfo, err := h.obj.GetObjectInfo(r.Context(), p)
|
2022-06-28 13:35:05 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not find object", reqInfo, err)
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:54:24 +00:00
|
|
|
encryption, err := h.formEncryptionParams(r.Header)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid sse headers", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = encryption.MatchObjectEncryption(objInfo.EncryptionInfo); err != nil {
|
|
|
|
h.logAndSendError(w, "encryption doesn't match object", reqInfo, errors.GetAPIError(errors.ErrBadRequest), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 00:54:21 +00:00
|
|
|
if err = checkPreconditions(objInfo, args.Conditional); err != nil {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, errors.GetAPIError(errors.ErrPreconditionFailed))
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
if metadata == nil {
|
2022-08-05 00:54:21 +00:00
|
|
|
if len(objInfo.ContentType) > 0 {
|
|
|
|
objInfo.Headers[api.ContentType] = objInfo.ContentType
|
2021-08-06 15:08:09 +00:00
|
|
|
}
|
2022-08-05 00:54:21 +00:00
|
|
|
metadata = objInfo.Headers
|
2021-08-06 15:08:09 +00:00
|
|
|
} else if contentType := r.Header.Get(api.ContentType); len(contentType) > 0 {
|
|
|
|
metadata[api.ContentType] = contentType
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:35:51 +00:00
|
|
|
params := &layer.CopyObjectParams{
|
2022-08-05 00:54:21 +00:00
|
|
|
SrcObject: objInfo,
|
2022-06-02 16:56:04 +00:00
|
|
|
ScrBktInfo: p.BktInfo,
|
2022-03-18 13:04:09 +00:00
|
|
|
DstBktInfo: dstBktInfo,
|
|
|
|
DstObject: reqInfo.ObjectName,
|
2022-08-05 00:54:21 +00:00
|
|
|
SrcSize: objInfo.Size,
|
2022-03-18 13:04:09 +00:00
|
|
|
Header: metadata,
|
2022-08-01 16:52:09 +00:00
|
|
|
Encryption: encryption,
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(r.Context(), dstBktInfo)
|
2022-02-28 10:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
params.Lock, err = formObjectLock(dstBktInfo, settings.LockConfiguration, r.Header)
|
2022-03-01 15:07:15 +00:00
|
|
|
if err != nil {
|
2022-02-28 10:22:07 +00:00
|
|
|
h.logAndSendError(w, "could not form object lock", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
additional := []zap.Field{zap.String("src_bucket_name", srcBucket), zap.String("src_object_name", srcObject)}
|
2022-08-05 00:54:21 +00:00
|
|
|
if objInfo, err = h.obj.CopyObject(r.Context(), params); err != nil {
|
2021-08-06 15:08:09 +00:00
|
|
|
h.logAndSendError(w, "couldn't copy object", reqInfo, err, additional...)
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2022-08-05 00:54:21 +00:00
|
|
|
} else if err = api.EncodeToResponse(w, &CopyObjectResponse{LastModified: objInfo.Created.UTC().Format(time.RFC3339), ETag: objInfo.HashSum}); err != nil {
|
2021-08-06 15:08:09 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err, additional...)
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2020-08-19 23:35:51 +00:00
|
|
|
}
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2022-06-03 13:37:38 +00:00
|
|
|
if containsACL {
|
2022-08-05 00:54:21 +00:00
|
|
|
newEaclTable, err := h.getNewEAclTable(r, dstBktInfo, objInfo)
|
2022-06-03 13:37:38 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get new eacl table", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.PutBucketACLParams{
|
2022-06-21 15:21:20 +00:00
|
|
|
BktInfo: dstBktInfo,
|
|
|
|
EACL: newEaclTable,
|
|
|
|
SessionToken: sessionTokenEACL,
|
2022-06-03 13:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.PutBucketACL(r.Context(), p); err != nil {
|
|
|
|
h.logAndSendError(w, "could not put bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
h.log.Info("object is copied",
|
2022-08-05 00:54:21 +00:00
|
|
|
zap.String("bucket", objInfo.Bucket),
|
|
|
|
zap.String("object", objInfo.Name),
|
|
|
|
zap.Stringer("object_id", objInfo.ID))
|
2022-03-31 06:21:38 +00:00
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
s := &SendNotificationParams{
|
2022-07-18 14:51:34 +00:00
|
|
|
Event: EventObjectCreatedCopy,
|
2022-08-05 00:54:21 +00:00
|
|
|
NotificationInfo: data.NotificationInfoFromObject(objInfo),
|
2022-07-18 14:51:34 +00:00
|
|
|
BktInfo: dstBktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
2022-03-31 06:21:38 +00:00
|
|
|
}
|
2022-04-29 13:08:22 +00:00
|
|
|
if err = h.sendNotifications(r.Context(), s); err != nil {
|
2022-03-31 06:21:38 +00:00
|
|
|
h.log.Error("couldn't send notification: %w", zap.Error(err))
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
if encryption.Enabled() {
|
|
|
|
addSSECHeaders(w.Header(), r.Header)
|
|
|
|
}
|
2020-08-19 23:35:51 +00:00
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
|
|
|
|
func parseCopyObjectArgs(headers http.Header) (*copyObjectArgs, error) {
|
|
|
|
var err error
|
2021-07-02 16:21:53 +00:00
|
|
|
args := &conditionalArgs{
|
|
|
|
IfMatch: headers.Get(api.AmzCopyIfMatch),
|
|
|
|
IfNoneMatch: headers.Get(api.AmzCopyIfNoneMatch),
|
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
|
|
|
|
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfModifiedSince)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfUnmodifiedSince)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
copyArgs := ©ObjectArgs{Conditional: args}
|
|
|
|
copyArgs.MetadataDirective = headers.Get(api.AmzMetadataDirective)
|
|
|
|
|
|
|
|
return copyArgs, nil
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|