@@ -17,8 +17,13 @@ limitations under the License.
17
17
package controlplane_test
18
18
19
19
import (
20
+ "crypto/x509"
21
+ "encoding/pem"
20
22
"errors"
23
+ "net"
21
24
"net/url"
25
+ "os"
26
+ "path"
22
27
23
28
. "github.com/onsi/ginkgo/v2"
24
29
. "github.com/onsi/gomega"
@@ -191,6 +196,66 @@ var _ = Describe("APIServer", func() {
191
196
})
192
197
})
193
198
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
+
194
259
Describe ("setting up auth" , func () {
195
260
var auth * fakeAuthn
196
261
BeforeEach (func () {
0 commit comments