netmaker/controllers/node_grpc.go

235 lines
5.6 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package controller
import (
2021-05-06 23:57:32 +08:00
"context"
2021-08-03 06:06:26 +08:00
"encoding/json"
"errors"
"strings"
2021-09-18 22:33:14 +08:00
2021-03-26 00:17:52 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
2021-12-07 04:31:08 +08:00
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
2021-03-26 00:17:52 +08:00
"github.com/gravitl/netmaker/models"
2022-01-06 02:13:03 +08:00
"github.com/gravitl/netmaker/servercfg"
2021-03-26 00:17:52 +08:00
)
2021-10-09 03:07:12 +08:00
// NodeServiceServer - represents the service server for gRPC
2021-03-26 00:17:52 +08:00
type NodeServiceServer struct {
nodepb.UnimplementedNodeServiceServer
}
2021-05-06 23:57:32 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.ReadNode - reads node and responds with gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
var node, err = getNewOrLegacyNode(req.Data)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2022-01-11 06:52:21 +08:00
2021-11-15 08:17:30 +08:00
node.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
if err != nil {
return nil, err
}
2021-08-06 02:19:19 +08:00
node.SetLastCheckIn()
2021-03-26 00:17:52 +08:00
// Cast to ReadNodeRes type
2021-11-15 08:17:30 +08:00
nodeData, errN := json.Marshal(&node)
if errN != nil {
2021-08-03 06:06:26 +08:00
return nil, err
}
2021-10-27 00:27:29 +08:00
logic.UpdateNode(&node, &node)
2021-08-03 06:06:26 +08:00
response := &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}
return response, nil
}
2021-07-22 06:55:19 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.CreateNode - creates a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-12-11 10:09:42 +08:00
var node = models.Node{}
var err error
2021-08-03 06:06:26 +08:00
data := req.GetData()
2021-12-11 10:09:42 +08:00
if err := json.Unmarshal([]byte(data), &node); err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-10-27 00:27:29 +08:00
validKey := logic.IsKeyValid(node.Network, node.AccessKey)
2021-12-11 10:09:42 +08:00
node.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
}
2021-05-06 23:57:32 +08:00
if !validKey {
2021-12-11 10:09:42 +08:00
if node.NetworkSettings.AllowManualSignUp == "yes" {
2021-07-27 00:24:36 +08:00
node.IsPending = "yes"
2021-05-06 23:57:32 +08:00
} else {
2021-08-03 06:06:26 +08:00
return nil, errors.New("invalid key, and network does not allow no-key signups")
2021-05-06 23:57:32 +08:00
}
}
2021-03-26 00:17:52 +08:00
2021-12-11 10:09:42 +08:00
err = logic.CreateNode(&node)
2021-11-15 08:17:30 +08:00
if err != nil {
return nil, err
}
2021-12-11 10:09:42 +08:00
2021-11-15 08:17:30 +08:00
nodeData, errN := json.Marshal(&node)
if errN != nil {
return nil, err
}
2021-12-11 10:09:42 +08:00
2021-08-03 06:06:26 +08:00
response := &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}
2021-12-11 10:09:42 +08:00
err = logic.SetNetworkNodesLastModified(node.Network)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-05-06 23:57:32 +08:00
}
2022-01-18 22:06:43 +08:00
2021-05-06 23:57:32 +08:00
return response, nil
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.UpdateNode updates a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-08-03 06:06:26 +08:00
var newnode models.Node
if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil {
return nil, err
2021-05-06 23:57:32 +08:00
}
2021-03-26 00:17:52 +08:00
2022-01-11 06:52:21 +08:00
node, err := logic.GetNodeByIDorMacAddress(newnode.ID, newnode.MacAddress, newnode.Network)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-05-06 23:57:32 +08:00
}
2022-01-06 02:13:03 +08:00
if !servercfg.GetRce() {
newnode.PostDown = node.PostDown
newnode.PostUp = node.PostUp
}
2021-10-27 00:27:29 +08:00
err = logic.UpdateNode(&node, &newnode)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-11-15 08:17:30 +08:00
newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
2021-08-06 11:38:34 +08:00
if err != nil {
return nil, err
}
2021-11-15 08:17:30 +08:00
nodeData, errN := json.Marshal(&newnode)
if errN != nil {
return nil, err
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}, nil
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.DeleteNode - deletes a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) DeleteNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2022-01-11 08:36:13 +08:00
var node, err = getNewOrLegacyNode(req.Data)
2022-01-11 08:36:13 +08:00
if err != nil {
return nil, err
2021-12-08 04:51:57 +08:00
}
2022-01-11 07:32:49 +08:00
err = logic.DeleteNodeByID(&node, true)
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2022-01-18 22:06:43 +08:00
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: "success",
Type: nodepb.STRING_TYPE,
2021-03-26 00:17:52 +08:00
}, nil
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.GetPeers - fetches peers over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) GetPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2022-01-11 08:36:13 +08:00
var node, err = getNewOrLegacyNode(req.Data)
2022-01-11 07:32:49 +08:00
if err != nil {
return nil, err
}
2022-01-11 07:32:49 +08:00
if node.IsServer == "yes" && logic.IsLeader(&node) {
logic.SetNetworkServerPeers(&node)
}
excludeIsRelayed := node.IsRelay != "yes"
var relayedNode string
if node.IsRelayed == "yes" {
relayedNode = node.Address
2022-01-11 07:32:49 +08:00
}
2022-01-11 07:32:49 +08:00
peers, err := logic.GetPeersList(node.Network, excludeIsRelayed, relayedNode)
if err != nil {
return nil, err
}
peersData, err := json.Marshal(&peers)
logger.Log(3, node.Address, "checked in successfully")
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
2022-01-11 07:32:49 +08:00
Data: string(peersData),
2021-08-03 06:06:26 +08:00
Type: nodepb.NODE_TYPE,
2022-01-11 07:32:49 +08:00
}, err
2021-03-26 00:17:52 +08:00
}
2021-05-20 01:59:10 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.GetExtPeers - returns ext peers for a gateway node
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) GetExtPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2022-01-11 07:32:49 +08:00
var node, err = getNewOrLegacyNode(req.Data)
2022-01-11 07:32:49 +08:00
if err != nil {
return nil, err
}
peers, err := logic.GetExtPeersList(&node)
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-07-22 06:55:19 +08:00
}
2021-08-03 06:06:26 +08:00
var extPeers []models.Node
2021-07-22 06:55:19 +08:00
for i := 0; i < len(peers); i++ {
2021-08-03 06:06:26 +08:00
extPeers = append(extPeers, models.Node{
Address: peers[i].Address,
Address6: peers[i].Address6,
Endpoint: peers[i].Endpoint,
PublicKey: peers[i].PublicKey,
PersistentKeepalive: peers[i].KeepAlive,
ListenPort: peers[i].ListenPort,
LocalAddress: peers[i].LocalAddress,
2021-07-22 06:55:19 +08:00
})
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
extData, err := json.Marshal(&extPeers)
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-07-22 06:55:19 +08:00
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: string(extData),
Type: nodepb.EXT_PEER,
}, nil
2021-05-20 01:59:10 +08:00
}
// == private methods ==
func getNewOrLegacyNode(data string) (models.Node, error) {
var reqNode, node models.Node
var err error
if err = json.Unmarshal([]byte(data), &reqNode); err != nil {
oldID := strings.Split(data, "###") // handle legacy client IDs
if len(oldID) == 2 {
if node, err = logic.GetNodeByIDorMacAddress(reqNode.ID, reqNode.MacAddress, reqNode.Network); err != nil {
return models.Node{}, err
}
} else {
return models.Node{}, err
}
} else {
node, err = logic.GetNodeByIDorMacAddress(reqNode.ID, reqNode.MacAddress, reqNode.Network)
if err != nil {
return models.Node{}, err
}
}
return node, nil
}