-
Notifications
You must be signed in to change notification settings - Fork 0
ROX-32852: Add filtering by cluster name #39
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package vulnerability | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| v1 "github.com/stackrox/rox/generated/api/v1" | ||
| "google.golang.org/grpc" | ||
| ) | ||
|
|
||
| // resolveClusterID resolves a cluster name to its ID. | ||
| // Returns error if cluster name is not found or if API call fails. | ||
| func resolveClusterID(ctx context.Context, conn *grpc.ClientConn, | ||
| clusterID string, clusterName string) (string, error) { | ||
| // Cluster ID has priority. | ||
| if clusterID != "" { | ||
| return clusterID, nil | ||
| } | ||
|
|
||
| if clusterName == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| client := v1.NewClustersServiceClient(conn) | ||
|
|
||
| // Use query to filter by cluster name server-side | ||
| query := fmt.Sprintf("Cluster:%q", clusterName) | ||
|
|
||
| resp, err := client.GetClusters(ctx, &v1.GetClustersRequest{ | ||
| Query: query, | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to fetch clusters: %w", err) | ||
| } | ||
|
|
||
| clusters := resp.GetClusters() | ||
| if len(clusters) == 0 { | ||
| return "", fmt.Errorf("cluster with name %q not found", clusterName) | ||
| } | ||
|
|
||
| // Return the first matching cluster's ID | ||
| return clusters[0].GetId(), nil | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
internal/toolsets/vulnerability/cluster_resolver_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package vulnerability | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/stackrox-mcp/internal/toolsets/mock" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/test/bufconn" | ||
| ) | ||
|
|
||
| func getBufferConnection(t *testing.T, listener *bufconn.Listener) *grpc.ClientConn { | ||
| t.Helper() | ||
|
|
||
| // Create a gRPC client connection to the mock server | ||
| conn, err := grpc.NewClient( | ||
| "passthrough://buffer", | ||
| grpc.WithLocalDNSResolution(), | ||
| grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) { | ||
| return listener.Dial() | ||
| }), | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| return conn | ||
| } | ||
|
|
||
| func TestResolveClusterID_Success(t *testing.T) { | ||
| tests := map[string]struct { | ||
| clusterID string | ||
| clusterName string | ||
| mockClusters []*storage.Cluster | ||
| mockError error | ||
| expectedID string | ||
| expectedQuery string | ||
| }{ | ||
| "only cluster ID": { | ||
| clusterID: "only-cluster-id", | ||
| clusterName: "", | ||
| mockClusters: []*storage.Cluster{{Id: "cluster-1", Name: "production"}}, | ||
| expectedID: "only-cluster-id", | ||
| }, | ||
| "cluster ID has priority": { | ||
| clusterID: "cluster-with-priority", | ||
| clusterName: "production", | ||
| mockClusters: []*storage.Cluster{{Id: "cluster-1", Name: "production"}}, | ||
| expectedID: "cluster-with-priority", | ||
| }, | ||
| "empty cluster name returns empty ID": { | ||
| clusterID: "", | ||
| clusterName: "", | ||
| mockClusters: []*storage.Cluster{{Id: "cluster-1", Name: "production"}}, | ||
| expectedID: "", | ||
| }, | ||
| "cluster name found returns correct ID": { | ||
| clusterID: "", | ||
| clusterName: "production", | ||
| mockClusters: []*storage.Cluster{{Id: "cluster-1", Name: "production"}}, | ||
| expectedID: "cluster-1", | ||
| expectedQuery: `Cluster:"production"`, | ||
| }, | ||
| } | ||
|
|
||
| for testName, testCase := range tests { | ||
| t.Run(testName, func(t *testing.T) { | ||
| mockService := mock.NewClustersServiceMock(testCase.mockClusters, testCase.mockError) | ||
|
|
||
| grpcServer, listener := mock.SetupClusterServer(mockService) | ||
| defer grpcServer.Stop() | ||
|
|
||
| conn := getBufferConnection(t, listener) | ||
|
|
||
| defer func() { _ = conn.Close() }() | ||
|
|
||
| clusterID, err := resolveClusterID( | ||
| context.Background(), | ||
| conn, | ||
| testCase.clusterID, | ||
| testCase.clusterName, | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, testCase.expectedID, clusterID) | ||
| assert.Equal(t, testCase.expectedQuery, mockService.GetLastCallQuery()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveClusterID_Failure(t *testing.T) { | ||
| tests := map[string]struct { | ||
| clusterName string | ||
| mockClusters []*storage.Cluster | ||
| mockError error | ||
| expectedErrText string | ||
| expectedQuery string | ||
| }{ | ||
| "cluster name not found returns error": { | ||
| clusterName: "nonexistent", | ||
| mockClusters: []*storage.Cluster{}, | ||
| expectedErrText: `cluster with name "nonexistent" not found`, | ||
| expectedQuery: `Cluster:"nonexistent"`, | ||
| }, | ||
| "API error propagation": { | ||
| clusterName: "production", | ||
| mockError: errors.New("API connection failed"), | ||
| expectedErrText: "failed to fetch clusters:", | ||
| expectedQuery: `Cluster:"production"`, | ||
| }, | ||
| } | ||
|
|
||
| for testName, testCase := range tests { | ||
| t.Run(testName, func(t *testing.T) { | ||
| mockService := mock.NewClustersServiceMock(testCase.mockClusters, testCase.mockError) | ||
|
|
||
| grpcServer, listener := mock.SetupClusterServer(mockService) | ||
| defer grpcServer.Stop() | ||
|
|
||
| conn := getBufferConnection(t, listener) | ||
|
|
||
| defer func() { _ = conn.Close() }() | ||
|
|
||
| clusterID, err := resolveClusterID( | ||
| context.Background(), | ||
| conn, | ||
| "", | ||
| testCase.clusterName, | ||
| ) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Empty(t, clusterID) | ||
| assert.Contains(t, err.Error(), testCase.expectedErrText) | ||
|
|
||
| assert.Equal(t, testCase.expectedQuery, mockService.GetLastCallQuery()) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.