2021-06-01 18:50:01 +00:00
|
|
|
package grpcconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-06-22 17:25:18 +00:00
|
|
|
"strconv"
|
2021-06-01 18:50:01 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errEndpointNotSet = errors.New("empty/not set endpoint, see `grpc.endpoint` section")
|
|
|
|
errTLSKeyNotSet = errors.New("empty/not set TLS key file path, see `grpc.tls.key` section")
|
|
|
|
errTLSCertNotSet = errors.New("empty/not set TLS certificate file path, see `grpc.tls.certificate` section")
|
|
|
|
)
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
// Config is a wrapper over the config section
|
|
|
|
// which provides access to gRPC server configurations.
|
|
|
|
type Config config.Config
|
2021-06-01 18:50:01 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Endpoint returns the value of "endpoint" config parameter.
|
2021-06-01 18:50:01 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Panics if the value is not a non-empty string.
|
2021-06-22 17:25:18 +00:00
|
|
|
func (x *Config) Endpoint() string {
|
|
|
|
v := config.StringSafe(
|
|
|
|
(*config.Config)(x),
|
|
|
|
"endpoint")
|
2021-06-01 18:50:01 +00:00
|
|
|
if v == "" {
|
|
|
|
panic(errEndpointNotSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
// TLS returns "tls" subsection as a TLSConfig.
|
|
|
|
//
|
|
|
|
// Returns nil if "enabled" value of "tls" subsection is false.
|
|
|
|
func (x *Config) TLS() *TLSConfig {
|
|
|
|
sub := (*config.Config)(x).
|
|
|
|
Sub("tls")
|
|
|
|
|
|
|
|
if !config.BoolSafe(sub, "enabled") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TLSConfig{
|
|
|
|
cfg: sub,
|
2021-06-01 18:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
// TLSConfig is a wrapper over the config section
|
|
|
|
// which provides access to TLS configurations
|
|
|
|
// of the gRPC server.
|
|
|
|
type TLSConfig struct {
|
|
|
|
cfg *config.Config
|
2021-06-01 18:50:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// KeyFile returns the value of "key" config parameter.
|
2021-06-01 18:50:01 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Panics if the value is not a non-empty string.
|
2021-06-01 18:50:01 +00:00
|
|
|
func (tls TLSConfig) KeyFile() string {
|
|
|
|
v := config.StringSafe(tls.cfg, "key")
|
|
|
|
if v == "" {
|
|
|
|
panic(errTLSKeyNotSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CertificateFile returns the value of "certificate" config parameter.
|
2021-06-01 18:50:01 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Panics if the value is not a non-empty string.
|
2021-06-01 18:50:01 +00:00
|
|
|
func (tls TLSConfig) CertificateFile() string {
|
|
|
|
v := config.StringSafe(tls.cfg, "certificate")
|
|
|
|
if v == "" {
|
|
|
|
panic(errTLSCertNotSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2021-06-22 17:25:18 +00:00
|
|
|
|
2021-11-30 10:07:46 +00:00
|
|
|
// UseInsecureCrypto returns true if TLS 1.2 cipher suite should not be restricted.
|
|
|
|
func (tls TLSConfig) UseInsecureCrypto() bool {
|
|
|
|
return config.BoolSafe(tls.cfg, "use_insecure_crypto")
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
// IterateEndpoints iterates over subsections ["0":"N") (N - "num" value)
|
|
|
|
// of "grpc" section of c, wrap them into Config and passes to f.
|
|
|
|
//
|
|
|
|
// Panics if N is not a positive number.
|
|
|
|
func IterateEndpoints(c *config.Config, f func(*Config)) {
|
|
|
|
c = c.Sub("grpc")
|
|
|
|
|
|
|
|
num := config.Uint(c, "num")
|
|
|
|
if num == 0 {
|
|
|
|
panic("no gRPC server configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint64(0); i < num; i++ {
|
|
|
|
si := strconv.FormatUint(i, 10)
|
|
|
|
|
|
|
|
sc := (*Config)(c.Sub(si))
|
|
|
|
|
|
|
|
f(sc)
|
|
|
|
}
|
|
|
|
}
|