-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.go
More file actions
216 lines (175 loc) · 5.81 KB
/
nodes.go
File metadata and controls
216 lines (175 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package vulnerability
import (
"context"
"fmt"
"io"
"sort"
"strings"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/pkg/errors"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/stackrox-mcp/internal/client"
"github.com/stackrox/stackrox-mcp/internal/client/auth"
"github.com/stackrox/stackrox-mcp/internal/logging"
"github.com/stackrox/stackrox-mcp/internal/toolsets"
"google.golang.org/grpc"
)
// getNodesForCVEInput defines the input parameters for get_nodes_for_cve tool.
type getNodesForCVEInput struct {
CVEName string `json:"cveName"`
FilterClusterID string `json:"filterClusterId,omitempty"`
}
func (input *getNodesForCVEInput) validate() error {
if input.CVEName == "" {
return errors.New("CVE name is required")
}
return nil
}
// NodeGroupResult contains aggregated node information by cluster and OS.
type NodeGroupResult struct {
ClusterID string `json:"clusterId"`
ClusterName string `json:"clusterName"`
OperatingSystem string `json:"operatingSystem"`
Count int `json:"count"`
}
// getNodesForCVEOutput defines the output structure for get_nodes_for_cve tool.
type getNodesForCVEOutput struct {
NodeGroups []NodeGroupResult `json:"nodeGroups"`
}
// getNodesForCVETool implements the get_nodes_for_cve tool.
type getNodesForCVETool struct {
name string
client *client.Client
}
// NewGetNodesForCVETool creates a new get_nodes_for_cve tool.
func NewGetNodesForCVETool(c *client.Client) toolsets.Tool {
return &getNodesForCVETool{
name: "get_nodes_for_cve",
client: c,
}
}
// IsReadOnly returns true as this tool only reads data.
func (t *getNodesForCVETool) IsReadOnly() bool {
return true
}
// GetName returns the tool name.
func (t *getNodesForCVETool) GetName() string {
return t.name
}
// GetTool returns the MCP Tool definition.
func (t *getNodesForCVETool) GetTool() *mcp.Tool {
return &mcp.Tool{
Name: t.name,
Description: "Get aggregated node groups where a specified CVE is detected in node operating system packages" +
", grouped by cluster and OS image. Checks OS-level vulnerabilities on cluster nodes." +
" For comprehensive CVE coverage, also use get_clusters_with_orchestrator_cve (K8s components)" +
" and get_deployments_for_cve (workloads).",
InputSchema: getNodesForCVEInputSchema(),
}
}
// getNodesForCVEInputSchema returns the JSON schema for input validation.
func getNodesForCVEInputSchema() *jsonschema.Schema {
schema, err := jsonschema.For[getNodesForCVEInput](nil)
if err != nil {
logging.Fatal("Could not get jsonschema for get_nodes_for_cve input", err)
return nil
}
// CVE name is required.
schema.Required = []string{"cveName"}
schema.Properties["cveName"].Description = "CVE name to filter nodes (e.g., CVE-2020-26159)"
schema.Properties["filterClusterId"].Description = "Optional cluster ID to filter nodes"
return schema
}
// RegisterWith registers the get_nodes_for_cve tool handler with the MCP server.
func (t *getNodesForCVETool) RegisterWith(server *mcp.Server) {
mcp.AddTool(server, t.GetTool(), t.handle)
}
// buildNodeQuery builds query used to search nodes in StackRox Central.
// We will quote values to have strict match. Without quote: CVE-2025-10, would match CVE-2025-101.
func buildNodeQuery(input getNodesForCVEInput) string {
queryParts := []string{fmt.Sprintf("CVE:%q", input.CVEName)}
if input.FilterClusterID != "" {
queryParts = append(queryParts, fmt.Sprintf("Cluster ID:%q", input.FilterClusterID))
}
return strings.Join(queryParts, "+")
}
// aggregateNodeGroups consumes entire stream and aggregates nodes by cluster and OS.
func aggregateNodeGroups(
stream grpc.ServerStreamingClient[v1.ExportNodeResponse],
) ([]NodeGroupResult, error) {
// Map key: "clusterId|osImage"
// Map value: NodeGroupResult with count and clusterName.
groups := make(map[string]*NodeGroupResult)
for {
resp, err := stream.Recv()
// Stream ended - no more nodes.
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, errors.Wrap(err, "error receiving from stream")
}
node := resp.GetNode()
if node == nil {
continue
}
// Create unique key for this cluster+OS combination.
key := fmt.Sprintf("%s|%s", node.GetClusterId(), node.GetOsImage())
if group, exists := groups[key]; exists {
group.Count++
continue
}
groups[key] = &NodeGroupResult{
ClusterID: node.GetClusterId(),
ClusterName: node.GetClusterName(),
OperatingSystem: node.GetOsImage(),
Count: 1,
}
}
result := make([]NodeGroupResult, 0, len(groups))
for _, group := range groups {
result = append(result, *group)
}
// Sort for consistent ordering (by clusterId, then OS).
sort.Slice(result, func(i, j int) bool {
if result[i].ClusterID != result[j].ClusterID {
return result[i].ClusterID < result[j].ClusterID
}
return result[i].OperatingSystem < result[j].OperatingSystem
})
return result, nil
}
// handle is the handler for get_nodes_for_cve tool.
func (t *getNodesForCVETool) handle(
ctx context.Context,
req *mcp.CallToolRequest,
input getNodesForCVEInput,
) (*mcp.CallToolResult, *getNodesForCVEOutput, error) {
err := input.validate()
if err != nil {
return nil, nil, err
}
conn, err := t.client.ReadyConn(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "unable to connect to server")
}
callCtx := auth.WithMCPRequestContext(ctx, req)
nodeClient := v1.NewNodeServiceClient(conn)
query := buildNodeQuery(input)
exportReq := &v1.ExportNodeRequest{
Query: query,
}
stream, err := nodeClient.ExportNodes(callCtx, exportReq)
if err != nil {
return nil, nil, client.NewError(err, "ExportNodes")
}
nodeGroups, err := aggregateNodeGroups(stream)
if err != nil {
return nil, nil, err
}
output := &getNodesForCVEOutput{
NodeGroups: nodeGroups,
}
return nil, output, nil
}