2020-07-06 09:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-22 13:25:29 +00:00
|
|
|
"encoding/hex"
|
2022-11-03 06:34:18 +00:00
|
|
|
"encoding/json"
|
2022-04-25 09:57:58 +00:00
|
|
|
"fmt"
|
2020-07-12 23:00:47 +00:00
|
|
|
"net/http"
|
2022-09-12 09:45:54 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-10-04 14:32:35 +00:00
|
|
|
"strconv"
|
2022-09-12 12:30:51 +00:00
|
|
|
"sync"
|
2022-09-12 09:45:54 +00:00
|
|
|
"syscall"
|
2021-09-08 07:08:56 +00:00
|
|
|
"time"
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-06-14 08:50:22 +00:00
|
|
|
grpctracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/handler"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/notifications"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/resolver"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs"
|
2023-04-03 11:04:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/version"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/wallet"
|
2023-03-02 14:54:33 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/xml"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/metrics"
|
2023-03-14 14:31:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
2023-06-09 06:34:04 +00:00
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
2022-07-26 13:29:07 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-06-22 14:11:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-06 09:18:16 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2023-03-14 14:31:15 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// App is the main application structure.
|
2020-07-06 09:18:16 +00:00
|
|
|
App struct {
|
2023-06-09 06:34:04 +00:00
|
|
|
ctr auth.Center
|
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
pool *pool.Pool
|
|
|
|
treePool *treepool.Pool
|
|
|
|
key *keys.PrivateKey
|
|
|
|
nc *notifications.Controller
|
|
|
|
obj layer.Client
|
|
|
|
api api.Handler
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
servers []Server
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
metrics *metrics.AppMetrics
|
2022-09-12 13:46:55 +00:00
|
|
|
bucketResolver *resolver.BucketResolver
|
2022-09-12 14:30:41 +00:00
|
|
|
services []*Service
|
|
|
|
settings *appSettings
|
|
|
|
maxClients api.MaxClients
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
webDone chan struct{}
|
|
|
|
wrkDone chan struct{}
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appSettings struct {
|
2023-03-02 14:54:33 +00:00
|
|
|
logLevel zap.AtomicLevel
|
|
|
|
policies *placementPolicy
|
|
|
|
xmlDecoder *xml.DecoderProvider
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
lvl zap.AtomicLevel
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
2020-07-13 15:50:11 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
placementPolicy struct {
|
2023-05-18 13:20:00 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
defaultPolicy netmap.PlacementPolicy
|
|
|
|
regionMap map[string]netmap.PlacementPolicy
|
|
|
|
copiesNumbers map[string][]uint32
|
|
|
|
defaultCopiesNumbers []uint32
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
func newApp(ctx context.Context, log *Logger, v *viper.Viper) *App {
|
2023-06-09 06:34:04 +00:00
|
|
|
objPool, treePool, key := getPools(ctx, log.logger, v)
|
2020-10-13 09:31:23 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
// prepare auth center
|
2023-06-09 06:34:04 +00:00
|
|
|
ctr := auth.New(frostfs.NewAuthmateFrostFS(objPool), key, v.GetStringSlice(cfgAllowedAccessKeyIDPrefixes), getAccessBoxCacheConfig(v, log.logger))
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
app := &App{
|
2023-06-09 06:34:04 +00:00
|
|
|
ctr: ctr,
|
|
|
|
log: log.logger,
|
|
|
|
cfg: v,
|
|
|
|
pool: objPool,
|
|
|
|
treePool: treePool,
|
|
|
|
key: key,
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
webDone: make(chan struct{}, 1),
|
|
|
|
wrkDone: make(chan struct{}, 1),
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
maxClients: newMaxClients(v),
|
2022-11-14 09:37:47 +00:00
|
|
|
settings: newAppSettings(log, v),
|
2020-10-13 09:31:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
app.init(ctx)
|
2022-07-29 06:26:11 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
return app
|
|
|
|
}
|
2020-10-13 09:31:23 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
func (a *App) init(ctx context.Context) {
|
2022-11-14 09:37:47 +00:00
|
|
|
a.initAPI(ctx)
|
2022-09-12 14:30:41 +00:00
|
|
|
a.initMetrics()
|
2022-11-09 10:07:18 +00:00
|
|
|
a.initServers(ctx)
|
2023-05-31 16:35:20 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
2020-11-24 07:09:58 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
func (a *App) initLayer(ctx context.Context) {
|
|
|
|
a.initResolver()
|
2022-04-07 14:36:44 +00:00
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
// prepare random key for anonymous requests
|
|
|
|
randomKey, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
2022-09-12 14:30:41 +00:00
|
|
|
a.log.Fatal("couldn't generate random key", zap.Error(err))
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 09:16:05 +00:00
|
|
|
layerCfg := &layer.Config{
|
2022-09-12 14:30:41 +00:00
|
|
|
Caches: getCacheOptions(a.cfg, a.log),
|
2021-11-22 09:16:05 +00:00
|
|
|
AnonKey: layer.AnonymousKey{
|
|
|
|
Key: randomKey,
|
|
|
|
},
|
2022-09-12 14:30:41 +00:00
|
|
|
Resolver: a.bucketResolver,
|
2023-06-09 06:34:04 +00:00
|
|
|
TreeService: tree.NewTree(services.NewPoolWrapper(a.treePool), a.log),
|
2021-11-22 09:16:05 +00:00
|
|
|
}
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
// prepare object layer
|
2022-12-20 08:38:58 +00:00
|
|
|
a.obj = layer.NewLayer(a.log, frostfs.NewFrostFS(a.pool), layerCfg)
|
2020-11-24 07:09:58 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
if a.cfg.GetBool(cfgEnableNATS) {
|
|
|
|
nopts := getNotificationsOptions(a.cfg, a.log)
|
|
|
|
a.nc, err = notifications.NewController(nopts, a.log)
|
2022-03-25 11:13:54 +00:00
|
|
|
if err != nil {
|
2022-09-12 14:30:41 +00:00
|
|
|
a.log.Fatal("failed to enable notifications", zap.Error(err))
|
2022-03-25 11:13:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
if err = a.obj.Initialize(ctx, a.nc); err != nil {
|
|
|
|
a.log.Fatal("couldn't initialize layer", zap.Error(err))
|
2022-03-25 11:13:54 +00:00
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
}
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func newAppSettings(log *Logger, v *viper.Viper) *appSettings {
|
2023-05-18 13:20:00 +00:00
|
|
|
policies, err := newPlacementPolicy(log.logger, v)
|
2022-11-14 09:37:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.logger.Fatal("failed to create new policy mapping", zap.Error(err))
|
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
return &appSettings{
|
2023-03-02 14:54:33 +00:00
|
|
|
logLevel: log.lvl,
|
|
|
|
policies: policies,
|
|
|
|
xmlDecoder: xml.NewDecoderProvider(v.GetBool(cfgKludgeUseDefaultXMLNSForCompleteMultipartUpload)),
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func getDefaultPolicyValue(v *viper.Viper) string {
|
|
|
|
defaultPolicyStr := handler.DefaultPolicy
|
|
|
|
if v.IsSet(cfgPolicyDefault) {
|
|
|
|
defaultPolicyStr = v.GetString(cfgPolicyDefault)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
2022-11-14 09:37:47 +00:00
|
|
|
|
|
|
|
return defaultPolicyStr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) initAPI(ctx context.Context) {
|
|
|
|
a.initLayer(ctx)
|
|
|
|
a.initHandler()
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
func (a *App) initMetrics() {
|
2023-02-09 08:59:31 +00:00
|
|
|
a.metrics = metrics.NewAppMetrics(a.log, frostfs.NewPoolStatistic(a.pool), a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetHealth(metrics.HealthStatusStarting)
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
func (a *App) initResolver() {
|
|
|
|
var err error
|
|
|
|
a.bucketResolver, err = resolver.NewBucketResolver(a.getResolverConfig())
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to create resolver", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) getResolverConfig() ([]string, *resolver.Config) {
|
|
|
|
resolveCfg := &resolver.Config{
|
2022-12-20 08:38:58 +00:00
|
|
|
FrostFS: frostfs.NewResolverFrostFS(a.pool),
|
2022-09-12 13:46:55 +00:00
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
|
|
|
}
|
|
|
|
|
|
|
|
order := a.cfg.GetStringSlice(cfgResolveOrder)
|
|
|
|
if resolveCfg.RPCAddress == "" {
|
|
|
|
order = remove(order, resolver.NNSResolver)
|
|
|
|
a.log.Warn(fmt.Sprintf("resolver '%s' won't be used since '%s' isn't provided", resolver.NNSResolver, cfgRPCEndpoint))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(order) == 0 {
|
|
|
|
a.log.Info("container resolver will be disabled because of resolvers 'resolver_order' is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return order, resolveCfg
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:20 +00:00
|
|
|
func (a *App) initTracing(ctx context.Context) {
|
|
|
|
instanceID := ""
|
|
|
|
if len(a.servers) > 0 {
|
|
|
|
instanceID = a.servers[0].Address()
|
|
|
|
}
|
|
|
|
cfg := tracing.Config{
|
|
|
|
Enabled: a.cfg.GetBool(cfgTracingEnabled),
|
|
|
|
Exporter: tracing.Exporter(a.cfg.GetString(cfgTracingExporter)),
|
|
|
|
Endpoint: a.cfg.GetString(cfgTracingEndpoint),
|
|
|
|
Service: "frostfs-s3-gw",
|
|
|
|
InstanceID: instanceID,
|
|
|
|
Version: version.Version,
|
|
|
|
}
|
|
|
|
updated, err := tracing.Setup(ctx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn("failed to initialize tracing", zap.Error(err))
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
a.log.Info("tracing config updated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) shutdownTracing() {
|
|
|
|
const tracingShutdownTimeout = 5 * time.Second
|
|
|
|
shdnCtx, cancel := context.WithTimeout(context.Background(), tracingShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := tracing.Shutdown(shdnCtx); err != nil {
|
|
|
|
a.log.Warn("failed to shutdown tracing", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
func newMaxClients(cfg *viper.Viper) api.MaxClients {
|
|
|
|
maxClientsCount := cfg.GetInt(cfgMaxClientsCount)
|
|
|
|
if maxClientsCount <= 0 {
|
|
|
|
maxClientsCount = defaultMaxClientsCount
|
|
|
|
}
|
|
|
|
|
|
|
|
maxClientsDeadline := cfg.GetDuration(cfgMaxClientsDeadline)
|
|
|
|
if maxClientsDeadline <= 0 {
|
|
|
|
maxClientsDeadline = defaultMaxClientsDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.NewMaxClientsMiddleware(maxClientsCount, maxClientsDeadline)
|
|
|
|
}
|
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
func getPools(ctx context.Context, logger *zap.Logger, cfg *viper.Viper) (*pool.Pool, *treepool.Pool, *keys.PrivateKey) {
|
2022-09-12 14:30:41 +00:00
|
|
|
var prm pool.InitParameters
|
2023-06-09 06:34:04 +00:00
|
|
|
var prmTree treepool.InitParameters
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
password := wallet.GetPassword(cfg, cfgWalletPassphrase)
|
|
|
|
key, err := wallet.GetKeyFromPath(cfg.GetString(cfgWalletPath), cfg.GetString(cfgWalletAddress), password)
|
|
|
|
if err != nil {
|
2022-12-20 08:38:58 +00:00
|
|
|
logger.Fatal("could not load FrostFS private key", zap.Error(err))
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm.SetKey(&key.PrivateKey)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetKey(key)
|
2022-12-20 08:38:58 +00:00
|
|
|
logger.Info("using credentials", zap.String("FrostFS", hex.EncodeToString(key.PublicKey().Bytes())))
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
for _, peer := range fetchPeers(logger, cfg) {
|
|
|
|
prm.AddNode(peer)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.AddNode(peer)
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connTimeout := cfg.GetDuration(cfgConnectTimeout)
|
|
|
|
if connTimeout <= 0 {
|
|
|
|
connTimeout = defaultConnectTimeout
|
|
|
|
}
|
|
|
|
prm.SetNodeDialTimeout(connTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetNodeDialTimeout(connTimeout)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2022-11-15 14:19:21 +00:00
|
|
|
streamTimeout := cfg.GetDuration(cfgStreamTimeout)
|
|
|
|
if streamTimeout <= 0 {
|
|
|
|
streamTimeout = defaultStreamTimeout
|
|
|
|
}
|
|
|
|
prm.SetNodeStreamTimeout(streamTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetNodeStreamTimeout(streamTimeout)
|
2022-11-15 14:19:21 +00:00
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
healthCheckTimeout := cfg.GetDuration(cfgHealthcheckTimeout)
|
|
|
|
if healthCheckTimeout <= 0 {
|
|
|
|
healthCheckTimeout = defaultHealthcheckTimeout
|
|
|
|
}
|
2022-11-15 14:19:21 +00:00
|
|
|
prm.SetHealthcheckTimeout(healthCheckTimeout)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetHealthcheckTimeout(healthCheckTimeout)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
rebalanceInterval := cfg.GetDuration(cfgRebalanceInterval)
|
|
|
|
if rebalanceInterval <= 0 {
|
|
|
|
rebalanceInterval = defaultRebalanceInterval
|
|
|
|
}
|
|
|
|
prm.SetClientRebalanceInterval(rebalanceInterval)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetClientRebalanceInterval(rebalanceInterval)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
|
|
|
errorThreshold := cfg.GetUint32(cfgPoolErrorThreshold)
|
|
|
|
if errorThreshold <= 0 {
|
|
|
|
errorThreshold = defaultPoolErrorThreshold
|
|
|
|
}
|
|
|
|
prm.SetErrorThreshold(errorThreshold)
|
2023-01-23 08:19:06 +00:00
|
|
|
prm.SetLogger(logger)
|
2023-06-09 06:34:04 +00:00
|
|
|
prmTree.SetLogger(logger)
|
2022-09-12 14:30:41 +00:00
|
|
|
|
2023-06-14 08:50:22 +00:00
|
|
|
var apiGRPCDialOpts []grpc.DialOption
|
|
|
|
var treeGRPCDialOpts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
|
|
|
if cfg.GetBool(cfgTracingEnabled) {
|
|
|
|
interceptors := []grpc.DialOption{
|
|
|
|
grpc.WithUnaryInterceptor(grpctracing.NewUnaryClientInteceptor()),
|
|
|
|
grpc.WithStreamInterceptor(grpctracing.NewStreamClientInterceptor()),
|
|
|
|
}
|
|
|
|
treeGRPCDialOpts = append(treeGRPCDialOpts, interceptors...)
|
|
|
|
apiGRPCDialOpts = append(apiGRPCDialOpts, interceptors...)
|
|
|
|
}
|
|
|
|
prm.SetGRPCDialOptions(apiGRPCDialOpts...)
|
|
|
|
prmTree.SetGRPCDialOptions(treeGRPCDialOpts...)
|
|
|
|
|
2022-09-12 14:30:41 +00:00
|
|
|
p, err := pool.NewPool(prm)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to create connection pool", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.Dial(ctx); err != nil {
|
|
|
|
logger.Fatal("failed to dial connection pool", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
treePool, err := treepool.NewPool(prmTree)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to create tree pool", zap.Error(err))
|
|
|
|
}
|
|
|
|
if err = treePool.Dial(ctx); err != nil {
|
|
|
|
logger.Fatal("failed to dial tree pool", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, treePool, key
|
2022-09-12 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
func newPlacementPolicy(l *zap.Logger, v *viper.Viper) (*placementPolicy, error) {
|
2022-11-14 09:37:47 +00:00
|
|
|
policies := &placementPolicy{
|
2023-05-18 13:20:00 +00:00
|
|
|
regionMap: make(map[string]netmap.PlacementPolicy),
|
|
|
|
defaultCopiesNumbers: []uint32{handler.DefaultCopiesNumber},
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
policies.updateCopiesNumbers(l, v)
|
|
|
|
policies.updateDefaultCopiesNumbers(l, v)
|
|
|
|
|
|
|
|
return policies, policies.updatePolicy(getDefaultPolicyValue(v), v.GetString(cfgPolicyRegionMapFile))
|
2022-11-14 09:37:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
func (p *placementPolicy) DefaultPlacementPolicy() netmap.PlacementPolicy {
|
2022-11-14 09:37:47 +00:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
return p.defaultPolicy
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
func (p *placementPolicy) PlacementPolicy(name string) (netmap.PlacementPolicy, bool) {
|
2022-11-14 09:37:47 +00:00
|
|
|
p.mu.RLock()
|
|
|
|
policy, ok := p.regionMap[name]
|
|
|
|
p.mu.RUnlock()
|
|
|
|
|
|
|
|
return policy, ok
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
func (p *placementPolicy) CopiesNumbers(locationConstraint string) ([]uint32, bool) {
|
|
|
|
p.mu.RLock()
|
|
|
|
copiesNumbers, ok := p.copiesNumbers[locationConstraint]
|
|
|
|
p.mu.RUnlock()
|
|
|
|
|
|
|
|
return copiesNumbers, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicy) DefaultCopiesNumbers() []uint32 {
|
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
return p.defaultCopiesNumbers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicy) update(l *zap.Logger, v *viper.Viper) {
|
|
|
|
if err := p.updatePolicy(getDefaultPolicyValue(v), v.GetString(cfgPolicyRegionMapFile)); err != nil {
|
|
|
|
l.Warn("policies won't be updated", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
p.updateCopiesNumbers(l, v)
|
|
|
|
p.updateDefaultCopiesNumbers(l, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicy) updatePolicy(defaultPolicy string, regionPolicyFilepath string) error {
|
2022-11-14 09:37:47 +00:00
|
|
|
var defaultPlacementPolicy netmap.PlacementPolicy
|
|
|
|
if err := defaultPlacementPolicy.DecodeString(defaultPolicy); err != nil {
|
|
|
|
return fmt.Errorf("parse default policy '%s': %w", defaultPolicy, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regionPolicyMap, err := readRegionMap(regionPolicyFilepath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read region map file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regionMap := make(map[string]netmap.PlacementPolicy, len(regionPolicyMap))
|
|
|
|
for region, policy := range regionPolicyMap {
|
|
|
|
var pp netmap.PlacementPolicy
|
|
|
|
if err = pp.DecodeString(policy); err == nil {
|
|
|
|
regionMap[region] = pp
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pp.UnmarshalJSON([]byte(policy)); err == nil {
|
|
|
|
regionMap[region] = pp
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("parse region '%s' to policy mapping: %w", region, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
p.defaultPolicy = defaultPlacementPolicy
|
|
|
|
p.regionMap = regionMap
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
func (p *placementPolicy) updateCopiesNumbers(l *zap.Logger, v *viper.Viper) {
|
|
|
|
if newCopiesNumbers, err := fetchCopiesNumbers(l, v); err != nil {
|
|
|
|
l.Warn("copies numbers won't be updated", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.copiesNumbers = newCopiesNumbers
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicy) updateDefaultCopiesNumbers(l *zap.Logger, v *viper.Viper) {
|
|
|
|
configuredValues, err := fetchDefaultCopiesNumbers(v)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.defaultCopiesNumbers = configuredValues
|
|
|
|
p.mu.Unlock()
|
|
|
|
l.Info("default copies numbers", zap.Uint32s("vector", p.defaultCopiesNumbers))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Error("cannot parse default copies numbers", zap.Error(err))
|
|
|
|
l.Warn("default copies numbers won't be updated", zap.Uint32s("current value", p.DefaultCopiesNumbers()))
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
func remove(list []string, element string) []string {
|
|
|
|
for i, item := range list {
|
|
|
|
if item == element {
|
|
|
|
return append(list[:i], list[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Wait waits for an application to finish.
|
2022-03-29 09:39:49 +00:00
|
|
|
//
|
|
|
|
// Pre-logs a message about the launch of the application mentioning its
|
2022-12-20 08:38:58 +00:00
|
|
|
// version (version.Version) and its name (frostfs-s3-gw). At the end, it writes
|
2022-04-13 16:56:58 +00:00
|
|
|
// about the stop to the log.
|
2020-07-12 23:00:47 +00:00
|
|
|
func (a *App) Wait() {
|
2022-03-29 09:39:49 +00:00
|
|
|
a.log.Info("application started",
|
2022-12-15 14:08:25 +00:00
|
|
|
zap.String("name", "frostfs-s3-gw"),
|
2022-03-29 09:39:49 +00:00
|
|
|
zap.String("version", version.Version),
|
|
|
|
)
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetVersion(version.Version)
|
2022-09-12 12:30:51 +00:00
|
|
|
a.setHealthStatus()
|
2022-07-21 14:42:24 +00:00
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
<-a.webDone // wait for web-server to be stopped
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
a.log.Info("application finished")
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) setHealthStatus() {
|
2023-04-07 14:28:21 +00:00
|
|
|
a.metrics.State().SetHealth(metrics.HealthStatusReady)
|
2022-09-12 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
// Serve runs HTTP server to handle S3 API requests.
|
|
|
|
func (a *App) Serve(ctx context.Context) {
|
2020-07-13 09:51:21 +00:00
|
|
|
// Attach S3 API:
|
2022-08-30 10:52:37 +00:00
|
|
|
domains := a.cfg.GetStringSlice(cfgListenDomains)
|
2022-09-12 13:46:55 +00:00
|
|
|
a.log.Info("fetch domains, prepare to use API", zap.Strings("domains", domains))
|
|
|
|
router := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2023-02-09 08:59:31 +00:00
|
|
|
api.Attach(router, domains, a.maxClients, a.api, a.ctr, a.log, a.metrics)
|
2020-07-13 09:51:21 +00:00
|
|
|
|
|
|
|
// Use mux.Router as http.Handler
|
2022-09-12 13:46:55 +00:00
|
|
|
srv := new(http.Server)
|
2020-07-13 09:51:21 +00:00
|
|
|
srv.Handler = router
|
2020-10-23 00:12:37 +00:00
|
|
|
srv.ErrorLog = zap.NewStdLog(a.log)
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.startServices()
|
2022-07-26 13:29:07 +00:00
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
for i := range a.servers {
|
|
|
|
go func(i int) {
|
|
|
|
a.log.Info("starting server", zap.String("address", a.servers[i].Address()))
|
2022-09-12 13:46:55 +00:00
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
if err := srv.Serve(a.servers[i].Listener()); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve", zap.Error(err))
|
2020-07-13 15:50:11 +00:00
|
|
|
}
|
2022-11-09 10:07:18 +00:00
|
|
|
}(i)
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGHUP)
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break LOOP
|
|
|
|
case <-sigs:
|
2023-05-31 16:35:20 +00:00
|
|
|
a.configReload(ctx)
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
ctx, cancel := shutdownContext()
|
2020-07-12 23:00:47 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.log.Info("stopping server", zap.Error(srv.Shutdown(ctx)))
|
|
|
|
|
|
|
|
a.metrics.Shutdown()
|
|
|
|
a.stopServices()
|
2023-05-31 16:35:20 +00:00
|
|
|
a.shutdownTracing()
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
close(a.webDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func shutdownContext() (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:20 +00:00
|
|
|
func (a *App) configReload(ctx context.Context) {
|
2022-09-12 09:45:54 +00:00
|
|
|
a.log.Info("SIGHUP config reload started")
|
|
|
|
|
2023-02-03 10:00:18 +00:00
|
|
|
if !a.cfg.IsSet(cmdConfig) && !a.cfg.IsSet(cmdConfigDir) {
|
2022-09-12 09:45:54 +00:00
|
|
|
a.log.Warn("failed to reload config because it's missed")
|
|
|
|
return
|
|
|
|
}
|
2023-02-03 10:00:18 +00:00
|
|
|
if err := readInConfig(a.cfg); err != nil {
|
2022-09-12 09:45:54 +00:00
|
|
|
a.log.Warn("failed to reload config", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
if err := a.bucketResolver.UpdateResolvers(a.getResolverConfig()); err != nil {
|
|
|
|
a.log.Warn("failed to reload resolvers", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
if err := a.updateServers(); err != nil {
|
|
|
|
a.log.Warn("failed to reload server parameters", zap.Error(err))
|
2022-09-12 13:46:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.stopServices()
|
|
|
|
a.startServices()
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
a.updateSettings()
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
a.metrics.SetEnabled(a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-05-31 16:35:20 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-12 12:30:51 +00:00
|
|
|
a.setHealthStatus()
|
|
|
|
|
2022-09-12 09:45:54 +00:00
|
|
|
a.log.Info("SIGHUP config reload completed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) updateSettings() {
|
|
|
|
if lvl, err := getLogLevel(a.cfg); err != nil {
|
|
|
|
a.log.Warn("log level won't be updated", zap.Error(err))
|
|
|
|
} else {
|
2022-11-14 09:37:47 +00:00
|
|
|
a.settings.logLevel.SetLevel(lvl)
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
a.settings.policies.update(a.log, a.cfg)
|
2023-03-02 14:54:33 +00:00
|
|
|
|
|
|
|
a.settings.xmlDecoder.UseDefaultNamespaceForCompleteMultipart(a.cfg.GetBool(cfgKludgeUseDefaultXMLNSForCompleteMultipartUpload))
|
2022-09-12 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) startServices() {
|
2022-11-09 10:07:18 +00:00
|
|
|
a.services = a.services[:0]
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
pprofService := NewPprofService(a.cfg, a.log)
|
|
|
|
a.services = append(a.services, pprofService)
|
|
|
|
go pprofService.Start()
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
prometheusService := NewPrometheusService(a.cfg, a.log, a.metrics.Handler())
|
2022-09-12 12:30:51 +00:00
|
|
|
a.services = append(a.services, prometheusService)
|
|
|
|
go prometheusService.Start()
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
func (a *App) initServers(ctx context.Context) {
|
|
|
|
serversInfo := fetchServers(a.cfg)
|
|
|
|
|
2023-01-18 10:07:26 +00:00
|
|
|
a.servers = make([]Server, 0, len(serversInfo))
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
fields := []zap.Field{
|
2022-11-09 10:07:18 +00:00
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
2023-01-18 10:07:26 +00:00
|
|
|
zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile),
|
|
|
|
}
|
|
|
|
srv, err := newServer(ctx, serverInfo)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn("failed to add server", append(fields, zap.Error(err))...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
|
|
|
a.log.Info("add server", fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.servers) == 0 {
|
|
|
|
a.log.Fatal("no healthy servers")
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) updateServers() error {
|
|
|
|
serversInfo := fetchServers(a.cfg)
|
|
|
|
|
2023-01-18 10:07:26 +00:00
|
|
|
var found bool
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
index := a.serverIndex(serverInfo.Address)
|
|
|
|
if index == -1 {
|
|
|
|
continue
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if serverInfo.TLS.Enabled {
|
2023-01-18 10:07:26 +00:00
|
|
|
if err := a.servers[index].UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil {
|
2022-11-09 10:07:18 +00:00
|
|
|
return fmt.Errorf("failed to update tls certs: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 10:07:26 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid servers configuration: no known server found")
|
2022-11-09 10:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:07:26 +00:00
|
|
|
func (a *App) serverIndex(address string) int {
|
|
|
|
for i := range a.servers {
|
|
|
|
if a.servers[i].Address() == address {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-09-12 12:30:51 +00:00
|
|
|
func (a *App) stopServices() {
|
|
|
|
ctx, cancel := shutdownContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for _, svc := range a.services {
|
|
|
|
svc.ShutDown(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 10:05:36 +00:00
|
|
|
func getNotificationsOptions(v *viper.Viper, l *zap.Logger) *notifications.Options {
|
|
|
|
cfg := notifications.Options{}
|
|
|
|
cfg.URL = v.GetString(cfgNATSEndpoint)
|
|
|
|
cfg.Timeout = v.GetDuration(cfgNATSTimeout)
|
|
|
|
if cfg.Timeout <= 0 {
|
|
|
|
l.Error("invalid lifetime, using default value (in seconds)",
|
|
|
|
zap.String("parameter", cfgNATSTimeout),
|
|
|
|
zap.Duration("value in config", cfg.Timeout),
|
|
|
|
zap.Duration("default", notifications.DefaultTimeout))
|
|
|
|
cfg.Timeout = notifications.DefaultTimeout
|
|
|
|
}
|
|
|
|
cfg.TLSCertFilepath = v.GetString(cfgNATSTLSCertFile)
|
|
|
|
cfg.TLSAuthPrivateKeyFilePath = v.GetString(cfgNATSAuthPrivateKeyFile)
|
|
|
|
cfg.RootCAFiles = v.GetStringSlice(cfgNATSRootCAFiles)
|
|
|
|
|
|
|
|
return &cfg
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func getCacheOptions(v *viper.Viper, l *zap.Logger) *layer.CachesConfig {
|
2022-06-06 08:01:12 +00:00
|
|
|
cacheCfg := layer.DefaultCachesConfigs(l)
|
2021-09-10 06:56:56 +00:00
|
|
|
|
|
|
|
cacheCfg.Objects.Lifetime = getLifetime(v, l, cfgObjectsCacheLifetime, cacheCfg.Objects.Lifetime)
|
|
|
|
cacheCfg.Objects.Size = getSize(v, l, cfgObjectsCacheSize, cacheCfg.Objects.Size)
|
|
|
|
|
|
|
|
cacheCfg.ObjectsList.Lifetime = getLifetime(v, l, cfgListObjectsCacheLifetime, cacheCfg.ObjectsList.Lifetime)
|
|
|
|
cacheCfg.ObjectsList.Size = getSize(v, l, cfgListObjectsCacheSize, cacheCfg.ObjectsList.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
cacheCfg.Buckets.Lifetime = getLifetime(v, l, cfgBucketsCacheLifetime, cacheCfg.Buckets.Lifetime)
|
|
|
|
cacheCfg.Buckets.Size = getSize(v, l, cfgBucketsCacheSize, cacheCfg.Buckets.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
cacheCfg.Names.Lifetime = getLifetime(v, l, cfgNamesCacheLifetime, cacheCfg.Names.Lifetime)
|
|
|
|
cacheCfg.Names.Size = getSize(v, l, cfgNamesCacheSize, cacheCfg.Names.Size)
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
cacheCfg.System.Lifetime = getLifetime(v, l, cfgSystemCacheLifetime, cacheCfg.System.Lifetime)
|
2021-09-10 06:56:56 +00:00
|
|
|
cacheCfg.System.Size = getSize(v, l, cfgSystemCacheSize, cacheCfg.System.Size)
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
cacheCfg.AccessControl.Lifetime = getLifetime(v, l, cfgAccessControlCacheLifetime, cacheCfg.AccessControl.Lifetime)
|
|
|
|
cacheCfg.AccessControl.Size = getSize(v, l, cfgAccessControlCacheSize, cacheCfg.AccessControl.Size)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
return cacheCfg
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLifetime(v *viper.Viper, l *zap.Logger, cfgEntry string, defaultValue time.Duration) time.Duration {
|
|
|
|
if v.IsSet(cfgEntry) {
|
|
|
|
lifetime := v.GetDuration(cfgEntry)
|
2021-08-16 09:58:09 +00:00
|
|
|
if lifetime <= 0 {
|
2021-09-08 07:08:56 +00:00
|
|
|
l.Error("invalid lifetime, using default value (in seconds)",
|
|
|
|
zap.String("parameter", cfgEntry),
|
2021-08-16 09:58:09 +00:00
|
|
|
zap.Duration("value in config", lifetime),
|
2021-09-08 07:08:56 +00:00
|
|
|
zap.Duration("default", defaultValue))
|
2021-08-16 09:58:09 +00:00
|
|
|
} else {
|
2021-09-08 07:08:56 +00:00
|
|
|
return lifetime
|
2021-08-16 09:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-08 07:08:56 +00:00
|
|
|
return defaultValue
|
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
|
2021-09-08 07:08:56 +00:00
|
|
|
func getSize(v *viper.Viper, l *zap.Logger, cfgEntry string, defaultValue int) int {
|
|
|
|
if v.IsSet(cfgEntry) {
|
|
|
|
size := v.GetInt(cfgEntry)
|
2021-09-01 16:10:31 +00:00
|
|
|
if size <= 0 {
|
|
|
|
l.Error("invalid cache size, using default value",
|
2021-09-08 07:08:56 +00:00
|
|
|
zap.String("parameter", cfgEntry),
|
2021-09-01 16:10:31 +00:00
|
|
|
zap.Int("value in config", size),
|
2021-09-08 07:08:56 +00:00
|
|
|
zap.Int("default", defaultValue))
|
2021-09-01 16:10:31 +00:00
|
|
|
} else {
|
2021-09-08 07:08:56 +00:00
|
|
|
return size
|
2021-09-01 16:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-08 07:08:56 +00:00
|
|
|
return defaultValue
|
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func getAccessBoxCacheConfig(v *viper.Viper, l *zap.Logger) *cache.Config {
|
2022-06-06 08:01:12 +00:00
|
|
|
cacheCfg := cache.DefaultAccessBoxConfig(l)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
|
|
|
cacheCfg.Lifetime = getLifetime(v, l, cfgAccessBoxCacheLifetime, cacheCfg.Lifetime)
|
|
|
|
cacheCfg.Size = getSize(v, l, cfgAccessBoxCacheSize, cacheCfg.Size)
|
|
|
|
|
|
|
|
return cacheCfg
|
2021-08-16 09:58:09 +00:00
|
|
|
}
|
2021-08-23 18:39:15 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func (a *App) initHandler() {
|
2022-11-03 06:34:18 +00:00
|
|
|
cfg := &handler.Config{
|
2023-05-18 13:20:00 +00:00
|
|
|
Policy: a.settings.policies,
|
|
|
|
DefaultMaxAge: handler.DefaultMaxAge,
|
|
|
|
NotificatorEnabled: a.cfg.GetBool(cfgEnableNATS),
|
|
|
|
XMLDecoder: a.settings.xmlDecoder,
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
if a.cfg.IsSet(cfgDefaultMaxAge) {
|
|
|
|
defaultMaxAge := a.cfg.GetInt(cfgDefaultMaxAge)
|
2021-10-04 14:32:35 +00:00
|
|
|
|
|
|
|
if defaultMaxAge <= 0 && defaultMaxAge != -1 {
|
2022-11-14 09:37:47 +00:00
|
|
|
a.log.Fatal("invalid defaultMaxAge",
|
2021-10-04 14:32:35 +00:00
|
|
|
zap.String("parameter", cfgDefaultMaxAge),
|
|
|
|
zap.String("value in config", strconv.Itoa(defaultMaxAge)))
|
|
|
|
}
|
2022-11-03 06:34:18 +00:00
|
|
|
cfg.DefaultMaxAge = defaultMaxAge
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 12:19:29 +00:00
|
|
|
cfg.ResolveZoneList = a.cfg.GetStringSlice(cfgResolveBucketAllow)
|
|
|
|
cfg.IsResolveListAllow = len(cfg.ResolveZoneList) > 0
|
|
|
|
if !cfg.IsResolveListAllow {
|
|
|
|
cfg.ResolveZoneList = a.cfg.GetStringSlice(cfgResolveBucketDeny)
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:44:35 +00:00
|
|
|
cfg.CompleteMultipartKeepalive = a.cfg.GetDuration(cfgKludgeCompleteMultipartUploadKeepalive)
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
var err error
|
|
|
|
a.api, err = handler.New(a.log, a.obj, a.nc, cfg)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("could not initialize API handler", zap.Error(err))
|
|
|
|
}
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
2021-10-04 14:32:35 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
func readRegionMap(filePath string) (map[string]string, error) {
|
2022-11-03 06:34:18 +00:00
|
|
|
regionMap := make(map[string]string)
|
|
|
|
|
|
|
|
if filePath == "" {
|
|
|
|
return regionMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("coudln't read file '%s'", filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(data, ®ionMap); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal policies: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := regionMap[api.DefaultLocationConstraint]; ok {
|
|
|
|
return nil, fmt.Errorf("config overrides %s location constraint", api.DefaultLocationConstraint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return regionMap, nil
|
2021-08-23 18:39:15 +00:00
|
|
|
}
|