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"
|
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{}
|
|
|
|
)
|
|
|
|
|
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{
|
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
|
|
|
|
|
|
|
|
_, objID, _, _, 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-17 09:44:38 +00:00
|
|
|
objsToDelete, err := n.treeService.PutBucketCORS(ctx, p.BktInfo, newAddress(corsBkt.CID, objID))
|
2024-07-15 15:35:54 +00:00
|
|
|
objToDeleteNotFound := errorsStd.Is(err, ErrNoNodeToRemove)
|
|
|
|
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) {
|
|
|
|
var prmAuth PrmAuth
|
|
|
|
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-07-15 15:35:54 +00:00
|
|
|
objNotFound := errorsStd.Is(err, ErrNoNodeToRemove)
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|