-
Notifications
You must be signed in to change notification settings - Fork 1.2k
🐛 Fix testing kube-apiserver serving certificate using wrong SANs #3284
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,13 @@ limitations under the License. | |
package controlplane_test | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"net" | ||
"net/url" | ||
"os" | ||
"path" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
@@ -191,6 +196,101 @@ var _ = Describe("APIServer", func() { | |
}) | ||
}) | ||
|
||
// These tests assume that 'localhost' resolves to 127.0.0.1. It can resolve | ||
// to other addresses as well (e.g. ::1 on IPv6), but it must always resolve | ||
// to 127.0.0.1. | ||
Describe(("generated certificates"), func() { | ||
getCertificate := func() *x509.Certificate { | ||
// Read the cert file | ||
certFile := path.Join(server.CertDir, "apiserver.crt") | ||
certBytes, err := os.ReadFile(certFile) | ||
Expect(err).NotTo(HaveOccurred(), "should be able to read the cert file") | ||
|
||
// Decode and parse it | ||
block, remainder := pem.Decode(certBytes) | ||
Expect(block).NotTo(BeNil(), "should be able to decode the cert file") | ||
Expect(remainder).To(BeEmpty(), "should not have any extra data in the cert file") | ||
Expect(block.Type).To(Equal("CERTIFICATE"), "should be a certificate block") | ||
|
||
cert, err := x509.ParseCertificate(block.Bytes) | ||
Expect(err).NotTo(HaveOccurred(), "should be able to parse the cert file") | ||
|
||
return cert | ||
} | ||
|
||
Context("when SecureServing host & port are set", func() { | ||
BeforeEach(func() { | ||
server.SecureServing = SecureServing{ | ||
ListenAddr: process.ListenAddr{ | ||
Address: "1.2.3.4", | ||
Port: "5678", | ||
}, | ||
} | ||
}) | ||
|
||
It("should have the host in the certificate altnames", func() { | ||
cert := getCertificate() | ||
|
||
Expect(cert.Subject.CommonName).To(Equal("localhost")) | ||
Expect(cert.DNSNames).To(ConsistOf("localhost")) | ||
expectedIPAddresses := []net.IP{ | ||
net.ParseIP("127.0.0.1").To4(), | ||
net.ParseIP(server.SecureServing.ListenAddr.Address).To4(), | ||
} | ||
Expect(cert.IPAddresses).To(ContainElements(expectedIPAddresses)) | ||
}) | ||
}) | ||
|
||
Context("when InsecureServing host & port are set", func() { | ||
BeforeEach(func() { | ||
server.InsecureServing = &process.ListenAddr{ | ||
Address: "1.2.3.4", | ||
Port: "5678", | ||
} | ||
}) | ||
|
||
It("should have the host in the certificate altnames", func() { | ||
cert := getCertificate() | ||
|
||
Expect(cert.Subject.CommonName).To(Equal("localhost")) | ||
Expect(cert.DNSNames).To(ConsistOf("localhost")) | ||
expectedIPAddresses := []net.IP{ | ||
net.ParseIP("127.0.0.1").To4(), | ||
net.ParseIP(server.InsecureServing.Address).To4(), | ||
} | ||
Expect(cert.IPAddresses).To(ContainElements(expectedIPAddresses)) | ||
}) | ||
}) | ||
|
||
Context("when SecureServing and InsecureServing host & port are set", func() { | ||
BeforeEach(func() { | ||
server.SecureServing = SecureServing{ | ||
ListenAddr: process.ListenAddr{ | ||
Address: "1.2.3.4", | ||
Port: "5678", | ||
}, | ||
} | ||
server.InsecureServing = &process.ListenAddr{ | ||
Address: "5.6.7.8", | ||
Port: "1234", | ||
} | ||
}) | ||
|
||
It("should have the host in the certificate altnames", func() { | ||
cert := getCertificate() | ||
|
||
Expect(cert.Subject.CommonName).To(Equal("localhost")) | ||
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. I might be missing it, but where do we test that there is actually a SAN injected? The tests here all seem to only check for 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. The following two There isn't really an easy way to verify that the DNS SAN extension will contain an additional entry if |
||
Expect(cert.DNSNames).To(ConsistOf("localhost")) | ||
expectedIPAddresses := []net.IP{ | ||
net.ParseIP("127.0.0.1").To4(), | ||
net.ParseIP(server.SecureServing.ListenAddr.Address).To4(), | ||
net.ParseIP(server.InsecureServing.Address).To4(), | ||
} | ||
Expect(cert.IPAddresses).To(ContainElements(expectedIPAddresses)) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("setting up auth", func() { | ||
var auth *fakeAuthn | ||
BeforeEach(func() { | ||
|
Uh oh!
There was an error while loading. Please reload this page.