2020-08-19 23:36:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-30 12:29:01 +00:00
|
|
|
"bytes"
|
2022-03-10 09:25:10 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-19 23:36:34 +00:00
|
|
|
"net/http"
|
2022-03-10 09:25:10 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
2020-08-19 23:36:34 +00:00
|
|
|
|
2022-12-14 16:58:00 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2020-08-19 23:36:34 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
const sizeToDetectType = 512
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
func getRangeToDetectContentType(maxSize int64) *layer.RangeParams {
|
|
|
|
end := uint64(maxSize)
|
|
|
|
if sizeToDetectType < end {
|
|
|
|
end = sizeToDetectType
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layer.RangeParams{
|
|
|
|
Start: 0,
|
|
|
|
End: end - 1,
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:34 +00:00
|
|
|
func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
2022-06-28 13:35:05 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-19 23:36:34 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-03-09 13:15:17 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-08-23 08:19:41 +00:00
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
conditional, err := parseConditionalHeaders(r.Header)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse request params", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:03:09 +00:00
|
|
|
p := &layer.HeadObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
2021-08-10 10:03:09 +00:00
|
|
|
Object: reqInfo.ObjectName,
|
2021-08-19 06:55:22 +00:00
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
h.logAndSendError(w, "could not find object", reqInfo, err)
|
2020-08-19 23:36:34 +00:00
|
|
|
return
|
2021-06-30 12:29:01 +00:00
|
|
|
}
|
2022-06-28 13:35:05 +00:00
|
|
|
info := extendedInfo.ObjectInfo
|
2022-06-01 14:09:28 +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
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
if err = checkPreconditions(info, conditional); err != nil {
|
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
t := &layer.ObjectVersion{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: info.Name,
|
2022-08-04 17:31:33 +00:00
|
|
|
VersionID: info.VersionID(),
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:35:05 +00:00
|
|
|
tagSet, lockInfo, err := h.obj.GetObjectTaggingAndLock(r.Context(), t, extendedInfo.NodeVersion)
|
2021-08-17 11:23:01 +00:00
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
2022-05-31 14:28:06 +00:00
|
|
|
h.logAndSendError(w, "could not get object meta data", reqInfo, err)
|
2021-08-17 11:23:01 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(info.ContentType) == 0 {
|
2022-09-07 06:59:24 +00:00
|
|
|
if info.ContentType = layer.MimeByFilePath(info.Name); len(info.ContentType) == 0 {
|
2022-06-29 10:51:05 +00:00
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
|
|
|
getParams := &layer.GetObjectParams{
|
|
|
|
ObjectInfo: info,
|
|
|
|
Writer: buffer,
|
|
|
|
Range: getRangeToDetectContentType(info.Size),
|
|
|
|
BucketInfo: bktInfo,
|
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", info.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
info.ContentType = http.DetectContentType(buffer.Bytes())
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2022-05-31 14:28:06 +00:00
|
|
|
if err = h.setLockingHeaders(bktInfo, lockInfo, w.Header()); err != nil {
|
2022-03-09 13:15:17 +00:00
|
|
|
h.logAndSendError(w, "could not get locking info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:25:10 +00:00
|
|
|
if err = h.setExpirationHeader(r.Context(), bktInfo, info, w.Header()); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get expiration info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:15:24 +00:00
|
|
|
bktSettings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
writeHeaders(w.Header(), r.Header, extendedInfo, len(tagSet), bktSettings.Unversioned())
|
2020-10-24 13:09:22 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-08-19 23:36:34 +00:00
|
|
|
}
|
2020-08-22 02:45:48 +00:00
|
|
|
|
|
|
|
func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-22 02:45:48 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-08-23 08:37:08 +00:00
|
|
|
if err != nil {
|
2021-08-23 08:19:41 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-22 02:45:48 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
w.Header().Set(api.ContainerID, bktInfo.CID.EncodeToString())
|
2022-11-03 06:34:18 +00:00
|
|
|
w.Header().Set(api.AmzBucketRegion, bktInfo.LocationConstraint)
|
2020-08-22 02:45:48 +00:00
|
|
|
api.WriteResponse(w, http.StatusOK, nil, api.MimeNone)
|
|
|
|
}
|
2022-03-09 13:15:17 +00:00
|
|
|
|
2022-05-31 14:28:06 +00:00
|
|
|
func (h *handler) setLockingHeaders(bktInfo *data.BucketInfo, lockInfo *data.LockInfo, header http.Header) error {
|
2022-03-09 13:15:17 +00:00
|
|
|
if !bktInfo.ObjectLockEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
legalHold := &data.LegalHold{Status: legalHoldOff}
|
2022-05-26 13:11:14 +00:00
|
|
|
retention := &data.Retention{Mode: governanceMode}
|
2022-03-09 13:15:17 +00:00
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsLegalHoldSet() {
|
2022-05-31 14:28:06 +00:00
|
|
|
legalHold.Status = legalHoldOn
|
2022-03-09 13:15:17 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsRetentionSet() {
|
|
|
|
retention.RetainUntilDate = lockInfo.UntilDate()
|
|
|
|
if lockInfo.IsCompliance() {
|
2022-05-31 14:28:06 +00:00
|
|
|
retention.Mode = complianceMode
|
2022-03-09 13:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeLockHeaders(header, legalHold, retention)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLockHeaders(h http.Header, legalHold *data.LegalHold, retention *data.Retention) {
|
|
|
|
h.Set(api.AmzObjectLockLegalHold, legalHold.Status)
|
|
|
|
|
|
|
|
if retention.RetainUntilDate != "" {
|
|
|
|
h.Set(api.AmzObjectLockRetainUntilDate, retention.RetainUntilDate)
|
|
|
|
h.Set(api.AmzObjectLockMode, retention.Mode)
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 09:25:10 +00:00
|
|
|
|
|
|
|
func (h *handler) setExpirationHeader(ctx context.Context, bktInfo *data.BucketInfo, objInfo *data.ObjectInfo, header http.Header) error {
|
|
|
|
var expirationObjInfo data.ObjectInfo
|
|
|
|
|
|
|
|
// todo get expiration object info
|
|
|
|
|
|
|
|
ruleID := expirationObjInfo.Headers[layer.AttributeExpireRuleID]
|
|
|
|
|
|
|
|
expDate, err := time.Parse(time.RFC3339, expirationObjInfo.Headers[layer.AttributeExpireDate])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't parse ivalid expiration time: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeExpirationHeader(header, ruleID, expDate)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeExpirationHeader(h http.Header, ruleID string, expDate time.Time) {
|
|
|
|
header := "expiry-date=\"%s\", rule-id=\"%s\""
|
|
|
|
h.Set(api.AmzExpiration, fmt.Sprintf(header, expDate.UTC().Format(http.TimeFormat), url.QueryEscape(ruleID)))
|
|
|
|
}
|