mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-28 21:26:40 +00:00
minor changes
This commit is contained in:
@@ -3,7 +3,6 @@ package types
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -132,16 +131,6 @@ func (p *Policy) Equal(other *Policy) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *Policy) Normalize() {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
slices.Sort(p.SourcePostureChecks)
|
||||
for _, r := range p.Rules {
|
||||
r.Normalize()
|
||||
}
|
||||
}
|
||||
|
||||
// EventMeta returns activity event meta related to this policy
|
||||
func (p *Policy) EventMeta() map[string]any {
|
||||
return map[string]any{"name": p.Name}
|
||||
|
||||
@@ -136,31 +136,6 @@ func TestPolicyEqual_RulesMismatchByID(t *testing.T) {
|
||||
assert.False(t, a.Equal(b))
|
||||
}
|
||||
|
||||
func TestPolicyNormalize(t *testing.T) {
|
||||
p := &Policy{
|
||||
SourcePostureChecks: []string{"pc3", "pc1", "pc2"},
|
||||
Rules: []*PolicyRule{
|
||||
{
|
||||
ID: "r1",
|
||||
Sources: []string{"g2", "g1"},
|
||||
Destinations: []string{"g4", "g3"},
|
||||
Ports: []string{"443", "80"},
|
||||
},
|
||||
},
|
||||
}
|
||||
p.Normalize()
|
||||
|
||||
assert.Equal(t, []string{"pc1", "pc2", "pc3"}, p.SourcePostureChecks)
|
||||
assert.Equal(t, []string{"g1", "g2"}, p.Rules[0].Sources)
|
||||
assert.Equal(t, []string{"g3", "g4"}, p.Rules[0].Destinations)
|
||||
assert.Equal(t, []string{"443", "80"}, p.Rules[0].Ports)
|
||||
}
|
||||
|
||||
func TestPolicyNormalize_Nil(t *testing.T) {
|
||||
var p *Policy
|
||||
p.Normalize()
|
||||
}
|
||||
|
||||
func TestPolicyEqual_FullScenario(t *testing.T) {
|
||||
a := &Policy{
|
||||
ID: "pol1",
|
||||
|
||||
@@ -2,7 +2,6 @@ package types
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
|
||||
"github.com/netbirdio/netbird/shared/management/proto"
|
||||
)
|
||||
@@ -160,25 +159,6 @@ func (pm *PolicyRule) Equal(other *PolicyRule) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (pm *PolicyRule) Normalize() {
|
||||
if pm == nil {
|
||||
return
|
||||
}
|
||||
slices.Sort(pm.Sources)
|
||||
slices.Sort(pm.Destinations)
|
||||
slices.Sort(pm.Ports)
|
||||
sort.Slice(pm.PortRanges, func(i, j int) bool {
|
||||
if pm.PortRanges[i].Start != pm.PortRanges[j].Start {
|
||||
return pm.PortRanges[i].Start < pm.PortRanges[j].Start
|
||||
}
|
||||
return pm.PortRanges[i].End < pm.PortRanges[j].End
|
||||
})
|
||||
for k, v := range pm.AuthorizedGroups {
|
||||
slices.Sort(v)
|
||||
pm.AuthorizedGroups[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func stringSlicesEqualUnordered(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
|
||||
@@ -192,34 +192,3 @@ func TestPolicyRuleEqual_EmptySlices(t *testing.T) {
|
||||
assert.True(t, a.Equal(b))
|
||||
}
|
||||
|
||||
func TestPolicyRuleNormalize(t *testing.T) {
|
||||
rule := &PolicyRule{
|
||||
Sources: []string{"g3", "g1", "g2"},
|
||||
Destinations: []string{"g6", "g4", "g5"},
|
||||
Ports: []string{"443", "80", "22"},
|
||||
PortRanges: []RulePortRange{
|
||||
{Start: 8000, End: 9000},
|
||||
{Start: 80, End: 80},
|
||||
{Start: 80, End: 443},
|
||||
},
|
||||
AuthorizedGroups: map[string][]string{
|
||||
"g1": {"u3", "u1", "u2"},
|
||||
},
|
||||
}
|
||||
rule.Normalize()
|
||||
|
||||
assert.Equal(t, []string{"g1", "g2", "g3"}, rule.Sources)
|
||||
assert.Equal(t, []string{"g4", "g5", "g6"}, rule.Destinations)
|
||||
assert.Equal(t, []string{"22", "443", "80"}, rule.Ports)
|
||||
assert.Equal(t, []RulePortRange{
|
||||
{Start: 80, End: 80},
|
||||
{Start: 80, End: 443},
|
||||
{Start: 8000, End: 9000},
|
||||
}, rule.PortRanges)
|
||||
assert.Equal(t, []string{"u1", "u2", "u3"}, rule.AuthorizedGroups["g1"])
|
||||
}
|
||||
|
||||
func TestPolicyRuleNormalize_Nil(t *testing.T) {
|
||||
var rule *PolicyRule
|
||||
rule.Normalize()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user