2022-05-12 21:15:08 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-10-27 15:15:33 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2022-06-22 19:40:52 +00:00
|
|
|
"fmt"
|
2022-05-12 21:15:08 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"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"
|
2022-08-01 16:52:09 +00:00
|
|
|
"go.uber.org/zap"
|
2022-05-12 21:15:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
GetObjectAttributesResponse struct {
|
|
|
|
ETag string `xml:"ETag,omitempty"`
|
2022-06-02 15:26:26 +00:00
|
|
|
Checksum *Checksum `xml:"Checksum,omitempty"`
|
2023-06-01 13:45:28 +00:00
|
|
|
ObjectSize uint64 `xml:"ObjectSize,omitempty"`
|
2022-05-12 21:15:08 +00:00
|
|
|
StorageClass string `xml:"StorageClass,omitempty"`
|
|
|
|
ObjectParts *ObjectParts `xml:"ObjectParts,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
Checksum struct {
|
|
|
|
ChecksumSHA256 string `xml:"ChecksumSHA256,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:15:08 +00:00
|
|
|
ObjectParts struct {
|
|
|
|
IsTruncated bool `xml:"IsTruncated,omitempty"`
|
|
|
|
MaxParts int `xml:"MaxParts,omitempty"`
|
|
|
|
NextPartNumberMarker int `xml:"NextPartNumberMarker,omitempty"`
|
|
|
|
PartNumberMarker int `xml:"PartNumberMarker,omitempty"`
|
|
|
|
Parts []Part `xml:"Part,omitempty"`
|
2022-06-02 15:26:26 +00:00
|
|
|
PartsCount int `xml:"PartsCount,omitempty"`
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Part struct {
|
2022-06-02 15:26:26 +00:00
|
|
|
ChecksumSHA256 string `xml:"ChecksumSHA256,omitempty"`
|
|
|
|
PartNumber int `xml:"PartNumber,omitempty"`
|
|
|
|
Size int `xml:"Size,omitempty"`
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GetObjectAttributesArgs struct {
|
2022-06-02 15:26:26 +00:00
|
|
|
MaxParts int
|
|
|
|
PartNumberMarker int
|
|
|
|
Attributes []string
|
|
|
|
VersionID string
|
|
|
|
Conditional *conditionalArgs
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eTag = "ETag"
|
|
|
|
checksum = "Checksum"
|
|
|
|
objectParts = "ObjectParts"
|
|
|
|
storageClass = "StorageClass"
|
|
|
|
objectSize = "ObjectSize"
|
|
|
|
)
|
|
|
|
|
|
|
|
var validAttributes = map[string]struct{}{
|
|
|
|
eTag: {},
|
|
|
|
checksum: {},
|
|
|
|
objectParts: {},
|
|
|
|
storageClass: {},
|
|
|
|
objectSize: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetObjectAttributesHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-05-12 21:15:08 +00:00
|
|
|
|
|
|
|
params, err := parseGetObjectAttributeArgs(r)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid request", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: params.VersionID,
|
|
|
|
}
|
|
|
|
|
2022-08-05 00:54:21 +00:00
|
|
|
extendedInfo, err := h.obj.GetExtendedObjectInfo(r.Context(), p)
|
2022-06-28 13:35:05 +00:00
|
|
|
if err != nil {
|
2022-05-12 21:15:08 +00:00
|
|
|
h.logAndSendError(w, "could not fetch object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-28 13:35:05 +00:00
|
|
|
info := extendedInfo.ObjectInfo
|
2022-05-12 21:15:08 +00:00
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
encryptionParams, err := formEncryptionParams(r)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid sse headers", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
if err = encryptionParams.MatchObjectEncryption(layer.FormEncryptionInfo(info.Headers)); err != nil {
|
2022-08-01 16:52:09 +00:00
|
|
|
h.logAndSendError(w, "encryption doesn't match object", reqInfo, errors.GetAPIError(errors.ErrBadRequest), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
if err = checkPreconditions(info, params.Conditional, h.cfg.MD5Enabled()); err != nil {
|
2022-06-01 14:09:28 +00:00
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:23:16 +00:00
|
|
|
bktSettings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
response, err := encodeToObjectAttributesResponse(info, params, h.cfg.MD5Enabled())
|
2022-05-12 21:15:08 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't encode object info to response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:23:16 +00:00
|
|
|
writeAttributesHeaders(w.Header(), extendedInfo, bktSettings.Unversioned())
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, response); err != nil {
|
2022-05-12 21:15:08 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:23:16 +00:00
|
|
|
func writeAttributesHeaders(h http.Header, info *data.ExtendedObjectInfo, isBucketUnversioned bool) {
|
2022-08-08 22:35:26 +00:00
|
|
|
h.Set(api.LastModified, info.ObjectInfo.Created.UTC().Format(http.TimeFormat))
|
2022-08-05 01:23:16 +00:00
|
|
|
if !isBucketUnversioned {
|
2022-05-12 21:15:08 +00:00
|
|
|
h.Set(api.AmzVersionID, info.Version())
|
|
|
|
}
|
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if info.NodeVersion.IsDeleteMarker {
|
2022-05-12 21:15:08 +00:00
|
|
|
h.Set(api.AmzDeleteMarker, strconv.FormatBool(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// x-amz-request-charged
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, error) {
|
2022-06-01 09:54:27 +00:00
|
|
|
res := &GetObjectAttributesArgs{
|
|
|
|
VersionID: r.URL.Query().Get(api.QueryVersionID),
|
|
|
|
}
|
2022-05-12 21:15:08 +00:00
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
attributesVal := r.Header.Get(api.AmzObjectAttributes)
|
2022-05-12 21:15:08 +00:00
|
|
|
if attributesVal == "" {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := strings.Split(attributesVal, ",")
|
|
|
|
for _, a := range attributes {
|
|
|
|
if _, ok := validAttributes[a]; !ok {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
|
|
|
|
}
|
|
|
|
res.Attributes = append(res.Attributes, a)
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
var err error
|
|
|
|
maxPartsVal := r.Header.Get(api.AmzMaxParts)
|
|
|
|
if maxPartsVal == "" {
|
|
|
|
res.MaxParts = layer.MaxSizePartsList
|
|
|
|
} else if res.MaxParts, err = strconv.Atoi(maxPartsVal); err != nil || res.MaxParts < 0 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidMaxKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
markerVal := r.Header.Get(api.AmzPartNumberMarker)
|
|
|
|
if markerVal != "" {
|
|
|
|
if res.PartNumberMarker, err = strconv.Atoi(markerVal); err != nil || res.PartNumberMarker < 0 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidPartNumberMarker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Conditional, err = parseConditionalHeaders(r.Header)
|
|
|
|
return res, err
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
func encodeToObjectAttributesResponse(info *data.ObjectInfo, p *GetObjectAttributesArgs, md5Enabled bool) (*GetObjectAttributesResponse, error) {
|
2022-05-12 21:15:08 +00:00
|
|
|
resp := &GetObjectAttributesResponse{}
|
|
|
|
|
|
|
|
for _, attr := range p.Attributes {
|
|
|
|
switch attr {
|
|
|
|
case eTag:
|
2023-10-27 15:15:33 +00:00
|
|
|
resp.ETag = data.Quote(info.ETag(md5Enabled))
|
2022-05-12 21:15:08 +00:00
|
|
|
case storageClass:
|
2023-10-25 11:49:39 +00:00
|
|
|
resp.StorageClass = api.DefaultStorageClass
|
2022-05-12 21:15:08 +00:00
|
|
|
case objectSize:
|
2024-01-22 11:11:12 +00:00
|
|
|
resp.ObjectSize = info.Size
|
2022-06-02 15:26:26 +00:00
|
|
|
case checksum:
|
2023-10-27 15:15:33 +00:00
|
|
|
checksumBytes, err := hex.DecodeString(info.HashSum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("form upload attributes: %w", err)
|
|
|
|
}
|
|
|
|
resp.Checksum = &Checksum{ChecksumSHA256: base64.StdEncoding.EncodeToString(checksumBytes)}
|
2022-05-12 21:15:08 +00:00
|
|
|
case objectParts:
|
2022-06-02 15:26:26 +00:00
|
|
|
parts, err := formUploadAttributes(info, p.MaxParts, p.PartNumberMarker)
|
2022-05-12 21:15:08 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("form upload attributes: %w", err)
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
if parts != nil {
|
|
|
|
resp.ObjectParts = parts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
func formUploadAttributes(info *data.ObjectInfo, maxParts, marker int) (*ObjectParts, error) {
|
|
|
|
completedParts, ok := info.Headers[layer.UploadCompletedParts]
|
2022-06-01 09:54:27 +00:00
|
|
|
if !ok {
|
2022-05-12 21:15:08 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
partInfos := strings.Split(completedParts, ",")
|
|
|
|
parts := make([]Part, len(partInfos))
|
|
|
|
for i, p := range partInfos {
|
2022-08-11 08:48:58 +00:00
|
|
|
part, err := layer.ParseCompletedPartHeader(p)
|
2022-06-02 15:26:26 +00:00
|
|
|
if err != nil {
|
2022-08-12 09:45:58 +00:00
|
|
|
return nil, fmt.Errorf("invalid completed part: %w", err)
|
2022-06-02 15:26:26 +00:00
|
|
|
}
|
2023-10-27 15:15:33 +00:00
|
|
|
// ETag value contains SHA256 checksum.
|
|
|
|
checksumBytes, err := hex.DecodeString(part.ETag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid sha256 checksum in completed part: %w", err)
|
|
|
|
}
|
2022-06-02 15:26:26 +00:00
|
|
|
parts[i] = Part{
|
2022-08-11 08:48:58 +00:00
|
|
|
PartNumber: part.PartNumber,
|
|
|
|
Size: int(part.Size),
|
2023-10-27 15:15:33 +00:00
|
|
|
ChecksumSHA256: base64.StdEncoding.EncodeToString(checksumBytes),
|
2022-06-02 15:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &ObjectParts{
|
|
|
|
PartsCount: len(parts),
|
|
|
|
}
|
|
|
|
|
|
|
|
if marker != 0 {
|
|
|
|
res.PartNumberMarker = marker
|
|
|
|
var found bool
|
|
|
|
for i, n := range parts {
|
|
|
|
if n.PartNumber == marker {
|
|
|
|
parts = parts[i:]
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidPartNumberMarker)
|
|
|
|
}
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
res.MaxParts = maxParts
|
|
|
|
if len(parts) > maxParts {
|
|
|
|
res.IsTruncated = true
|
|
|
|
res.NextPartNumberMarker = parts[maxParts].PartNumber
|
|
|
|
parts = parts[:maxParts]
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Parts = parts
|
|
|
|
|
|
|
|
return res, nil
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|