mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-12 08:04:25 +08:00
prevent nodes from changing address out of range or to .0 or .255 addresses
This commit is contained in:
parent
de8c4d782d
commit
2430eb0a47
6 changed files with 43 additions and 18 deletions
|
@ -82,7 +82,6 @@ func grpcAuthorize(ctx context.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return status.Errorf(codes.Unauthenticated, "Unauthorized. Network does not exist: "+network)
|
return status.Errorf(codes.Unauthenticated, "Unauthorized. Network does not exist: "+network)
|
||||||
}
|
}
|
||||||
emptynode := models.Node{}
|
|
||||||
node, err := logic.GetNodeByIDorMacAddress(nodeID, mac, network)
|
node, err := logic.GetNodeByIDorMacAddress(nodeID, mac, network)
|
||||||
if database.IsEmptyRecord(err) {
|
if database.IsEmptyRecord(err) {
|
||||||
// == DELETE replace logic after 2 major version updates ==
|
// == DELETE replace logic after 2 major version updates ==
|
||||||
|
@ -94,7 +93,7 @@ func grpcAuthorize(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
return status.Errorf(codes.Unauthenticated, "Empty record")
|
return status.Errorf(codes.Unauthenticated, "Empty record")
|
||||||
}
|
}
|
||||||
if err != nil || node.MacAddress == emptynode.MacAddress {
|
if err != nil || node.ID == "" {
|
||||||
return status.Errorf(codes.Unauthenticated, "Node does not exist.")
|
return status.Errorf(codes.Unauthenticated, "Node does not exist.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -570,7 +570,7 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
|
||||||
returnErrorResponse(w, r, formatError(err, "internal"))
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Log(1, r.Header.Get("user"), "updated node", node.MacAddress, "on network", node.Network)
|
logger.Log(1, r.Header.Get("user"), "updated node", node.ID)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(newNode)
|
json.NewEncoder(w).Encode(newNode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,13 @@ package controller
|
||||||
import (
|
import (
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/logic"
|
"github.com/gravitl/netmaker/logic"
|
||||||
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runServerPeerUpdate(network string, shouldPeerUpdate bool) error {
|
func runServerPeerUpdate(network string, shouldPeerUpdate bool) error {
|
||||||
|
if servercfg.IsClientMode() != "on" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
var currentServerNodeID, err = logic.GetNetworkServerNodeID(network)
|
var currentServerNodeID, err = logic.GetNetworkServerNodeID(network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -111,6 +111,13 @@ func IsLeader(node *models.Node) bool {
|
||||||
|
|
||||||
// UpdateNode - takes a node and updates another node with it's values
|
// UpdateNode - takes a node and updates another node with it's values
|
||||||
func UpdateNode(currentNode *models.Node, newNode *models.Node) error {
|
func UpdateNode(currentNode *models.Node, newNode *models.Node) error {
|
||||||
|
if newNode.Address != currentNode.Address {
|
||||||
|
if network, err := GetParentNetwork(newNode.Network); err == nil {
|
||||||
|
if !IsAddressInCIDR(newNode.Address, network.AddressRange) {
|
||||||
|
return fmt.Errorf("invalid address provided; out of network range for node %s", newNode.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
newNode.Fill(currentNode)
|
newNode.Fill(currentNode)
|
||||||
if err := ValidateNode(newNode, true); err != nil {
|
if err := ValidateNode(newNode, true); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -4,7 +4,9 @@ package logic
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -39,6 +41,29 @@ func FileExists(f string) bool {
|
||||||
return !info.IsDir()
|
return !info.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
||||||
|
func IsAddressInCIDR(address, cidr string) bool {
|
||||||
|
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
||||||
|
if cidrErr != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var addrParts = strings.Split(address, ".")
|
||||||
|
var addrPartLength = len(addrParts)
|
||||||
|
if addrPartLength != 4 {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
if addrParts[addrPartLength-1] == "0" ||
|
||||||
|
addrParts[addrPartLength-1] == "255" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ip, _, err := net.ParseCIDR(fmt.Sprintf("%s/32", address))
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return currentCIDR.Contains(ip)
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteNodeByMacAddress - deletes a node from database or moves into delete nodes table
|
// DeleteNodeByMacAddress - deletes a node from database or moves into delete nodes table
|
||||||
func DeleteNodeByMacAddress(node *models.Node, exterminate bool) error {
|
func DeleteNodeByMacAddress(node *models.Node, exterminate bool) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -310,21 +310,12 @@ func IsAgentBackend() bool {
|
||||||
// IsClientMode - checks if it should run in client mode
|
// IsClientMode - checks if it should run in client mode
|
||||||
func IsClientMode() string {
|
func IsClientMode() string {
|
||||||
isclient := "on"
|
isclient := "on"
|
||||||
if os.Getenv("CLIENT_MODE") != "" {
|
|
||||||
if os.Getenv("CLIENT_MODE") == "off" {
|
if os.Getenv("CLIENT_MODE") == "off" {
|
||||||
isclient = "off"
|
isclient = "off"
|
||||||
}
|
}
|
||||||
if os.Getenv("CLIENT_MODE") == "contained" {
|
|
||||||
isclient = "contained"
|
|
||||||
}
|
|
||||||
} else if config.Config.Server.ClientMode != "" {
|
|
||||||
if config.Config.Server.ClientMode == "off" {
|
if config.Config.Server.ClientMode == "off" {
|
||||||
isclient = "off"
|
isclient = "off"
|
||||||
}
|
}
|
||||||
if config.Config.Server.ClientMode == "contained" {
|
|
||||||
isclient = "contained"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return isclient
|
return isclient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue