2020-08-19 23:36:17 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-08-30 19:44:53 +00:00
|
|
|
"bytes"
|
2022-08-01 16:52:09 +00:00
|
|
|
"crypto/md5"
|
2021-08-30 19:44:53 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2021-07-16 12:35:07 +00:00
|
|
|
"encoding/xml"
|
2021-08-30 19:44:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-08-06 13:05:57 +00:00
|
|
|
"net"
|
2020-08-19 23:36:17 +00:00
|
|
|
"net/http"
|
2021-08-17 11:57:24 +00:00
|
|
|
"net/url"
|
2021-08-30 19:44:53 +00:00
|
|
|
"strconv"
|
2021-06-23 20:21:15 +00:00
|
|
|
"strings"
|
2021-08-30 19:44:53 +00:00
|
|
|
"time"
|
2020-08-19 23:36:17 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/encryption"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2020-08-19 23:36:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
type postPolicy struct {
|
|
|
|
Expiration time.Time `json:"expiration"`
|
|
|
|
Conditions []*policyCondition `json:"conditions"`
|
|
|
|
empty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postPolicy) condition(key string) *policyCondition {
|
|
|
|
for _, condition := range p.Conditions {
|
|
|
|
if condition.Key == key {
|
|
|
|
return condition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:45:28 +00:00
|
|
|
func (p *postPolicy) CheckContentLength(size uint64) bool {
|
2021-08-30 19:44:53 +00:00
|
|
|
if p.empty {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, condition := range p.Conditions {
|
|
|
|
if condition.Matching == "content-length-range" {
|
2023-06-01 13:45:28 +00:00
|
|
|
length := strconv.FormatUint(size, 10)
|
2021-08-30 19:44:53 +00:00
|
|
|
return condition.Key <= length && length <= condition.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *policyCondition) match(value string) bool {
|
|
|
|
switch p.Matching {
|
|
|
|
case "eq":
|
|
|
|
p.Matched = p.Value == value
|
|
|
|
case "starts-with":
|
|
|
|
if p.Key == api.ContentType {
|
|
|
|
p.Matched = true
|
|
|
|
for _, contentType := range strings.Split(value, ",") {
|
|
|
|
if !strings.HasPrefix(contentType, p.Value) {
|
|
|
|
p.Matched = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.Matched = strings.HasPrefix(value, p.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p.Matched
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postPolicy) CheckField(key string, value string) error {
|
|
|
|
if p.empty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cond := p.condition(key)
|
|
|
|
if cond == nil {
|
|
|
|
return errors.GetAPIError(errors.ErrPostPolicyConditionInvalidFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cond.match(value) {
|
|
|
|
return errors.GetAPIError(errors.ErrPostPolicyConditionInvalidFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postPolicy) AllConditionMatched() bool {
|
|
|
|
for _, condition := range p.Conditions {
|
|
|
|
if !condition.Matched {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type policyCondition struct {
|
|
|
|
Matching string
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
Matched bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var errInvalidCondition = fmt.Errorf("invalid condition")
|
|
|
|
|
|
|
|
func (p *policyCondition) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
v interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("unmarshal policy condition: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
if len(v) != 3 {
|
|
|
|
return errInvalidCondition
|
|
|
|
}
|
|
|
|
if p.Matching, ok = v[0].(string); !ok {
|
|
|
|
return errInvalidCondition
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Matching == "content-length-range" {
|
|
|
|
min, ok := v[1].(float64)
|
|
|
|
max, ok2 := v[2].(float64)
|
|
|
|
if !ok || !ok2 {
|
|
|
|
return errInvalidCondition
|
|
|
|
}
|
|
|
|
p.Key = strconv.FormatFloat(min, 'f', 0, 32)
|
|
|
|
p.Value = strconv.FormatFloat(max, 'f', 0, 32)
|
|
|
|
} else {
|
|
|
|
key, ok2 := v[1].(string)
|
|
|
|
p.Value, ok = v[2].(string)
|
|
|
|
if !ok || !ok2 {
|
|
|
|
return errInvalidCondition
|
|
|
|
}
|
|
|
|
p.Key = strings.ToLower(strings.TrimPrefix(key, "$"))
|
|
|
|
}
|
|
|
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
p.Matching = "eq"
|
|
|
|
for key, val := range v {
|
|
|
|
p.Key = strings.ToLower(key)
|
|
|
|
if p.Value, ok = val.(string); !ok {
|
|
|
|
return errInvalidCondition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown condition type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:21:15 +00:00
|
|
|
// keywords of predefined basic ACL values.
|
|
|
|
const (
|
2021-07-21 11:59:46 +00:00
|
|
|
basicACLPrivate = "private"
|
|
|
|
basicACLReadOnly = "public-read"
|
|
|
|
basicACLPublic = "public-read-write"
|
|
|
|
cannedACLAuthRead = "authenticated-read"
|
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) {
|
2022-04-18 15:35:25 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
newEaclTable *eacl.Table
|
2022-05-04 12:29:11 +00:00
|
|
|
sessionTokenEACL *session.Container
|
2022-04-18 15:35:25 +00:00
|
|
|
containsACL = containsACLHeaders(r)
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx = r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo = middleware.GetReqInfo(ctx)
|
2022-04-18 15:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if containsACL {
|
|
|
|
if sessionTokenEACL, err = getSessionTokenSetEACL(r.Context()); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get eacl session token from a box", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
tagSet, err := parseTaggingHeader(r.Header)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse tagging header", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-19 23:36:17 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-08-30 07:55:42 +00:00
|
|
|
if err != nil {
|
2022-10-14 14:36:43 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket objInfo", reqInfo, err)
|
2021-08-30 07:55:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := parseMetadata(r)
|
|
|
|
if contentType := r.Header.Get(api.ContentType); len(contentType) > 0 {
|
|
|
|
metadata[api.ContentType] = contentType
|
|
|
|
}
|
2022-01-27 13:37:43 +00:00
|
|
|
if cacheControl := r.Header.Get(api.CacheControl); len(cacheControl) > 0 {
|
|
|
|
metadata[api.CacheControl] = cacheControl
|
|
|
|
}
|
|
|
|
if expires := r.Header.Get(api.Expires); len(expires) > 0 {
|
|
|
|
metadata[api.Expires] = expires
|
|
|
|
}
|
2023-10-16 15:32:08 +00:00
|
|
|
if contentLanguage := r.Header.Get(api.ContentLanguage); len(contentLanguage) > 0 {
|
|
|
|
metadata[api.ContentLanguage] = contentLanguage
|
|
|
|
}
|
2021-08-30 07:55:42 +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
|
|
|
|
}
|
|
|
|
|
2023-07-07 14:31:22 +00:00
|
|
|
body, err := h.getBodyReader(r)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "failed to get body reader", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-10 09:17:44 +00:00
|
|
|
if encodings := r.Header.Get(api.ContentEncoding); len(encodings) > 0 {
|
|
|
|
metadata[api.ContentEncoding] = encodings
|
|
|
|
}
|
2023-07-07 14:31:22 +00:00
|
|
|
|
2023-06-01 13:45:28 +00:00
|
|
|
var size uint64
|
|
|
|
if r.ContentLength > 0 {
|
|
|
|
size = uint64(r.ContentLength)
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
params := &layer.PutObjectParams{
|
2023-11-13 08:01:47 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
Reader: body,
|
|
|
|
Size: size,
|
|
|
|
Header: metadata,
|
|
|
|
Encryption: encryptionParams,
|
|
|
|
ContentMD5: r.Header.Get(api.ContentMD5),
|
|
|
|
ContentSHA256Hash: r.Header.Get(api.AmzContentSha256),
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.CopiesNumbers, err = h.pickCopiesNumbers(metadata, bktInfo.LocationConstraint)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid copies number", reqInfo, err)
|
|
|
|
return
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(ctx, bktInfo)
|
2022-02-28 10:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
params.Lock, err = formObjectLock(ctx, bktInfo, settings.LockConfiguration, r.Header)
|
2022-03-01 15:07:15 +00:00
|
|
|
if err != nil {
|
2022-02-28 10:22:07 +00:00
|
|
|
h.logAndSendError(w, "could not form object lock", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
extendedObjInfo, err := h.obj.PutObject(ctx, params)
|
2021-08-30 07:55:42 +00:00
|
|
|
if err != nil {
|
2023-07-07 14:31:22 +00:00
|
|
|
_, err2 := io.Copy(io.Discard, body)
|
|
|
|
err3 := body.Close()
|
2022-08-29 13:34:13 +00:00
|
|
|
h.logAndSendError(w, "could not upload object", reqInfo, err, zap.Errors("body close errors", []error{err2, err3}))
|
2021-08-30 07:55:42 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-14 14:36:43 +00:00
|
|
|
objInfo := extendedObjInfo.ObjectInfo
|
2021-08-30 07:55:42 +00:00
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
s := &SendNotificationParams{
|
2022-07-18 14:51:34 +00:00
|
|
|
Event: EventObjectCreatedPut,
|
2023-10-27 15:15:33 +00:00
|
|
|
NotificationInfo: data.NotificationInfoFromObject(objInfo, h.cfg.MD5Enabled()),
|
2022-07-18 14:51:34 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
2022-03-31 06:21:38 +00:00
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
if err = h.sendNotifications(ctx, s); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Error(logs.CouldntSendNotification, zap.Error(err))
|
2022-03-31 06:21:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 15:35:25 +00:00
|
|
|
if containsACL {
|
2022-10-14 14:36:43 +00:00
|
|
|
if newEaclTable, err = h.getNewEAclTable(r, bktInfo, objInfo); err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "could not get new eacl table", reqInfo, err)
|
2021-08-19 14:14:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
if tagSet != nil {
|
2022-10-14 14:36:43 +00:00
|
|
|
tagPrm := &layer.PutObjectTaggingParams{
|
|
|
|
ObjectVersion: &layer.ObjectVersion{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: objInfo.Name,
|
|
|
|
VersionID: objInfo.VersionID(),
|
|
|
|
},
|
|
|
|
TagSet: tagSet,
|
|
|
|
NodeVersion: extendedObjInfo.NodeVersion,
|
|
|
|
}
|
|
|
|
if _, err = h.obj.PutObjectTagging(r.Context(), tagPrm); err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "could not upload object tagging", reqInfo, err)
|
|
|
|
return
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2021-08-30 07:55:42 +00:00
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
if newEaclTable != nil {
|
|
|
|
p := &layer.PutBucketACLParams{
|
2022-06-21 15:21:20 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
EACL: newEaclTable,
|
|
|
|
SessionToken: sessionTokenEACL,
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.PutBucketACL(r.Context(), p); err != nil {
|
|
|
|
h.logAndSendError(w, "could not put bucket acl", reqInfo, err)
|
2021-08-19 14:14:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:58:18 +00:00
|
|
|
if settings.VersioningEnabled() {
|
2022-10-14 14:36:43 +00:00
|
|
|
w.Header().Set(api.AmzVersionID, objInfo.VersionID())
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2022-11-09 10:07:18 +00:00
|
|
|
if encryptionParams.Enabled() {
|
2022-08-01 16:52:09 +00:00
|
|
|
addSSECHeaders(w.Header(), r.Header)
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
w.Header().Set(api.ETag, data.Quote(objInfo.ETag(h.cfg.MD5Enabled())))
|
2023-10-02 08:52:07 +00:00
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
middleware.WriteSuccessResponseHeadersOnly(w)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 14:31:22 +00:00
|
|
|
func (h *handler) getBodyReader(r *http.Request) (io.ReadCloser, error) {
|
|
|
|
if !api.IsSignedStreamingV4(r) {
|
|
|
|
return r.Body, nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 09:17:44 +00:00
|
|
|
encodings := r.Header.Values(api.ContentEncoding)
|
|
|
|
var chunkedEncoding bool
|
|
|
|
resultContentEncoding := make([]string, 0, len(encodings))
|
|
|
|
for _, enc := range encodings {
|
|
|
|
for _, e := range strings.Split(enc, ",") {
|
|
|
|
e = strings.TrimSpace(e)
|
|
|
|
if e == api.AwsChunked { // probably we should also check position of this header value
|
|
|
|
chunkedEncoding = true
|
|
|
|
} else {
|
|
|
|
resultContentEncoding = append(resultContentEncoding, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Header.Set(api.ContentEncoding, strings.Join(resultContentEncoding, ","))
|
|
|
|
|
2023-09-08 11:17:14 +00:00
|
|
|
if !chunkedEncoding && !h.cfg.BypassContentEncodingInChunks() {
|
2023-07-10 09:17:44 +00:00
|
|
|
return nil, fmt.Errorf("%w: request is not chunk encoded, encodings '%s'",
|
|
|
|
errors.GetAPIError(errors.ErrInvalidEncodingMethod), strings.Join(encodings, ","))
|
|
|
|
}
|
|
|
|
|
2023-07-07 14:31:22 +00:00
|
|
|
decodeContentSize := r.Header.Get(api.AmzDecodedContentLength)
|
|
|
|
if len(decodeContentSize) == 0 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrMissingContentLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := strconv.Atoi(decodeContentSize); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: parse decoded content length: %s", errors.GetAPIError(errors.ErrMissingContentLength), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkReader, err := newSignV4ChunkedReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("initialize chunk reader: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunkReader, nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
func formEncryptionParams(r *http.Request) (enc encryption.Params, err error) {
|
2023-10-19 14:22:26 +00:00
|
|
|
return formEncryptionParamsBase(r, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func formCopySourceEncryptionParams(r *http.Request) (enc encryption.Params, err error) {
|
|
|
|
return formEncryptionParamsBase(r, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func formEncryptionParamsBase(r *http.Request, isCopySource bool) (enc encryption.Params, err error) {
|
|
|
|
var sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMD5 string
|
|
|
|
if isCopySource {
|
|
|
|
sseCustomerAlgorithm = r.Header.Get(api.AmzCopySourceServerSideEncryptionCustomerAlgorithm)
|
|
|
|
sseCustomerKey = r.Header.Get(api.AmzCopySourceServerSideEncryptionCustomerKey)
|
|
|
|
sseCustomerKeyMD5 = r.Header.Get(api.AmzCopySourceServerSideEncryptionCustomerKeyMD5)
|
|
|
|
} else {
|
|
|
|
sseCustomerAlgorithm = r.Header.Get(api.AmzServerSideEncryptionCustomerAlgorithm)
|
|
|
|
sseCustomerKey = r.Header.Get(api.AmzServerSideEncryptionCustomerKey)
|
|
|
|
sseCustomerKeyMD5 = r.Header.Get(api.AmzServerSideEncryptionCustomerKeyMD5)
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
if len(sseCustomerAlgorithm) == 0 && len(sseCustomerKey) == 0 && len(sseCustomerKeyMD5) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
if r.TLS == nil {
|
2023-10-19 14:22:26 +00:00
|
|
|
return enc, errors.GetAPIError(errors.ErrInsecureSSECustomerRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sseCustomerKey) > 0 && len(sseCustomerAlgorithm) == 0 {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrMissingSSECustomerAlgorithm)
|
|
|
|
}
|
|
|
|
if len(sseCustomerAlgorithm) > 0 && len(sseCustomerKey) == 0 {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrMissingSSECustomerKey)
|
2022-08-10 18:54:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
if sseCustomerAlgorithm != layer.AESEncryptionAlgorithm {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrInvalidEncryptionAlgorithm)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := base64.StdEncoding.DecodeString(sseCustomerKey)
|
|
|
|
if err != nil {
|
2023-10-19 14:22:26 +00:00
|
|
|
if isCopySource {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrInvalidSSECustomerParameters)
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
return enc, errors.GetAPIError(errors.ErrInvalidSSECustomerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(key) != layer.AESKeySize {
|
2023-10-19 14:22:26 +00:00
|
|
|
if isCopySource {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrInvalidSSECustomerParameters)
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
return enc, errors.GetAPIError(errors.ErrInvalidSSECustomerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyMD5, err := base64.StdEncoding.DecodeString(sseCustomerKeyMD5)
|
|
|
|
if err != nil {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrSSECustomerKeyMD5Mismatch)
|
|
|
|
}
|
|
|
|
|
|
|
|
md5Sum := md5.Sum(key)
|
|
|
|
if !bytes.Equal(md5Sum[:], keyMD5) {
|
|
|
|
return enc, errors.GetAPIError(errors.ErrSSECustomerKeyMD5Mismatch)
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:56:38 +00:00
|
|
|
params, err := encryption.NewParams(key)
|
|
|
|
if err == nil {
|
|
|
|
enc = *params
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc, err
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
func (h *handler) PostObject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2022-04-18 15:35:25 +00:00
|
|
|
newEaclTable *eacl.Table
|
|
|
|
tagSet map[string]string
|
2022-05-04 12:29:11 +00:00
|
|
|
sessionTokenEACL *session.Container
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx = r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo = middleware.GetReqInfo(ctx)
|
2022-04-18 15:35:25 +00:00
|
|
|
metadata = make(map[string]string)
|
|
|
|
containsACL = containsACLHeaders(r)
|
2021-08-30 19:44:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
policy, err := checkPostPolicy(r, reqInfo, metadata)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "failed check policy", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
if tagging := auth.MultipartFormValue(r, "tagging"); tagging != "" {
|
|
|
|
buffer := bytes.NewBufferString(tagging)
|
2023-10-09 12:34:51 +00:00
|
|
|
tagSet, err = h.readTagSet(buffer)
|
2021-08-19 14:14:19 +00:00
|
|
|
if err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "could not read tag set", reqInfo, err)
|
2021-08-19 14:14:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2022-04-18 15:35:25 +00:00
|
|
|
if containsACL {
|
2023-06-09 13:19:23 +00:00
|
|
|
if sessionTokenEACL, err = getSessionTokenSetEACL(ctx); err != nil {
|
2022-04-18 15:35:25 +00:00
|
|
|
h.logAndSendError(w, "could not get eacl session token from a box", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
var contentReader io.Reader
|
2023-06-01 13:45:28 +00:00
|
|
|
var size uint64
|
2021-08-30 19:44:53 +00:00
|
|
|
if content, ok := r.MultipartForm.Value["file"]; ok {
|
|
|
|
contentReader = bytes.NewBufferString(content[0])
|
2023-06-01 13:45:28 +00:00
|
|
|
size = uint64(len(content[0]))
|
2021-08-30 19:44:53 +00:00
|
|
|
} else {
|
|
|
|
file, head, err := r.FormFile("file")
|
2021-08-19 14:14:19 +00:00
|
|
|
if err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "could get uploading file", reqInfo, err)
|
2021-08-19 14:14:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
contentReader = file
|
2023-06-01 13:45:28 +00:00
|
|
|
size = uint64(head.Size)
|
2021-08-30 19:44:53 +00:00
|
|
|
reqInfo.ObjectName = strings.ReplaceAll(reqInfo.ObjectName, "${filename}", head.Filename)
|
|
|
|
}
|
|
|
|
if !policy.CheckContentLength(size) {
|
|
|
|
h.logAndSendError(w, "invalid content-length", reqInfo, errors.GetAPIError(errors.ErrInvalidArgument))
|
|
|
|
return
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
bktInfo, err := h.obj.GetBucketInfo(ctx, reqInfo.BucketName)
|
2022-03-18 13:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
params := &layer.PutObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
Reader: contentReader,
|
|
|
|
Size: size,
|
|
|
|
Header: metadata,
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
extendedObjInfo, err := h.obj.PutObject(ctx, params)
|
2021-08-30 19:44:53 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not upload object", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-14 14:36:43 +00:00
|
|
|
objInfo := extendedObjInfo.ObjectInfo
|
2021-08-30 19:44:53 +00:00
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
s := &SendNotificationParams{
|
2022-07-18 14:51:34 +00:00
|
|
|
Event: EventObjectCreatedPost,
|
2023-10-27 15:15:33 +00:00
|
|
|
NotificationInfo: data.NotificationInfoFromObject(objInfo, h.cfg.MD5Enabled()),
|
2022-07-18 14:51:34 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
2022-03-31 06:21:38 +00:00
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
if err = h.sendNotifications(ctx, s); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Error(logs.CouldntSendNotification, zap.Error(err))
|
2022-03-31 06:21:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
if acl := auth.MultipartFormValue(r, "acl"); acl != "" {
|
|
|
|
r.Header.Set(api.AmzACL, acl)
|
|
|
|
r.Header.Set(api.AmzGrantFullControl, "")
|
|
|
|
r.Header.Set(api.AmzGrantWrite, "")
|
|
|
|
r.Header.Set(api.AmzGrantRead, "")
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
if newEaclTable, err = h.getNewEAclTable(r, bktInfo, objInfo); err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "could not get new eacl table", reqInfo, err)
|
|
|
|
return
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
if tagSet != nil {
|
2022-10-14 14:36:43 +00:00
|
|
|
tagPrm := &layer.PutObjectTaggingParams{
|
|
|
|
ObjectVersion: &layer.ObjectVersion{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: objInfo.Name,
|
|
|
|
VersionID: objInfo.VersionID(),
|
|
|
|
},
|
|
|
|
NodeVersion: extendedObjInfo.NodeVersion,
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
if _, err = h.obj.PutObjectTagging(ctx, tagPrm); err != nil {
|
2021-08-17 11:57:24 +00:00
|
|
|
h.logAndSendError(w, "could not upload object tagging", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:14:19 +00:00
|
|
|
if newEaclTable != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
p := &layer.PutBucketACLParams{
|
2022-06-21 15:21:20 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
EACL: newEaclTable,
|
|
|
|
SessionToken: sessionTokenEACL,
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
if err = h.obj.PutBucketACL(ctx, p); err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could not put bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
if settings, err := h.obj.GetBucketSettings(ctx, bktInfo); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Warn(logs.CouldntGetBucketVersioning, zap.String("bucket name", reqInfo.BucketName), zap.Error(err))
|
2022-07-19 14:58:18 +00:00
|
|
|
} else if settings.VersioningEnabled() {
|
2022-10-14 14:36:43 +00:00
|
|
|
w.Header().Set(api.AmzVersionID, objInfo.VersionID())
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
if redirectURL := auth.MultipartFormValue(r, "success_action_redirect"); redirectURL != "" {
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
status := http.StatusNoContent
|
|
|
|
if statusStr := auth.MultipartFormValue(r, "success_action_status"); statusStr != "" {
|
|
|
|
switch statusStr {
|
|
|
|
case "200":
|
|
|
|
status = http.StatusOK
|
|
|
|
case "201":
|
|
|
|
status = http.StatusCreated
|
|
|
|
resp := &PostResponse{
|
2022-10-14 14:36:43 +00:00
|
|
|
Bucket: objInfo.Bucket,
|
|
|
|
Key: objInfo.Name,
|
2023-10-27 15:15:33 +00:00
|
|
|
ETag: data.Quote(objInfo.ETag(h.cfg.MD5Enabled())),
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
w.WriteHeader(status)
|
2023-07-05 14:05:45 +00:00
|
|
|
if _, err = w.Write(middleware.EncodeResponse(resp)); err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
w.Header().Set(api.ETag, data.Quote(objInfo.ETag(h.cfg.MD5Enabled())))
|
2021-08-30 19:44:53 +00:00
|
|
|
w.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
func checkPostPolicy(r *http.Request, reqInfo *middleware.ReqInfo, metadata map[string]string) (*postPolicy, error) {
|
2022-01-17 12:25:29 +00:00
|
|
|
policy := &postPolicy{empty: true}
|
2021-08-30 19:44:53 +00:00
|
|
|
if policyStr := auth.MultipartFormValue(r, "policy"); policyStr != "" {
|
|
|
|
policyData, err := base64.StdEncoding.DecodeString(policyStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not decode policy: %w", err)
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(policyData, policy); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal policy: %w", err)
|
|
|
|
}
|
|
|
|
if policy.Expiration.Before(time.Now()) {
|
|
|
|
return nil, fmt.Errorf("policy is expired: %w", errors.GetAPIError(errors.ErrInvalidArgument))
|
|
|
|
}
|
2022-01-17 12:25:29 +00:00
|
|
|
policy.empty = false
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, v := range r.MultipartForm.Value {
|
|
|
|
value := v[0]
|
|
|
|
if key == "file" || key == "policy" || key == "x-amz-signature" || strings.HasPrefix(key, "x-ignore-") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := policy.CheckField(key, value); err != nil {
|
|
|
|
return nil, fmt.Errorf("'%s' form field doesn't match the policy: %w", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := strings.ToLower(api.MetadataPrefix)
|
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
metadata[strings.TrimPrefix(key, prefix)] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == "content-type" {
|
|
|
|
metadata[api.ContentType] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == "key" {
|
|
|
|
reqInfo.ObjectName = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cond := range policy.Conditions {
|
|
|
|
if cond.Key == "bucket" {
|
|
|
|
if !cond.match(reqInfo.BucketName) {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrPostPolicyConditionInvalidFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return policy, nil
|
2020-08-19 23:36:17 +00:00
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-08-19 14:14:19 +00:00
|
|
|
func containsACLHeaders(r *http.Request) bool {
|
|
|
|
return r.Header.Get(api.AmzACL) != "" || r.Header.Get(api.AmzGrantRead) != "" ||
|
|
|
|
r.Header.Get(api.AmzGrantFullControl) != "" || r.Header.Get(api.AmzGrantWrite) != ""
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
func (h *handler) getNewEAclTable(r *http.Request, bktInfo *data.BucketInfo, objInfo *data.ObjectInfo) (*eacl.Table, error) {
|
2021-08-30 19:44:53 +00:00
|
|
|
var newEaclTable *eacl.Table
|
2022-04-06 08:27:47 +00:00
|
|
|
key, err := h.bearerTokenIssuerKey(r.Context())
|
2021-10-19 15:08:07 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get bearer token issuer: %w", err)
|
2021-10-19 15:08:07 +00:00
|
|
|
}
|
2022-04-06 08:27:47 +00:00
|
|
|
objectACL, err := parseACLHeaders(r.Header, key)
|
2021-08-30 19:44:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse object acl: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resInfo := &resourceInfo{
|
|
|
|
Bucket: objInfo.Bucket,
|
|
|
|
Object: objInfo.Name,
|
2022-08-04 17:31:33 +00:00
|
|
|
Version: objInfo.VersionID(),
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bktPolicy, err := aclToPolicy(objectACL, resInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not translate object acl to bucket policy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
astChild, err := policyToAst(bktPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not translate policy to ast: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bacl, err := h.obj.GetBucketACL(r.Context(), bktInfo)
|
2021-08-30 19:44:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get bucket eacl: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parentAst := tableToAst(bacl.EACL, objInfo.Bucket)
|
2022-05-25 17:25:43 +00:00
|
|
|
strCID := bacl.Info.CID.EncodeToString()
|
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
for _, resource := range parentAst.Resources {
|
2022-05-25 17:25:43 +00:00
|
|
|
if resource.Bucket == strCID {
|
2021-08-30 19:44:53 +00:00
|
|
|
resource.Bucket = objInfo.Bucket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resAst, updated := mergeAst(parentAst, astChild); updated {
|
|
|
|
if newEaclTable, err = astToTable(resAst); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not translate ast to table: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newEaclTable, nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
func parseTaggingHeader(header http.Header) (map[string]string, error) {
|
|
|
|
var tagSet map[string]string
|
|
|
|
if tagging := header.Get(api.AmzTagging); len(tagging) > 0 {
|
|
|
|
queries, err := url.ParseQuery(tagging)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidArgument)
|
|
|
|
}
|
|
|
|
if len(queries) > maxTags {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidTagsSizeExceed)
|
|
|
|
}
|
|
|
|
tagSet = make(map[string]string, len(queries))
|
|
|
|
for k, v := range queries {
|
|
|
|
tag := Tag{Key: k, Value: v[0]}
|
|
|
|
if err = checkTag(tag); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tagSet[tag.Key] = tag.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tagSet, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-11-03 06:34:18 +00:00
|
|
|
p := &layer.CreateBucketParams{
|
|
|
|
Name: reqInfo.BucketName,
|
|
|
|
}
|
2021-08-05 09:18:52 +00:00
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
if err := checkBucketName(reqInfo.BucketName); err != nil {
|
2021-08-06 13:05:57 +00:00
|
|
|
h.logAndSendError(w, "invalid bucket name", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
key, err := h.bearerTokenIssuerKey(ctx)
|
2021-10-19 15:08:07 +00:00
|
|
|
if err != nil {
|
2022-04-06 08:27:47 +00:00
|
|
|
h.logAndSendError(w, "couldn't get bearer token signature key", reqInfo, err)
|
2021-10-19 15:08:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:27:47 +00:00
|
|
|
bktACL, err := parseACLHeaders(r.Header, key)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse bucket acl", reqInfo, err)
|
|
|
|
return
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
2021-08-30 07:55:42 +00:00
|
|
|
resInfo := &resourceInfo{Bucket: reqInfo.BucketName}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
p.EACL, err = bucketACLToTable(bktACL, resInfo)
|
2021-06-23 20:21:15 +00:00
|
|
|
if err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could translate bucket acl to eacl", reqInfo, err)
|
2021-07-09 08:57:44 +00:00
|
|
|
return
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
createParams, err := h.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-10-19 15:08:07 +00:00
|
|
|
var policies []*accessbox.ContainerPolicy
|
2023-08-14 15:34:41 +00:00
|
|
|
boxData, err := middleware.GetBoxData(ctx)
|
2021-10-19 15:08:07 +00:00
|
|
|
if err == nil {
|
|
|
|
policies = boxData.Policies
|
2022-06-21 15:21:20 +00:00
|
|
|
p.SessionContainerCreation = boxData.Gate.SessionTokenForPut()
|
|
|
|
p.SessionEACL = boxData.Gate.SessionTokenForSetEACL()
|
2022-04-18 15:35:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 15:21:20 +00:00
|
|
|
if p.SessionContainerCreation == nil {
|
2022-04-18 15:35:25 +00:00
|
|
|
h.logAndSendError(w, "couldn't find session token for put", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:21:20 +00:00
|
|
|
if p.SessionEACL == nil {
|
2022-04-18 15:35:25 +00:00
|
|
|
h.logAndSendError(w, "couldn't find session token for setEACL", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
|
|
|
|
return
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2023-02-08 09:56:42 +00:00
|
|
|
if err = h.setPolicy(p, createParams.LocationConstraint, policies); err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't set placement policy", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-16 12:35:07 +00:00
|
|
|
|
2022-02-25 09:06:40 +00:00
|
|
|
p.ObjectLockEnabled = isLockEnabled(r.Header)
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
bktInfo, err := h.obj.CreateBucket(ctx, 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
|
|
|
}
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Info(logs.BucketIsCreated, zap.Stringer("container_id", bktInfo.CID))
|
2022-12-09 13:30:51 +00:00
|
|
|
|
2022-02-25 09:06:40 +00:00
|
|
|
if p.ObjectLockEnabled {
|
2022-02-28 08:02:05 +00:00
|
|
|
sp := &layer.PutSettingsParams{
|
|
|
|
BktInfo: bktInfo,
|
2022-07-20 10:30:19 +00:00
|
|
|
Settings: &data.BucketSettings{Versioning: data.VersioningEnabled},
|
2022-02-28 08:02:05 +00:00
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
if err = h.obj.PutBucketSettings(ctx, sp); err != nil {
|
2022-02-28 08:02:05 +00:00
|
|
|
h.logAndSendError(w, "couldn't enable bucket versioning", reqInfo, err,
|
2022-06-27 09:08:26 +00:00
|
|
|
zap.String("container_id", bktInfo.CID.EncodeToString()))
|
2022-02-28 08:02:05 +00:00
|
|
|
return
|
2022-02-25 09:06:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
middleware.WriteSuccessResponseHeadersOnly(w)
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:56:42 +00:00
|
|
|
func (h handler) setPolicy(prm *layer.CreateBucketParams, locationConstraint string, userPolicies []*accessbox.ContainerPolicy) error {
|
2023-09-08 11:17:14 +00:00
|
|
|
prm.Policy = h.cfg.DefaultPlacementPolicy()
|
2023-02-08 09:56:42 +00:00
|
|
|
prm.LocationConstraint = locationConstraint
|
2022-11-03 06:34:18 +00:00
|
|
|
|
|
|
|
if locationConstraint == "" {
|
2023-02-08 09:56:42 +00:00
|
|
|
return nil
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, placementPolicy := range userPolicies {
|
|
|
|
if placementPolicy.LocationConstraint == locationConstraint {
|
|
|
|
prm.Policy = placementPolicy.Policy
|
2023-02-08 09:56:42 +00:00
|
|
|
return nil
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:56:42 +00:00
|
|
|
|
2023-09-08 11:17:14 +00:00
|
|
|
if policy, ok := h.cfg.PlacementPolicy(locationConstraint); ok {
|
2023-02-08 09:56:42 +00:00
|
|
|
prm.Policy = policy
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.GetAPIError(errors.ErrInvalidLocationConstraint)
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:06:40 +00:00
|
|
|
func isLockEnabled(header http.Header) bool {
|
|
|
|
lockEnabledStr := header.Get(api.AmzBucketObjectLockEnabled)
|
|
|
|
lockEnabled, _ := strconv.ParseBool(lockEnabledStr)
|
|
|
|
return lockEnabled
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:05:57 +00:00
|
|
|
func checkBucketName(bucketName string) error {
|
|
|
|
if len(bucketName) < 3 || len(bucketName) > 63 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(bucketName, "xn--") || strings.HasSuffix(bucketName, "-s3alias") {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
if net.ParseIP(bucketName) != nil {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
labels := strings.Split(bucketName, ".")
|
|
|
|
for _, label := range labels {
|
|
|
|
if len(label) == 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
for i, r := range label {
|
|
|
|
if !isAlphaNum(r) && r != '-' {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
if (i == 0 || i == len(label)-1) && r == '-' {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrInvalidBucketName)
|
2021-08-06 13:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAlphaNum(char int32) bool {
|
|
|
|
return 'a' <= char && char <= 'z' || '0' <= char && char <= '9'
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
func (h *handler) parseLocationConstraint(r *http.Request) (*createBucketParams, error) {
|
2021-07-16 12:35:07 +00:00
|
|
|
if r.ContentLength == 0 {
|
|
|
|
return new(createBucketParams), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := new(createBucketParams)
|
2023-10-09 12:34:51 +00:00
|
|
|
if err := h.cfg.NewXMLDecoder(r.Body).Decode(params); err != nil {
|
2022-01-19 20:20:07 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrMalformedXML)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
return params, nil
|
|
|
|
}
|