@@ -17,8 +17,13 @@ limitations under the License.
1717package controlplane_test
1818
1919import (
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