2021-08-17 08:04:42 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-08-17 11:57:24 +00:00
|
|
|
"sort"
|
2021-08-17 08:04:42 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
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"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2021-08-17 08:04:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
allowedTagChars = "+-=._:/@"
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
maxTags = 10
|
2021-08-17 08:04:42 +00:00
|
|
|
keyTagMaxLength = 128
|
|
|
|
valueTagMaxLength = 256
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *handler) PutObjectTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-08-17 08:04:42 +00:00
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
tagSet, err := h.readTagSet(reqInfo.Tagging)
|
2021-08-17 11:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not read tag set", reqInfo, err)
|
2021-08-17 08:04:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-19 12:33:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
tagPrm := &data.PutObjectTaggingParams{
|
|
|
|
ObjectVersion: &data.ObjectVersion{
|
2022-10-14 14:36:43 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
|
|
|
},
|
|
|
|
TagSet: tagSet,
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
2024-06-25 12:24:29 +00:00
|
|
|
|
|
|
|
if err = h.obj.PutObjectTagging(ctx, tagPrm); err != nil {
|
2021-08-17 08:04:42 +00:00
|
|
|
h.logAndSendError(w, "could not put object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-09 16:46:43 +00:00
|
|
|
|
2021-09-21 09:31:23 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:01 +00:00
|
|
|
func (h *handler) GetObjectTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-08-17 11:23:01 +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-19 12:33:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:12:07 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
tagPrm := &data.GetObjectTaggingParams{
|
|
|
|
ObjectVersion: &data.ObjectVersion{
|
2022-10-14 14:36:43 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
|
|
|
},
|
2021-08-17 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
versionID, tagSet, err := h.obj.GetObjectTagging(r.Context(), tagPrm)
|
2021-08-17 11:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:58:18 +00:00
|
|
|
if settings.VersioningEnabled() {
|
2022-06-07 09:12:07 +00:00
|
|
|
w.Header().Set(api.AmzVersionID, versionID)
|
|
|
|
}
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, encodeTagging(tagSet)); err != nil {
|
2021-08-17 11:23:01 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:49 +00:00
|
|
|
func (h *handler) DeleteObjectTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-08-17 11:23:49 +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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
p := &data.ObjectVersion{
|
2022-05-26 13:11:14 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
2022-07-18 14:51:34 +00:00
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2021-08-17 11:23:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:24:29 +00:00
|
|
|
if err = h.obj.DeleteObjectTagging(ctx, p); err != nil {
|
2021-08-17 11:23:49 +00:00
|
|
|
h.logAndSendError(w, "could not delete object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-09 16:46:43 +00:00
|
|
|
|
2021-08-17 11:23:49 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
func (h *handler) PutBucketTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-08-17 11:57:24 +00:00
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
tagSet, err := h.readTagSet(reqInfo.Tagging)
|
2021-08-17 11:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not read tag set", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-19 12:33:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
if err = h.obj.PutBucketTagging(r.Context(), bktInfo, tagSet); err != nil {
|
2021-08-17 11:57:24 +00:00
|
|
|
h.logAndSendError(w, "could not put object tagging", reqInfo, err)
|
2022-03-18 13:04:09 +00:00
|
|
|
return
|
2021-08-17 11:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetBucketTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-08-17 11:57:24 +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-19 12:33:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
tagSet, err := h.obj.GetBucketTagging(r.Context(), bktInfo)
|
2021-08-17 11:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, encodeTagging(tagSet)); err != nil {
|
2021-08-17 11:57:24 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2022-03-18 13:04:09 +00:00
|
|
|
return
|
2021-08-17 11:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) DeleteBucketTaggingHandler(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)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-08-19 12:33:02 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-18 13:04:09 +00:00
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
if err = h.obj.DeleteBucketTagging(r.Context(), bktInfo); err != nil {
|
2022-03-18 13:04:09 +00:00
|
|
|
h.logAndSendError(w, "could not delete bucket tagging", reqInfo, err)
|
|
|
|
return
|
2021-08-17 11:57:24 +00:00
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
func (h *handler) readTagSet(tagging *data.Tagging) (map[string]string, error) {
|
2021-08-17 11:57:24 +00:00
|
|
|
if err := checkTagSet(tagging.TagSet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tagSet := make(map[string]string, len(tagging.TagSet))
|
|
|
|
for _, tag := range tagging.TagSet {
|
2023-10-10 13:11:09 +00:00
|
|
|
if _, ok := tagSet[tag.Key]; ok {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidTagKeyUniqueness)
|
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
tagSet[tag.Key] = tag.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagSet, nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
func encodeTagging(tagSet map[string]string) *data.Tagging {
|
|
|
|
tagging := &data.Tagging{}
|
2021-08-17 11:57:24 +00:00
|
|
|
for k, v := range tagSet {
|
2024-04-10 06:41:07 +00:00
|
|
|
tagging.TagSet = append(tagging.TagSet, data.Tag{Key: k, Value: v})
|
2021-08-17 11:57:24 +00:00
|
|
|
}
|
|
|
|
sort.Slice(tagging.TagSet, func(i, j int) bool {
|
|
|
|
return tagging.TagSet[i].Key < tagging.TagSet[j].Key
|
|
|
|
})
|
|
|
|
|
|
|
|
return tagging
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
func checkTagSet(tagSet []data.Tag) error {
|
2021-08-17 11:57:24 +00:00
|
|
|
if len(tagSet) > maxTags {
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagsSizeExceed)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tagSet {
|
|
|
|
if err := checkTag(tag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
func checkTag(tag data.Tag) error {
|
2021-08-17 08:04:42 +00:00
|
|
|
if len(tag.Key) < 1 || len(tag.Key) > keyTagMaxLength {
|
2021-08-17 11:57:24 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagKey)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
if len(tag.Value) > valueTagMaxLength {
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagValue)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(tag.Key, "aws:") {
|
2021-08-17 11:57:24 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagKey)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
if !isValidTag(tag.Key) {
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagKey)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
if !isValidTag(tag.Value) {
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagValue)
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
func isValidTag(str string) bool {
|
2021-08-17 08:04:42 +00:00
|
|
|
for _, r := range str {
|
|
|
|
if !unicode.IsLetter(r) && !unicode.IsDigit(r) &&
|
|
|
|
!unicode.IsSpace(r) && !strings.ContainsRune(allowedTagChars, r) {
|
2021-08-17 11:57:24 +00:00
|
|
|
return false
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
return true
|
2021-08-17 08:04:42 +00:00
|
|
|
}
|