2020-03-31 08:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-25 16:01:58 +00:00
|
|
|
"bytes"
|
2020-03-31 08:37:10 +00:00
|
|
|
"context"
|
2024-09-10 07:09:51 +00:00
|
|
|
"crypto/x509"
|
2024-03-26 11:34:20 +00:00
|
|
|
"errors"
|
2021-06-22 11:53:59 +00:00
|
|
|
"fmt"
|
2022-11-24 14:49:21 +00:00
|
|
|
"net/http"
|
2022-09-08 14:57:22 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-08-31 16:19:57 +00:00
|
|
|
"runtime/debug"
|
2023-11-07 08:00:38 +00:00
|
|
|
"strconv"
|
2024-10-24 14:27:32 +00:00
|
|
|
"strings"
|
2022-09-08 14:57:22 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2023-03-15 08:07:44 +00:00
|
|
|
"time"
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
2023-10-04 11:50:37 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/cache"
|
2023-08-31 08:37:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler"
|
2023-11-28 08:29:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler/middleware"
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2024-10-29 11:41:49 +00:00
|
|
|
internalnet "git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/net"
|
2024-09-18 04:35:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/service/frostfs"
|
2024-09-26 14:32:27 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/templates"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/metrics"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
2023-05-30 14:01:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tokens"
|
2023-05-05 08:19:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tree"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
2023-06-09 06:39:01 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
2023-06-09 06:39:01 +00:00
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/fasthttp/router"
|
2021-06-22 11:53:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2022-04-07 12:56:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-06-22 11:53:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
2024-10-08 09:08:39 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-03-31 08:37:10 +00:00
|
|
|
"go.uber.org/zap"
|
2023-11-28 08:29:08 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
app struct {
|
2023-05-30 14:01:20 +00:00
|
|
|
ctx context.Context
|
2022-03-25 13:06:33 +00:00
|
|
|
log *zap.Logger
|
2022-09-08 14:57:22 +00:00
|
|
|
logLevel zap.AtomicLevel
|
2022-03-25 13:06:33 +00:00
|
|
|
pool *pool.Pool
|
2023-06-09 06:39:01 +00:00
|
|
|
treePool *treepool.Pool
|
2023-05-05 08:19:35 +00:00
|
|
|
key *keys.PrivateKey
|
2022-07-25 09:47:48 +00:00
|
|
|
owner *user.ID
|
2022-03-25 13:06:33 +00:00
|
|
|
cfg *viper.Viper
|
|
|
|
webServer *fasthttp.Server
|
|
|
|
webDone chan struct{}
|
2022-04-20 09:17:20 +00:00
|
|
|
resolver *resolver.ContainerResolver
|
2022-09-08 14:57:22 +00:00
|
|
|
metrics *gateMetrics
|
|
|
|
services []*metrics.Service
|
2023-09-05 15:17:22 +00:00
|
|
|
settings *appSettings
|
2024-03-26 11:34:20 +00:00
|
|
|
|
|
|
|
servers []Server
|
|
|
|
unbindServers []ServerInfo
|
|
|
|
mu sync.RWMutex
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// App is an interface for the main gateway function.
|
2020-03-31 08:37:10 +00:00
|
|
|
App interface {
|
|
|
|
Wait()
|
2023-05-30 14:01:20 +00:00
|
|
|
Serve()
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Option is an application option.
|
2020-03-31 08:37:10 +00:00
|
|
|
Option func(a *app)
|
2022-07-22 08:30:57 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
gateMetrics struct {
|
|
|
|
logger *zap.Logger
|
2023-04-17 13:28:27 +00:00
|
|
|
provider *metrics.GateMetrics
|
2022-09-08 14:57:22 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
}
|
2023-09-05 15:17:22 +00:00
|
|
|
|
|
|
|
// appSettings stores reloading parameters, so it has to provide getters and setters which use RWMutex.
|
|
|
|
appSettings struct {
|
2024-03-26 11:34:20 +00:00
|
|
|
reconnectInterval time.Duration
|
2024-10-29 11:41:49 +00:00
|
|
|
dialerSource *internalnet.DialerSource
|
2024-03-26 11:34:20 +00:00
|
|
|
|
2024-10-24 14:27:32 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
defaultTimestamp bool
|
|
|
|
zipCompression bool
|
|
|
|
clientCut bool
|
|
|
|
returnIndexPage bool
|
|
|
|
indexPageTemplate string
|
|
|
|
bufferMaxSizeForPut uint64
|
|
|
|
namespaceHeader string
|
|
|
|
defaultNamespaces []string
|
|
|
|
corsAllowOrigin string
|
|
|
|
corsAllowMethods []string
|
|
|
|
corsAllowHeaders []string
|
|
|
|
corsExposeHeaders []string
|
|
|
|
corsAllowCredentials bool
|
|
|
|
corsMaxAge int
|
|
|
|
}
|
|
|
|
|
|
|
|
CORS struct {
|
|
|
|
AllowOrigin string
|
|
|
|
AllowMethods []string
|
|
|
|
AllowHeaders []string
|
|
|
|
ExposeHeaders []string
|
|
|
|
AllowCredentials bool
|
|
|
|
MaxAge int
|
2023-09-05 15:17:22 +00:00
|
|
|
}
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// WithLogger returns Option to set a specific logger.
|
2022-09-08 14:57:22 +00:00
|
|
|
func WithLogger(l *zap.Logger, lvl zap.AtomicLevel) Option {
|
2020-03-31 08:37:10 +00:00
|
|
|
return func(a *app) {
|
|
|
|
if l == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.log = l
|
2022-09-08 14:57:22 +00:00
|
|
|
a.logLevel = lvl
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// WithConfig returns Option to use specific Viper configuration.
|
2020-03-31 08:37:10 +00:00
|
|
|
func WithConfig(c *viper.Viper) Option {
|
|
|
|
return func(a *app) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.cfg = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
func newApp(ctx context.Context, opt ...Option) App {
|
2020-03-31 08:37:10 +00:00
|
|
|
a := &app{
|
2023-05-30 14:01:20 +00:00
|
|
|
ctx: ctx,
|
2021-03-31 19:08:39 +00:00
|
|
|
log: zap.L(),
|
|
|
|
cfg: viper.GetViper(),
|
|
|
|
webServer: new(fasthttp.Server),
|
|
|
|
webDone: make(chan struct{}),
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
for i := range opt {
|
|
|
|
opt[i](a)
|
|
|
|
}
|
2022-03-25 13:06:33 +00:00
|
|
|
|
2024-10-29 11:41:49 +00:00
|
|
|
a.initAppSettings()
|
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- setup FastHTTP server --
|
2022-12-16 08:45:27 +00:00
|
|
|
a.webServer.Name = "frost-http-gw"
|
2021-03-31 19:08:39 +00:00
|
|
|
a.webServer.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
|
|
|
a.webServer.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
|
|
|
a.webServer.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
|
|
|
a.webServer.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
|
|
|
a.webServer.DisableHeaderNamesNormalizing = true
|
|
|
|
a.webServer.NoDefaultServerHeader = true
|
|
|
|
a.webServer.NoDefaultContentType = true
|
|
|
|
a.webServer.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
|
|
|
a.webServer.DisablePreParseMultipartForm = true
|
|
|
|
a.webServer.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-10-29 11:41:49 +00:00
|
|
|
a.pool, a.treePool, a.key = getPools(ctx, a.log, a.cfg, a.settings.dialerSource)
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
var owner user.ID
|
2023-05-05 08:19:35 +00:00
|
|
|
user.IDFromKey(&owner, a.key.PrivateKey.PublicKey)
|
2022-07-25 09:47:48 +00:00
|
|
|
a.owner = &owner
|
|
|
|
|
2023-08-31 16:19:57 +00:00
|
|
|
a.setRuntimeParameters()
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
a.initResolver()
|
|
|
|
a.initMetrics()
|
2023-03-15 08:07:44 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-08 16:00:22 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2024-10-24 13:17:28 +00:00
|
|
|
func (a *app) initAppSettings() {
|
|
|
|
a.settings = &appSettings{
|
|
|
|
reconnectInterval: fetchReconnectInterval(a.cfg),
|
2024-10-29 11:41:49 +00:00
|
|
|
dialerSource: getDialerSource(a.log, a.cfg),
|
2024-10-24 13:17:28 +00:00
|
|
|
}
|
|
|
|
a.settings.update(a.cfg, a.log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) update(v *viper.Viper, l *zap.Logger) {
|
|
|
|
defaultTimestamp := v.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
|
|
|
zipCompression := v.GetBool(cfgZipCompression)
|
|
|
|
returnIndexPage := v.GetBool(cfgIndexPageEnabled)
|
|
|
|
clientCut := v.GetBool(cfgClientCut)
|
|
|
|
bufferMaxSizeForPut := v.GetUint64(cfgBufferMaxSizeForPut)
|
|
|
|
namespaceHeader := v.GetString(cfgResolveNamespaceHeader)
|
|
|
|
defaultNamespaces := fetchDefaultNamespaces(v)
|
|
|
|
indexPage, indexEnabled := fetchIndexPageTemplate(v, l)
|
2024-10-24 14:27:32 +00:00
|
|
|
corsAllowOrigin := v.GetString(cfgCORSAllowOrigin)
|
|
|
|
corsAllowMethods := v.GetStringSlice(cfgCORSAllowMethods)
|
|
|
|
corsAllowHeaders := v.GetStringSlice(cfgCORSAllowHeaders)
|
|
|
|
corsExposeHeaders := v.GetStringSlice(cfgCORSExposeHeaders)
|
|
|
|
corsAllowCredentials := v.GetBool(cfgCORSAllowCredentials)
|
|
|
|
corsMaxAge := fetchCORSMaxAge(v)
|
2024-10-24 13:17:28 +00:00
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.defaultTimestamp = defaultTimestamp
|
|
|
|
s.zipCompression = zipCompression
|
|
|
|
s.returnIndexPage = returnIndexPage
|
|
|
|
s.clientCut = clientCut
|
|
|
|
s.bufferMaxSizeForPut = bufferMaxSizeForPut
|
|
|
|
s.namespaceHeader = namespaceHeader
|
|
|
|
s.defaultNamespaces = defaultNamespaces
|
|
|
|
s.returnIndexPage = indexEnabled
|
|
|
|
s.indexPageTemplate = indexPage
|
2024-10-24 14:27:32 +00:00
|
|
|
s.corsAllowOrigin = corsAllowOrigin
|
|
|
|
s.corsAllowMethods = corsAllowMethods
|
|
|
|
s.corsAllowHeaders = corsAllowHeaders
|
|
|
|
s.corsExposeHeaders = corsExposeHeaders
|
|
|
|
s.corsAllowCredentials = corsAllowCredentials
|
|
|
|
s.corsMaxAge = corsMaxAge
|
2024-10-24 13:17:28 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 15:17:22 +00:00
|
|
|
func (s *appSettings) DefaultTimestamp() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.defaultTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) ZipCompression() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.zipCompression
|
|
|
|
}
|
|
|
|
|
2024-09-26 14:32:27 +00:00
|
|
|
func (s *appSettings) IndexPageEnabled() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.returnIndexPage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appSettings) IndexPageTemplate() string {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
if s.indexPageTemplate == "" {
|
|
|
|
return templates.DefaultIndexTemplate
|
|
|
|
}
|
|
|
|
return s.indexPageTemplate
|
|
|
|
}
|
|
|
|
|
2024-10-24 14:27:32 +00:00
|
|
|
func (s *appSettings) CORS() CORS {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
|
|
|
allowMethods := make([]string, len(s.corsAllowMethods))
|
|
|
|
copy(allowMethods, s.corsAllowMethods)
|
|
|
|
|
|
|
|
allowHeaders := make([]string, len(s.corsAllowHeaders))
|
|
|
|
copy(allowHeaders, s.corsAllowHeaders)
|
|
|
|
|
|
|
|
exposeHeaders := make([]string, len(s.corsExposeHeaders))
|
|
|
|
copy(exposeHeaders, s.corsExposeHeaders)
|
|
|
|
|
|
|
|
return CORS{
|
|
|
|
AllowOrigin: s.corsAllowOrigin,
|
|
|
|
AllowMethods: allowMethods,
|
|
|
|
AllowHeaders: allowHeaders,
|
|
|
|
ExposeHeaders: exposeHeaders,
|
|
|
|
AllowCredentials: s.corsAllowCredentials,
|
|
|
|
MaxAge: s.corsMaxAge,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:50:23 +00:00
|
|
|
func (s *appSettings) ClientCut() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.clientCut
|
|
|
|
}
|
|
|
|
|
2023-08-25 11:53:59 +00:00
|
|
|
func (s *appSettings) BufferMaxSizeForPut() uint64 {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.bufferMaxSizeForPut
|
|
|
|
}
|
|
|
|
|
2024-10-24 13:17:28 +00:00
|
|
|
func (s *appSettings) NamespaceHeader() string {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.namespaceHeader
|
2023-08-25 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:17:28 +00:00
|
|
|
func (s *appSettings) FormContainerZone(ns string) (zone string, isDefault bool) {
|
|
|
|
s.mu.RLock()
|
|
|
|
namespaces := s.defaultNamespaces
|
|
|
|
s.mu.RUnlock()
|
|
|
|
if slices.Contains(namespaces, ns) {
|
|
|
|
return v2container.SysAttributeZoneDefault, true
|
2024-03-26 11:34:20 +00:00
|
|
|
}
|
2024-10-24 13:17:28 +00:00
|
|
|
|
|
|
|
return ns + ".ns", false
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func (a *app) initResolver() {
|
|
|
|
var err error
|
|
|
|
a.resolver, err = resolver.NewContainerResolver(a.getResolverConfig())
|
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Fatal(logs.FailedToCreateResolver, zap.Error(err))
|
2022-09-08 16:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) getResolverConfig() ([]string, *resolver.Config) {
|
2022-04-20 09:17:20 +00:00
|
|
|
resolveCfg := &resolver.Config{
|
2024-07-19 14:55:34 +00:00
|
|
|
FrostFS: frostfs.NewResolverFrostFS(a.pool),
|
2022-04-20 09:17:20 +00:00
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
2023-11-28 08:29:08 +00:00
|
|
|
Settings: a.settings,
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
order := a.cfg.GetStringSlice(cfgResolveOrder)
|
|
|
|
if resolveCfg.RPCAddress == "" {
|
|
|
|
order = remove(order, resolver.NNSResolver)
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.ResolverNNSWontBeUsedSinceRPCEndpointIsntProvided)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
if len(order) == 0 {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.ContainerResolverWillBeDisabledBecauseOfResolversResolverOrderIsEmpty)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
return order, resolveCfg
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) initMetrics() {
|
|
|
|
gateMetricsProvider := metrics.NewGateMetrics(a.pool)
|
|
|
|
a.metrics = newGateMetrics(a.log, gateMetricsProvider, a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-04-17 13:28:27 +00:00
|
|
|
a.metrics.SetHealth(metrics.HealthStatusStarting)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:28:27 +00:00
|
|
|
func newGateMetrics(logger *zap.Logger, provider *metrics.GateMetrics, enabled bool) *gateMetrics {
|
2022-09-08 14:57:22 +00:00
|
|
|
if !enabled {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Warn(logs.MetricsAreDisabled)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
return &gateMetrics{
|
|
|
|
logger: logger,
|
|
|
|
provider: provider,
|
2023-04-26 06:51:37 +00:00
|
|
|
enabled: enabled,
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:17:20 +00:00
|
|
|
func (m *gateMetrics) isEnabled() bool {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
return m.enabled
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (m *gateMetrics) SetEnabled(enabled bool) {
|
|
|
|
if !enabled {
|
2023-08-27 15:09:02 +00:00
|
|
|
m.logger.Warn(logs.MetricsAreDisabled)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
m.enabled = enabled
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-04-17 13:28:27 +00:00
|
|
|
func (m *gateMetrics) SetHealth(status metrics.HealthStatus) {
|
2023-08-29 12:17:20 +00:00
|
|
|
if !m.isEnabled() {
|
2022-09-08 14:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.provider.SetHealth(status)
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:14:31 +00:00
|
|
|
func (m *gateMetrics) SetVersion(ver string) {
|
2023-08-29 12:17:20 +00:00
|
|
|
if !m.isEnabled() {
|
2023-04-07 15:14:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.provider.SetVersion(ver)
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:57:48 +00:00
|
|
|
func (m *gateMetrics) Shutdown() {
|
|
|
|
m.mu.Lock()
|
|
|
|
if m.enabled {
|
2023-04-17 13:28:27 +00:00
|
|
|
m.provider.SetHealth(metrics.HealthStatusShuttingDown)
|
2022-09-09 06:57:48 +00:00
|
|
|
m.enabled = false
|
|
|
|
}
|
|
|
|
m.provider.Unregister()
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:17:20 +00:00
|
|
|
func (m *gateMetrics) MarkHealthy(endpoint string) {
|
|
|
|
if !m.isEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.provider.MarkHealthy(endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *gateMetrics) MarkUnhealthy(endpoint string) {
|
|
|
|
if !m.isEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.provider.MarkUnhealthy(endpoint)
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +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
|
|
|
|
}
|
|
|
|
|
2023-06-09 06:39:01 +00:00
|
|
|
func getFrostFSKey(cfg *viper.Viper, log *zap.Logger) (*keys.PrivateKey, error) {
|
|
|
|
walletPath := cfg.GetString(cfgWalletPath)
|
2022-04-08 09:01:57 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
if len(walletPath) == 0 {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Info(logs.NoWalletPathSpecifiedCreatingEphemeralKeyAutomaticallyForThisRun)
|
2022-04-07 12:56:18 +00:00
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-05 08:19:35 +00:00
|
|
|
return key, nil
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
w, err := wallet.NewWalletFromFile(walletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var password *string
|
2023-06-09 06:39:01 +00:00
|
|
|
if cfg.IsSet(cfgWalletPassphrase) {
|
|
|
|
pwd := cfg.GetString(cfgWalletPassphrase)
|
2021-06-22 11:53:59 +00:00
|
|
|
password = &pwd
|
|
|
|
}
|
2022-04-08 09:01:57 +00:00
|
|
|
|
2023-06-09 06:39:01 +00:00
|
|
|
address := cfg.GetString(cfgWalletAddress)
|
2022-04-08 09:01:57 +00:00
|
|
|
|
|
|
|
return getKeyFromWallet(w, address, password)
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
func getKeyFromWallet(w *wallet.Wallet, addrStr string, password *string) (*keys.PrivateKey, error) {
|
2021-06-22 11:53:59 +00:00
|
|
|
var addr util.Uint160
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if addrStr == "" {
|
|
|
|
addr = w.GetChangeAddress()
|
|
|
|
} else {
|
|
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return nil, fmt.Errorf("couldn't find wallet account for %s", addrStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if password == nil {
|
|
|
|
pwd, err := input.ReadPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't read password")
|
|
|
|
}
|
|
|
|
password = &pwd
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.Decrypt(*password, w.Scrypt); err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't decrypt account: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
return acc.PrivateKey(), nil
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
func (a *app) Wait() {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.StartingApplication, zap.String("app_name", "frostfs-http-gw"), zap.String("version", Version))
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2023-04-07 15:14:31 +00:00
|
|
|
a.metrics.SetVersion(Version)
|
2022-09-08 14:57:22 +00:00
|
|
|
a.setHealthStatus()
|
2022-07-22 08:30:57 +00:00
|
|
|
|
2021-04-15 14:16:44 +00:00
|
|
|
<-a.webDone // wait for web-server to be stopped
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) setHealthStatus() {
|
2023-04-17 13:28:27 +00:00
|
|
|
a.metrics.SetHealth(metrics.HealthStatusReady)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:01:20 +00:00
|
|
|
func (a *app) Serve() {
|
2024-09-18 04:35:26 +00:00
|
|
|
handler := handler.New(a.AppParams(), a.settings, tree.NewTree(frostfs.NewPoolWrapper(a.treePool)))
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// Configure router.
|
2023-08-31 08:37:03 +00:00
|
|
|
a.configureRouter(handler)
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.startServices()
|
2023-05-30 14:01:20 +00:00
|
|
|
a.initServers(a.ctx)
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2024-03-26 11:34:20 +00:00
|
|
|
servs := a.getServers()
|
|
|
|
|
|
|
|
for i := range servs {
|
2022-11-24 14:49:21 +00:00
|
|
|
go func(i int) {
|
2024-03-26 11:34:20 +00:00
|
|
|
a.log.Info(logs.StartingServer, zap.String("address", servs[i].Address()))
|
|
|
|
if err := a.webServer.Serve(servs[i].Listener()); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.metrics.MarkUnhealthy(servs[i].Address())
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Fatal(logs.ListenAndServe, zap.Error(err))
|
2022-09-09 16:00:04 +00:00
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
}(i)
|
|
|
|
}
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2024-03-26 11:34:20 +00:00
|
|
|
if len(a.unbindServers) != 0 {
|
|
|
|
a.scheduleReconnect(a.ctx, a.webServer)
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGHUP)
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
2023-05-30 14:01:20 +00:00
|
|
|
case <-a.ctx.Done():
|
2022-09-08 14:57:22 +00:00
|
|
|
break LOOP
|
|
|
|
case <-sigs:
|
2023-05-30 14:01:20 +00:00
|
|
|
a.configReload(a.ctx)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.ShuttingDownWebServer, zap.Error(a.webServer.Shutdown()))
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-09-09 06:57:48 +00:00
|
|
|
a.metrics.Shutdown()
|
2022-09-08 14:57:22 +00:00
|
|
|
a.stopServices()
|
2023-03-15 08:07:44 +00:00
|
|
|
a.shutdownTracing()
|
2022-09-08 14:57:22 +00:00
|
|
|
|
|
|
|
close(a.webDone)
|
|
|
|
}
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
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 {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToShutdownTracing, zap.Error(err))
|
2023-03-15 08:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) configReload(ctx context.Context) {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.SIGHUPConfigReloadStarted)
|
2023-02-03 09:46:25 +00:00
|
|
|
if !a.cfg.IsSet(cmdConfig) && !a.cfg.IsSet(cmdConfigDir) {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadConfigBecauseItsMissed)
|
2022-09-08 14:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
if err := readInConfig(a.cfg); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadConfig, zap.Error(err))
|
2022-09-08 14:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
if lvl, err := getLogLevel(a.cfg); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.LogLevelWontBeUpdated, zap.Error(err))
|
2022-09-08 14:57:22 +00:00
|
|
|
} else {
|
|
|
|
a.logLevel.SetLevel(lvl)
|
|
|
|
}
|
|
|
|
|
2024-10-29 11:41:49 +00:00
|
|
|
if err := a.settings.dialerSource.Update(fetchMultinetConfig(a.cfg, a.log)); err != nil {
|
|
|
|
a.log.Warn(logs.MultinetConfigWontBeUpdated, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
if err := a.resolver.UpdateResolvers(a.getResolverConfig()); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToUpdateResolvers, zap.Error(err))
|
2022-09-08 16:00:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if err := a.updateServers(); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToReloadServerParameters, zap.Error(err))
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:19:57 +00:00
|
|
|
a.setRuntimeParameters()
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.stopServices()
|
|
|
|
a.startServices()
|
|
|
|
|
2024-10-24 13:17:28 +00:00
|
|
|
a.settings.update(a.cfg, a.log)
|
2022-09-09 06:33:31 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.metrics.SetEnabled(a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-03-15 08:07:44 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-08 14:57:22 +00:00
|
|
|
a.setHealthStatus()
|
2022-09-09 06:33:31 +00:00
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.SIGHUPConfigReloadCompleted)
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) startServices() {
|
|
|
|
pprofConfig := metrics.Config{Enabled: a.cfg.GetBool(cfgPprofEnabled), Address: a.cfg.GetString(cfgPprofAddress)}
|
|
|
|
pprofService := metrics.NewPprofService(a.log, pprofConfig)
|
|
|
|
a.services = append(a.services, pprofService)
|
|
|
|
go pprofService.Start()
|
|
|
|
|
|
|
|
prometheusConfig := metrics.Config{Enabled: a.cfg.GetBool(cfgPrometheusEnabled), Address: a.cfg.GetString(cfgPrometheusAddress)}
|
|
|
|
prometheusService := metrics.NewPrometheusService(a.log, prometheusConfig)
|
|
|
|
a.services = append(a.services, prometheusService)
|
|
|
|
go prometheusService.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) stopServices() {
|
2022-07-27 06:52:08 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
for _, svc := range a.services {
|
|
|
|
svc.ShutDown(ctx)
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
func (a *app) configureRouter(handler *handler.Handler) {
|
2022-09-08 14:57:22 +00:00
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
|
|
|
r.NotFound = func(r *fasthttp.RequestCtx) {
|
|
|
|
response.Error(r, "Not found", fasthttp.StatusNotFound)
|
|
|
|
}
|
|
|
|
r.MethodNotAllowed = func(r *fasthttp.RequestCtx) {
|
|
|
|
response.Error(r, "Method Not Allowed", fasthttp.StatusMethodNotAllowed)
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-10-24 14:27:32 +00:00
|
|
|
r.POST("/upload/{cid}", a.addMiddlewares(handler.Upload))
|
|
|
|
r.OPTIONS("/upload/{cid}", a.addPreflight())
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.AddedPathUploadCid)
|
2024-10-24 14:27:32 +00:00
|
|
|
r.GET("/get/{cid}/{oid:*}", a.addMiddlewares(handler.DownloadByAddressOrBucketName))
|
|
|
|
r.HEAD("/get/{cid}/{oid:*}", a.addMiddlewares(handler.HeadByAddressOrBucketName))
|
|
|
|
r.OPTIONS("/get/{cid}/{oid:*}", a.addPreflight())
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.AddedPathGetCidOid)
|
2024-10-24 14:27:32 +00:00
|
|
|
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.addMiddlewares(handler.DownloadByAttribute))
|
|
|
|
r.HEAD("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.addMiddlewares(handler.HeadByAttribute))
|
|
|
|
r.OPTIONS("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.addPreflight())
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.AddedPathGetByAttributeCidAttrKeyAttrVal)
|
2024-10-24 14:27:32 +00:00
|
|
|
r.GET("/zip/{cid}/{prefix:*}", a.addMiddlewares(handler.DownloadZipped))
|
|
|
|
r.OPTIONS("/zip/{cid}/{prefix:*}", a.addPreflight())
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.AddedPathZipCidPrefix)
|
2022-09-08 14:57:22 +00:00
|
|
|
|
|
|
|
a.webServer.Handler = r.Handler
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
2021-07-09 09:49:08 +00:00
|
|
|
|
2024-10-24 14:27:32 +00:00
|
|
|
func (a *app) addMiddlewares(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
list := []func(fasthttp.RequestHandler) fasthttp.RequestHandler{
|
|
|
|
a.tracer,
|
|
|
|
a.logger,
|
|
|
|
a.canonicalizer,
|
|
|
|
a.tokenizer,
|
|
|
|
a.reqNamespace,
|
|
|
|
a.cors,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(list) - 1; i >= 0; i-- {
|
|
|
|
h = list[i](h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) addPreflight() fasthttp.RequestHandler {
|
|
|
|
list := []func(fasthttp.RequestHandler) fasthttp.RequestHandler{
|
|
|
|
a.tracer,
|
|
|
|
a.logger,
|
|
|
|
a.reqNamespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
h := a.preflightHandler
|
|
|
|
for i := len(list) - 1; i >= 0; i-- {
|
|
|
|
h = list[i](h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) preflightHandler(c *fasthttp.RequestCtx) {
|
|
|
|
cors := a.settings.CORS()
|
|
|
|
setCORSHeaders(c, cors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) cors(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(c *fasthttp.RequestCtx) {
|
|
|
|
h(c)
|
|
|
|
code := c.Response.StatusCode()
|
|
|
|
if code >= fasthttp.StatusOK && code < fasthttp.StatusMultipleChoices {
|
|
|
|
cors := a.settings.CORS()
|
|
|
|
setCORSHeaders(c, cors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCORSHeaders(c *fasthttp.RequestCtx, cors CORS) {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlMaxAge, strconv.Itoa(cors.MaxAge))
|
|
|
|
|
|
|
|
if len(cors.AllowOrigin) != 0 {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlAllowOrigin, cors.AllowOrigin)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cors.AllowMethods) != 0 {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlAllowMethods, strings.Join(cors.AllowMethods, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cors.AllowHeaders) != 0 {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlAllowHeaders, strings.Join(cors.AllowHeaders, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cors.ExposeHeaders) != 0 {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlExposeHeaders, strings.Join(cors.ExposeHeaders, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cors.AllowCredentials {
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderAccessControlAllowCredentials, "true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 13:34:04 +00:00
|
|
|
func (a *app) logger(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(req *fasthttp.RequestCtx) {
|
2024-10-08 09:08:39 +00:00
|
|
|
requiredFields := []zap.Field{zap.Uint64("id", req.ID())}
|
|
|
|
reqCtx := utils.GetContextFromRequest(req)
|
|
|
|
if traceID := trace.SpanFromContext(reqCtx).SpanContext().TraceID(); traceID.IsValid() {
|
|
|
|
requiredFields = append(requiredFields, zap.String("trace_id", traceID.String()))
|
|
|
|
}
|
|
|
|
log := a.log.With(requiredFields...)
|
|
|
|
|
|
|
|
reqCtx = utils.SetReqLog(reqCtx, log)
|
|
|
|
utils.SetContextToRequest(reqCtx, req)
|
|
|
|
|
|
|
|
fields := []zap.Field{
|
|
|
|
zap.String("remote", req.RemoteAddr().String()),
|
2023-05-31 13:34:04 +00:00
|
|
|
zap.ByteString("method", req.Method()),
|
|
|
|
zap.ByteString("path", req.Path()),
|
|
|
|
zap.ByteString("query", req.QueryArgs().QueryString()),
|
2024-10-08 09:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info(logs.Request, fields...)
|
2023-05-31 13:34:04 +00:00
|
|
|
h(req)
|
2023-05-30 14:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 16:01:58 +00:00
|
|
|
func (a *app) canonicalizer(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(req *fasthttp.RequestCtx) {
|
|
|
|
// regardless of DisableHeaderNamesNormalizing setting, some headers
|
|
|
|
// MUST be normalized in order to process execution. They are normalized
|
|
|
|
// here.
|
|
|
|
|
|
|
|
toAddKeys := make([][]byte, 0, 10)
|
|
|
|
toAddValues := make([][]byte, 0, 10)
|
|
|
|
prefix := []byte(utils.UserAttributeHeaderPrefix)
|
|
|
|
|
|
|
|
req.Request.Header.VisitAll(func(k, v []byte) {
|
|
|
|
if bytes.HasPrefix(k, prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toAddKeys = append(toAddKeys, k)
|
|
|
|
toAddValues = append(toAddValues, v)
|
|
|
|
})
|
|
|
|
|
|
|
|
// this is safe to do after all headers were read into header structure
|
|
|
|
req.Request.Header.EnableNormalizing()
|
|
|
|
|
|
|
|
for i := range toAddKeys {
|
|
|
|
req.Request.Header.SetBytesKV(toAddKeys[i], toAddValues[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// return normalization setting back
|
|
|
|
req.Request.Header.DisableNormalizing()
|
|
|
|
|
|
|
|
h(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 13:34:04 +00:00
|
|
|
func (a *app) tokenizer(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(req *fasthttp.RequestCtx) {
|
2024-10-08 09:08:39 +00:00
|
|
|
reqCtx := utils.GetContextFromRequest(req)
|
|
|
|
appCtx, err := tokens.StoreBearerTokenAppCtx(reqCtx, req)
|
2023-05-30 14:01:20 +00:00
|
|
|
if err != nil {
|
2024-10-08 09:08:39 +00:00
|
|
|
log := utils.GetReqLogOrDefault(reqCtx, a.log)
|
|
|
|
|
|
|
|
log.Error(logs.CouldNotFetchAndStoreBearerToken, zap.Error(err))
|
2023-05-31 13:34:04 +00:00
|
|
|
response.Error(req, "could not fetch and store bearer token: "+err.Error(), fasthttp.StatusBadRequest)
|
2024-02-29 09:17:22 +00:00
|
|
|
return
|
2023-05-30 14:01:20 +00:00
|
|
|
}
|
2023-05-31 13:34:04 +00:00
|
|
|
utils.SetContextToRequest(appCtx, req)
|
|
|
|
h(req)
|
2023-05-30 14:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 13:34:04 +00:00
|
|
|
func (a *app) tracer(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(req *fasthttp.RequestCtx) {
|
2024-10-08 09:08:39 +00:00
|
|
|
appCtx, span := utils.StartHTTPServerSpan(a.ctx, req, "REQUEST")
|
2023-05-30 14:01:20 +00:00
|
|
|
defer func() {
|
2023-05-31 13:34:04 +00:00
|
|
|
utils.SetHTTPTraceInfo(appCtx, span, req)
|
2023-05-30 14:01:20 +00:00
|
|
|
span.End()
|
|
|
|
}()
|
|
|
|
|
2023-11-07 08:00:38 +00:00
|
|
|
appCtx = treepool.SetRequestID(appCtx, strconv.FormatUint(req.ID(), 10))
|
|
|
|
|
2023-05-31 13:34:04 +00:00
|
|
|
utils.SetContextToRequest(appCtx, req)
|
|
|
|
h(req)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
2021-07-09 09:49:08 +00:00
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
func (a *app) reqNamespace(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(req *fasthttp.RequestCtx) {
|
|
|
|
appCtx := utils.GetContextFromRequest(req)
|
|
|
|
|
|
|
|
nsBytes := req.Request.Header.Peek(a.settings.NamespaceHeader())
|
|
|
|
appCtx = middleware.SetNamespace(appCtx, string(nsBytes))
|
|
|
|
|
|
|
|
utils.SetContextToRequest(appCtx, req)
|
|
|
|
h(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:56:43 +00:00
|
|
|
func (a *app) AppParams() *handler.AppParams {
|
|
|
|
return &handler.AppParams{
|
2022-04-20 09:17:20 +00:00
|
|
|
Logger: a.log,
|
2024-06-17 14:56:43 +00:00
|
|
|
FrostFS: frostfs.NewFrostFS(a.pool),
|
2022-07-25 09:47:48 +00:00
|
|
|
Owner: a.owner,
|
2022-04-20 09:17:20 +00:00
|
|
|
Resolver: a.resolver,
|
2023-10-04 11:50:37 +00:00
|
|
|
Cache: cache.NewBucketCache(getCacheOptions(a.cfg, a.log)),
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
|
|
|
|
func (a *app) initServers(ctx context.Context) {
|
2024-03-26 11:34:20 +00:00
|
|
|
serversInfo := fetchServers(a.cfg, a.log)
|
2022-11-24 14:49:21 +00:00
|
|
|
|
2023-01-18 09:44:44 +00:00
|
|
|
a.servers = make([]Server, 0, len(serversInfo))
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
fields := []zap.Field{
|
2022-11-24 14:49:21 +00:00
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
2023-01-18 09:44:44 +00:00
|
|
|
zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile),
|
|
|
|
}
|
|
|
|
srv, err := newServer(ctx, serverInfo)
|
|
|
|
if err != nil {
|
2024-03-26 11:34:20 +00:00
|
|
|
a.unbindServers = append(a.unbindServers, serverInfo)
|
2023-08-29 12:17:20 +00:00
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToAddServer, append(fields, zap.Error(err))...)
|
2023-01-18 09:44:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-08-29 12:17:20 +00:00
|
|
|
a.metrics.MarkHealthy(serverInfo.Address)
|
2023-01-18 09:44:44 +00:00
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.AddServer, fields...)
|
2023-01-18 09:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.servers) == 0 {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Fatal(logs.NoHealthyServers)
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) updateServers() error {
|
2024-03-26 11:34:20 +00:00
|
|
|
serversInfo := fetchServers(a.cfg, a.log)
|
|
|
|
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2022-11-24 14:49:21 +00:00
|
|
|
|
2023-01-18 09:44:44 +00:00
|
|
|
var found bool
|
|
|
|
for _, serverInfo := range serversInfo {
|
2024-03-26 11:34:20 +00:00
|
|
|
ser := a.getServer(serverInfo.Address)
|
|
|
|
if ser != nil {
|
|
|
|
if serverInfo.TLS.Enabled {
|
|
|
|
if err := ser.UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil {
|
|
|
|
return fmt.Errorf("failed to update tls certs: %w", err)
|
|
|
|
}
|
|
|
|
found = true
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
2024-03-26 11:34:20 +00:00
|
|
|
} else if unbind := a.updateUnbindServerInfo(serverInfo); unbind {
|
|
|
|
found = true
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
2023-01-18 09:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid servers configuration: no known server found")
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-18 09:44:44 +00:00
|
|
|
|
2024-03-26 11:34:20 +00:00
|
|
|
func (a *app) getServers() []Server {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
return a.servers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) getServer(address string) Server {
|
2023-01-18 09:44:44 +00:00
|
|
|
for i := range a.servers {
|
|
|
|
if a.servers[i].Address() == address {
|
2024-03-26 11:34:20 +00:00
|
|
|
return a.servers[i]
|
2023-01-18 09:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-26 11:34:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) updateUnbindServerInfo(info ServerInfo) bool {
|
|
|
|
for i := range a.unbindServers {
|
|
|
|
if a.unbindServers[i].Address == info.Address {
|
|
|
|
a.unbindServers[i] = info
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-01-18 09:44:44 +00:00
|
|
|
}
|
2023-03-15 08:07:44 +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-http-gw",
|
|
|
|
InstanceID: instanceID,
|
|
|
|
Version: Version,
|
|
|
|
}
|
2024-09-10 07:09:51 +00:00
|
|
|
|
|
|
|
if trustedCa := a.cfg.GetString(cfgTracingTrustedCa); trustedCa != "" {
|
|
|
|
caBytes, err := os.ReadFile(trustedCa)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn(logs.FailedToInitializeTracing, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
ok := certPool.AppendCertsFromPEM(caBytes)
|
|
|
|
if !ok {
|
|
|
|
a.log.Warn(logs.FailedToInitializeTracing, zap.String("error", "can't fill cert pool by ca cert"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg.ServerCaCertPool = certPool
|
|
|
|
}
|
|
|
|
|
2024-11-15 07:53:46 +00:00
|
|
|
attributes, err := fetchTracingAttributes(a.cfg)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn(logs.FailedToInitializeTracing, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg.Attributes = attributes
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
updated, err := tracing.Setup(ctx, cfg)
|
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Warn(logs.FailedToInitializeTracing, zap.Error(err))
|
2023-03-15 08:07:44 +00:00
|
|
|
}
|
|
|
|
if updated {
|
2023-08-27 15:09:02 +00:00
|
|
|
a.log.Info(logs.TracingConfigUpdated)
|
2023-03-15 08:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-31 16:19:57 +00:00
|
|
|
|
|
|
|
func (a *app) setRuntimeParameters() {
|
|
|
|
if len(os.Getenv("GOMEMLIMIT")) != 0 {
|
|
|
|
// default limit < yaml limit < app env limit < GOMEMLIMIT
|
|
|
|
a.log.Warn(logs.RuntimeSoftMemoryDefinedWithGOMEMLIMIT)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
softMemoryLimit := fetchSoftMemoryLimit(a.cfg)
|
|
|
|
previous := debug.SetMemoryLimit(softMemoryLimit)
|
|
|
|
if softMemoryLimit != previous {
|
|
|
|
a.log.Info(logs.RuntimeSoftMemoryLimitUpdated,
|
|
|
|
zap.Int64("new_value", softMemoryLimit),
|
|
|
|
zap.Int64("old_value", previous))
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 08:29:08 +00:00
|
|
|
|
2024-03-26 11:34:20 +00:00
|
|
|
func (a *app) scheduleReconnect(ctx context.Context, srv *fasthttp.Server) {
|
|
|
|
go func() {
|
|
|
|
t := time.NewTicker(a.settings.reconnectInterval)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
if a.tryReconnect(ctx, srv) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Reset(a.settings.reconnectInterval)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) tryReconnect(ctx context.Context, sr *fasthttp.Server) bool {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
|
|
|
a.log.Info(logs.ServerReconnecting)
|
|
|
|
var failedServers []ServerInfo
|
|
|
|
|
|
|
|
for _, serverInfo := range a.unbindServers {
|
|
|
|
fields := []zap.Field{
|
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
|
|
|
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(logs.ServerReconnectFailed, zap.Error(err))
|
|
|
|
failedServers = append(failedServers, serverInfo)
|
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
a.log.Info(logs.StartingServer, zap.String("address", srv.Address()))
|
|
|
|
a.metrics.MarkHealthy(serverInfo.Address)
|
|
|
|
if err = sr.Serve(srv.Listener()); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
a.log.Warn(logs.ListenAndServe, zap.Error(err))
|
|
|
|
a.metrics.MarkUnhealthy(serverInfo.Address)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
|
|
|
a.log.Info(logs.ServerReconnectedSuccessfully, fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.unbindServers = failedServers
|
|
|
|
|
|
|
|
return len(a.unbindServers) == 0
|
|
|
|
}
|