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"
|
2024-09-27 09:18:41 +00:00
|
|
|
"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"
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2024-09-27 08:13:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/tree"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-07-15 15:35:54 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
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": {}}
|
|
|
|
|
2024-06-25 12:57:55 +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{}
|
|
|
|
)
|
|
|
|
|
2024-12-08 12:02:31 +00:00
|
|
|
if err := p.NewDecoder(tee, p.UserAgent).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 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return apierr.GetAPIError(apierr.ErrMalformedXML)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkCORS(cors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectCreate{
|
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),
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 15:35:54 +00:00
|
|
|
var corsBkt *data.BucketInfo
|
|
|
|
if n.corsCnrInfo == nil {
|
|
|
|
corsBkt = p.BktInfo
|
|
|
|
prm.CopiesNumber = p.CopiesNumbers
|
|
|
|
} else {
|
|
|
|
corsBkt = n.corsCnrInfo
|
|
|
|
prm.PrmAuth.PrivateKey = &n.gateKey.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
prm.Container = corsBkt.CID
|
|
|
|
|
2024-07-22 09:00:17 +00:00
|
|
|
createdObj, err := n.objectPutAndHash(ctx, prm, corsBkt)
|
2022-05-12 02:33:03 +00:00
|
|
|
if err != nil {
|
2024-07-15 15:35:54 +00:00
|
|
|
return fmt.Errorf("put cors object: %w", err)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 09:00:17 +00:00
|
|
|
objsToDelete, err := n.treeService.PutBucketCORS(ctx, p.BktInfo, newAddress(corsBkt.CID, createdObj.ID))
|
2024-09-27 09:18:41 +00:00
|
|
|
objToDeleteNotFound := errors.Is(err, tree.ErrNoNodeToRemove)
|
2024-07-15 15:35:54 +00:00
|
|
|
if err != nil && !objToDeleteNotFound {
|
2022-05-12 02:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-15 15:35:54 +00:00
|
|
|
if !objToDeleteNotFound {
|
2024-07-17 09:44:38 +00:00
|
|
|
for _, addr := range objsToDelete {
|
|
|
|
n.deleteCORSObject(ctx, p.BktInfo, addr)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-07-15 15:35:54 +00:00
|
|
|
// deleteCORSObject removes object and logs in case of error.
|
|
|
|
func (n *Layer) deleteCORSObject(ctx context.Context, bktInfo *data.BucketInfo, addr oid.Address) {
|
2024-09-27 08:14:45 +00:00
|
|
|
var prmAuth frostfs.PrmAuth
|
2024-07-15 15:35:54 +00:00
|
|
|
corsBkt := bktInfo
|
|
|
|
if !addr.Container().Equals(bktInfo.CID) && !addr.Container().Equals(cid.ID{}) {
|
|
|
|
corsBkt = &data.BucketInfo{CID: addr.Container()}
|
|
|
|
prmAuth.PrivateKey = &n.gateKey.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.objectDeleteWithAuth(ctx, corsBkt, addr.Object(), prmAuth); err != nil {
|
|
|
|
n.reqLogger(ctx).Error(logs.CouldntDeleteCorsObject, zap.Error(err),
|
|
|
|
zap.String("cnrID", corsBkt.CID.EncodeToString()),
|
|
|
|
zap.String("objID", addr.Object().EncodeToString()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
return cors, nil
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) DeleteBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) error {
|
2024-07-17 09:44:38 +00:00
|
|
|
objs, err := n.treeService.DeleteBucketCORS(ctx, bktInfo)
|
2024-09-27 09:18:41 +00:00
|
|
|
objNotFound := errors.Is(err, tree.ErrNoNodeToRemove)
|
2024-07-15 15:35:54 +00:00
|
|
|
if err != nil && !objNotFound {
|
2022-05-12 02:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-07-15 15:35:54 +00:00
|
|
|
|
|
|
|
if !objNotFound {
|
2024-07-17 09:44:38 +00:00
|
|
|
for _, addr := range objs {
|
|
|
|
n.deleteCORSObject(ctx, bktInfo, addr)
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return apierr.GetAPIErrorWithError(apierr.ErrCORSUnsupportedMethod, fmt.Errorf("unsupported method is %s", m))
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, h := range r.ExposeHeaders {
|
|
|
|
if h == wildcard {
|
2024-09-27 09:18:41 +00:00
|
|
|
return apierr.GetAPIError(apierr.ErrCORSWildcardExposeHeaders)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|