From 2299db4e81f90a68dbb41d9dbf34f525af0e808e Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Mon, 23 Aug 2021 21:39:15 +0300 Subject: [PATCH] [#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 --- api/handler/api.go | 13 ++++++++++++- api/handler/put.go | 8 +------- cmd/s3-gw/app.go | 24 +++++++++++++++++++++++- cmd/s3-gw/app_settings.go | 3 +++ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/api/handler/api.go b/api/handler/api.go index 76a6151f..1040746f 100644 --- a/api/handler/api.go +++ b/api/handler/api.go @@ -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 } diff --git a/api/handler/put.go b/api/handler/put.go index e4a89a51..368c9e64 100644 --- a/api/handler/put.go +++ b/api/handler/put.go @@ -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) diff --git a/cmd/s3-gw/app.go b/cmd/s3-gw/app.go index 0e22d008..2fea7a90 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -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 +} diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index 85c75bf5..9fd899f4 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -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"