[#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:
Angira Kekteeva 2021-08-23 21:39:15 +03:00
parent 6d4fe34f3d
commit 2299db4e81
4 changed files with 39 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package handler
import ( import (
"errors" "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"
"github.com/nspcc-dev/neofs-s3-gw/api/layer" "github.com/nspcc-dev/neofs-s3-gw/api/layer"
"go.uber.org/zap" "go.uber.org/zap"
@ -12,13 +13,22 @@ type (
handler struct { handler struct {
log *zap.Logger log *zap.Logger
obj layer.Client 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) var _ api.Handler = (*handler)(nil)
// New creates new api.Handler using given logger and client. // 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 { switch {
case obj == nil: case obj == nil:
return nil, errors.New("empty NeoFS Object Layer") 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{ return &handler{
log: log, log: log,
obj: obj, obj: obj,
cfg: cfg,
}, nil }, nil
} }

View file

@ -7,7 +7,6 @@ import (
"strings" "strings"
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl" "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"
"github.com/nspcc-dev/neofs-s3-gw/api/errors" "github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer" "github.com/nspcc-dev/neofs-s3-gw/api/layer"
@ -20,7 +19,6 @@ const (
basicACLReadOnly = "public-read" basicACLReadOnly = "public-read"
basicACLPublic = "public-read-write" basicACLPublic = "public-read-write"
cannedACLAuthRead = "authenticated-read" cannedACLAuthRead = "authenticated-read"
defaultPolicy = "REP 3"
publicBasicRule = 0x0FFFFFFF publicBasicRule = 0x0FFFFFFF
) )
@ -181,11 +179,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
if p.Policy == nil { if p.Policy == nil {
p.Policy, err = policy.Parse(defaultPolicy) p.Policy = h.cfg.DefaultPolicy
if err != nil {
h.logAndSendError(w, "could not parse policy", reqInfo, err)
return
}
} }
cid, err := h.obj.CreateBucket(r.Context(), &p) cid, err := h.obj.CreateBucket(r.Context(), &p)

View file

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "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"
"github.com/nspcc-dev/neofs-s3-gw/api/auth" "github.com/nspcc-dev/neofs-s3-gw/api/auth"
"github.com/nspcc-dev/neofs-s3-gw/api/cache" "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 // prepare auth center
ctr = auth.New(conns, key) 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)) 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 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
}

View file

@ -59,6 +59,9 @@ const ( // Settings.
cfgCacheSize = "cache.size" cfgCacheSize = "cache.size"
cfgListObjectsCacheLifetime = "cache.list_objects_lifetime" cfgListObjectsCacheLifetime = "cache.list_objects_lifetime"
// Policy.
cfgDefaultPolicy = "default_policy"
// MaxClients. // MaxClients.
cfgMaxClientsCount = "max_clients_count" cfgMaxClientsCount = "max_clients_count"
cfgMaxClientsDeadline = "max_clients_deadline" cfgMaxClientsDeadline = "max_clients_deadline"