From fecf2b11db5ea296546dd915f492b34e7d522e95 Mon Sep 17 00:00:00 2001 From: Afek Berger Date: Mon, 7 Oct 2024 11:22:05 +0300 Subject: [PATCH] added merge endpoints --- .../file/applicationprofile_processor.go | 7 ++-- .../dynamicpathdetector/analyze_endpoints.go | 18 ++++------- .../tests/analyze_endpoints_test.go | 32 +++++++++++++------ 3 files changed, 31 insertions(+), 26 deletions(-) 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..9bb23c93c 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) { @@ -45,7 +42,6 @@ func ProcessEndpoint(endpoint *types.HTTPEndpoint, analyzer *PathAnalyzer, newEn if url != endpoint.Endpoint { - // Check if this dynamic exists for i, e := range newEndpoints { if e.Endpoint == url { newEndpoints[i].Methods = MergeStrings(e.Methods, endpoint.Methods) @@ -91,7 +87,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 +101,11 @@ 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..a1974d297 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,23 @@ 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)) } + +func TestAnalyzeEndpointsBug(t *testing.T) { + analyzer := dynamicpathdetector.NewPathAnalyzer(100) + + endpoints := []types.HTTPEndpoint{ + types.HTTPEndpoint{Endpoint: ":8000/", Methods: []string{"GET"}, Internal: false, Direction: "inbound", Headers: json.RawMessage{0x7b, 0x22, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x5b, 0x22, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x22, 0x5d, 0x2c, 0x22, 0x48, 0x6f, 0x73, 0x74, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x22, 0x5d, 0x7d}}, + types.HTTPEndpoint{Endpoint: ":8000/", Methods: []string{"POST"}, Internal: false, Direction: "inbound", Headers: json.RawMessage{0x7b, 0x22, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x5b, 0x22, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x22, 0x5d, 0x2c, 0x22, 0x48, 0x6f, 0x73, 0x74, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x22, 0x5d, 0x7d}}, + types.HTTPEndpoint{Endpoint: ":8000/users/", Methods: []string{"GET"}, Internal: false, Direction: "inbound", Headers: json.RawMessage{0x7b, 0x22, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x32, 0x33, 0x34, 0x72, 0x22, 0x2c, 0x22, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x22, 0x2c, 0x22, 0x7a, 0x69, 0x7a, 0x22, 0x5d, 0x2c, 0x22, 0x48, 0x6f, 0x73, 0x74, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x22, 0x5d, 0x7d}}, + } + + fmt.Println(endpoints) + + result := dynamicpathdetector.AnalyzeEndpoints(&endpoints, analyzer) + + fmt.Println(result) + +}