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

Net 1784 user rules #3238

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func aclDebug(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
allowed := logic.IsNodeAllowedToCommunicate(node, peer)
allowed, _ := logic.IsNodeAllowedToCommunicate(node, peer)
logic.ReturnSuccessResponseWithJson(w, r, allowed, "fetched all acls in the network ")
}

Expand Down
146 changes: 93 additions & 53 deletions logic/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@
}
acl, err = GetAcl(fmt.Sprintf("%s.%s", netID, "all-users"))
if err == nil {
if acl.Proto.String() == "" {
acl.Proto = models.ALL
acl.ServiceType = models.Custom
acl.Port = []string{}
UpsertAcl(acl)
}
//if acl.Proto.String() == "" {
acl.Proto = models.ALL
acl.ServiceType = models.Custom
acl.Port = []string{}
UpsertAcl(acl)
//}
}
acl, err = GetAcl(fmt.Sprintf("%s.%s", netID, "all-remote-access-gws"))
if err == nil {
if acl.Proto.String() == "" {
acl.Proto = models.ALL
acl.ServiceType = models.Custom
acl.Port = []string{}
UpsertAcl(acl)
}
//if acl.Proto.String() == "" {
acl.Proto = models.ALL
acl.ServiceType = models.Custom
acl.Port = []string{}
UpsertAcl(acl)
//}
}
}

Expand Down Expand Up @@ -526,40 +526,45 @@
}

// IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
func IsUserAllowedToCommunicate(userName string, peer models.Node) bool {
func IsUserAllowedToCommunicate(userName string, peer models.Node) (bool, []models.Acl) {
if peer.IsStatic {
peer = peer.StaticNode.ConvertToStaticNode()
}
acl, _ := GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
if acl.Enabled {
return true
return true, []models.Acl{acl}
}
user, err := GetUser(userName)
if err != nil {
return false
return false, []models.Acl{}
}

allowedPolicies := []models.Acl{}
policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
for _, policy := range policies {
if !policy.Enabled {
continue
}
dstMap := convAclTagToValueMap(policy.Dst)
if _, ok := dstMap["*"]; ok {
return true
allowedPolicies = append(allowedPolicies, policy)
continue
}
for tagID := range peer.Tags {
if _, ok := dstMap[tagID.String()]; ok {
return true
allowedPolicies = append(allowedPolicies, policy)
break
}
}

}
return false
if len(allowedPolicies) > 0 {
return true, allowedPolicies
}
return false, []models.Acl{}
}

// IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer
func IsNodeAllowedToCommunicate(node, peer models.Node) bool {
func IsNodeAllowedToCommunicate(node, peer models.Node) (bool, []models.Acl) {
if node.IsStatic {
node = node.StaticNode.ConvertToStaticNode()
}
Expand All @@ -570,10 +575,10 @@
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
if err == nil {
if defaultPolicy.Enabled {
return true
return true, []models.Acl{defaultPolicy}
}
}

allowedPolicies := []models.Acl{}
// list device policies
policies := listDevicePolicies(models.NetworkID(peer.Network))
for _, policy := range policies {
Expand All @@ -587,52 +592,86 @@
// fmt.Printf("\n======> node Tags: %+v\n", node.Tags)
// fmt.Printf("\n======> peer Tags: %+v\n", peer.Tags)
for tagID := range node.Tags {
allowed := false
if _, ok := dstMap[tagID.String()]; ok {
if _, ok := srcMap["*"]; ok {
return true
allowed = true
allowedPolicies = append(allowedPolicies, policy)
break
}
for tagID := range peer.Tags {
if _, ok := srcMap[tagID.String()]; ok {
return true
allowed = true
break
}
}
}
if allowed {
allowedPolicies = append(allowedPolicies, policy)
break
}
if _, ok := srcMap[tagID.String()]; ok {
if _, ok := dstMap["*"]; ok {
return true
allowed = true
allowedPolicies = append(allowedPolicies, policy)
break
}
for tagID := range peer.Tags {
if _, ok := dstMap[tagID.String()]; ok {
return true
allowed = true
break
}
}
}
if allowed {
allowedPolicies = append(allowedPolicies, policy)
break
}
}
for tagID := range peer.Tags {
allowed := false
if _, ok := dstMap[tagID.String()]; ok {
if _, ok := srcMap["*"]; ok {
return true
allowed = true
allowedPolicies = append(allowedPolicies, policy)
break
}
for tagID := range node.Tags {

if _, ok := srcMap[tagID.String()]; ok {
return true
allowed = true
break
}
}
}
if allowed {
allowedPolicies = append(allowedPolicies, policy)
break
}

if _, ok := srcMap[tagID.String()]; ok {
if _, ok := dstMap["*"]; ok {
return true
allowed = true
allowedPolicies = append(allowedPolicies, policy)
break
}
for tagID := range node.Tags {
if _, ok := dstMap[tagID.String()]; ok {
return true
allowed = true
break
}
}
}
if allowed {
allowedPolicies = append(allowedPolicies, policy)
break
}
}
}
return false
if len(allowedPolicies) > 0 {
return true, allowedPolicies
}
return false, allowedPolicies
}

// SortTagEntrys - Sorts slice of Tag entries by their id
Expand Down Expand Up @@ -720,13 +759,14 @@
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
rules = make(map[string]models.AclRule)
if err == nil && defaultPolicy.Enabled {
return
return map[string]models.AclRule{

Check failure on line 763 in logic/acls.go

View workflow job for this annotation

GitHub Actions / tests

unreachable code
defaultPolicy.ID: {
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocols: models.ALL,
Direction: models.TrafficDirectionBi,
Allowed: true,
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocol: models.ALL,
Direction: models.TrafficDirectionBi,
Allowed: true,
},
}
}
Expand All @@ -742,11 +782,11 @@
srcTags := convAclTagToValueMap(acl.Src)
dstTags := convAclTagToValueMap(acl.Dst)
aclRule := models.AclRule{
ID: acl.ID,
AllowedProtocols: acl.Proto,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
ID: acl.ID,
AllowedProtocol: acl.Proto,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
}
if acl.AllowedDirection == models.TrafficDirectionBi {
var existsInSrcTag bool
Expand All @@ -755,24 +795,24 @@
if _, ok := srcTags["*"]; ok {
return map[string]models.AclRule{
acl.ID: {
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocols: models.ALL,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocol: models.ALL,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
},
}
}
if _, ok := dstTags["*"]; ok {
return map[string]models.AclRule{
acl.ID: {
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocols: models.ALL,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
IPList: []net.IPNet{node.NetworkRange},
IP6List: []net.IPNet{node.NetworkRange6},
AllowedProtocol: models.ALL,
AllowedPorts: acl.Port,
Direction: acl.AllowedDirection,
Allowed: true,
},
}
}
Expand Down
Loading
Loading