2020-07-06 09:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-07-13 12:27:05 +00:00
|
|
|
"runtime"
|
2020-10-19 08:47:13 +00:00
|
|
|
"sort"
|
2020-07-06 09:18:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/resolver"
|
2021-05-20 12:41:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/internal/version"
|
2021-08-27 12:48:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2020-07-06 09:18:16 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-18 13:11:25 +00:00
|
|
|
defaultRebalanceInterval = 60 * time.Second
|
2022-04-14 15:09:57 +00:00
|
|
|
defaultHealthcheckTimeout = 15 * time.Second
|
2022-07-18 13:11:25 +00:00
|
|
|
defaultConnectTimeout = 10 * time.Second
|
2022-04-14 15:09:57 +00:00
|
|
|
defaultShutdownTimeout = 15 * time.Second
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-07-29 06:26:11 +00:00
|
|
|
defaultPoolErrorThreshold uint32 = 100
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
defaultMaxClientsCount = 100
|
|
|
|
defaultMaxClientsDeadline = time.Second * 30
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
const ( // Settings.
|
|
|
|
// Logger.
|
2022-03-22 10:22:00 +00:00
|
|
|
cfgLoggerLevel = "logger.level"
|
2020-07-07 11:25:13 +00:00
|
|
|
|
2021-06-22 13:25:29 +00:00
|
|
|
// Wallet.
|
2022-07-28 13:26:42 +00:00
|
|
|
cfgWalletPath = "wallet.path"
|
|
|
|
cfgWalletAddress = "wallet.address"
|
2021-06-22 13:25:29 +00:00
|
|
|
cfgWalletPassphrase = "wallet.passphrase"
|
2022-07-28 13:26:42 +00:00
|
|
|
cmdWallet = "wallet"
|
|
|
|
cmdAddress = "address"
|
2020-07-15 20:16:27 +00:00
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// HTTPS/TLS.
|
2020-07-13 15:50:11 +00:00
|
|
|
cfgTLSKeyFile = "tls.key_file"
|
|
|
|
cfgTLSCertFile = "tls.cert_file"
|
|
|
|
|
2022-07-29 06:26:11 +00:00
|
|
|
// Pool config.
|
2022-04-14 15:09:57 +00:00
|
|
|
cfgConnectTimeout = "connect_timeout"
|
|
|
|
cfgHealthcheckTimeout = "healthcheck_timeout"
|
|
|
|
cfgRebalanceInterval = "rebalance_interval"
|
2022-07-29 06:26:11 +00:00
|
|
|
cfgPoolErrorThreshold = "pool_error_threshold"
|
2020-07-07 11:25:13 +00:00
|
|
|
|
2021-08-16 09:58:09 +00:00
|
|
|
// Caching.
|
2021-09-08 07:08:56 +00:00
|
|
|
cfgObjectsCacheLifetime = "cache.objects.lifetime"
|
|
|
|
cfgObjectsCacheSize = "cache.objects.size"
|
|
|
|
cfgListObjectsCacheLifetime = "cache.list.lifetime"
|
|
|
|
cfgListObjectsCacheSize = "cache.list.size"
|
2021-09-10 06:56:56 +00:00
|
|
|
cfgBucketsCacheLifetime = "cache.buckets.lifetime"
|
|
|
|
cfgBucketsCacheSize = "cache.buckets.size"
|
|
|
|
cfgNamesCacheLifetime = "cache.names.lifetime"
|
|
|
|
cfgNamesCacheSize = "cache.names.size"
|
|
|
|
cfgSystemLifetimeSize = "cache.system.lifetime"
|
|
|
|
cfgSystemCacheSize = "cache.system.size"
|
2021-09-08 07:08:56 +00:00
|
|
|
cfgAccessBoxCacheLifetime = "cache.accessbox.lifetime"
|
|
|
|
cfgAccessBoxCacheSize = "cache.accessbox.size"
|
2021-08-16 09:58:09 +00:00
|
|
|
|
2022-02-04 10:05:36 +00:00
|
|
|
// NATS.
|
|
|
|
cfgEnableNATS = "nats.enabled"
|
|
|
|
cfgNATSEndpoint = "nats.endpoint"
|
|
|
|
cfgNATSTimeout = "nats.timeout"
|
|
|
|
cfgNATSTLSCertFile = "nats.cert_file"
|
|
|
|
cfgNATSAuthPrivateKeyFile = "nats.key_file"
|
|
|
|
cfgNATSRootCAFiles = "nats.root_ca"
|
|
|
|
|
2021-08-23 18:39:15 +00:00
|
|
|
// Policy.
|
|
|
|
cfgDefaultPolicy = "default_policy"
|
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
// CORS.
|
|
|
|
cfgDefaultMaxAge = "cors.default_max_age"
|
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// MaxClients.
|
2020-07-22 13:02:32 +00:00
|
|
|
cfgMaxClientsCount = "max_clients_count"
|
|
|
|
cfgMaxClientsDeadline = "max_clients_deadline"
|
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// Metrics / Profiler / Web.
|
2022-07-26 13:29:07 +00:00
|
|
|
cfgPrometheusEnabled = "prometheus.enabled"
|
|
|
|
cfgPrometheusAddress = "prometheus.address"
|
|
|
|
cfgPProfEnabled = "pprof.enabled"
|
|
|
|
cfgPProfAddress = "pprof.address"
|
|
|
|
|
|
|
|
cfgListenAddress = "listen_address"
|
|
|
|
cfgListenDomains = "listen_domains"
|
2020-07-07 11:25:13 +00:00
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// Peers.
|
2020-10-19 08:47:13 +00:00
|
|
|
cfgPeers = "peers"
|
|
|
|
|
2022-04-22 07:18:21 +00:00
|
|
|
cfgTreeServiceEndpoint = "tree.service"
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
// NeoGo.
|
2022-04-14 18:10:57 +00:00
|
|
|
cfgRPCEndpoint = "rpc_endpoint"
|
2022-01-11 10:09:34 +00:00
|
|
|
|
|
|
|
// Resolving.
|
2022-04-14 18:10:57 +00:00
|
|
|
cfgResolveOrder = "resolve_order"
|
2022-01-11 10:09:34 +00:00
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// Application.
|
2020-07-07 11:25:13 +00:00
|
|
|
cfgApplicationBuildTime = "app.build_time"
|
2020-10-19 08:47:13 +00:00
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// Command line args.
|
2020-10-19 08:47:13 +00:00
|
|
|
cmdHelp = "help"
|
|
|
|
cmdVersion = "version"
|
2021-06-28 10:44:22 +00:00
|
|
|
cmdConfig = "config"
|
2022-07-26 13:29:07 +00:00
|
|
|
cmdPProf = "pprof"
|
|
|
|
cmdMetrics = "metrics"
|
2021-05-20 12:41:32 +00:00
|
|
|
|
2022-08-11 22:48:56 +00:00
|
|
|
// Configuration of parameters of requests to NeoFS.
|
|
|
|
// Number of the object copies to consider PUT to NeoFS successful.
|
|
|
|
cfgSetCopiesNumber = "neofs.set_copies_number"
|
|
|
|
|
2022-08-31 22:43:36 +00:00
|
|
|
// List of allowed AccessKeyID prefixes.
|
|
|
|
cfgAllowedAccessKeyIDPrefixes = "allowed_access_key_id_prefixes"
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// envPrefix is an environment variables prefix used for configuration.
|
2021-05-20 12:41:32 +00:00
|
|
|
envPrefix = "S3_GW"
|
2020-07-07 11:25:13 +00:00
|
|
|
)
|
|
|
|
|
2020-10-19 08:47:13 +00:00
|
|
|
var ignore = map[string]struct{}{
|
|
|
|
cfgApplicationBuildTime: {},
|
|
|
|
|
|
|
|
cfgPeers: {},
|
|
|
|
|
|
|
|
cmdHelp: {},
|
|
|
|
cmdVersion: {},
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
func fetchPeers(l *zap.Logger, v *viper.Viper) []pool.NodeParam {
|
|
|
|
var nodes []pool.NodeParam
|
2020-07-07 11:25:13 +00:00
|
|
|
for i := 0; ; i++ {
|
2020-10-19 08:47:13 +00:00
|
|
|
key := cfgPeers + "." + strconv.Itoa(i) + "."
|
2020-07-07 11:25:13 +00:00
|
|
|
address := v.GetString(key + "address")
|
|
|
|
weight := v.GetFloat64(key + "weight")
|
2021-12-29 13:38:23 +00:00
|
|
|
priority := v.GetInt(key + "priority")
|
2020-07-07 11:25:13 +00:00
|
|
|
|
|
|
|
if address == "" {
|
|
|
|
l.Warn("skip, empty address")
|
|
|
|
break
|
|
|
|
}
|
2021-05-26 16:48:27 +00:00
|
|
|
if weight <= 0 { // unspecified or wrong
|
|
|
|
weight = 1
|
|
|
|
}
|
2021-12-29 13:38:23 +00:00
|
|
|
if priority <= 0 { // unspecified or wrong
|
|
|
|
priority = 1
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
nodes = append(nodes, pool.NewNodeParam(priority, address, weight))
|
2020-07-07 11:25:13 +00:00
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
l.Info("added connection peer",
|
2020-11-24 07:09:58 +00:00
|
|
|
zap.String("address", address),
|
|
|
|
zap.Float64("weight", weight))
|
2020-07-07 11:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
return nodes
|
2020-07-07 11:25:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
func newSettings() *viper.Viper {
|
|
|
|
v := viper.New()
|
|
|
|
|
|
|
|
v.AutomaticEnv()
|
2021-05-20 12:41:32 +00:00
|
|
|
v.SetEnvPrefix(envPrefix)
|
2020-07-06 09:18:16 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2021-06-24 15:21:34 +00:00
|
|
|
v.AllowEmptyEnv(true)
|
2020-07-06 09:18:16 +00:00
|
|
|
|
|
|
|
// flags setup:
|
|
|
|
flags := pflag.NewFlagSet("commandline", pflag.ExitOnError)
|
2020-12-02 09:46:51 +00:00
|
|
|
flags.SetOutput(os.Stdout)
|
2020-07-06 09:18:16 +00:00
|
|
|
flags.SortFlags = false
|
|
|
|
|
2022-07-26 13:29:07 +00:00
|
|
|
flags.Bool(cmdPProf, false, "enable pprof")
|
|
|
|
flags.Bool(cmdMetrics, false, "enable prometheus metrics")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2020-10-19 08:47:13 +00:00
|
|
|
help := flags.BoolP(cmdHelp, "h", false, "show help")
|
2021-05-20 12:41:32 +00:00
|
|
|
versionFlag := flags.BoolP(cmdVersion, "v", false, "show version")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-07-28 13:26:42 +00:00
|
|
|
flags.StringP(cmdWallet, "w", "", `path to the wallet`)
|
|
|
|
flags.String(cmdAddress, "", `address of wallet account`)
|
2021-06-28 10:44:22 +00:00
|
|
|
config := flags.String(cmdConfig, "", "config path")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-04-14 15:09:57 +00:00
|
|
|
flags.Duration(cfgHealthcheckTimeout, defaultHealthcheckTimeout, "set timeout to check node health during rebalance")
|
|
|
|
flags.Duration(cfgConnectTimeout, defaultConnectTimeout, "set timeout to connect to NeoFS nodes")
|
|
|
|
flags.Duration(cfgRebalanceInterval, defaultRebalanceInterval, "set rebalance interval")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
flags.Int(cfgMaxClientsCount, defaultMaxClientsCount, "set max-clients count")
|
|
|
|
flags.Duration(cfgMaxClientsDeadline, defaultMaxClientsDeadline, "set max-clients deadline")
|
|
|
|
|
2020-07-15 20:16:27 +00:00
|
|
|
flags.String(cfgListenAddress, "0.0.0.0:8080", "set address to listen")
|
2021-06-10 12:26:57 +00:00
|
|
|
flags.String(cfgTLSCertFile, "", "TLS certificate file to use")
|
|
|
|
flags.String(cfgTLSKeyFile, "", "TLS key file to use")
|
|
|
|
|
2020-10-19 08:47:13 +00:00
|
|
|
peers := flags.StringArrayP(cfgPeers, "p", nil, "set NeoFS nodes")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
flags.StringP(cfgRPCEndpoint, "r", "", "set RPC endpoint")
|
|
|
|
resolveMethods := flags.StringSlice(cfgResolveOrder, []string{resolver.DNSResolver}, "set bucket name resolve order")
|
|
|
|
|
2022-08-30 10:52:37 +00:00
|
|
|
domains := flags.StringSliceP(cfgListenDomains, "d", nil, "set domains to be listened")
|
2020-12-10 15:11:45 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
// set defaults:
|
|
|
|
|
|
|
|
// logger:
|
2020-07-07 11:25:13 +00:00
|
|
|
v.SetDefault(cfgLoggerLevel, "debug")
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2022-07-29 06:26:11 +00:00
|
|
|
// pool:
|
|
|
|
v.SetDefault(cfgPoolErrorThreshold, defaultPoolErrorThreshold)
|
|
|
|
|
2022-07-26 13:29:07 +00:00
|
|
|
v.SetDefault(cfgPProfAddress, "localhost:8085")
|
|
|
|
v.SetDefault(cfgPrometheusAddress, "localhost:8086")
|
|
|
|
|
|
|
|
// Binding flags
|
|
|
|
if err := v.BindPFlag(cfgPProfEnabled, flags.Lookup(cmdPProf)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgPrometheusEnabled, flags.Lookup(cmdMetrics)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
if err := v.BindPFlags(flags); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:26:42 +00:00
|
|
|
if err := v.BindPFlag(cfgWalletPath, flags.Lookup(cmdWallet)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgWalletAddress, flags.Lookup(cmdAddress)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
if err := flags.Parse(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
if resolveMethods != nil {
|
|
|
|
v.SetDefault(cfgResolveOrder, *resolveMethods)
|
|
|
|
}
|
|
|
|
|
2020-10-19 08:47:13 +00:00
|
|
|
if peers != nil && len(*peers) > 0 {
|
|
|
|
for i := range *peers {
|
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i])
|
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".weight", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:11:45 +00:00
|
|
|
if domains != nil && len(*domains) > 0 {
|
2022-08-30 10:52:37 +00:00
|
|
|
v.SetDefault(cfgListenDomains, *domains)
|
2020-12-10 15:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
switch {
|
|
|
|
case help != nil && *help:
|
2021-05-20 12:41:32 +00:00
|
|
|
fmt.Printf("NeoFS S3 gateway %s\n", version.Version)
|
2020-07-06 09:18:16 +00:00
|
|
|
flags.PrintDefaults()
|
2020-10-19 08:47:13 +00:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Default environments:")
|
|
|
|
fmt.Println()
|
|
|
|
keys := v.AllKeys()
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for i := range keys {
|
|
|
|
if _, ok := ignore[keys[i]]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:27:18 +00:00
|
|
|
defaultValue := v.GetString(keys[i])
|
|
|
|
if len(defaultValue) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-19 08:47:13 +00:00
|
|
|
k := strings.Replace(keys[i], ".", "_", -1)
|
2022-07-28 13:27:18 +00:00
|
|
|
fmt.Printf("%s_%s = %s\n", envPrefix, strings.ToUpper(k), defaultValue)
|
2020-10-19 08:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Peers preset:")
|
|
|
|
fmt.Println()
|
|
|
|
|
2021-05-20 12:41:32 +00:00
|
|
|
fmt.Printf("%s_%s_[N]_ADDRESS = string\n", envPrefix, strings.ToUpper(cfgPeers))
|
|
|
|
fmt.Printf("%s_%s_[N]_WEIGHT = 0..1 (float)\n", envPrefix, strings.ToUpper(cfgPeers))
|
2020-10-19 08:47:13 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
os.Exit(0)
|
2021-05-20 12:41:32 +00:00
|
|
|
case versionFlag != nil && *versionFlag:
|
2022-07-13 12:27:05 +00:00
|
|
|
fmt.Printf("NeoFS S3 Gateway\nVersion: %s\nGoVersion: %s\n", version.Version, runtime.Version())
|
2020-07-06 09:18:16 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:44:22 +00:00
|
|
|
if v.IsSet(cmdConfig) {
|
|
|
|
if cfgFile, err := os.Open(*config); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if err := v.ReadConfig(cfgFile); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
return v
|
|
|
|
}
|