feat: add debug endpoints for services and policies#1604
feat: add debug endpoints for services and policies#1604yashisrani wants to merge 2 commits intokmesh-net:mainfrom
Conversation
Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances Kmesh's debuggability by introducing several new HTTP endpoints. These endpoints allow administrators to gain visibility into the internal state of Kmesh, specifically concerning active TLS certificates, known Kubernetes services, and configured authorization policies. This provides crucial diagnostic capabilities for understanding and troubleshooting Kmesh's security and service management. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. State hidden no more, Debug paths now show the way, Secrets brought to light. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces new debug endpoints for inspecting certificates, services, and authorization policies. However, it introduces a critical security vulnerability where the /debug/config_dump/security endpoint leaks TLS private keys in plain text. Additionally, there's a recurring bug in the new HTTP handlers where http.ResponseWriter.WriteHeader can be called multiple times, leading to incorrect HTTP responses on error.
| certs := client.WorkloadController.SecretManager.DumpCerts() | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| data, err := json.MarshalIndent(certs, "", " ") | ||
| if err != nil { | ||
| log.Errorf("Failed to marshal certificates: %v", err) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
| _, _ = w.Write(data) |
There was a problem hiding this comment.
This code exposes sensitive TLS private keys via the /debug/config_dump/security endpoint. The client.WorkloadController.SecretManager.DumpCerts() method returns istiosecurity.SecretItem objects, which contain a PrivateKey field. When these are marshaled to JSON, the private keys are included, creating a Critical security vulnerability. You must redact or exclude the PrivateKey field before serialization.
Furthermore, the current implementation incorrectly calls w.WriteHeader multiple times. Specifically, w.WriteHeader(http.StatusInternalServerError) is called after w.WriteHeader(http.StatusOK) has already been invoked, resulting in a misleading 200 OK status on error. To address this, marshal the data to JSON first, and only write the 200 OK status header if marshalling is successful.
data, err := json.MarshalIndent(certs, "", " ")
if err != nil {
log.Errorf("Failed to marshal certificates: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)| w.WriteHeader(http.StatusOK) | ||
| data, err := json.MarshalIndent(serviceDump, "", " ") | ||
| if err != nil { | ||
| log.Errorf("Failed to marshal services: %v", err) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
| _, _ = w.Write(data) |
There was a problem hiding this comment.
Calling w.WriteHeader more than once for a given request is not allowed. In the error path, w.WriteHeader(http.StatusInternalServerError) is called after w.WriteHeader(http.StatusOK) has already been called. The second call will be ignored, and the client will receive a 200 OK status with an empty body, which is misleading. To fix this, you should marshal the data to JSON first, and only write the 200 OK status header if marshalling is successful.
data, err := json.MarshalIndent(serviceDump, "", " ")
if err != nil {
log.Errorf("Failed to marshal services: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)| w.WriteHeader(http.StatusOK) | ||
| data, err := json.MarshalIndent(policyDump, "", " ") | ||
| if err != nil { | ||
| log.Errorf("Failed to marshal policies: %v", err) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
| _, _ = w.Write(data) |
There was a problem hiding this comment.
Calling w.WriteHeader more than once for a given request is not allowed. In the error path, w.WriteHeader(http.StatusInternalServerError) is called after w.WriteHeader(http.StatusOK) has already been called. The second call will be ignored, and the client will receive a 200 OK status with an empty body, which is misleading. To fix this, you should marshal the data to JSON first, and only write the 200 OK status header if marshalling is successful.
data, err := json.MarshalIndent(policyDump, "", " ")
if err != nil {
log.Errorf("Failed to marshal policies: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
What type of PR is this?
/kind feature
What this PR does / why we need it:
Implement TODO to expose Kmesh internal state via debug endpoints:
/debug/config_dump/security: Display all active TLS certificates
/debug/config_dump/services: Display all K8s services
/debug/config_dump/policies: Display all authorization policies
Add SecretManager.DumpCerts() to expose cached certificates.
These endpoints allow administrators to debug Kmesh security configuration by inspecting certificates, services, and policies via simple HTTP GET requests."
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: