2020-07-24 16:10:41 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-03-02 14:54:33 +00:00
|
|
|
"encoding/xml"
|
2020-08-06 10:49:25 +00:00
|
|
|
"errors"
|
2023-04-24 23:49:12 +00:00
|
|
|
"fmt"
|
2023-03-02 14:54:33 +00:00
|
|
|
"io"
|
2023-04-24 23:49:12 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-11-08 09:12:55 +00:00
|
|
|
"time"
|
2020-07-24 16:10:41 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-12-08 07:44:13 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-12-08 07:44:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2020-07-24 16:10:41 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
handler struct {
|
2022-04-29 13:08:22 +00:00
|
|
|
log *zap.Logger
|
|
|
|
obj layer.Client
|
|
|
|
notificator Notificator
|
2023-09-08 11:17:14 +00:00
|
|
|
cfg Config
|
2023-12-08 07:44:13 +00:00
|
|
|
ape APE
|
|
|
|
frostfsid FrostFSID
|
2022-04-29 13:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Notificator interface {
|
|
|
|
SendNotifications(topics map[string]string, p *SendNotificationParams) error
|
2022-11-08 09:12:55 +00:00
|
|
|
SendTestNotification(topic, bucketName, requestID, HostID string, now time.Time) error
|
2021-08-23 18:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Config contains data which handler needs to keep.
|
2023-09-08 11:17:14 +00:00
|
|
|
Config interface {
|
2023-11-21 08:51:07 +00:00
|
|
|
DefaultPlacementPolicy(namespace string) netmap.PlacementPolicy
|
|
|
|
PlacementPolicy(namespace, constraint string) (netmap.PlacementPolicy, bool)
|
|
|
|
CopiesNumbers(namespace, constraint string) ([]uint32, bool)
|
|
|
|
DefaultCopiesNumbers(namespace string) []uint32
|
2023-10-09 12:34:51 +00:00
|
|
|
NewXMLDecoder(io.Reader) *xml.Decoder
|
2023-09-08 11:17:14 +00:00
|
|
|
DefaultMaxAge() int
|
|
|
|
NotificatorEnabled() bool
|
|
|
|
ResolveZoneList() []string
|
|
|
|
IsResolveListAllow() bool
|
2023-07-10 09:17:44 +00:00
|
|
|
BypassContentEncodingInChunks() bool
|
2023-10-02 08:52:07 +00:00
|
|
|
MD5Enabled() bool
|
2024-02-14 14:04:35 +00:00
|
|
|
ACLEnabled() bool
|
2023-07-10 09:17:44 +00:00
|
|
|
}
|
2023-12-08 07:44:13 +00:00
|
|
|
|
|
|
|
FrostFSID interface {
|
|
|
|
GetUserAddress(account, user string) (string, error)
|
2024-02-19 07:51:41 +00:00
|
|
|
GetUserKey(account, name string) (string, error)
|
2023-12-08 07:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// APE is Access Policy Engine that needs to save policy and acl info to different places.
|
|
|
|
APE interface {
|
2024-02-19 07:51:41 +00:00
|
|
|
PutBucketPolicy(ns string, cnrID cid.ID, policy []byte, chains []*chain.Chain) error
|
2024-04-01 09:51:05 +00:00
|
|
|
DeleteBucketPolicy(ns string, cnrID cid.ID, chainIDs []chain.ID) error
|
2024-02-13 08:50:11 +00:00
|
|
|
GetBucketPolicy(ns string, cnrID cid.ID) ([]byte, error)
|
|
|
|
SaveACLChains(ns string, chains []*chain.Chain) error
|
2023-12-08 07:44:13 +00:00
|
|
|
}
|
2020-07-24 16:10:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ api.Handler = (*handler)(nil)
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// New creates new api.Handler using given logger and client.
|
2023-12-08 07:44:13 +00:00
|
|
|
func New(log *zap.Logger, obj layer.Client, notificator Notificator, cfg Config, storage APE, ffsid FrostFSID) (api.Handler, error) {
|
2020-07-24 16:10:41 +00:00
|
|
|
switch {
|
2020-08-06 10:49:25 +00:00
|
|
|
case obj == nil:
|
2022-12-20 08:38:58 +00:00
|
|
|
return nil, errors.New("empty FrostFS Object Layer")
|
2020-08-06 10:49:25 +00:00
|
|
|
case log == nil:
|
2020-07-24 16:10:41 +00:00
|
|
|
return nil, errors.New("empty logger")
|
2023-12-08 07:44:13 +00:00
|
|
|
case storage == nil:
|
|
|
|
return nil, errors.New("empty policy storage")
|
2024-02-19 08:45:18 +00:00
|
|
|
case ffsid == nil:
|
|
|
|
return nil, errors.New("empty frostfsid")
|
2020-07-24 16:10:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 11:17:14 +00:00
|
|
|
if !cfg.NotificatorEnabled() {
|
2023-08-23 11:07:52 +00:00
|
|
|
log.Warn(logs.NotificatorIsDisabledS3WontProduceNotificationEvents)
|
2022-06-24 14:34:44 +00:00
|
|
|
} else if notificator == nil {
|
2022-04-29 13:08:22 +00:00
|
|
|
return nil, errors.New("empty notificator")
|
|
|
|
}
|
|
|
|
|
2020-07-24 16:10:41 +00:00
|
|
|
return &handler{
|
2022-04-29 13:08:22 +00:00
|
|
|
log: log,
|
|
|
|
obj: obj,
|
|
|
|
cfg: cfg,
|
2023-12-08 07:44:13 +00:00
|
|
|
ape: storage,
|
2022-04-29 13:08:22 +00:00
|
|
|
notificator: notificator,
|
2023-12-08 07:44:13 +00:00
|
|
|
frostfsid: ffsid,
|
2020-07-24 16:10:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-04-24 23:49:12 +00:00
|
|
|
|
|
|
|
// pickCopiesNumbers chooses the return values following this logic:
|
2023-05-16 14:24:10 +00:00
|
|
|
// 1) array of copies numbers sent in request's header has the highest priority.
|
|
|
|
// 2) array of copies numbers with corresponding location constraint provided in the config file.
|
|
|
|
// 3) default copies number from the config file wrapped into array.
|
2023-11-21 08:51:07 +00:00
|
|
|
func (h *handler) pickCopiesNumbers(metadata map[string]string, namespace, locationConstraint string) ([]uint32, error) {
|
2023-04-24 23:49:12 +00:00
|
|
|
copiesNumbersStr, ok := metadata[layer.AttributeFrostfsCopiesNumber]
|
|
|
|
if ok {
|
|
|
|
result, err := parseCopiesNumbers(copiesNumbersStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
copiesNumbers, ok := h.cfg.CopiesNumbers(namespace, locationConstraint)
|
2023-04-24 23:49:12 +00:00
|
|
|
if ok {
|
|
|
|
return copiesNumbers, nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
return h.cfg.DefaultCopiesNumbers(namespace), nil
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseCopiesNumbers(copiesNumbersStr string) ([]uint32, error) {
|
|
|
|
var result []uint32
|
|
|
|
copiesNumbersSplit := strings.Split(copiesNumbersStr, ",")
|
|
|
|
|
|
|
|
for i := range copiesNumbersSplit {
|
|
|
|
item := strings.ReplaceAll(copiesNumbersSplit[i], " ", "")
|
|
|
|
if len(item) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
copiesNumber, err := strconv.ParseUint(item, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pasrse copies number: %w", err)
|
|
|
|
}
|
|
|
|
result = append(result, uint32(copiesNumber))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|