|
@@ -690,188 +690,6 @@ func RemoveUserFromAclPolicy(userName string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,
|
|
|
|
-func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
|
|
|
|
- var nodeId, peerId string
|
|
|
|
- // if peer.IsFailOver && node.FailedOverBy != uuid.Nil && node.FailedOverBy == peer.ID {
|
|
|
|
- // return true, []models.Acl{}
|
|
|
|
- // }
|
|
|
|
- // if node.IsFailOver && peer.FailedOverBy != uuid.Nil && peer.FailedOverBy == node.ID {
|
|
|
|
- // return true, []models.Acl{}
|
|
|
|
- // }
|
|
|
|
- // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
|
|
|
|
- // return true, []models.Acl{}
|
|
|
|
- // }
|
|
|
|
- // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
|
|
|
|
- // return true, []models.Acl{}
|
|
|
|
- // }
|
|
|
|
- if node.IsStatic {
|
|
|
|
- nodeId = node.StaticNode.ClientID
|
|
|
|
- node = node.StaticNode.ConvertToStaticNode()
|
|
|
|
- } else {
|
|
|
|
- nodeId = node.ID.String()
|
|
|
|
- }
|
|
|
|
- if peer.IsStatic {
|
|
|
|
- peerId = peer.StaticNode.ClientID
|
|
|
|
- peer = peer.StaticNode.ConvertToStaticNode()
|
|
|
|
- } else {
|
|
|
|
- peerId = peer.ID.String()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var nodeTags, peerTags map[models.TagID]struct{}
|
|
|
|
- if node.Mutex != nil {
|
|
|
|
- node.Mutex.Lock()
|
|
|
|
- nodeTags = maps.Clone(node.Tags)
|
|
|
|
- node.Mutex.Unlock()
|
|
|
|
- } else {
|
|
|
|
- nodeTags = node.Tags
|
|
|
|
- }
|
|
|
|
- if peer.Mutex != nil {
|
|
|
|
- peer.Mutex.Lock()
|
|
|
|
- peerTags = maps.Clone(peer.Tags)
|
|
|
|
- peer.Mutex.Unlock()
|
|
|
|
- } else {
|
|
|
|
- peerTags = peer.Tags
|
|
|
|
- }
|
|
|
|
- if nodeTags == nil {
|
|
|
|
- nodeTags = make(map[models.TagID]struct{})
|
|
|
|
- }
|
|
|
|
- if peerTags == nil {
|
|
|
|
- peerTags = make(map[models.TagID]struct{})
|
|
|
|
- }
|
|
|
|
- nodeTags[models.TagID(nodeId)] = struct{}{}
|
|
|
|
- peerTags[models.TagID(peerId)] = struct{}{}
|
|
|
|
- if checkDefaultPolicy {
|
|
|
|
- // check default policy if all allowed return true
|
|
|
|
- defaultPolicy, err := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
|
|
|
- if err == nil {
|
|
|
|
- if defaultPolicy.Enabled {
|
|
|
|
- return true, []models.Acl{defaultPolicy}
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- allowedPolicies := []models.Acl{}
|
|
|
|
- defer func() {
|
|
|
|
- allowedPolicies = logic.UniquePolicies(allowedPolicies)
|
|
|
|
- }()
|
|
|
|
- // list device policies
|
|
|
|
- policies := logic.ListDevicePolicies(models.NetworkID(peer.Network))
|
|
|
|
- srcMap := make(map[string]struct{})
|
|
|
|
- dstMap := make(map[string]struct{})
|
|
|
|
- defer func() {
|
|
|
|
- srcMap = nil
|
|
|
|
- dstMap = nil
|
|
|
|
- }()
|
|
|
|
- for _, policy := range policies {
|
|
|
|
- if !policy.Enabled {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- allowed := false
|
|
|
|
- srcMap = logic.ConvAclTagToValueMap(policy.Src)
|
|
|
|
- dstMap = logic.ConvAclTagToValueMap(policy.Dst)
|
|
|
|
- for _, dst := range policy.Dst {
|
|
|
|
- if dst.ID == models.EgressID {
|
|
|
|
- e := schema.Egress{ID: dst.Value}
|
|
|
|
- err := e.Get(db.WithContext(context.TODO()))
|
|
|
|
- if err == nil && e.Status {
|
|
|
|
- for nodeID := range e.Nodes {
|
|
|
|
- dstMap[nodeID] = struct{}{}
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- _, srcAll := srcMap["*"]
|
|
|
|
- _, dstAll := dstMap["*"]
|
|
|
|
- if policy.AllowedDirection == models.TrafficDirectionBi {
|
|
|
|
- if _, ok := srcMap[nodeId]; ok || srcAll {
|
|
|
|
- if _, ok := dstMap[peerId]; ok || dstAll {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- if _, ok := dstMap[nodeId]; ok || dstAll {
|
|
|
|
- if _, ok := srcMap[peerId]; ok || srcAll {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if _, ok := dstMap[peerId]; ok || dstAll {
|
|
|
|
- if _, ok := srcMap[nodeId]; ok || srcAll {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if policy.AllowedDirection == models.TrafficDirectionBi {
|
|
|
|
-
|
|
|
|
- for tagID := range nodeTags {
|
|
|
|
-
|
|
|
|
- if _, ok := dstMap[tagID.String()]; ok || dstAll {
|
|
|
|
- if srcAll {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- for tagID := range peerTags {
|
|
|
|
- if _, ok := srcMap[tagID.String()]; ok {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if allowed {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- if _, ok := srcMap[tagID.String()]; ok || srcAll {
|
|
|
|
- if dstAll {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- for tagID := range peerTags {
|
|
|
|
- if _, ok := dstMap[tagID.String()]; ok {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if allowed {
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if allowed {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- for tagID := range peerTags {
|
|
|
|
- if _, ok := dstMap[tagID.String()]; ok || dstAll {
|
|
|
|
- if srcAll {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- for tagID := range nodeTags {
|
|
|
|
- if _, ok := srcMap[tagID.String()]; ok {
|
|
|
|
- allowed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if allowed {
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if allowed {
|
|
|
|
- allowedPolicies = append(allowedPolicies, policy)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(allowedPolicies) > 0 {
|
|
|
|
- return true, allowedPolicies
|
|
|
|
- }
|
|
|
|
- return false, allowedPolicies
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// UpdateDeviceTag - updates device tag on acl policies
|
|
// UpdateDeviceTag - updates device tag on acl policies
|
|
func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
|
|
func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
|
|
acls := logic.ListDevicePolicies(netID)
|
|
acls := logic.ListDevicePolicies(netID)
|
|
@@ -1465,47 +1283,6 @@ func GetAclRulesForNode(targetnodeI *models.Node) (rules map[string]models.AclRu
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // if existsInDstTag && existsInSrcTag {
|
|
|
|
- // nodes := taggedNodes[nodeTag]
|
|
|
|
- // for srcID := range srcTags {
|
|
|
|
- // if srcID == targetnode.ID.String() {
|
|
|
|
- // continue
|
|
|
|
- // }
|
|
|
|
- // node, err := GetNodeByID(srcID)
|
|
|
|
- // if err == nil {
|
|
|
|
- // nodes = append(nodes, node)
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // for dstID := range dstTags {
|
|
|
|
- // if dstID == targetnode.ID.String() {
|
|
|
|
- // continue
|
|
|
|
- // }
|
|
|
|
- // node, err := GetNodeByID(dstID)
|
|
|
|
- // if err == nil {
|
|
|
|
- // nodes = append(nodes, node)
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // for _, node := range nodes {
|
|
|
|
- // if node.ID == targetnode.ID {
|
|
|
|
- // continue
|
|
|
|
- // }
|
|
|
|
- // if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
|
|
|
|
- // continue
|
|
|
|
- // }
|
|
|
|
- // if node.Address.IP != nil {
|
|
|
|
- // aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
|
|
|
|
- // }
|
|
|
|
- // if node.Address6.IP != nil {
|
|
|
|
- // aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
|
|
|
|
- // }
|
|
|
|
- // if node.IsStatic && node.StaticNode.Address != "" {
|
|
|
|
- // aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
|
|
|
|
- // }
|
|
|
|
- // if node.IsStatic && node.StaticNode.Address6 != "" {
|
|
|
|
- // aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
} else {
|
|
} else {
|
|
_, all := dstTags["*"]
|
|
_, all := dstTags["*"]
|
|
if _, ok := dstTags[nodeTag.String()]; ok || all {
|
|
if _, ok := dstTags[nodeTag.String()]; ok || all {
|