2020-08-19 23:36:17 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-08-19 23:36:17 +00:00
|
|
|
func (h *handler) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-06-29 13:40:26 +00:00
|
|
|
err error
|
|
|
|
info *layer.ObjectInfo
|
|
|
|
req = mux.Vars(r)
|
|
|
|
bkt = req["bucket"]
|
|
|
|
obj = req["object"]
|
|
|
|
rid = api.GetRequestID(r.Context())
|
2020-08-19 23:36:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := h.obj.GetBucketInfo(r.Context(), bkt); err != nil {
|
|
|
|
h.log.Error("could not find bucket",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("bucket_name", bkt),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrBadRequest).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
}, r.URL)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &layer.PutObjectParams{
|
|
|
|
Bucket: bkt,
|
|
|
|
Object: obj,
|
|
|
|
Reader: r.Body,
|
|
|
|
Size: r.ContentLength,
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
if info, err = h.obj.PutObject(r.Context(), params); err != nil {
|
2020-08-19 23:36:17 +00:00
|
|
|
h.log.Error("could not upload object",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("bucket_name", bkt),
|
|
|
|
zap.String("object_name", obj),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
p = layer.CreateBucketParams{}
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
req = mux.Vars(r)
|
|
|
|
)
|
|
|
|
p.Name = req["bucket"]
|
|
|
|
if val, ok := r.Header["X-Amz-Acl"]; ok {
|
|
|
|
p.ACL, err = parseBasicACL(val[0])
|
|
|
|
} else {
|
|
|
|
p.ACL = acl.PrivateBasicRule
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error("could not parse basic ACL",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrBadRequest).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Policy, err = policy.Parse(defaultPolicy)
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error("could not parse policy",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrBadRequest).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := h.obj.CreateBucket(r.Context(), &p)
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error("could not create bucket",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|