Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #3211

Merged
merged 12 commits into from
Nov 18, 2024
5 changes: 5 additions & 0 deletions controllers/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func deleteTag(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
// check if active policy is using the tag
if logic.CheckIfTagAsActivePolicy(tag.ID, tag.Network) {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("tag is currently in use by an active policy"), "badrequest"))
return
}
err = logic.DeleteTag(models.TagID(tagID), true)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
Expand Down
19 changes: 19 additions & 0 deletions logic/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,25 @@ func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
}
}

func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
acls := listDevicePolicies(netID)
for _, acl := range acls {
for _, srcTagI := range acl.Src {
if srcTagI.ID == models.DeviceAclID {
if tagID.String() == srcTagI.Value {
return true
}
}
}
for _, dstTagI := range acl.Dst {
if dstTagI.ID == models.DeviceAclID {
return true
}
}
}
return false
}

// RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
acls := listDevicePolicies(netID)
Expand Down
3 changes: 3 additions & 0 deletions logic/acls/nodeacls/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func CreateNodeACL(networkID NetworkID, nodeID NodeID, defaultVal byte) (acls.AC
acls.AclMutex.Lock()
var newNodeACL = make(acls.ACL)
for existingNodeID := range currentNetworkACL {
if currentNetworkACL[existingNodeID] == nil {
currentNetworkACL[existingNodeID] = make(acls.ACL)
}
currentNetworkACL[existingNodeID][acls.AclID(nodeID)] = defaultVal // set the old nodes to default value for new node
newNodeACL[existingNodeID] = defaultVal // set the old nodes in new node ACL to default value
}
Expand Down
32 changes: 31 additions & 1 deletion logic/extpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package logic

import (
"encoding/json"
"errors"
"fmt"
"net"
"reflect"
"strings"
"sync"
"time"

"github.com/goombaio/namegenerator"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic/acls"
Expand Down Expand Up @@ -281,13 +283,41 @@ func CreateExtClient(extclient *models.ExtClient) error {
}

if extclient.ClientID == "" {
extclient.ClientID = models.GenerateNodeName()
extclient.ClientID, err = GenerateNodeName(extclient.Network)
if err != nil {
return err
}
}

extclient.LastModified = time.Now().Unix()
return SaveExtClient(extclient)
}

// GenerateNodeName - generates a random node name
func GenerateNodeName(network string) (string, error) {
seed := time.Now().UTC().UnixNano()
nameGenerator := namegenerator.NewNameGenerator(seed)
var name string
cnt := 0
for {
if cnt > 10 {
return "", errors.New("couldn't generate random name, try again")
}
cnt += 1
name = nameGenerator.Generate()
if len(name) > 15 {
continue
}
_, err := GetExtClient(name, network)
if err == nil {
// config exists with same name
continue
}
break
}
return name, nil
}

// SaveExtClient - saves an ext client to database
func SaveExtClient(extclient *models.ExtClient) error {
key, err := GetRecordKey(extclient.ClientID, extclient.Network)
Expand Down
7 changes: 7 additions & 0 deletions logic/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
peerEndpoint = peerHost.EndpointIPv6
}
}
if node.IsRelay && peer.RelayedBy == node.ID.String() && !peer.IsStatic {
// don't set endpoint on relayed peer
peerEndpoint = nil
}
if isFailOverPeer && peer.FailedOverBy == node.ID && !peer.IsStatic {
peerEndpoint = nil
}

peerConfig.Endpoint = &net.UDPAddr{
IP: peerEndpoint,
Expand Down
Loading