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"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
s := &PutSystemObjectParams{
|
|
|
|
BktInfo: p.BktInfo,
|
|
|
|
ObjName: p.BktInfo.CORSObjectName(),
|
|
|
|
Metadata: map[string]string{},
|
|
|
|
Prefix: "",
|
2021-10-13 18:50:02 +00:00
|
|
|
Reader: &buf,
|
2022-05-25 15:59:36 +00:00
|
|
|
Size: int64(buf.Len()),
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:40:52 +00:00
|
|
|
if _, err := n.putSystemObjectIntoNeoFS(ctx, s); err != nil {
|
|
|
|
return fmt.Errorf("put system object: %w", err)
|
2021-10-13 18:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:40:52 +00:00
|
|
|
if err := n.systemCache.PutCORS(systemObjectKey(p.BktInfo, s.ObjName), cors); err != nil {
|
2021-10-13 18:50:02 +00:00
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
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) {
|
|
|
|
cors, err := n.getCORS(ctx, bktInfo, bktInfo.CORSObjectName())
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
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-02-28 13:39:04 +00:00
|
|
|
return n.DeleteSystemObject(ctx, bktInfo, bktInfo.CORSObjectName())
|
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
|
|
|
|
}
|