forked from TrueCloudLab/frostfs-s3-gw
[#218] handler,s3-gw: Make policy configurable
Now default policy of placing containers can be set via config/env variable. Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
parent
6d4fe34f3d
commit
2299db4e81
4 changed files with 39 additions and 9 deletions
|
@ -3,6 +3,7 @@ package handler
|
|||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
||||
"go.uber.org/zap"
|
||||
|
@ -12,13 +13,22 @@ type (
|
|||
handler struct {
|
||||
log *zap.Logger
|
||||
obj layer.Client
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
// Config contains data which handler need to keep.
|
||||
Config struct {
|
||||
DefaultPolicy *netmap.PlacementPolicy
|
||||
}
|
||||
)
|
||||
|
||||
// DefaultPolicy is a default policy of placing container in NeoFS if it's not set at the request.
|
||||
const DefaultPolicy = "REP 3"
|
||||
|
||||
var _ api.Handler = (*handler)(nil)
|
||||
|
||||
// New creates new api.Handler using given logger and client.
|
||||
func New(log *zap.Logger, obj layer.Client) (api.Handler, error) {
|
||||
func New(log *zap.Logger, obj layer.Client, cfg *Config) (api.Handler, error) {
|
||||
switch {
|
||||
case obj == nil:
|
||||
return nil, errors.New("empty NeoFS Object Layer")
|
||||
|
@ -29,5 +39,6 @@ func New(log *zap.Logger, obj layer.Client) (api.Handler, error) {
|
|||
return &handler{
|
||||
log: log,
|
||||
obj: obj,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/policy"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
||||
|
@ -20,7 +19,6 @@ const (
|
|||
basicACLReadOnly = "public-read"
|
||||
basicACLPublic = "public-read-write"
|
||||
cannedACLAuthRead = "authenticated-read"
|
||||
defaultPolicy = "REP 3"
|
||||
|
||||
publicBasicRule = 0x0FFFFFFF
|
||||
)
|
||||
|
@ -181,11 +179,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
if p.Policy == nil {
|
||||
p.Policy, err = policy.Parse(defaultPolicy)
|
||||
if err != nil {
|
||||
h.logAndSendError(w, "could not parse policy", reqInfo, err)
|
||||
return
|
||||
}
|
||||
p.Policy = h.cfg.DefaultPolicy
|
||||
}
|
||||
|
||||
cid, err := h.obj.CreateBucket(r.Context(), &p)
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/policy"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api/auth"
|
||||
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
||||
|
@ -117,7 +118,9 @@ func newApp(ctx context.Context, l *zap.Logger, v *viper.Viper) *App {
|
|||
// prepare auth center
|
||||
ctr = auth.New(conns, key)
|
||||
|
||||
if caller, err = handler.New(l, obj); err != nil {
|
||||
handlerOptions := getHandlerOptions(v, l)
|
||||
|
||||
if caller, err = handler.New(l, obj, handlerOptions); err != nil {
|
||||
l.Fatal("could not initialize API handler", zap.Error(err))
|
||||
}
|
||||
|
||||
|
@ -252,3 +255,22 @@ func getCacheOptions(v *viper.Viper, l *zap.Logger) *layer.CacheConfig {
|
|||
}
|
||||
return &cacheCfg
|
||||
}
|
||||
|
||||
func getHandlerOptions(v *viper.Viper, l *zap.Logger) *handler.Config {
|
||||
var (
|
||||
cfg handler.Config
|
||||
err error
|
||||
policyStr = handler.DefaultPolicy
|
||||
)
|
||||
|
||||
if v.IsSet(cfgDefaultPolicy) {
|
||||
policyStr = v.GetString(cfgDefaultPolicy)
|
||||
}
|
||||
|
||||
if cfg.DefaultPolicy, err = policy.Parse(policyStr); err != nil {
|
||||
l.Fatal("couldn't parse container default policy",
|
||||
zap.Error(err))
|
||||
}
|
||||
|
||||
return &cfg
|
||||
}
|
||||
|
|
|
@ -59,6 +59,9 @@ const ( // Settings.
|
|||
cfgCacheSize = "cache.size"
|
||||
cfgListObjectsCacheLifetime = "cache.list_objects_lifetime"
|
||||
|
||||
// Policy.
|
||||
cfgDefaultPolicy = "default_policy"
|
||||
|
||||
// MaxClients.
|
||||
cfgMaxClientsCount = "max_clients_count"
|
||||
cfgMaxClientsDeadline = "max_clients_deadline"
|
||||
|
|
Loading…
Reference in a new issue