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 the valid client certificate check #378

Merged

Conversation

mprahl
Copy link
Member

@mprahl mprahl commented Mar 14, 2024

Summary

The commit edef33d introduced additional checks on the certificate subject. The issue is that the order of the groups and organizational units are not consistent, so it was causing the addon-framework to generate several CSRs with many coming back as invalid.

This takes the approach of sorting the strings before doing the comparison.

@openshift-ci openshift-ci bot requested review from elgnay and ldpliu March 14, 2024 20:11
@mprahl
Copy link
Member Author

mprahl commented Mar 14, 2024

/cc @xuezhaojun

Copy link

codecov bot commented Mar 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.40%. Comparing base (edef33d) to head (7835988).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #378      +/-   ##
==========================================
- Coverage   61.41%   61.40%   -0.01%     
==========================================
  Files         132      132              
  Lines       14064    14061       -3     
==========================================
- Hits         8637     8634       -3     
  Misses       4672     4672              
  Partials      755      755              
Flag Coverage Δ
unit 61.40% <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 Sentry.
📢 Have feedback on the report? Share it here.

@mprahl
Copy link
Member Author

mprahl commented Mar 14, 2024

/cc @qiujian16 @zhujian7

@qiujian16
Copy link
Member

Do you have an idea why the order is not consistent given it is a slice? Alternatively, I would suggest to use Set[string].

The commit edef33d introduced
additional checks on the certificate subject. The issue is that the
order of the groups and organizational units are not consistent, so it
was causing the addon-framework to generate several CSRs with many
coming back as invalid.

This takes the approach of sorting the strings before doing the
comparison.

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
@mprahl
Copy link
Member Author

mprahl commented Mar 15, 2024

Do you have an idea why the order is not consistent given it is a slice? Alternatively, I would suggest to use Set[string].

I don't know why it's not ordered but I can try and figure that out.

Thanks for the suggestion! I didn't know apimachinery included a set library.

@mprahl
Copy link
Member Author

mprahl commented Mar 15, 2024

Do you have an idea why the order is not consistent given it is a slice? Alternatively, I would suggest to use Set[string].

It seems that x509.CreateCertificateRequest uses a set under the hood. You can see this simple example of how the parsed CSR returns the organizations as sorted:

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"math/rand"
	"net"
)

func main() {
	insecureRand := rand.New(rand.NewSource(0)) //nolint:gosec
	pk, err := ecdsa.GenerateKey(elliptic.P256(), insecureRand)
	if err != nil {
		panic(err)
	}

	csrb, err := x509.CreateCertificateRequest(insecureRand, &x509.CertificateRequest{
		Subject: pkix.Name{
			CommonName: "system:open-cluster-management:cluster:cluster1:addon:config-policy-controller:agent:config-policy-controller",
			Organization: []string{
				"system:open-cluster-management:cluster:cluster1:addon:config-policy-controller",
				"system:open-cluster-management:addon:config-policy-controller",
				"system:authenticated",
			},
		},
		DNSNames:       []string{},
		EmailAddresses: []string{},
		IPAddresses:    []net.IP{},
	}, pk)
	if err != nil {
		panic(err)
	}

	csrParsed, err := x509.ParseCertificateRequest(csrb)
	if err != nil {
		panic(err)
	}

	fmt.Println(csrParsed.Subject.Organization)
}

The output is:

/tmp/temp-csr via 🐹 v1.22.0 ❯ go run main.go
[system:authenticated system:open-cluster-management:addon:config-policy-controller system:open-cluster-management:cluster:cluster1:addon:config-policy-controller]

@qiujian16
Copy link
Member

ah thanks for the info.

/approve
/lgtm

Copy link
Contributor

openshift-ci bot commented Mar 15, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mprahl, qiujian16

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

@openshift-merge-bot openshift-merge-bot bot merged commit 2cd6a6f into open-cluster-management-io:main Mar 15, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants