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"
|
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
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
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{}
|
|
|
|
)
|
|
|
|
|
2023-10-09 12:34:51 +00:00
|
|
|
if err := p.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,
|
2023-02-20 09:18:29 +00:00
|
|
|
Payload: &buf,
|
2022-09-07 06:59:24 +00:00
|
|
|
Filepath: p.BktInfo.CORSObjectName(),
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: TimeNow(ctx),
|
2023-04-24 23:49:12 +00:00
|
|
|
CopiesNumber: p.CopiesNumbers,
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +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 {
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Error(logs.CouldntDeleteCorsObject, zap.Error(err),
|
2022-05-12 02:33:03 +00:00
|
|
|
zap.String("cnrID", p.BktInfo.CID.EncodeToString()),
|
2022-05-17 13:52:51 +00:00
|
|
|
zap.String("objID", objIDToDelete.EncodeToString()))
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
n.cache.PutCORS(n.BearerOwner(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) {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", errors.GetAPIError(errors.ErrNoSuchCORSConfiguration), err.Error())
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|