netmaker/controllers/node_grpc.go

225 lines
5.9 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"
2021-08-06 02:01:16 +08:00
"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) {
2021-03-26 00:17:52 +08:00
// convert string id (from proto) to mongoDB ObjectId
2021-08-06 02:15:32 +08:00
macAndNetwork := strings.Split(req.Data, "###")
2021-03-26 00:17:52 +08:00
2021-08-06 02:15:32 +08:00
if len(macAndNetwork) != 2 {
return nil, errors.New("could not read node, invalid node id given")
}
2021-12-08 04:51:57 +08:00
node, err := logic.GetNode(macAndNetwork[0], macAndNetwork[1])
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
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
}
2021-03-26 00:17:52 +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-03-26 00:17:52 +08:00
// Get the node data from the request
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-07-25 04:13:24 +08:00
macaddress := newnode.MacAddress
networkName := newnode.Network
2021-03-26 00:17:52 +08:00
2021-10-27 00:27:29 +08:00
node, err := logic.GetNodeByMacAddress(networkName, macaddress)
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) {
nodeID := req.GetData()
2021-12-08 04:51:57 +08:00
var nodeInfo = strings.Split(nodeID, "###")
if len(nodeInfo) != 2 {
return nil, errors.New("node not found")
}
var node, err = logic.GetNode(nodeInfo[0], nodeInfo[1])
err = logic.DeleteNode(&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
}
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) {
2021-08-06 02:01:16 +08:00
macAndNetwork := strings.Split(req.Data, "###")
if len(macAndNetwork) == 2 {
2021-08-06 11:08:59 +08:00
// TODO: Make constant and new variable for isServer
2021-12-08 04:51:57 +08:00
node, err := logic.GetNode(macAndNetwork[0], macAndNetwork[1])
2021-08-06 11:08:59 +08:00
if err != nil {
return nil, err
}
2021-10-09 03:07:12 +08:00
if node.IsServer == "yes" && logic.IsLeader(&node) {
logic.SetNetworkServerPeers(&node)
2021-08-06 11:08:59 +08:00
}
2021-09-18 22:33:14 +08:00
excludeIsRelayed := node.IsRelay != "yes"
var relayedNode string
if node.IsRelayed == "yes" {
2021-09-17 08:00:40 +08:00
relayedNode = node.Address
}
peers, err := logic.GetPeersList(macAndNetwork[1], excludeIsRelayed, relayedNode)
2021-08-06 02:01:16 +08:00
if err != nil {
return nil, err
}
2021-03-26 00:17:52 +08:00
2021-08-06 02:01:16 +08:00
peersData, err := json.Marshal(&peers)
2021-12-07 04:31:08 +08:00
logger.Log(3, node.Address, "checked in successfully")
2021-08-06 02:01:16 +08:00
return &nodepb.Object{
Data: string(peersData),
Type: nodepb.NODE_TYPE,
}, err
2021-05-06 23:57:32 +08:00
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
2021-08-06 02:01:16 +08:00
Data: "",
2021-08-03 06:06:26 +08:00
Type: nodepb.NODE_TYPE,
2021-08-06 02:01:16 +08:00
}, errors.New("could not fetch peers, invalid node id")
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) {
2021-07-22 06:55:19 +08:00
// Initiate a NodeItem type to write decoded data to
//data := &models.PeersResponse{}
// collection.Find returns a cursor for our (empty) query
2021-08-06 02:10:35 +08:00
macAndNetwork := strings.Split(req.Data, "###")
if len(macAndNetwork) != 2 {
return nil, errors.New("did not receive valid node id when fetching ext peers")
2021-08-03 06:06:26 +08:00
}
peers, err := logic.GetExtPeersList(macAndNetwork[0], macAndNetwork[1])
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
}