[#579] cmd/node: Add grpc section to config

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-01 21:50:01 +03:00 committed by Alex Vanin
parent 6ab7efb358
commit 7dbeb08c58
5 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,76 @@
package grpcconfig
import (
"errors"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
)
const (
subsection = "grpc"
tlsSubsection = "tls"
)
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")
)
// TLSConfig is a wrapper over "tls" config section which provides access
// to TLS configuration of gRPC connection.
type TLSConfig struct {
cfg *config.Config
}
// Endpoint returns value of "endpoint" config parameter
// from "grpc" section.
//
// Panics if value is not a non-empty string.
func Endpoint(c *config.Config) string {
v := config.StringSafe(c.Sub(subsection), "endpoint")
if v == "" {
panic(errEndpointNotSet)
}
return v
}
// TLS returns structure that provides access to "tls" subsection of
// "grpc" section.
func TLS(c *config.Config) TLSConfig {
return TLSConfig{
cfg: c.Sub(subsection).Sub(tlsSubsection),
}
}
// Enabled returns value of "enabled" config parameter.
//
// Returns false if value is not set.
func (tls TLSConfig) Enabled() bool {
return config.BoolSafe(tls.cfg, "enabled")
}
// KeyFile returns value of "key" config parameter.
//
// Panics if value is not a non-empty string.
func (tls TLSConfig) KeyFile() string {
v := config.StringSafe(tls.cfg, "key")
if v == "" {
panic(errTLSKeyNotSet)
}
return v
}
// CertificateFile returns value of "certificate" config parameter.
//
// Panics if value is not a non-empty string.
func (tls TLSConfig) CertificateFile() string {
v := config.StringSafe(tls.cfg, "certificate")
if v == "" {
panic(errTLSCertNotSet)
}
return v
}

View file

@ -0,0 +1,63 @@
package grpcconfig
import (
"testing"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
"github.com/stretchr/testify/require"
)
func TestGRPCSection(t *testing.T) {
t.Run("defaults", func(t *testing.T) {
empty := configtest.EmptyConfig()
tlsEnabled := TLS(empty).Enabled()
require.Equal(t, false, tlsEnabled)
require.PanicsWithError(
t,
errEndpointNotSet.Error(),
func() {
Endpoint(empty)
},
)
require.PanicsWithError(
t,
errTLSKeyNotSet.Error(),
func() {
TLS(empty).KeyFile()
},
)
require.PanicsWithError(
t,
errTLSCertNotSet.Error(),
func() {
TLS(empty).CertificateFile()
},
)
})
const path = "../../../../config/example/node"
var fileConfigTest = func(c *config.Config) {
addr := Endpoint(c)
tlsEnabled := TLS(c).Enabled()
tlsCert := TLS(c).CertificateFile()
tlsKey := TLS(c).KeyFile()
require.Equal(t, "s01.neofs.devenv:8080", addr)
require.Equal(t, true, tlsEnabled)
require.Equal(t, "/path/to/cert", tlsCert)
require.Equal(t, "/path/to/key", tlsKey)
}
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
})
}

View file

@ -13,6 +13,12 @@ NEOFS_NODE_ATTRIBUTE_0=Price:11
NEOFS_NODE_ATTRIBUTE_1=UN-LOCODE:RU MSK NEOFS_NODE_ATTRIBUTE_1=UN-LOCODE:RU MSK
NEOFS_NODE_RELAY=true NEOFS_NODE_RELAY=true
# gRPC section
NEOFS_GRPC_ENDPOINT=s01.neofs.devenv:8080
NEOFS_GRPC_TLS_ENABLED=true
NEOFS_GRPC_TLS_CERTIFICATE=/path/to/cert
NEOFS_GRPC_TLS_KEY=/path/to/key
# Storage engine section # Storage engine section
NEOFS_STORAGE_SHARD_NUM=2 NEOFS_STORAGE_SHARD_NUM=2
## 0 shard ## 0 shard

View file

@ -17,6 +17,14 @@
"attribute_1": "UN-LOCODE:RU MSK", "attribute_1": "UN-LOCODE:RU MSK",
"relay": true "relay": true
}, },
"grpc": {
"endpoint": "s01.neofs.devenv:8080",
"tls": {
"enabled": true,
"certificate": "/path/to/cert",
"key": "/path/to/key"
}
},
"storage": { "storage": {
"shard_num": 2, "shard_num": 2,
"shard": { "shard": {

View file

@ -16,6 +16,13 @@ node:
attribute_1: UN-LOCODE:RU MSK attribute_1: UN-LOCODE:RU MSK
relay: true relay: true
grpc:
endpoint: s01.neofs.devenv:8080
tls:
enabled: true
certificate: /path/to/cert
key: /path/to/key
storage: storage:
shard_num: 2 shard_num: 2
shard: shard: