2021-01-13 13:46:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"net"
|
|
|
|
|
2021-02-21 08:30:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-01-13 13:46:39 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2021-02-21 08:30:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2021-01-13 13:46:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
|
|
|
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cfgCtrlSvcSection = "control"
|
|
|
|
|
2021-01-14 07:48:59 +00:00
|
|
|
cfgCtrlSvcAuthorizedKeys = cfgCtrlSvcSection + ".authorized_keys"
|
2021-01-13 13:46:39 +00:00
|
|
|
|
|
|
|
cfgCtrlSvcGRPCSection = cfgCtrlSvcSection + ".grpc"
|
|
|
|
cfgCtrlGRPCEndpoint = cfgCtrlSvcGRPCSection + ".endpoint"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initControlService(c *cfg) {
|
2021-01-14 07:48:59 +00:00
|
|
|
strKeys := c.viper.GetStringSlice(cfgCtrlSvcAuthorizedKeys)
|
2021-01-13 13:46:39 +00:00
|
|
|
keys := make([][]byte, 0, len(strKeys)+1) // +1 for node key
|
|
|
|
|
|
|
|
keys = append(keys, crypto.MarshalPublicKey(&c.key.PublicKey))
|
|
|
|
|
|
|
|
for i := range strKeys {
|
|
|
|
key, err := hex.DecodeString(strKeys[i])
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
if crypto.UnmarshalPublicKey(key) == nil {
|
|
|
|
fatalOnErr(errors.Errorf("invalid permitted key for Control service %s", strKeys[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlSvc := controlSvc.New(
|
|
|
|
controlSvc.WithKey(c.key),
|
2021-01-14 07:47:18 +00:00
|
|
|
controlSvc.WithAuthorizedKeys(keys),
|
2021-01-13 13:46:39 +00:00
|
|
|
controlSvc.WithHealthChecker(c),
|
2021-01-14 16:32:23 +00:00
|
|
|
controlSvc.WithNetMapSource(c.cfgNetmap.wrapper),
|
2021-01-15 11:53:41 +00:00
|
|
|
controlSvc.WithNodeState(c),
|
2021-02-21 08:30:32 +00:00
|
|
|
controlSvc.WithDeletedObjectHandler(func(addrList []*object.Address) error {
|
|
|
|
prm := new(engine.DeletePrm).WithAddresses(addrList...)
|
|
|
|
|
|
|
|
_, err := c.cfgObject.cfgLocalStorage.localStorage.Delete(prm)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}),
|
2021-01-13 13:46:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
lis net.Listener
|
|
|
|
endpoint = c.viper.GetString(cfgCtrlGRPCEndpoint)
|
|
|
|
)
|
|
|
|
|
|
|
|
if endpoint == "" || endpoint == c.viper.GetString(cfgListenAddress) {
|
|
|
|
lis = c.cfgGRPC.listener
|
|
|
|
c.cfgControlService.server = c.cfgGRPC.server
|
|
|
|
} else {
|
|
|
|
lis, err = net.Listen("tcp", endpoint)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
c.cfgControlService.server = grpc.NewServer()
|
|
|
|
}
|
|
|
|
|
2021-01-18 08:56:14 +00:00
|
|
|
c.onShutdown(func() {
|
|
|
|
stopGRPC("NeoFS Control API", c.cfgControlService.server, c.log)
|
|
|
|
})
|
|
|
|
|
2021-01-13 13:46:39 +00:00
|
|
|
control.RegisterControlServiceServer(c.cfgControlService.server, ctlSvc)
|
|
|
|
|
|
|
|
c.workers = append(c.workers, newWorkerFromFunc(func(ctx context.Context) {
|
|
|
|
fatalOnErr(c.cfgControlService.server.Serve(lis))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:42:31 +00:00
|
|
|
func (c *cfg) setNetmapStatus(st control.NetmapStatus) {
|
2021-01-15 10:23:46 +00:00
|
|
|
c.netStatus.Store(int32(st))
|
2021-01-13 13:46:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:42:31 +00:00
|
|
|
func (c *cfg) NetmapStatus() control.NetmapStatus {
|
2021-01-15 10:23:46 +00:00
|
|
|
return control.NetmapStatus(c.netStatus.Load())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) setHealthStatus(st control.HealthStatus) {
|
|
|
|
c.healthStatus.Store(int32(st))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) HealthStatus() control.HealthStatus {
|
|
|
|
return control.HealthStatus(c.healthStatus.Load())
|
2021-01-13 13:46:39 +00:00
|
|
|
}
|