-
Notifications
You must be signed in to change notification settings - Fork 265
certrotation: require UserInfo on PeerRotation, add tests #2157
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
Open
sanchezl
wants to merge
5
commits into
openshift:master
Choose a base branch
from
sanchezl:peerrotation-userinfo-followup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5b26b2
certrotation: require UserInfo on PeerRotation unconditionally
sanchezl 0e503f4
certrotation: add PeerRotation NewCertificate tests
sanchezl 7cd1e22
certrotation: use t.Context() in tests
sanchezl 08ccb6a
certrotation: use legacy-first branching in NewCertificate methods
sanchezl 27f907d
certrotation: use slices.Contains and add key gen test cases
sanchezl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| package certrotation | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -302,7 +302,7 @@ func TestEnsureTargetCertKeyPair(t *testing.T) { | |
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = c.EnsureTargetCertKeyPair(context.TODO(), newCA, newCA.Config.Certs) | ||
| _, err = c.EnsureTargetCertKeyPair(t.Context(), newCA, newCA.Config.Certs) | ||
| switch { | ||
| case err != nil && len(test.expectedError) == 0: | ||
| t.Error(err) | ||
|
|
@@ -493,7 +493,7 @@ func TestEnsureTargetSignerCertKeyPair(t *testing.T) { | |
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = c.EnsureTargetCertKeyPair(context.TODO(), newCA, newCA.Config.Certs) | ||
| _, err = c.EnsureTargetCertKeyPair(t.Context(), newCA, newCA.Config.Certs) | ||
| switch { | ||
| case err != nil && len(test.expectedError) == 0: | ||
| t.Error(err) | ||
|
|
@@ -727,6 +727,11 @@ func TestClientRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | |
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P256}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| { | ||
| name: "ECDSA-P384", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P384}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
|
|
@@ -765,11 +770,21 @@ func TestServingRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | |
| keyGen: nil, | ||
| wantAlg: x509.RSA, | ||
| }, | ||
| { | ||
| name: "RSA-4096", | ||
| keyGen: crypto.RSAKeyPairGenerator{Bits: 4096}, | ||
| wantAlg: x509.RSA, | ||
| }, | ||
| { | ||
| name: "ECDSA-P256", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P256}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| { | ||
| name: "ECDSA-P384", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P384}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
|
|
@@ -797,6 +812,113 @@ func TestServingRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestPeerRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| keyGen crypto.KeyPairGenerator | ||
| userInfo user.Info | ||
| hostnames []string | ||
| wantAlg x509.PublicKeyAlgorithm | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "nil keyGen (legacy) uses RSA with dual ExtKeyUsage", | ||
| keyGen: nil, | ||
| userInfo: &user.DefaultInfo{Name: "127.0.0.1"}, | ||
| hostnames: []string{"localhost", "127.0.0.1"}, | ||
| wantAlg: x509.RSA, | ||
| }, | ||
| { | ||
| name: "ECDSA keyGen with UserInfo", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P256}, | ||
| userInfo: &user.DefaultInfo{Name: "peer-user", Groups: []string{"peer-group"}}, | ||
| hostnames: []string{"localhost", "127.0.0.1"}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| { | ||
| name: "nil UserInfo errors", | ||
| keyGen: nil, | ||
| userInfo: nil, | ||
| hostnames: []string{"localhost"}, | ||
| wantErr: "requires UserInfo", | ||
| }, | ||
| { | ||
| name: "nil UserInfo with keyGen errors", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P256}, | ||
| userInfo: nil, | ||
| hostnames: []string{"localhost"}, | ||
| wantErr: "requires UserInfo", | ||
| }, | ||
| { | ||
| name: "mismatched UserInfo name on legacy path", | ||
| keyGen: nil, | ||
| userInfo: &user.DefaultInfo{Name: "wrong-name"}, | ||
| hostnames: []string{"localhost", "127.0.0.1"}, | ||
| wantErr: "conflicts", | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ca, err := newTestCACertificate(pkix.Name{CommonName: "test-ca"}, int64(1), metav1.Duration{Duration: time.Hour * 24}, time.Now) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| r := &PeerRotation{ | ||
| Hostnames: func() []string { return tc.hostnames }, | ||
| UserInfo: tc.userInfo, | ||
| } | ||
| certConfig, err := r.NewCertificate(ca, time.Hour, tc.keyGen) | ||
| if tc.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil", tc.wantErr) | ||
| } | ||
| if !strings.Contains(err.Error(), tc.wantErr) { | ||
| t.Fatalf("expected error containing %q, got %q", tc.wantErr, err.Error()) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("NewCertificate() error = %v", err) | ||
| } | ||
|
|
||
| cert := certConfig.Certs[0] | ||
| if cert.PublicKeyAlgorithm != tc.wantAlg { | ||
| t.Errorf("PublicKeyAlgorithm = %v, want %v", cert.PublicKeyAlgorithm, tc.wantAlg) | ||
| } | ||
|
|
||
| // Verify both ExtKeyUsages are present | ||
| if !slices.Contains(cert.ExtKeyUsage, x509.ExtKeyUsageClientAuth) { | ||
| t.Error("missing ExtKeyUsageClientAuth") | ||
| } | ||
| if !slices.Contains(cert.ExtKeyUsage, x509.ExtKeyUsageServerAuth) { | ||
| t.Error("missing ExtKeyUsageServerAuth") | ||
| } | ||
|
Comment on lines
+890
to
+896
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you pull these out into a helper you can also add them to Client and Serving tests, which don't already have them. |
||
|
|
||
| // Verify hostnames in SANs | ||
| if len(cert.DNSNames) == 0 { | ||
| t.Error("expected DNS SANs") | ||
| } | ||
|
|
||
| // Verify Subject based on path | ||
| if tc.keyGen != nil { | ||
| // ConfigurablePKI path: UserInfo encoded in Subject | ||
| if cert.Subject.CommonName != tc.userInfo.GetName() { | ||
| t.Errorf("CN = %q, want %q", cert.Subject.CommonName, tc.userInfo.GetName()) | ||
| } | ||
| if len(tc.userInfo.GetGroups()) > 0 && len(cert.Subject.Organization) == 0 { | ||
| t.Error("expected Organization from UserInfo.Groups") | ||
| } | ||
| } else { | ||
| // Legacy path: CN = sorted first hostname (MakeServerCertForDuration sorts) | ||
| if cert.Subject.CommonName != tc.userInfo.GetName() { | ||
| t.Errorf("CN = %q, want %q (must match UserInfo.Name which must match sorted first hostname)", cert.Subject.CommonName, tc.userInfo.GetName()) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSignerRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
|
|
@@ -808,6 +930,16 @@ func TestSignerRotation_NewCertificate_WithKeyPairGenerator(t *testing.T) { | |
| keyGen: nil, | ||
| wantAlg: x509.RSA, | ||
| }, | ||
| { | ||
| name: "RSA-4096", | ||
| keyGen: crypto.RSAKeyPairGenerator{Bits: 4096}, | ||
| wantAlg: x509.RSA, | ||
| }, | ||
| { | ||
| name: "ECDSA-P256", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P256}, | ||
| wantAlg: x509.ECDSA, | ||
| }, | ||
| { | ||
| name: "ECDSA-P384", | ||
| keyGen: crypto.ECDSAKeyPairGenerator{Curve: crypto.P384}, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This project imports testify, you could shorten the test to