-
Notifications
You must be signed in to change notification settings - Fork 224
OCPBUGS-81675: pkg/cvo/egress: trust user-ca-bundle in HyperShift mode #1370
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
dpateriya
wants to merge
4
commits into
openshift:main
Choose a base branch
from
dpateriya:hypershift-trust-user-ca-bundle
base: main
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec14b44
pkg/cvo/egress: trust user-ca-bundle in HyperShift mode
dpateriya d335cc8
pkg/cvo/egress: address CodeRabbit review suggestions
dpateriya 7d0b4a5
pkg/cvo/egress_test: fix gofmt formatting
dpateriya 56c1973
pkg/cvo/egress: seed cert pool from system roots
dpateriya 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| package cvo | ||
|
|
||
| import ( | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "testing" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // testCABundle1 is a self-signed CA certificate for testing only (CN=test-ca). | ||
| const testCABundle1 = `-----BEGIN CERTIFICATE----- | ||
| MIIDBTCCAe2gAwIBAg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Ma2cBKOv1m447AuNQryxc8zEZx | ||
| umQEpCT2KLUo | ||
| -----END CERTIFICATE----- | ||
| ` | ||
|
|
||
| // testCABundle2 is a second self-signed CA certificate for testing only (CN=test-ca-2). | ||
| const testCABundle2 = `-----BEGIN CERTIFICATE----- | ||
| MIIDCTCCAfGgAwIBAgIUdsXWk3Ayiv/1BO/NrI0rpOr8q94wDQYJKoZIhvcNAQEL | ||
| BQAwFDESMBAGA1UEAwwJdGVzdC1jYS0yMB4XDTI2MDQxMDA2NTkyNFoXDTI2MDQx | ||
| MTA2NTkyNFowFDESMBAGA1UEAwwJdGVzdC1jYS0yMIIBIjANBgkqhkiG9w0BAQEF | ||
| AAOCAQ8AMIIBCgKCAQEAukjI/q0yQHN+uYbjW1YDxUbQdwN9oGo+p3e5eFvxShHL | ||
| EahJvZ673JrZhULRKMsoESJn9eMtkABXNMtugEGTexQag++Zly7xvEYDciyVxncE | ||
| uh8yrKyzyfkgYaBUoj+XimGnWk5DyGHET/7Ex2xY/3iXRknPkOO4TTlMyp0KZkAL | ||
| 2xWQFgAnrrZ89HHAqgtpQ1gHvbmC80215vTBynBSbacgRjDK1NPnSmaA6Pkxa7mX | ||
| znUyBGeRct1+RIq9n0Fyadz/glZV8S7cGJUGAWuL2v7i6aWY1WJ+vt+SOzSkx/bK | ||
| wYOk9l6hvZKkqHgzAy1fFxGzdJ+eHNqllRKjsyWsdQIDAQABo1MwUTAdBgNVHQ4E | ||
| FgQUobxtuZWbsm6Xa1r6+v3VRmR81HUwHwYDVR0jBBgwFoAUobxtuZWbsm6Xa1r6 | ||
| +v3VRmR81HUwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAQdal | ||
| mib3V/3dMkHiajIIafT6IJp0peJh3LvP7BSpTfAE5v4cAApmmKZFko1TLq0qcItd | ||
| A9jdA8VcbGsCcWWMDTYQZhSZurGLrIbZ26tqlVX/jDcun75jx4P3Bcgj9tpEA7WF | ||
| DM9AIBF0SHmZKN2mOo7Dn+BcyUrlM1Sygyb1hAfpcu7D0bI+yJQj5aqNW8OG2zK0 | ||
| zzWzHyuLepB67+vrl6Zoi3QPzR7nfXiGU52ggVZpCbluRhBIn5okNMBf3DdCtV2U | ||
| KjER4+2xPvFl3thrdb3V8ph7ujmtyPHx51F5+dDNmRRr/VOYSPxHfHgAPZ0bEvwN | ||
| bfKkg9UU+lJbK6aEOw== | ||
| -----END CERTIFICATE----- | ||
| ` | ||
|
|
||
| // parseCertsFromPEM decodes all PEM certificates in data and returns them. | ||
| func parseCertsFromPEM(t *testing.T, data string) []*x509.Certificate { | ||
| t.Helper() | ||
| var certs []*x509.Certificate | ||
| rest := []byte(data) | ||
| for { | ||
| var block *pem.Block | ||
| block, rest = pem.Decode(rest) | ||
| if block == nil { | ||
| break | ||
| } | ||
| cert, err := x509.ParseCertificate(block.Bytes) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse certificate: %v", err) | ||
| } | ||
| certs = append(certs, cert) | ||
| } | ||
| return certs | ||
| } | ||
|
|
||
| // poolContainsCert returns true when cert's raw subject DER is present in pool. | ||
| func poolContainsCert(pool *x509.CertPool, cert *x509.Certificate) bool { | ||
| for _, s := range pool.Subjects() { //nolint:staticcheck // SA1019: acceptable here, we build the pool ourselves | ||
| if string(s) == string(cert.RawSubject) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func newCMForTest(name, caBundle string) *corev1.ConfigMap { | ||
| return &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: name}, | ||
| Data: map[string]string{"ca-bundle.crt": caBundle}, | ||
| } | ||
| } | ||
|
|
||
| func TestGetTLSConfig(t *testing.T) { | ||
| ca1Certs := parseCertsFromPEM(t, testCABundle1) | ||
| ca2Certs := parseCertsFromPEM(t, testCABundle2) | ||
| if len(ca1Certs) == 0 || len(ca2Certs) == 0 { | ||
| t.Fatal("failed to parse test CA certificates") | ||
| } | ||
| ca1Cert := ca1Certs[0] | ||
| ca2Cert := ca2Certs[0] | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| trustedCABundle *corev1.ConfigMap // openshift-config-managed/trusted-ca-bundle | ||
| userCABundle *corev1.ConfigMap // openshift-config/user-ca-bundle | ||
| hypershift bool | ||
| wantNilConfig bool | ||
| wantCA1Present bool | ||
| wantCA2Present bool | ||
| }{ | ||
| { | ||
| name: "non-hypershift: no bundles → nil config", | ||
| hypershift: false, | ||
| wantNilConfig: true, | ||
| }, | ||
| { | ||
| name: "non-hypershift: trusted-ca-bundle only → CA1 trusted", | ||
| hypershift: false, | ||
| trustedCABundle: newCMForTest("trusted-ca-bundle", testCABundle1), | ||
| wantCA1Present: true, | ||
| wantCA2Present: false, | ||
| }, | ||
| { | ||
| name: "non-hypershift: user-ca-bundle present but hypershift=false → ignored", | ||
| hypershift: false, | ||
| userCABundle: newCMForTest("user-ca-bundle", testCABundle2), | ||
| wantNilConfig: true, | ||
| }, | ||
| { | ||
| name: "non-hypershift: both bundles but hypershift=false → only trusted-ca-bundle used", | ||
| hypershift: false, | ||
| trustedCABundle: newCMForTest("trusted-ca-bundle", testCABundle1), | ||
| userCABundle: newCMForTest("user-ca-bundle", testCABundle2), | ||
| wantCA1Present: true, | ||
| wantCA2Present: false, | ||
| }, | ||
| { | ||
| name: "hypershift: no bundles → nil config", | ||
| hypershift: true, | ||
| wantNilConfig: true, | ||
| }, | ||
| { | ||
| name: "hypershift: trusted-ca-bundle only → CA1 trusted", | ||
| hypershift: true, | ||
| trustedCABundle: newCMForTest("trusted-ca-bundle", testCABundle1), | ||
| wantCA1Present: true, | ||
| wantCA2Present: false, | ||
| }, | ||
| { | ||
| name: "hypershift: user-ca-bundle only → CA2 trusted", | ||
| hypershift: true, | ||
| userCABundle: newCMForTest("user-ca-bundle", testCABundle2), | ||
| wantCA1Present: false, | ||
| wantCA2Present: true, | ||
| }, | ||
| { | ||
| name: "hypershift: both bundles → both CAs trusted", | ||
| hypershift: true, | ||
| trustedCABundle: newCMForTest("trusted-ca-bundle", testCABundle1), | ||
| userCABundle: newCMForTest("user-ca-bundle", testCABundle2), | ||
| wantCA1Present: true, | ||
| wantCA2Present: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| managedLister := &cmConfigLister{} | ||
| if tt.trustedCABundle != nil { | ||
| managedLister.Items = append(managedLister.Items, tt.trustedCABundle) | ||
| } | ||
|
|
||
| configLister := &cmConfigLister{} | ||
| if tt.userCABundle != nil { | ||
| configLister.Items = append(configLister.Items, tt.userCABundle) | ||
| } | ||
|
|
||
| optr := &Operator{ | ||
| hypershift: tt.hypershift, | ||
| cmConfigManagedLister: managedLister, | ||
| cmConfigLister: configLister, | ||
| } | ||
|
|
||
| tlsCfg, err := optr.getTLSConfig() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if tt.wantNilConfig { | ||
| if tlsCfg != nil { | ||
| t.Fatal("expected nil tls.Config, got non-nil") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if tlsCfg == nil { | ||
| t.Fatal("expected non-nil tls.Config, got nil") | ||
| } | ||
| if tlsCfg.RootCAs == nil { | ||
| t.Fatal("expected non-nil RootCAs, got nil") | ||
| } | ||
|
|
||
| if got := poolContainsCert(tlsCfg.RootCAs, ca1Cert); got != tt.wantCA1Present { | ||
| if tt.wantCA1Present { | ||
| t.Error("expected CA1 (from trusted-ca-bundle) to be in RootCAs but it is absent") | ||
| } else { | ||
| t.Error("expected CA1 (from trusted-ca-bundle) NOT to be in RootCAs but it is present") | ||
| } | ||
| } | ||
|
|
||
| if got := poolContainsCert(tlsCfg.RootCAs, ca2Cert); got != tt.wantCA2Present { | ||
| if tt.wantCA2Present { | ||
| t.Error("expected CA2 (from user-ca-bundle) to be in RootCAs but it is absent") | ||
| } else { | ||
| t.Error("expected CA2 (from user-ca-bundle) NOT to be in RootCAs but it is present") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.