Skip to content
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

fix(util/proxy): fix tls.config when secret.spec.caBundle is nil #4371

Merged
merged 1 commit into from
Dec 9, 2023

Conversation

CharlesQQ
Copy link
Contributor

@CharlesQQ CharlesQQ commented Dec 5, 2023

What type of PR is this?

What this PR does / why we need it:

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

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

`karmadactl`: Fixed return err in case of  secret.spec. caBundle is nil

@karmada-bot karmada-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Dec 5, 2023
@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (09a43c6) 51.87% compared to head (fca22c2) 51.89%.
Report is 10 commits behind head on master.

Files Patch % Lines
pkg/util/proxy/proxy.go 0.00% 5 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4371      +/-   ##
==========================================
+ Coverage   51.87%   51.89%   +0.01%     
==========================================
  Files         243      243              
  Lines       24114    24112       -2     
==========================================
+ Hits        12509    12512       +3     
+ Misses      10922    10918       -4     
+ Partials      683      682       -1     
Flag Coverage Δ
unittests 51.89% <0.00%> (+0.01%) ⬆️

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

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

@XiShanYongYe-Chang
Copy link
Member

/assign @jwcesign

@@ -124,7 +124,7 @@ func NewThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.Roun

func GetTlsConfigForCluster(ctx context.Context, cluster *clusterapis.Cluster, secretGetter SecretGetterFunc) (*tls.Config, error) {
// The secret is optional for a pull-mode cluster, if no secret just returns a config with root CA unset.
if cluster.Spec.SecretRef == nil {
if cluster.Spec.SecretRef == nil || cluster.Spec.InsecureSkipTLSVerification {
Copy link
Member

Choose a reason for hiding this comment

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

Should we set the RootCAs even if InsecureSkipTLSVerification is true(If the caBundle exists)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we set the RootCAs even if InsecureSkipTLSVerification is true(If the caBundle exists)?

en.., sure, It's a batter choice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/cc @jwcesign

Copy link
Member

@jwcesign jwcesign left a comment

Choose a reason for hiding this comment

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

lgtm

cc @chaosi-zju for double checking

Comment on lines 140 to 148
caBundle, err := getClusterCABundle(cluster.Name, caSecret)
if err != nil {
return nil, fmt.Errorf("failed to get CA bundle for cluster %s: %v", cluster.Name, err)
return &tls.Config{
MinVersion: tls.VersionTLS13,
// Ignore false positive warning: "TLS InsecureSkipVerify may be true. (gosec)"
// Whether to skip server certificate verification depends on the
// configuration(.spec.insecureSkipTLSVerification, defaults to false) in a Cluster object.
InsecureSkipVerify: cluster.Spec.InsecureSkipTLSVerification, //nolint:gosec
}, nil
Copy link
Member

Choose a reason for hiding this comment

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

hi, in your case, cluster.Spec.InsecureSkipTLSVerification=true and caBundle="", you code works fine.

But, if cluster.Spec.InsecureSkipTLSVerification=false and caBundle="", this case may be we should report error if caBundle is empty?

Otherwise, you see, caBundle is empty and we return &tls.Config{InsecureSkipVerify: false}, this is a invalid config.

Copy link
Member

@jwcesign jwcesign Dec 6, 2023

Choose a reason for hiding this comment

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

if cluster.Spec.InsecureSkipTLSVerification=false and caBundle="", this case may be we should report error if caBundle is empty?

I think, if the ca in the container is correct, it will still work fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, in your case, cluster.Spec.InsecureSkipTLSVerification=true and caBundle="", you code works fine.

But, if cluster.Spec.InsecureSkipTLSVerification=false and caBundle="", this case may be we should report error if caBundle is empty?

Otherwise, you see, caBundle is empty and we return &tls.Config{InsecureSkipVerify: false}, this is a invalid config.

got it!!!

@karmada-bot karmada-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 6, 2023
@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 6, 2023
InsecureSkipVerify: cluster.Spec.InsecureSkipTLSVerification, //nolint:gosec
}, nil
}
return nil, fmt.Errorf("insecureSkipTLSVerification is false, but failed to get CA bundle for cluster %s: %v", cluster.Name, err)
Copy link
Member

Choose a reason for hiding this comment

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

How about this? @chaosi-zju @CharlesQQ

func getClusterCABundle(clusterName string, secret *corev1.Secret) []byte {
	caBundle, found := secret.Data[clusterapis.SecretCADataKey]
	if !found {
		return []byte{}
	}
	return caBundle
}

func GetTlsConfigForCluster(ctx context.Context, cluster *clusterapis.Cluster, secretGetter SecretGetterFunc) (*tls.Config, error) {
        ...
        caBundle := getClusterCABundle(cluster.Name, caSecret)
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM(caBundle)
	return &tls.Config{
		RootCAs:    caCertPool,
		MinVersion: tls.VersionTLS13,
		// Ignore false positive warning: "TLS InsecureSkipVerify may be true. (gosec)"
		// Whether to skip server certificate verification depends on the
		// configuration(.spec.insecureSkipTLSVerification, defaults to false) in a Cluster object.
		InsecureSkipVerify: cluster.Spec.InsecureSkipTLSVerification, //nolint:gosec
	}, nil

Copy link
Member

@chaosi-zju chaosi-zju Dec 6, 2023

Choose a reason for hiding this comment

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

I think, if the ca in the container is correct, it will still work fine

I got your point.

How about this?

I think perfect.

Copy link
Member

Choose a reason for hiding this comment

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

Hi, @CharlesQQ
Can you help update this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this? @chaosi-zju @CharlesQQ

func getClusterCABundle(clusterName string, secret *corev1.Secret) []byte {
	caBundle, found := secret.Data[clusterapis.SecretCADataKey]
	if !found {
		return []byte{}
	}
	return caBundle
}

func GetTlsConfigForCluster(ctx context.Context, cluster *clusterapis.Cluster, secretGetter SecretGetterFunc) (*tls.Config, error) {
        ...
        caBundle := getClusterCABundle(cluster.Name, caSecret)
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM(caBundle)
	return &tls.Config{
		RootCAs:    caCertPool,
		MinVersion: tls.VersionTLS13,
		// Ignore false positive warning: "TLS InsecureSkipVerify may be true. (gosec)"
		// Whether to skip server certificate verification depends on the
		// configuration(.spec.insecureSkipTLSVerification, defaults to false) in a Cluster object.
		InsecureSkipVerify: cluster.Spec.InsecureSkipTLSVerification, //nolint:gosec
	}, nil

motified!!!

@karmada-bot karmada-bot removed the lgtm Indicates that a PR is ready to be merged. label Dec 6, 2023
@jwcesign
Copy link
Member

jwcesign commented Dec 6, 2023

Nice work! Thanks for your contribution.

/lgtm

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 6, 2023
@jwcesign
Copy link
Member

jwcesign commented Dec 6, 2023

/cc @RainbowMango

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks @CharlesQQ
LGMT

I think this should affect user behavior. Can we add a release note to explain it?

@XiShanYongYe-Chang
Copy link
Member

/kind bug

@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Dec 8, 2023
@CharlesQQ
Copy link
Contributor Author

CharlesQQ commented Dec 8, 2023

Thanks @CharlesQQ LGMT

I think this should affect user behavior. Can we add a release note to explain it?

Of cause

@XiShanYongYe-Chang
Copy link
Member

/approve

@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: XiShanYongYe-Chang

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

The pull request process is described here

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

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 9, 2023
@karmada-bot karmada-bot merged commit e2c6ece into karmada-io:master Dec 9, 2023
12 checks passed
karmada-bot added a commit that referenced this pull request Dec 11, 2023
…k-of-#4371-upstream-release-1.8

Automated cherry pick of #4371: fix(util/proxy): fix tls.config when secret.spec.caBundle is
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

insecureSkipTLSVerification is true,but cluster.spec.secret.caBundle must be set, it's unexpected
6 participants