Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Sep 18, 2024
1 parent a1d6ca2 commit 460930b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
12 changes: 2 additions & 10 deletions pkg/registry/file/applicationprofile_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,16 @@ func (a ApplicationProfileProcessor) PreSave(object runtime.Object) error {
}

func deflateApplicationProfileContainer(container softwarecomposition.ApplicationProfileContainer) softwarecomposition.ApplicationProfileContainer {
opens := DeflateStringer(container.Opens)

opens, err := dynamicpathdetector.AnalyzeOpens(opens, dynamicpathdetector.NewPathAnalyzer(OpenDynamicThreshold))
opens, err := dynamicpathdetector.AnalyzeOpens(container.Opens, dynamicpathdetector.NewPathAnalyzer(OpenDynamicThreshold))
if err != nil {
logger.L().Warning("failed to analyze opens", loggerhelpers.Error(err))
opens = DeflateStringer(container.Opens)
}

if opens == nil {
opens = []softwarecomposition.OpenCalls{}
}

endpoints, err := dynamicpathdetector.AnalyzeEndpoints(&container.Endpoints, dynamicpathdetector.NewPathAnalyzer(100))
endpoints, err := dynamicpathdetector.AnalyzeEndpoints(&container.Endpoints, dynamicpathdetector.NewPathAnalyzer(EndpointDynamicThreshold))
if err != nil {
logger.L().Warning("failed to analyze endpoints", loggerhelpers.Error(err))
endpoints = container.Endpoints
}

return softwarecomposition.ApplicationProfileContainer{
Name: container.Name,
Capabilities: mapset.Sorted(mapset.NewThreadUnsafeSet(container.Capabilities...)),
Expand Down
6 changes: 4 additions & 2 deletions pkg/registry/file/dynamicpathdetector/analyze_opens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package dynamicpathdetector

import (
"fmt"
"slices"

mapset "github.com/deckarep/golang-set/v2"
types "github.com/kubescape/storage/pkg/apis/softwarecomposition"
)

func AnalyzeOpens(opens []types.OpenCalls, analyzer *PathAnalyzer) ([]types.OpenCalls, error) {
var dynamicOpens []types.OpenCalls
for _, open := range opens {
AnalyzeOpen(open.Path, analyzer)
_, _ = AnalyzeOpen(open.Path, analyzer)
}

for i := range opens {
Expand All @@ -20,7 +22,7 @@ func AnalyzeOpens(opens []types.OpenCalls, analyzer *PathAnalyzer) ([]types.Open

if result != opens[i].Path {
if existing, err := getIfExists(result, dynamicOpens); err == nil {
existing.Flags = MergeStrings(existing.Flags, opens[i].Flags)
existing.Flags = mapset.Sorted(mapset.NewThreadUnsafeSet(slices.Concat(existing.Flags, opens[i].Flags)...))
} else {
dynamicOpen := types.OpenCalls{Path: result, Flags: opens[i].Flags}
dynamicOpens = append(dynamicOpens, dynamicOpen)
Expand Down
32 changes: 10 additions & 22 deletions pkg/registry/file/dynamicpathdetector/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dynamicpathdetector

import (
pathUtils "path"
"path"
"strings"
)

Expand All @@ -11,8 +11,9 @@ func NewPathAnalyzer(threshold int) *PathAnalyzer {
threshold: threshold,
}
}
func (ua *PathAnalyzer) AnalyzePath(path, identifier string) (string, error) {
path = pathUtils.Clean(path)

func (ua *PathAnalyzer) AnalyzePath(p, identifier string) (string, error) {
p = path.Clean(p)
node, exists := ua.RootNodes[identifier]
if !exists {
node = &SegmentNode{
Expand All @@ -22,35 +23,28 @@ func (ua *PathAnalyzer) AnalyzePath(path, identifier string) (string, error) {
}
ua.RootNodes[identifier] = node
}

segments := strings.Split(strings.Trim(path, "/"), "/")

segments := strings.Split(strings.Trim(p, "/"), "/")
return ua.processSegments(node, segments), nil
}

func (ua *PathAnalyzer) processSegments(node *SegmentNode, segments []string) string {
resultPath := []string{}
var resultPath []string
currentNode := node
for _, segment := range segments {
currentNode = ua.processSegment(currentNode, segment)
ua.updateNodeStats(currentNode)
resultPath = append(resultPath, currentNode.SegmentName)
}
return "/" + strings.Join(resultPath, "/")

}

func (ua *PathAnalyzer) processSegment(node *SegmentNode, segment string) *SegmentNode {

switch {
case segment == DynamicIdentifier:
if segment == DynamicIdentifier {
return ua.handleDynamicSegment(node)
case KeyInMap(node.Children, segment) || node.IsNextDynamic():
child, exists := node.Children[segment]
} else if child, exists := node.Children[segment]; exists || node.IsNextDynamic() {
return ua.handleExistingSegment(node, child, exists)
default:
} else {
return ua.handleNewSegment(node, segment)

}
}

Expand Down Expand Up @@ -103,7 +97,6 @@ func (ua *PathAnalyzer) createDynamicNode(node *SegmentNode) *SegmentNode {

func (ua *PathAnalyzer) updateNodeStats(node *SegmentNode) {
if node.Count > ua.threshold && !node.IsNextDynamic() {

dynamicChild := &SegmentNode{
SegmentName: DynamicIdentifier,
Count: 0,
Expand All @@ -123,16 +116,11 @@ func (ua *PathAnalyzer) updateNodeStats(node *SegmentNode) {

func shallowChildrenCopy(src, dst *SegmentNode) {
for segmentName := range src.Children {
if !KeyInMap(dst.Children, segmentName) {
if _, ok := dst.Children[segmentName]; !ok {
dst.Children[segmentName] = src.Children[segmentName]
} else {
dst.Children[segmentName].Count += src.Children[segmentName].Count
shallowChildrenCopy(src.Children[segmentName], dst.Children[segmentName])
}
}
}

func KeyInMap[T any](TestMap map[string]T, key string) bool {
_, ok := TestMap[key]
return ok
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) {
{Path: "/home/user4/file.txt", Flags: []string{"READ", "WRITE"}},
},
expected: []types.OpenCalls{
{Path: "/home/<dynamic>/file.txt", Flags: []string{"READ", "WRITE", "APPEND"}},
{Path: "/home/<dynamic>/file.txt", Flags: []string{"APPEND", "READ", "WRITE"}},
},
},
{
Expand All @@ -70,7 +70,7 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) {
{Path: "/var/log/app2.log", Flags: []string{"WRITE"}},
},
expected: []types.OpenCalls{
{Path: "/home/<dynamic>/common.txt", Flags: []string{"READ", "WRITE", "APPEND"}},
{Path: "/home/<dynamic>/common.txt", Flags: []string{"APPEND", "READ", "WRITE"}},
{Path: "/var/log/app1.log", Flags: []string{"READ"}},
{Path: "/var/log/app2.log", Flags: []string{"WRITE"}},
},
Expand All @@ -88,8 +88,8 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) {
{Path: "/home/user4/file2.txt", Flags: []string{"READ", "WRITE"}},
},
expected: []types.OpenCalls{
{Path: "/home/<dynamic>/file1.txt", Flags: []string{"READ", "WRITE", "APPEND"}},
{Path: "/home/<dynamic>/file2.txt", Flags: []string{"READ", "WRITE", "APPEND"}},
{Path: "/home/<dynamic>/file1.txt", Flags: []string{"APPEND", "READ", "WRITE"}},
{Path: "/home/<dynamic>/file2.txt", Flags: []string{"APPEND", "READ", "WRITE"}},
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ func BenchmarkAnalyzeOpensVsDeflateStringer(b *testing.B) {
_ = file.DeflateStringer(paths)
_, _ = dynamicpathdetector.AnalyzeOpens(paths, analyzer)
}
b.ReportAllocs()
})

b.Run("deflateStringer", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = file.DeflateStringer(paths)
}
b.ReportAllocs()
})
}

Expand Down

0 comments on commit 460930b

Please sign in to comment.