Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed component test #163

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no error is good error handling :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not require one lol

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))
}
Loading