2020-08-19 23:36:17 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-07-16 12:35:07 +00:00
|
|
|
"encoding/xml"
|
2021-06-23 20:21:15 +00:00
|
|
|
"fmt"
|
2020-08-19 23:36:17 +00:00
|
|
|
"net/http"
|
2021-06-23 20:21:15 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-08-19 23:36:17 +00:00
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/policy"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:36:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
// keywords of predefined basic ACL values.
|
|
|
|
const (
|
|
|
|
basicACLPrivate = "private"
|
|
|
|
basicACLReadOnly = "public-read"
|
|
|
|
basicACLPublic = "public-read-write"
|
|
|
|
defaultPolicy = "REP 3"
|
2021-07-07 14:58:53 +00:00
|
|
|
|
|
|
|
publicBasicRule = 0x0FFFFFFF
|
2021-06-23 20:21:15 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
type createBucketParams struct {
|
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ CreateBucketConfiguration" json:"-"`
|
|
|
|
LocationConstraint string
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:17 +00:00
|
|
|
func (h *handler) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-08-05 09:18:52 +00:00
|
|
|
err error
|
|
|
|
info *layer.ObjectInfo
|
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
2020-08-19 23:36:17 +00:00
|
|
|
)
|
|
|
|
|
2021-07-07 14:52:36 +00:00
|
|
|
metadata := parseMetadata(r)
|
|
|
|
|
2020-08-19 23:36:17 +00:00
|
|
|
params := &layer.PutObjectParams{
|
2021-08-05 09:18:52 +00:00
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Object: reqInfo.ObjectName,
|
2020-08-19 23:36:17 +00:00
|
|
|
Reader: r.Body,
|
|
|
|
Size: r.ContentLength,
|
2021-07-07 14:52:36 +00:00
|
|
|
Header: metadata,
|
2020-08-19 23:36:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
if info, err = h.obj.PutObject(r.Context(), params); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not upload object", reqInfo, err)
|
2020-08-19 23:36:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
w.Header().Set(api.ETag, info.HashSum)
|
2020-08-19 23:36:17 +00:00
|
|
|
api.WriteSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-07-07 14:52:36 +00:00
|
|
|
func parseMetadata(r *http.Request) map[string]string {
|
|
|
|
res := make(map[string]string)
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if strings.HasPrefix(k, api.MetadataPrefix) {
|
2021-08-06 11:46:19 +00:00
|
|
|
key := strings.ToLower(strings.TrimPrefix(k, api.MetadataPrefix))
|
2021-07-07 14:52:36 +00:00
|
|
|
res[key] = v[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-08-05 09:18:52 +00:00
|
|
|
err error
|
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
|
|
|
p = layer.CreateBucketParams{Name: reqInfo.BucketName}
|
2021-06-23 20:21:15 +00:00
|
|
|
)
|
2021-08-05 09:18:52 +00:00
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
if val, ok := r.Header["X-Amz-Acl"]; ok {
|
|
|
|
p.ACL, err = parseBasicACL(val[0])
|
|
|
|
} else {
|
2021-07-07 14:58:53 +00:00
|
|
|
p.ACL = publicBasicRule
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse basic ACL", reqInfo, err)
|
2021-07-09 08:57:44 +00:00
|
|
|
return
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
createParams, err := parseLocationConstraint(r)
|
2021-06-23 20:21:15 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse body", reqInfo, err)
|
2021-07-09 08:57:44 +00:00
|
|
|
return
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 13:27:06 +00:00
|
|
|
p.BoxData, err = layer.GetBoxData(r.Context())
|
2021-06-23 20:21:15 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not get boxData", reqInfo, err)
|
2021-07-16 12:35:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
if createParams.LocationConstraint != "" {
|
2021-07-26 10:30:51 +00:00
|
|
|
for _, placementPolicy := range p.BoxData.Policies {
|
2021-07-16 12:35:07 +00:00
|
|
|
if placementPolicy.LocationConstraint == createParams.LocationConstraint {
|
|
|
|
p.Policy = placementPolicy.Policy
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Policy == nil {
|
|
|
|
p.Policy, err = policy.Parse(defaultPolicy)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse policy", reqInfo, err)
|
2021-07-16 12:35:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 10:30:51 +00:00
|
|
|
cid, err := h.obj.CreateBucket(r.Context(), &p)
|
2021-07-16 12:35:07 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not create bucket", reqInfo, err)
|
2021-07-07 14:56:29 +00:00
|
|
|
return
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.log.Info("bucket is created",
|
|
|
|
zap.String("container_id", cid.String()))
|
|
|
|
|
|
|
|
api.WriteSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
func parseLocationConstraint(r *http.Request) (*createBucketParams, error) {
|
|
|
|
if r.ContentLength == 0 {
|
|
|
|
return new(createBucketParams), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := new(createBucketParams)
|
|
|
|
if err := xml.NewDecoder(r.Body).Decode(params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return params, nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
func parseBasicACL(basicACL string) (uint32, error) {
|
|
|
|
switch basicACL {
|
|
|
|
case basicACLPublic:
|
|
|
|
return acl.PublicBasicRule, nil
|
|
|
|
case basicACLPrivate:
|
|
|
|
return acl.PrivateBasicRule, nil
|
|
|
|
case basicACLReadOnly:
|
|
|
|
return acl.ReadOnlyBasicRule, nil
|
|
|
|
default:
|
|
|
|
basicACL = strings.Trim(strings.ToLower(basicACL), "0x")
|
|
|
|
|
|
|
|
value, err := strconv.ParseUint(basicACL, 16, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't parse basic ACL: %s", basicACL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(value), nil
|
|
|
|
}
|
|
|
|
}
|