@@ -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,101 @@ 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 host & port are set" , func () {
222+ BeforeEach (func () {
223+ server .SecureServing = SecureServing {
224+ ListenAddr : process.ListenAddr {
225+ Address : "1.2.3.4" ,
226+ Port : "5678" ,
227+ },
228+ }
229+ })
230+
231+ It ("should have the host in the certificate altnames" , func () {
232+ cert := getCertificate ()
233+
234+ Expect (cert .Subject .CommonName ).To (Equal ("localhost" ))
235+ Expect (cert .DNSNames ).To (ConsistOf ("localhost" ))
236+ expectedIPAddresses := []net.IP {
237+ net .ParseIP ("127.0.0.1" ).To4 (),
238+ net .ParseIP (server .SecureServing .ListenAddr .Address ).To4 (),
239+ }
240+ Expect (cert .IPAddresses ).To (ContainElements (expectedIPAddresses ))
241+ })
242+ })
243+
244+ Context ("when InsecureServing host & port are set" , func () {
245+ BeforeEach (func () {
246+ server .InsecureServing = & process.ListenAddr {
247+ Address : "1.2.3.4" ,
248+ Port : "5678" ,
249+ }
250+ })
251+
252+ It ("should have the host in the certificate altnames" , func () {
253+ cert := getCertificate ()
254+
255+ Expect (cert .Subject .CommonName ).To (Equal ("localhost" ))
256+ Expect (cert .DNSNames ).To (ConsistOf ("localhost" ))
257+ expectedIPAddresses := []net.IP {
258+ net .ParseIP ("127.0.0.1" ).To4 (),
259+ net .ParseIP (server .InsecureServing .Address ).To4 (),
260+ }
261+ Expect (cert .IPAddresses ).To (ContainElements (expectedIPAddresses ))
262+ })
263+ })
264+
265+ Context ("when SecureServing and InsecureServing host & port are set" , func () {
266+ BeforeEach (func () {
267+ server .SecureServing = SecureServing {
268+ ListenAddr : process.ListenAddr {
269+ Address : "1.2.3.4" ,
270+ Port : "5678" ,
271+ },
272+ }
273+ server .InsecureServing = & process.ListenAddr {
274+ Address : "5.6.7.8" ,
275+ Port : "1234" ,
276+ }
277+ })
278+
279+ It ("should have the host in the certificate altnames" , func () {
280+ cert := getCertificate ()
281+
282+ Expect (cert .Subject .CommonName ).To (Equal ("localhost" ))
283+ Expect (cert .DNSNames ).To (ConsistOf ("localhost" ))
284+ expectedIPAddresses := []net.IP {
285+ net .ParseIP ("127.0.0.1" ).To4 (),
286+ net .ParseIP (server .SecureServing .ListenAddr .Address ).To4 (),
287+ net .ParseIP (server .InsecureServing .Address ).To4 (),
288+ }
289+ Expect (cert .IPAddresses ).To (ContainElements (expectedIPAddresses ))
290+ })
291+ })
292+ })
293+
194294 Describe ("setting up auth" , func () {
195295 var auth * fakeAuthn
196296 BeforeEach (func () {
0 commit comments