Skip to content

karmadactl: validate --cert-external-ip to avoid silently using 127.0…#7656

Open
Anand-240 wants to merge 1 commit into
karmada-io:masterfrom
Anand-240:fix-cert-external-ip-validation
Open

karmadactl: validate --cert-external-ip to avoid silently using 127.0…#7656
Anand-240 wants to merge 1 commit into
karmada-io:masterfrom
Anand-240:fix-cert-external-ip-validation

Conversation

@Anand-240

Copy link
Copy Markdown

What type of PR is this?
/kind bug

What this PR does / why we need it:

karmadactl init accepts the --cert-external-ip flag but never validates it. The value is passed to utils.FlagsIP -> utils.StringToNetIP, which silently falls back to 127.0.0.1 for any value it can't parse as an IP. So an invalid --cert-external-ip slips through and the certificate ends up signed for 127.0.0.1 instead of the address the user asked for, which surfaces later as confusing TLS errors.

This PR validates --cert-external-ip in Validate() (splitting on comma and parsing each entry with netutils.ParseIPSloppy), the same way --karmada-apiserver-advertise-address is already validated, and returns a clear error on invalid input.

Which issue(s) this PR fixes:
Fixes #7655

Special notes for your reviewer:

Added regression tests (Invalid cert-external-ip, Valid cert-external-ip) to TestCommandInitOption_Validate. The validation splits exactly the way FlagsIP consumes the value, so anything that passes validation is guaranteed to produce real IPs in the cert SANs. gofmt, go vet, go build, the package tests, and the import-aliases check all pass.

Does this PR introduce a user-facing change?:

`karmadactl`: Fixed the issue that `init` silently uses `127.0.0.1` when `--cert-external-ip` is set to an invalid value.

Copilot AI review requested due to automatic review settings June 22, 2026 08:28
@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Jun 22, 2026
@karmada-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign prodanlabs for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, 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 addresses a bug in karmadactl init where invalid values provided to the --cert-external-ip flag were silently defaulting to 127.0.0.1. By implementing strict validation, the tool now correctly identifies and rejects malformed IP inputs, preventing downstream TLS configuration errors.

Highlights

  • Input Validation: Added validation logic to the Validate method in CommandInitOption to ensure that the --cert-external-ip flag contains only valid IP addresses.
  • Regression Testing: Included new test cases in TestCommandInitOption_Validate to verify both valid and invalid IP address inputs for the certificate external IP configuration.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the 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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. 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.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@karmada-bot karmada-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jun 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds validation for the cert-external-ip parameter in karmadactl init by splitting the input comma-separated string and validating each IP address, along with adding corresponding unit tests. The review feedback points out a consistency issue where netutils.ParseIPSloppy is used for validation (which trims spaces), but downstream certificate generation uses net.ParseIP (which does not trim spaces). This mismatch could lead to silent failures or unexpected fallbacks if users include spaces in their IP list, so switching to net.ParseIP for validation is recommended.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

}
if i.ExternalIP != "" {
for _, ip := range strings.Split(i.ExternalIP, ",") {
if netutils.ParseIPSloppy(ip) == nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

There is a potential consistency issue here. netutils.ParseIPSloppy(ip) tolerates leading and trailing spaces by trimming them internally. However, utils.StringToNetIP (called via utils.FlagsIP during certificate generation) uses net.ParseIP which does not tolerate spaces and will return nil, silently falling back to 127.0.0.1.

If a user provides --cert-external-ip "192.168.1.1, 192.168.1.2", the validation will pass because ParseIPSloppy successfully parses the trimmed IP. But during certificate generation, utils.StringToNetIP will fail to parse " 192.168.1.2" and silently fall back to 127.0.0.1.

To prevent this, we should use net.ParseIP(ip) directly for validation to ensure strict alignment with utils.StringToNetIP's parsing behavior, or ensure that utils.StringToNetIP also uses netutils.ParseIPSloppy.

Suggested change
if netutils.ParseIPSloppy(ip) == nil {
if net.ParseIP(ip) == nil {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. You're right that ParseIPSloppy was more lenient than the actual consumption path. I switched the validation to net.ParseIP so it matches exactly how utils.StringToNetIP parses the value during cert generation, and added a test for the spaced input ("192.0.2.1, 192.0.2.2") which is now correctly rejected. Updated in the latest push.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a karmadactl init bug where invalid --cert-external-ip values were silently converted to 127.0.0.1 during certificate SAN generation, leading to confusing TLS errors later.

Changes:

  • Added explicit validation for --cert-external-ip in CommandInitOption.Validate() by splitting on commas and parsing each IP.
  • Added regression tests covering invalid and valid cert-external-ip inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/karmadactl/cmdinit/kubernetes/deploy.go Adds validation for ExternalIP to fail fast on invalid IP values.
pkg/karmadactl/cmdinit/kubernetes/deploy_test.go Adds unit tests for valid/invalid cert-external-ip validation behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +323 to +329
if i.ExternalIP != "" {
for _, ip := range strings.Split(i.ExternalIP, ",") {
if netutils.ParseIPSloppy(ip) == nil {
return fmt.Errorf("cert-external-ip %q is not a valid IP address", ip)
}
}
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, switched to strings.SplitSeq to match how utils.FlagsIP splits the value. Thanks!

@codecov-commenter

codecov-commenter commented Jun 22, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.06%. Comparing base (7edc6e6) to head (f2a4a7d).
⚠️ Report is 6 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7656      +/-   ##
==========================================
+ Coverage   42.04%   42.06%   +0.01%     
==========================================
  Files         879      879              
  Lines       54827    54831       +4     
==========================================
+ Hits        23053    23064      +11     
+ Misses      30027    30022       -5     
+ Partials     1747     1745       -2     
Flag Coverage Δ
unittests 42.06% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

….0.1

Signed-off-by: Anand-240 <srivastavaanandprakash16@gmail.com>
@Anand-240 Anand-240 force-pushed the fix-cert-external-ip-validation branch from 4f416af to f2a4a7d Compare June 22, 2026 09:29
@karmada-bot karmada-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

karmadactl init does not validate --cert-external-ip, invalid IPs silently become 127.0.0.1 in the cert

4 participants