Skip to content

Commit 8d76426

Browse files
authored
fix(kiali): misc follow-up changes for kiali toolsets (#498)
- Additional tests - Regeneration of dynamic README.md content Signed-off-by: Marc Nuri <[email protected]>
1 parent 2811c07 commit 8d76426

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ The following sets of tools are available (toolsets marked with ✓ in the Defau
208208

209209
<!-- AVAILABLE-TOOLSETS-START -->
210210

211-
| Toolset | Description | Default |
212-
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
213-
| config | View and manage the current local Kubernetes configuration (kubeconfig) ||
214-
| core | Most common tools for Kubernetes management (Pods, Generic Resources, Events, etc.) ||
215-
| helm | Tools for managing Helm charts and releases ||
211+
| Toolset | Description | Default |
212+
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
213+
| config | View and manage the current local Kubernetes configuration (kubeconfig) ||
214+
| core | Most common tools for Kubernetes management (Pods, Generic Resources, Events, etc.) ||
215+
| helm | Tools for managing Helm charts and releases ||
216216
| kiali | Most common tools for managing Kiali, check the [Kiali documentation](https://github.com/containers/kubernetes-mcp-server/blob/main/docs/KIALI.md) for more details. | |
217217

218218
<!-- AVAILABLE-TOOLSETS-END -->

pkg/kiali/kiali.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ func (k *Kiali) validateAndGetURL(endpoint string) (string, error) {
6060
if err != nil {
6161
return "", fmt.Errorf("invalid endpoint path: %w", err)
6262
}
63+
// Reject absolute URLs - endpoint should be a relative path
64+
if endpointURL.Scheme != "" || endpointURL.Host != "" {
65+
return "", fmt.Errorf("endpoint must be a relative path, not an absolute URL")
66+
}
6367
resultURL, err := url.JoinPath(baseURL.String(), endpointURL.Path)
6468
if err != nil {
6569
return "", fmt.Errorf("failed to join kiali base URL with endpoint path: %w", err)
6670
}
6771

68-
u, _ := url.Parse(resultURL)
72+
u, err := url.Parse(resultURL)
73+
if err != nil {
74+
return "", fmt.Errorf("failed to parse joined URL: %w", err)
75+
}
6976
u.RawQuery = endpointURL.RawQuery
7077
u.Fragment = endpointURL.Fragment
7178

@@ -145,7 +152,10 @@ func (k *Kiali) executeRequest(ctx context.Context, method, endpoint, contentTyp
145152
return "", err
146153
}
147154
defer func() { _ = resp.Body.Close() }()
148-
respBody, _ := io.ReadAll(resp.Body)
155+
respBody, err := io.ReadAll(resp.Body)
156+
if err != nil {
157+
return "", fmt.Errorf("failed to read response body: %w", err)
158+
}
149159
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
150160
if len(respBody) > 0 {
151161
return "", fmt.Errorf("kiali API error: %s", strings.TrimSpace(string(respBody)))

pkg/kiali/kiali_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,49 @@ func (s *KialiSuite) TestValidateAndGetURL() {
128128
s.Equal("true", u.Query().Get("health"), "Unexpected query parameter health")
129129
})
130130
})
131+
132+
s.Run("Rejects absolute URLs in endpoint", func() {
133+
s.Config = test.Must(config.ReadToml([]byte(`
134+
[toolset_configs.kiali]
135+
url = "https://kiali.example/"
136+
insecure = true
137+
`)))
138+
k := NewKiali(s.Config, s.MockServer.Config())
139+
140+
s.Run("rejects http URLs", func() {
141+
_, err := k.validateAndGetURL("http://other-server.com/api")
142+
s.Require().Error(err, "Expected error for absolute URL")
143+
s.ErrorContains(err, "endpoint must be a relative path", "Unexpected error message")
144+
})
145+
146+
s.Run("rejects https URLs", func() {
147+
_, err := k.validateAndGetURL("https://other-server.com/api")
148+
s.Require().Error(err, "Expected error for absolute URL")
149+
s.ErrorContains(err, "endpoint must be a relative path", "Unexpected error message")
150+
})
151+
152+
s.Run("rejects URLs with host but no scheme", func() {
153+
_, err := k.validateAndGetURL("//other-server.com/api")
154+
s.Require().Error(err, "Expected error for URL with host")
155+
s.ErrorContains(err, "endpoint must be a relative path", "Unexpected error message")
156+
})
157+
})
158+
159+
s.Run("Preserves fragment in endpoint", func() {
160+
s.Config = test.Must(config.ReadToml([]byte(`
161+
[toolset_configs.kiali]
162+
url = "https://kiali.example/"
163+
insecure = true
164+
`)))
165+
k := NewKiali(s.Config, s.MockServer.Config())
166+
167+
full, err := k.validateAndGetURL("/api/path#section")
168+
s.Require().NoError(err, "Expected no error validating URL with fragment")
169+
u, err := url.Parse(full)
170+
s.Require().NoError(err, "Expected to parse full URL")
171+
s.Equal("/api/path", u.Path, "Unexpected path in parsed URL")
172+
s.Equal("section", u.Fragment, "Unexpected fragment in parsed URL")
173+
})
131174
}
132175

133176
// CurrentAuthorizationHeader behavior is now implicit via executeRequest using Manager.BearerToken

pkg/toolsets/kiali/toolset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (t *Toolset) GetName() string {
1717
}
1818

1919
func (t *Toolset) GetDescription() string {
20-
return "Most common tools for managing Kiali, check the [Kiali integration documentation](https://github.com/containers/kubernetes-mcp-server/blob/main/docs/KIALI_INTEGRATION.md) for more details."
20+
return "Most common tools for managing Kiali, check the [Kiali documentation](https://github.com/containers/kubernetes-mcp-server/blob/main/docs/KIALI.md) for more details."
2121
}
2222

2323
func (t *Toolset) GetTools(_ internalk8s.Openshift) []api.ServerTool {

0 commit comments

Comments
 (0)