diff --git a/pkg/registry/file/applicationprofile_processor.go b/pkg/registry/file/applicationprofile_processor.go index dc56ee9f3..42ffd2fd8 100644 --- a/pkg/registry/file/applicationprofile_processor.go +++ b/pkg/registry/file/applicationprofile_processor.go @@ -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...)), diff --git a/pkg/registry/file/dynamicpathdetector/analyze_endpoints.go b/pkg/registry/file/dynamicpathdetector/analyze_endpoints.go index ffab52edb..5e88fce04 100644 --- a/pkg/registry/file/dynamicpathdetector/analyze_endpoints.go +++ b/pkg/registry/file/dynamicpathdetector/analyze_endpoints.go @@ -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 @@ -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) { @@ -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 @@ -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 { @@ -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) { diff --git a/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go b/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go index 38eea1f8f..852eb4c9b 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go @@ -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) @@ -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) } @@ -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)) @@ -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{ @@ -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)) }