Skip to content

Commit 28871a1

Browse files
authored
🐛 Fix testing kube-apiserver serving certificate using wrong SANs (#3284)
* Fix testing kube-apiserver serving certificate using wrong SANs Signed-off-by: solidDoWant <[email protected]> * Address PR feedback Signed-off-by: solidDoWant <[email protected]> --------- Signed-off-by: solidDoWant <[email protected]>
1 parent 8deb602 commit 28871a1

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

pkg/internal/testing/controlplane/apiserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ func (s *APIServer) populateAPIServerCerts() error {
374374
return err
375375
}
376376

377-
servingCerts, err := ca.NewServingCert()
377+
servingAddresses := []string{"localhost"}
378+
if s.SecureServing.ListenAddr.Address != "" {
379+
servingAddresses = append(servingAddresses, s.SecureServing.ListenAddr.Address)
380+
}
381+
382+
servingCerts, err := ca.NewServingCert(servingAddresses...)
378383
if err != nil {
379384
return err
380385
}

pkg/internal/testing/controlplane/apiserver_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ limitations under the License.
1717
package controlplane_test
1818

1919
import (
20+
"crypto/x509"
21+
"encoding/pem"
2022
"errors"
23+
"net"
2124
"net/url"
25+
"os"
26+
"path"
2227

2328
. "github.com/onsi/ginkgo/v2"
2429
. "github.com/onsi/gomega"
@@ -191,6 +196,66 @@ var _ = Describe("APIServer", func() {
191196
})
192197
})
193198

199+
// These tests assume that 'localhost' resolves to 127.0.0.1. It can resolve
200+
// to other addresses as well (e.g. ::1 on IPv6), but it must always resolve
201+
// to 127.0.0.1.
202+
Describe(("generated certificates"), func() {
203+
getCertificate := func() *x509.Certificate {
204+
// Read the cert file
205+
certFile := path.Join(server.CertDir, "apiserver.crt")
206+
certBytes, err := os.ReadFile(certFile)
207+
Expect(err).NotTo(HaveOccurred(), "should be able to read the cert file")
208+
209+
// Decode and parse it
210+
block, remainder := pem.Decode(certBytes)
211+
Expect(block).NotTo(BeNil(), "should be able to decode the cert file")
212+
Expect(remainder).To(BeEmpty(), "should not have any extra data in the cert file")
213+
Expect(block.Type).To(Equal("CERTIFICATE"), "should be a certificate block")
214+
215+
cert, err := x509.ParseCertificate(block.Bytes)
216+
Expect(err).NotTo(HaveOccurred(), "should be able to parse the cert file")
217+
218+
return cert
219+
}
220+
221+
Context("when SecureServing are not set", func() {
222+
It("should have localhost/127.0.0.1 in the certificate altnames", func() {
223+
cert := getCertificate()
224+
225+
Expect(cert.Subject.CommonName).To(Equal("localhost"))
226+
Expect(cert.DNSNames).To(ConsistOf("localhost"))
227+
expectedIPAddresses := []net.IP{
228+
net.ParseIP("127.0.0.1").To4(),
229+
net.ParseIP(server.SecureServing.ListenAddr.Address).To4(),
230+
}
231+
Expect(cert.IPAddresses).To(ContainElements(expectedIPAddresses))
232+
})
233+
})
234+
235+
Context("when SecureServing host & port are set", func() {
236+
BeforeEach(func() {
237+
server.SecureServing = SecureServing{
238+
ListenAddr: process.ListenAddr{
239+
Address: "1.2.3.4",
240+
Port: "5678",
241+
},
242+
}
243+
})
244+
245+
It("should have the host in the certificate altnames", func() {
246+
cert := getCertificate()
247+
248+
Expect(cert.Subject.CommonName).To(Equal("localhost"))
249+
Expect(cert.DNSNames).To(ConsistOf("localhost"))
250+
expectedIPAddresses := []net.IP{
251+
net.ParseIP("127.0.0.1").To4(),
252+
net.ParseIP(server.SecureServing.ListenAddr.Address).To4(),
253+
}
254+
Expect(cert.IPAddresses).To(ContainElements(expectedIPAddresses))
255+
})
256+
})
257+
})
258+
194259
Describe("setting up auth", func() {
195260
var auth *fakeAuthn
196261
BeforeEach(func() {

0 commit comments

Comments
 (0)