nodeGrpcController.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package controller
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gravitl/netmaker/functions"
  6. nodepb "github.com/gravitl/netmaker/grpc"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/gravitl/netmaker/servercfg"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "google.golang.org/grpc/codes"
  11. "google.golang.org/grpc/status"
  12. )
  13. type NodeServiceServer struct {
  14. NodeDB *mongo.Collection
  15. nodepb.UnimplementedNodeServiceServer
  16. }
  17. func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.ReadNodeReq) (*nodepb.ReadNodeRes, error) {
  18. // convert string id (from proto) to mongoDB ObjectId
  19. macaddress := req.GetMacaddress()
  20. networkName := req.GetNetwork()
  21. network, _ := functions.GetParentNetwork(networkName)
  22. node, err := GetNode(macaddress, networkName)
  23. if err != nil {
  24. return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("Something went wrong: %v", err))
  25. }
  26. /*
  27. if node == nil {
  28. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not find node with Mac Address %s: %v", req.GetMacaddress(), err))
  29. }
  30. */
  31. // Cast to ReadNodeRes type
  32. dualvar := false
  33. if network.IsDualStack != nil {
  34. dualvar = *network.IsDualStack
  35. }
  36. localvar := false
  37. if network.IsLocal != nil {
  38. localvar = *network.IsLocal
  39. }
  40. response := &nodepb.ReadNodeRes{
  41. Node: &nodepb.Node{
  42. Macaddress: node.MacAddress,
  43. Name: node.Name,
  44. Address: node.Address,
  45. Address6: node.Address6,
  46. Endpoint: node.Endpoint,
  47. Password: node.Password,
  48. Nodenetwork: node.Network,
  49. Interface: node.Interface,
  50. Localaddress: node.LocalAddress,
  51. Postdown: node.PostDown,
  52. Postup: node.PostUp,
  53. Checkininterval: node.CheckInInterval,
  54. Dnsoff: !servercfg.IsDNSMode(),
  55. Ispending: node.IsPending,
  56. Isingressgateway: node.IsIngressGateway,
  57. Ingressgatewayrange: node.IngressGatewayRange,
  58. Publickey: node.PublicKey,
  59. Listenport: node.ListenPort,
  60. Keepalive: node.PersistentKeepalive,
  61. Islocal: localvar,
  62. Isdualstack: dualvar,
  63. Localrange: network.LocalRange,
  64. },
  65. }
  66. return response, nil
  67. }
  68. /*
  69. func (s *NodeServiceServer) GetConn(ctx context.Context, data *nodepb.Client) (*nodepb.Client, error) {
  70. // Get the protobuf node type from the protobuf request type
  71. // Essentially doing req.Node to access the struct with a nil check
  72. // Now we have to convert this into a NodeItem type to convert into BSON
  73. clientreq := models.IntClient{
  74. // ID: primitive.NilObjectID,
  75. Address: data.GetAddress(),
  76. Address6: data.GetAddress6(),
  77. AccessKey: data.GetAccesskey(),
  78. PublicKey: data.GetPublickey(),
  79. PrivateKey: data.GetPrivatekey(),
  80. ServerPort: data.GetServerport(),
  81. ServerKey: data.GetServerkey(),
  82. ServerWGEndpoint: data.GetServerwgendpoint(),
  83. }
  84. //Check to see if key is valid
  85. //TODO: Triple inefficient!!! This is the third call to the DB we make for networks
  86. if servercfg.IsRegisterKeyRequired() {
  87. validKey := functions.IsKeyValidGlobal(clientreq.AccessKey)
  88. if !validKey {
  89. return nil, status.Errorf(
  90. codes.Internal,
  91. fmt.Sprintf("Invalid key, and server does not allow no-key signups"),
  92. )
  93. }
  94. }
  95. client, err := RegisterIntClient(clientreq)
  96. if err != nil {
  97. // return internal gRPC error to be handled later
  98. return nil, status.Errorf(
  99. codes.Internal,
  100. fmt.Sprintf("Internal error: %v", err),
  101. )
  102. }
  103. // return the node in a CreateNodeRes type
  104. response := &nodepb.Client{
  105. Privatekey: client.PrivateKey,
  106. Publickey: client.PublicKey,
  107. Accesskey: client.AccessKey,
  108. Address: client.Address,
  109. Address6: client.Address6,
  110. Serverwgendpoint: client.ServerWGEndpoint,
  111. Serverport: client.ServerPort,
  112. Serverkey: client.ServerKey,
  113. }
  114. return response, nil
  115. }
  116. */
  117. func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNodeReq) (*nodepb.CreateNodeRes, error) {
  118. // Get the protobuf node type from the protobuf request type
  119. // Essentially doing req.Node to access the struct with a nil check
  120. data := req.GetNode()
  121. // Now we have to convert this into a NodeItem type to convert into BSON
  122. node := models.Node{
  123. // ID: primitive.NilObjectID,
  124. MacAddress: data.GetMacaddress(),
  125. LocalAddress: data.GetLocaladdress(),
  126. Name: data.GetName(),
  127. Address: data.GetAddress(),
  128. Address6: data.GetAddress6(),
  129. AccessKey: data.GetAccesskey(),
  130. Endpoint: data.GetEndpoint(),
  131. PersistentKeepalive: data.GetKeepalive(),
  132. Password: data.GetPassword(),
  133. Interface: data.GetInterface(),
  134. Network: data.GetNodenetwork(),
  135. IsPending: data.GetIspending(),
  136. PublicKey: data.GetPublickey(),
  137. ListenPort: data.GetListenport(),
  138. }
  139. err := ValidateNodeCreate(node.Network, node)
  140. if err != nil {
  141. // return internal gRPC error to be handled later
  142. return nil, err
  143. }
  144. //Check to see if key is valid
  145. //TODO: Triple inefficient!!! This is the third call to the DB we make for networks
  146. validKey := functions.IsKeyValid(node.Network, node.AccessKey)
  147. network, err := functions.GetParentNetwork(node.Network)
  148. if err != nil {
  149. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not find network: %v", err))
  150. } else {
  151. fmt.Println("Creating node in network " + network.NetID)
  152. }
  153. if !validKey {
  154. //Check to see if network will allow manual sign up
  155. //may want to switch this up with the valid key check and avoid a DB call that way.
  156. if *network.AllowManualSignUp {
  157. node.IsPending = true
  158. } else {
  159. return nil, status.Errorf(
  160. codes.Internal,
  161. fmt.Sprintf("Invalid key, and network does not allow no-key signups"),
  162. )
  163. }
  164. }
  165. node, err = CreateNode(node, node.Network)
  166. if err != nil {
  167. // return internal gRPC error to be handled later
  168. return nil, status.Errorf(
  169. codes.Internal,
  170. fmt.Sprintf("Internal error: %v", err),
  171. )
  172. }
  173. dualvar := false
  174. if network.IsDualStack != nil {
  175. dualvar = *network.IsDualStack
  176. }
  177. localvar := false
  178. if network.IsLocal != nil {
  179. localvar = *network.IsLocal
  180. }
  181. // return the node in a CreateNodeRes type
  182. response := &nodepb.CreateNodeRes{
  183. Node: &nodepb.Node{
  184. Macaddress: node.MacAddress,
  185. Localaddress: node.LocalAddress,
  186. Name: node.Name,
  187. Address: node.Address,
  188. Address6: node.Address6,
  189. Endpoint: node.Endpoint,
  190. Password: node.Password,
  191. Interface: node.Interface,
  192. Nodenetwork: node.Network,
  193. Dnsoff: !servercfg.IsDNSMode(),
  194. Ispending: node.IsPending,
  195. Publickey: node.PublicKey,
  196. Listenport: node.ListenPort,
  197. Keepalive: node.PersistentKeepalive,
  198. Islocal: localvar,
  199. Isdualstack: dualvar,
  200. Localrange: network.LocalRange,
  201. },
  202. }
  203. err = SetNetworkNodesLastModified(node.Network)
  204. if err != nil {
  205. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not update network last modified date: %v", err))
  206. }
  207. return response, nil
  208. }
  209. func (s *NodeServiceServer) CheckIn(ctx context.Context, req *nodepb.CheckInReq) (*nodepb.CheckInRes, error) {
  210. // Get the protobuf node type from the protobuf request type
  211. // Essentially doing req.Node to access the struct with a nil check
  212. data := req.GetNode()
  213. //postchanges := req.GetPostchanges()
  214. // Now we have to convert this into a NodeItem type to convert into BSON
  215. node := models.Node{
  216. // ID: primitive.NilObjectID,
  217. MacAddress: data.GetMacaddress(),
  218. Address: data.GetAddress(),
  219. Address6: data.GetAddress6(),
  220. Endpoint: data.GetEndpoint(),
  221. Network: data.GetNodenetwork(),
  222. Password: data.GetPassword(),
  223. LocalAddress: data.GetLocaladdress(),
  224. ListenPort: data.GetListenport(),
  225. PersistentKeepalive: data.GetKeepalive(),
  226. PublicKey: data.GetPublickey(),
  227. }
  228. checkinresponse, err := NodeCheckIn(node, node.Network)
  229. if err != nil {
  230. // return internal gRPC error to be handled later
  231. if checkinresponse == (models.CheckInResponse{}) || !checkinresponse.IsPending {
  232. return nil, status.Errorf(
  233. codes.Internal,
  234. fmt.Sprintf("Internal error: %v", err),
  235. )
  236. }
  237. }
  238. // return the node in a CreateNodeRes type
  239. response := &nodepb.CheckInRes{
  240. Checkinresponse: &nodepb.CheckInResponse{
  241. Success: checkinresponse.Success,
  242. Needpeerupdate: checkinresponse.NeedPeerUpdate,
  243. Needdelete: checkinresponse.NeedDelete,
  244. Needconfigupdate: checkinresponse.NeedConfigUpdate,
  245. Needkeyupdate: checkinresponse.NeedKeyUpdate,
  246. Nodemessage: checkinresponse.NodeMessage,
  247. Ispending: checkinresponse.IsPending,
  248. },
  249. }
  250. return response, nil
  251. }
  252. func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNodeReq) (*nodepb.UpdateNodeRes, error) {
  253. // Get the node data from the request
  254. data := req.GetNode()
  255. // Now we have to convert this into a NodeItem type to convert into BSON
  256. nodechange := models.NodeUpdate{
  257. // ID: primitive.NilObjectID,
  258. MacAddress: data.GetMacaddress(),
  259. Name: data.GetName(),
  260. Address: data.GetAddress(),
  261. Address6: data.GetAddress6(),
  262. LocalAddress: data.GetLocaladdress(),
  263. Endpoint: data.GetEndpoint(),
  264. Password: data.GetPassword(),
  265. PersistentKeepalive: data.GetKeepalive(),
  266. Network: data.GetNodenetwork(),
  267. Interface: data.GetInterface(),
  268. PostDown: data.GetPostdown(),
  269. PostUp: data.GetPostup(),
  270. IsPending: data.GetIspending(),
  271. PublicKey: data.GetPublickey(),
  272. ListenPort: data.GetListenport(),
  273. }
  274. // Convert the Id string to a MongoDB ObjectId
  275. macaddress := nodechange.MacAddress
  276. networkName := nodechange.Network
  277. network, _ := functions.GetParentNetwork(networkName)
  278. err := ValidateNodeUpdate(networkName, nodechange)
  279. if err != nil {
  280. return nil, err
  281. }
  282. node, err := functions.GetNodeByMacAddress(networkName, macaddress)
  283. if err != nil {
  284. return nil, status.Errorf(
  285. codes.NotFound,
  286. fmt.Sprintf("Could not find node with supplied Mac Address: %v", err),
  287. )
  288. }
  289. newnode, err := UpdateNode(nodechange, node)
  290. if err != nil {
  291. return nil, status.Errorf(
  292. codes.NotFound,
  293. fmt.Sprintf("Could not find node with supplied Mac Address: %v", err),
  294. )
  295. }
  296. dualvar := false
  297. if network.IsDualStack != nil {
  298. dualvar = *network.IsDualStack
  299. }
  300. localvar := false
  301. if network.IsLocal != nil {
  302. localvar = *network.IsLocal
  303. }
  304. return &nodepb.UpdateNodeRes{
  305. Node: &nodepb.Node{
  306. Macaddress: newnode.MacAddress,
  307. Localaddress: newnode.LocalAddress,
  308. Name: newnode.Name,
  309. Address: newnode.Address,
  310. Address6: newnode.Address6,
  311. Endpoint: newnode.Endpoint,
  312. Password: newnode.Password,
  313. Interface: newnode.Interface,
  314. Postdown: newnode.PostDown,
  315. Postup: newnode.PostUp,
  316. Nodenetwork: newnode.Network,
  317. Ispending: newnode.IsPending,
  318. Publickey: newnode.PublicKey,
  319. Dnsoff: !servercfg.IsDNSMode(),
  320. Listenport: newnode.ListenPort,
  321. Keepalive: newnode.PersistentKeepalive,
  322. Islocal: localvar,
  323. Isdualstack: dualvar,
  324. Localrange: network.LocalRange,
  325. },
  326. }, nil
  327. }
  328. func (s *NodeServiceServer) DeleteNode(ctx context.Context, req *nodepb.DeleteNodeReq) (*nodepb.DeleteNodeRes, error) {
  329. fmt.Println("beginning node delete")
  330. macaddress := req.GetMacaddress()
  331. network := req.GetNetworkName()
  332. success, err := DeleteNode(macaddress, network)
  333. if err != nil || !success {
  334. fmt.Println("Error deleting node.")
  335. fmt.Println(err)
  336. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not find/delete node with mac address %s", macaddress))
  337. }
  338. fmt.Println("updating network last modified of " + req.GetNetworkName())
  339. err = SetNetworkNodesLastModified(req.GetNetworkName())
  340. if err != nil {
  341. fmt.Println("Error updating Network")
  342. fmt.Println(err)
  343. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not update network last modified date: %v", err))
  344. }
  345. return &nodepb.DeleteNodeRes{
  346. Success: true,
  347. }, nil
  348. }
  349. func (s *NodeServiceServer) GetPeers(req *nodepb.GetPeersReq, stream nodepb.NodeService_GetPeersServer) error {
  350. // Initiate a NodeItem type to write decoded data to
  351. //data := &models.PeersResponse{}
  352. // collection.Find returns a cursor for our (empty) query
  353. //cursor, err := s.NodeDB.Find(context.Background(), bson.M{})
  354. peers, err := GetPeersList(req.GetNetwork())
  355. if err != nil {
  356. return status.Errorf(codes.Internal, fmt.Sprintf("Unknown internal error: %v", err))
  357. }
  358. // cursor.Next() returns a boolean, if false there are no more items and loop will break
  359. for i := 0; i < len(peers); i++ {
  360. // If no error is found send node over stream
  361. stream.Send(&nodepb.GetPeersRes{
  362. Peers: &nodepb.PeersResponse{
  363. Address: peers[i].Address,
  364. Address6: peers[i].Address6,
  365. Endpoint: peers[i].Endpoint,
  366. Egressgatewayrange: peers[i].EgressGatewayRange,
  367. Isegressgateway: peers[i].IsEgressGateway,
  368. Publickey: peers[i].PublicKey,
  369. Keepalive: peers[i].KeepAlive,
  370. Listenport: peers[i].ListenPort,
  371. Localaddress: peers[i].LocalAddress,
  372. },
  373. })
  374. }
  375. node, err := functions.GetNodeByMacAddress(req.GetNetwork(), req.GetMacaddress())
  376. if err != nil {
  377. return status.Errorf(codes.Internal, fmt.Sprintf("Could not get node: %v", err))
  378. }
  379. err = TimestampNode(node, false, true, false)
  380. if err != nil {
  381. return status.Errorf(codes.Internal, fmt.Sprintf("Internal error occurred: %v", err))
  382. }
  383. return nil
  384. }
  385. func (s *NodeServiceServer) GetExtPeers(req *nodepb.GetExtPeersReq, stream nodepb.NodeService_GetExtPeersServer) error {
  386. // Initiate a NodeItem type to write decoded data to
  387. //data := &models.PeersResponse{}
  388. // collection.Find returns a cursor for our (empty) query
  389. //cursor, err := s.NodeDB.Find(context.Background(), bson.M{})
  390. peers, err := GetExtPeersList(req.GetNetwork(), req.GetMacaddress())
  391. if err != nil {
  392. return status.Errorf(codes.Internal, fmt.Sprintf("Unknown internal error: %v", err))
  393. }
  394. // cursor.Next() returns a boolean, if false there are no more items and loop will break
  395. for i := 0; i < len(peers); i++ {
  396. // If no error is found send node over stream
  397. stream.Send(&nodepb.GetExtPeersRes{
  398. Extpeers: &nodepb.ExtPeersResponse{
  399. Address: peers[i].Address,
  400. Address6: peers[i].Address6,
  401. Endpoint: peers[i].Endpoint,
  402. Publickey: peers[i].PublicKey,
  403. Keepalive: peers[i].KeepAlive,
  404. Listenport: peers[i].ListenPort,
  405. Localaddress: peers[i].LocalAddress,
  406. },
  407. })
  408. }
  409. node, err := functions.GetNodeByMacAddress(req.GetNetwork(), req.GetMacaddress())
  410. if err != nil {
  411. return status.Errorf(codes.Internal, fmt.Sprintf("Could not get node: %v", err))
  412. }
  413. err = TimestampNode(node, false, true, false)
  414. if err != nil {
  415. return status.Errorf(codes.Internal, fmt.Sprintf("Internal error occurred: %v", err))
  416. }
  417. return nil
  418. }