|
@@ -4,6 +4,7 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "net"
|
|
"sort"
|
|
"sort"
|
|
"sync"
|
|
"sync"
|
|
"time"
|
|
"time"
|
|
@@ -674,19 +675,17 @@ func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) map[string][]models.AclRule {
|
|
|
|
|
|
+func GetAclRulesForNode(node *models.Node) (rules map[string]models.AclRule) {
|
|
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
|
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
|
- rules = make(map[string][]models.AclRule)
|
|
|
|
|
|
+ rules = make(map[string]models.AclRule)
|
|
if err == nil && defaultPolicy.Enabled {
|
|
if err == nil && defaultPolicy.Enabled {
|
|
- return map[string][]models.AclRule{
|
|
|
|
|
|
+ return map[string]models.AclRule{
|
|
defaultPolicy.ID: {
|
|
defaultPolicy.ID: {
|
|
- {
|
|
|
|
- SrcIP: node.NetworkRange,
|
|
|
|
- SrcIP6: node.NetworkRange6,
|
|
|
|
- Proto: []models.Protocol{models.ALL},
|
|
|
|
- Direction: models.TrafficDirectionBi,
|
|
|
|
- Allowed: true,
|
|
|
|
- },
|
|
|
|
|
|
+ IPList: []net.IPNet{node.NetworkRange},
|
|
|
|
+ IP6List: []net.IPNet{node.NetworkRange6},
|
|
|
|
+ AllowedProtocols: []models.Protocol{models.ALL},
|
|
|
|
+ Direction: models.TrafficDirectionBi,
|
|
|
|
+ Allowed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -701,36 +700,38 @@ func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) ma
|
|
}
|
|
}
|
|
srcTags := convAclTagToValueMap(acl.Src)
|
|
srcTags := convAclTagToValueMap(acl.Src)
|
|
dstTags := convAclTagToValueMap(acl.Dst)
|
|
dstTags := convAclTagToValueMap(acl.Dst)
|
|
- aclRules := []models.AclRule{}
|
|
|
|
|
|
+ aclRule := models.AclRule{
|
|
|
|
+ ID: acl.ID,
|
|
|
|
+ AllowedProtocols: acl.Proto,
|
|
|
|
+ AllowedPorts: acl.Port,
|
|
|
|
+ Direction: acl.AllowedDirection,
|
|
|
|
+ Allowed: true,
|
|
|
|
+ }
|
|
if acl.AllowedDirection == models.TrafficDirectionBi {
|
|
if acl.AllowedDirection == models.TrafficDirectionBi {
|
|
var existsInSrcTag bool
|
|
var existsInSrcTag bool
|
|
var existsInDstTag bool
|
|
var existsInDstTag bool
|
|
// if contains all resources, return entire cidr
|
|
// if contains all resources, return entire cidr
|
|
if _, ok := srcTags["*"]; ok {
|
|
if _, ok := srcTags["*"]; ok {
|
|
- return map[string][]models.AclRule{
|
|
|
|
|
|
+ return map[string]models.AclRule{
|
|
acl.ID: {
|
|
acl.ID: {
|
|
- {
|
|
|
|
- SrcIP: node.NetworkRange,
|
|
|
|
- SrcIP6: node.NetworkRange6,
|
|
|
|
- Proto: []models.Protocol{models.ALL},
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- },
|
|
|
|
|
|
+ IPList: []net.IPNet{node.NetworkRange},
|
|
|
|
+ IP6List: []net.IPNet{node.NetworkRange6},
|
|
|
|
+ AllowedProtocols: []models.Protocol{models.ALL},
|
|
|
|
+ AllowedPorts: acl.Port,
|
|
|
|
+ Direction: acl.AllowedDirection,
|
|
|
|
+ Allowed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if _, ok := dstTags["*"]; ok {
|
|
if _, ok := dstTags["*"]; ok {
|
|
- return map[string][]models.AclRule{
|
|
|
|
|
|
+ return map[string]models.AclRule{
|
|
acl.ID: {
|
|
acl.ID: {
|
|
- {
|
|
|
|
- SrcIP: node.NetworkRange,
|
|
|
|
- SrcIP6: node.NetworkRange6,
|
|
|
|
- Proto: []models.Protocol{models.ALL},
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- },
|
|
|
|
|
|
+ IPList: []net.IPNet{node.NetworkRange},
|
|
|
|
+ IP6List: []net.IPNet{node.NetworkRange6},
|
|
|
|
+ AllowedProtocols: []models.Protocol{models.ALL},
|
|
|
|
+ AllowedPorts: acl.Port,
|
|
|
|
+ Direction: acl.AllowedDirection,
|
|
|
|
+ Allowed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -741,6 +742,7 @@ func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) ma
|
|
if _, ok := dstTags[nodeTag.String()]; ok {
|
|
if _, ok := dstTags[nodeTag.String()]; ok {
|
|
existsInDstTag = true
|
|
existsInDstTag = true
|
|
}
|
|
}
|
|
|
|
+
|
|
if existsInSrcTag {
|
|
if existsInSrcTag {
|
|
// get all dst tags
|
|
// get all dst tags
|
|
for dst := range dstTags {
|
|
for dst := range dstTags {
|
|
@@ -750,17 +752,14 @@ func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) ma
|
|
// Get peers in the tags and add allowed rules
|
|
// Get peers in the tags and add allowed rules
|
|
nodes := taggedNodes[models.TagID(dst)]
|
|
nodes := taggedNodes[models.TagID(dst)]
|
|
for _, node := range nodes {
|
|
for _, node := range nodes {
|
|
- aclRules = append(aclRules, models.AclRule{
|
|
|
|
- SrcIP: node.Address,
|
|
|
|
- SrcIP6: node.Address6,
|
|
|
|
- Proto: acl.Proto,
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- })
|
|
|
|
|
|
+ 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 existsInDstTag {
|
|
if existsInDstTag {
|
|
// get all src tags
|
|
// get all src tags
|
|
@@ -771,28 +770,24 @@ func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) ma
|
|
// Get peers in the tags and add allowed rules
|
|
// Get peers in the tags and add allowed rules
|
|
nodes := taggedNodes[models.TagID(src)]
|
|
nodes := taggedNodes[models.TagID(src)]
|
|
for _, node := range nodes {
|
|
for _, node := range nodes {
|
|
- aclRules = append(aclRules, models.AclRule{
|
|
|
|
- SrcIP: node.Address,
|
|
|
|
- SrcIP6: node.Address6,
|
|
|
|
- Proto: acl.Proto,
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- })
|
|
|
|
|
|
+ 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 existsInDstTag && existsInSrcTag {
|
|
if existsInDstTag && existsInSrcTag {
|
|
nodes := taggedNodes[nodeTag]
|
|
nodes := taggedNodes[nodeTag]
|
|
for _, node := range nodes {
|
|
for _, node := range nodes {
|
|
- aclRules = append(aclRules, models.AclRule{
|
|
|
|
- SrcIP: node.Address,
|
|
|
|
- SrcIP6: node.Address6,
|
|
|
|
- Proto: acl.Proto,
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- })
|
|
|
|
|
|
+ if node.Address.IP != nil {
|
|
|
|
+ aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
|
|
|
|
+ }
|
|
|
|
+ if node.Address6.IP != nil {
|
|
|
|
+ aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -805,20 +800,18 @@ func GetAclRulesForNode(node *models.Node, rules map[string][]models.AclRule) ma
|
|
// Get peers in the tags and add allowed rules
|
|
// Get peers in the tags and add allowed rules
|
|
nodes := taggedNodes[models.TagID(src)]
|
|
nodes := taggedNodes[models.TagID(src)]
|
|
for _, node := range nodes {
|
|
for _, node := range nodes {
|
|
- aclRules = append(aclRules, models.AclRule{
|
|
|
|
- SrcIP: node.Address,
|
|
|
|
- SrcIP6: node.Address6,
|
|
|
|
- Proto: acl.Proto,
|
|
|
|
- Port: acl.Port,
|
|
|
|
- Direction: acl.AllowedDirection,
|
|
|
|
- Allowed: true,
|
|
|
|
- })
|
|
|
|
|
|
+ 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 len(aclRules) > 0 {
|
|
|
|
- rules[acl.ID] = aclRules
|
|
|
|
|
|
+ if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
|
|
|
|
+ rules[acl.ID] = aclRule
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|