2021-08-17 08:04:42 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-08-17 11:57:24 +00:00
|
|
|
"io"
|
2021-08-17 08:04:42 +00:00
|
|
|
"net/http"
|
2021-08-17 11:57:24 +00:00
|
|
|
"sort"
|
2021-08-17 08:04:42 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
tagSet, err := readTagSet(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not read tag set", reqInfo, err)
|
2021-08-17 08:04:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get("versionId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := h.obj.GetObjectInfo(r.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p2 := &layer.PutTaggingParams{
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
TagSet: tagSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.PutObjectTagging(r.Context(), p2); err != nil {
|
|
|
|
h.logAndSendError(w, "could not put object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:01 +00:00
|
|
|
func (h *handler) GetObjectTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get("versionId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := h.obj.GetObjectInfo(r.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tagSet, err := h.obj.GetObjectTagging(r.Context(), objInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set(api.AmzVersionID, objInfo.Version())
|
2021-08-17 11:57:24 +00:00
|
|
|
if err = api.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) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get("versionId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := h.obj.GetObjectInfo(r.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.DeleteObjectTagging(r.Context(), objInfo); err != nil {
|
|
|
|
h.logAndSendError(w, "could not delete object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
func (h *handler) PutBucketTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
tagSet, err := readTagSet(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not read tag set", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.obj.PutBucketTagging(r.Context(), reqInfo.BucketName, tagSet); err != nil {
|
|
|
|
h.logAndSendError(w, "could not put object tagging", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetBucketTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
tagSet, err := h.obj.GetBucketTagging(r.Context(), reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = api.EncodeToResponse(w, encodeTagging(tagSet)); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) DeleteBucketTaggingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
if err := h.obj.DeleteBucketTagging(r.Context(), reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "could not delete object tagging", reqInfo, err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTagSet(reader io.Reader) (map[string]string, error) {
|
|
|
|
tagging := new(Tagging)
|
|
|
|
if err := xml.NewDecoder(reader).Decode(tagging); err != nil {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrMalformedXML)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkTagSet(tagging.TagSet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tagSet := make(map[string]string, len(tagging.TagSet))
|
|
|
|
for _, tag := range tagging.TagSet {
|
|
|
|
tagSet[tag.Key] = tag.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagSet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeTagging(tagSet map[string]string) *Tagging {
|
|
|
|
tagging := &Tagging{}
|
|
|
|
for k, v := range tagSet {
|
|
|
|
tagging.TagSet = append(tagging.TagSet, Tag{Key: k, Value: v})
|
|
|
|
}
|
|
|
|
sort.Slice(tagging.TagSet, func(i, j int) bool {
|
|
|
|
return tagging.TagSet[i].Key < tagging.TagSet[j].Key
|
|
|
|
})
|
|
|
|
|
|
|
|
return tagging
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkTagSet(tagSet []Tag) error {
|
|
|
|
if len(tagSet) > maxTags {
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidTagsSizeExceed)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tagSet {
|
|
|
|
if err := checkTag(tag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 08:04:42 +00:00
|
|
|
func checkTag(tag Tag) error {
|
|
|
|
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
|
|
|
}
|