Skip to content

Commit

Permalink
fixed component test (#163)
Browse files Browse the repository at this point in the history
* added merge endpoints

* WIP

* WIP: Removed test

* WIP: Compare by endpoint key
  • Loading branch information
afek854 authored Nov 4, 2024
1 parent e864a24 commit 280d905
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
7 changes: 2 additions & 5 deletions pkg/registry/file/applicationprofile_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ func deflateApplicationProfileContainer(container softwarecomposition.Applicatio
opens = []softwarecomposition.OpenCalls{}
}

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
}
endpoints := dynamicpathdetector.AnalyzeEndpoints(&container.Endpoints, dynamicpathdetector.NewPathAnalyzer(EndpointDynamicThreshold))

return softwarecomposition.ApplicationProfileContainer{
Name: container.Name,
Capabilities: mapset.Sorted(mapset.NewThreadUnsafeSet(container.Capabilities...)),
Expand Down
22 changes: 10 additions & 12 deletions pkg/registry/file/dynamicpathdetector/analyze_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
types "github.com/kubescape/storage/pkg/apis/softwarecomposition"
)

func AnalyzeEndpoints(endpoints *[]types.HTTPEndpoint, analyzer *PathAnalyzer) ([]types.HTTPEndpoint, error) {
func AnalyzeEndpoints(endpoints *[]types.HTTPEndpoint, analyzer *PathAnalyzer) []types.HTTPEndpoint {
if len(*endpoints) == 0 {
return nil, nil
return nil
}

var newEndpoints []*types.HTTPEndpoint
Expand All @@ -29,12 +29,9 @@ func AnalyzeEndpoints(endpoints *[]types.HTTPEndpoint, analyzer *PathAnalyzer) (
}
}

newEndpoints, err := MergeDuplicateEndpoints(newEndpoints)
if err != nil {
return nil, err
}
newEndpoints = MergeDuplicateEndpoints(newEndpoints)

return convertPointerToValueSlice(newEndpoints), nil
return convertPointerToValueSlice(newEndpoints)
}

func ProcessEndpoint(endpoint *types.HTTPEndpoint, analyzer *PathAnalyzer, newEndpoints []*types.HTTPEndpoint) (*types.HTTPEndpoint, error) {
Expand All @@ -44,10 +41,10 @@ func ProcessEndpoint(endpoint *types.HTTPEndpoint, analyzer *PathAnalyzer, newEn
}

if url != endpoint.Endpoint {
endpoint.Endpoint = url

// Check if this dynamic exists
for i, e := range newEndpoints {
if e.Endpoint == url {
if getEndpointKey(e) == getEndpointKey(endpoint) {
newEndpoints[i].Methods = MergeStrings(e.Methods, endpoint.Methods)
mergeHeaders(e, endpoint)
return nil, nil
Expand Down Expand Up @@ -91,7 +88,7 @@ func AnalyzeURL(urlString string, analyzer *PathAnalyzer) (string, error) {
return ":" + port + path, nil
}

func MergeDuplicateEndpoints(endpoints []*types.HTTPEndpoint) ([]*types.HTTPEndpoint, error) {
func MergeDuplicateEndpoints(endpoints []*types.HTTPEndpoint) []*types.HTTPEndpoint {
seen := make(map[string]*types.HTTPEndpoint)
var newEndpoints []*types.HTTPEndpoint
for _, endpoint := range endpoints {
Expand All @@ -105,11 +102,12 @@ func MergeDuplicateEndpoints(endpoints []*types.HTTPEndpoint) ([]*types.HTTPEndp
newEndpoints = append(newEndpoints, endpoint)
}
}
return newEndpoints, nil

return newEndpoints
}

func getEndpointKey(endpoint *types.HTTPEndpoint) string {
return fmt.Sprintf("%s|%v|%v", endpoint.Endpoint, endpoint.Internal, endpoint.Direction)
return fmt.Sprintf("%s|%s", endpoint.Endpoint, endpoint.Direction)
}

func mergeHeaders(existing, new *types.HTTPEndpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ func TestAnalyzeEndpoints(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := dynamicpathdetector.AnalyzeEndpoints(&tt.input, analyzer)
assert.NoError(t, err)
result := dynamicpathdetector.AnalyzeEndpoints(&tt.input, analyzer)
ja := jsonassert.New(t)
for i := range result {
assert.Equal(t, tt.expected[i].Endpoint, result[i].Endpoint)
Expand Down Expand Up @@ -163,8 +162,7 @@ func TestAnalyzeEndpointsWithThreshold(t *testing.T) {
},
}

result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.NoError(t, err)
result := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.Equal(t, expected, result)
}

Expand All @@ -179,8 +177,7 @@ func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) {
})
}

result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.NoError(t, err)
result := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)

// Check that all 100 endpoints are still individual
assert.Equal(t, 100, len(result))
Expand All @@ -191,8 +188,7 @@ func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) {
Methods: []string{"GET"},
})

result, err = dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.NoError(t, err)
result = dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)

// Check that all endpoints are now merged into one dynamic endpoint
expected := []types.HTTPEndpoint{
Expand All @@ -214,7 +210,6 @@ func TestAnalyzeEndpointsWithInvalidURL(t *testing.T) {
},
}

result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.NoError(t, err)
result := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer)
assert.Equal(t, 0, len(result))
}

0 comments on commit 280d905

Please sign in to comment.