Skip to content

Commit

Permalink
Added rule policy deflation (#168)
Browse files Browse the repository at this point in the history
Signed-off-by: Afek Berger <[email protected]>
  • Loading branch information
afek854 authored Nov 18, 2024
1 parent ebcd04e commit 5785b47
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/registry/file/applicationprofile_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func deflateApplicationProfileContainer(container softwarecomposition.Applicatio
Endpoints: endpoints,
ImageTag: container.ImageTag,
ImageID: container.ImageID,
PolicyByRuleId: container.PolicyByRuleId,
PolicyByRuleId: DeflateRulePolicies(container.PolicyByRuleId),
}
}
94 changes: 94 additions & 0 deletions pkg/registry/file/applicationprofile_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,97 @@ func TestApplicationProfileProcessor_PreSave(t *testing.T) {
})
}
}

func TestDeflateRulePolicies(t *testing.T) {
tests := []struct {
name string
in map[string]softwarecomposition.RulePolicy
want map[string]softwarecomposition.RulePolicy
}{
{
name: "nil map",
in: nil,
want: nil,
},
{
name: "empty map",
in: map[string]softwarecomposition.RulePolicy{},
want: map[string]softwarecomposition.RulePolicy{},
},
{
name: "single rule with unsorted processes",
in: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{"cat", "bash", "ls"},
AllowedContainer: true,
},
},
want: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{"bash", "cat", "ls"},
AllowedContainer: true,
},
},
},
{
name: "multiple rules with duplicate processes",
in: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{"cat", "bash", "ls", "bash"},
AllowedContainer: true,
},
"rule2": {
AllowedProcesses: []string{"nginx", "nginx", "python"},
AllowedContainer: false,
},
},
want: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{"bash", "cat", "ls"},
AllowedContainer: true,
},
"rule2": {
AllowedProcesses: []string{"nginx", "python"},
AllowedContainer: false,
},
},
},
{
name: "rule with empty processes",
in: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{},
AllowedContainer: true,
},
},
want: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{},
AllowedContainer: true,
},
},
},
{
name: "rule with nil processes",
in: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: nil,
AllowedContainer: true,
},
},
want: map[string]softwarecomposition.RulePolicy{
"rule1": {
AllowedProcesses: []string{},
AllowedContainer: true,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := DeflateRulePolicies(tt.in)
assert.Equal(t, tt.want, got)
})
}
}
13 changes: 13 additions & 0 deletions pkg/registry/file/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package file

import (
mapset "github.com/deckarep/golang-set/v2"
"github.com/kubescape/storage/pkg/apis/softwarecomposition"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -34,3 +35,15 @@ func DeflateStringer[T Stringer](in []T) []T {
}
return out
}

func DeflateRulePolicies(in map[string]softwarecomposition.RulePolicy) map[string]softwarecomposition.RulePolicy {
if in == nil {
return nil
}

for key, item := range in {
item.AllowedProcesses = mapset.Sorted(mapset.NewThreadUnsafeSet(item.AllowedProcesses...))
in[key] = item
}
return in
}

0 comments on commit 5785b47

Please sign in to comment.