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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
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
|
2023-07-10 09:17:44 +00:00
|
|
|
}
|
2023-12-08 07:44:13 +00:00
|
|
|
|
|
|
|
FrostFSID interface {
|
|
|
|
GetUserAddress(account, user string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// APE is Access Policy Engine that needs to save policy and acl info to different places.
|
|
|
|
APE interface {
|
|
|
|
MorphRuleChainStorage
|
|
|
|
PolicyStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// MorphRuleChainStorage is a similar to engine.MorphRuleChainStorage
|
|
|
|
// but doesn't know anything about tx.
|
|
|
|
MorphRuleChainStorage interface {
|
|
|
|
AddChain(target engine.Target, c *chain.Chain) error
|
|
|
|
RemoveChain(target engine.Target, chainID chain.ID) error
|
|
|
|
ListChains(target engine.Target) ([]*chain.Chain, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolicyStorage is interface to save intact initial user provided policy.
|
|
|
|
PolicyStorage interface {
|
|
|
|
PutPolicy(namespace string, cnrID cid.ID, policy []byte) error
|
|
|
|
GetPolicy(namespace string, cnrID cid.ID) ([]byte, error)
|
|
|
|
DeletePolicy(namespace string, cnrID cid.ID) error
|
|
|
|
}
|
|
|
|
|
|
|
|
frostfsIDDisabled struct{}
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ffsid == nil {
|
|
|
|
ffsid = frostfsIDDisabled{}
|
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
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (f frostfsIDDisabled) GetUserAddress(_, _ string) (string, error) {
|
|
|
|
return "", errors.New("frostfsid disabled")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|