2021-10-04 14:32:35 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2021-10-13 18:50:02 +00:00
|
|
|
"bytes"
|
2021-10-04 14:32:35 +00:00
|
|
|
"context"
|
2021-10-13 18:50:02 +00:00
|
|
|
"encoding/xml"
|
2022-05-12 02:33:03 +00:00
|
|
|
errorsStd "errors"
|
2021-10-13 18:50:02 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-10-04 14:32:35 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-10-13 18:50:02 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-04 14:32:35 +00:00
|
|
|
)
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
const wildcard = "*"
|
|
|
|
|
|
|
|
var supportedMethods = map[string]struct{}{"GET": {}, "HEAD": {}, "POST": {}, "PUT": {}, "DELETE": {}}
|
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
func (n *layer) PutBucketCORS(ctx context.Context, p *PutCORSParams) error {
|
2021-10-13 18:50:02 +00:00
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
tee = io.TeeReader(p.Reader, &buf)
|
|
|
|
cors = &data.CORSConfiguration{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := xml.NewDecoder(tee).Decode(cors); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("xml decode cors: %w", err)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cors.CORSRules == nil {
|
|
|
|
return errors.GetAPIError(errors.ErrMalformedXML)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkCORS(cors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
prm := PrmObjectCreate{
|
2022-08-11 22:48:56 +00:00
|
|
|
Container: p.BktInfo.CID,
|
|
|
|
Creator: p.BktInfo.Owner,
|
|
|
|
Payload: p.Reader,
|
2022-09-07 06:59:24 +00:00
|
|
|
Filepath: p.BktInfo.CORSObjectName(),
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: TimeNow(ctx),
|
2022-08-11 22:48:56 +00:00
|
|
|
CopiesNumber: p.CopiesNumber,
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
objID, _, err := n.objectPutAndHash(ctx, prm, p.BktInfo)
|
2022-05-12 02:33:03 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("put system object: %w", err)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
objIDToDelete, err := n.treeService.PutBucketCORS(ctx, p.BktInfo, objID)
|
2022-06-27 09:33:36 +00:00
|
|
|
objIDToDeleteNotFound := errorsStd.Is(err, ErrNoNodeToRemove)
|
|
|
|
if err != nil && !objIDToDeleteNotFound {
|
2022-05-12 02:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if !objIDToDeleteNotFound {
|
|
|
|
if err = n.objectDelete(ctx, p.BktInfo, objIDToDelete); err != nil {
|
2022-05-12 02:33:03 +00:00
|
|
|
n.log.Error("couldn't delete cors object", zap.Error(err),
|
|
|
|
zap.String("cnrID", p.BktInfo.CID.EncodeToString()),
|
|
|
|
zap.String("bucket name", p.BktInfo.Name),
|
2022-05-17 13:52:51 +00:00
|
|
|
zap.String("objID", objIDToDelete.EncodeToString()))
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
n.cache.PutCORS(n.Owner(ctx), p.BktInfo, cors)
|
2021-10-04 14:32:35 +00:00
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
return nil
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (n *layer) GetBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) (*data.CORSConfiguration, error) {
|
2022-10-03 14:33:49 +00:00
|
|
|
cors, err := n.getCORS(ctx, bktInfo)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
2022-05-12 02:33:03 +00:00
|
|
|
if errorsStd.Is(err, ErrNodeNotFound) {
|
2021-10-04 14:32:35 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchCORSConfiguration)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
return cors, nil
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) DeleteBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) error {
|
2022-09-13 09:44:18 +00:00
|
|
|
objID, err := n.treeService.DeleteBucketCORS(ctx, bktInfo)
|
2022-06-27 09:33:36 +00:00
|
|
|
objIDNotFound := errorsStd.Is(err, ErrNoNodeToRemove)
|
|
|
|
if err != nil && !objIDNotFound {
|
2022-05-12 02:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if !objIDNotFound {
|
|
|
|
if err = n.objectDelete(ctx, bktInfo, objID); err != nil {
|
2022-05-12 02:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.DeleteCORS(bktInfo)
|
2022-05-12 02:33:03 +00:00
|
|
|
|
|
|
|
return nil
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
2021-10-13 18:50:02 +00:00
|
|
|
|
|
|
|
func checkCORS(cors *data.CORSConfiguration) error {
|
|
|
|
for _, r := range cors.CORSRules {
|
|
|
|
for _, m := range r.AllowedMethods {
|
|
|
|
if _, ok := supportedMethods[m]; !ok {
|
|
|
|
return errors.GetAPIErrorWithError(errors.ErrCORSUnsupportedMethod, fmt.Errorf("unsupported method is %s", m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, h := range r.ExposeHeaders {
|
|
|
|
if h == wildcard {
|
|
|
|
return errors.GetAPIError(errors.ErrCORSWildcardExposeHeaders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|