@@ -2,7 +2,9 @@ package config_test
22
33import (
44 "fmt"
5+ "math/rand/v2"
56 "os"
7+ "path"
68 "path/filepath"
79 "time"
810
@@ -221,15 +223,15 @@ var _ = Describe("Config", Serial, func() {
221223 )
222224
223225 BeforeEach (func () {
224- tmpDir = GinkgoT ().TempDir ()
226+ tmpDir = path . Join ( GinkgoT ().TempDir (), fmt . Sprintf ( "frontend-%d" , rand . IntN ( 100 )) )
225227 os .Setenv ("FRONTEND_TLS_BASE_PATH" , tmpDir )
226228 })
227229
228230 AfterEach (func () {
229231 os .Unsetenv ("FRONTEND_TLS_BASE_PATH" )
230232 })
231233
232- Context ("with valid cert and key" , func () {
234+ Context ("with valid cert and key and enableCertCreation set to true " , func () {
233235 BeforeEach (func () {
234236 cfg , err = config .New ("fixtures/valid_frontend_cert.yml" , true )
235237 })
@@ -238,7 +240,7 @@ var _ = Describe("Config", Serial, func() {
238240 Expect (err ).NotTo (HaveOccurred ())
239241 })
240242
241- It ("adds the certs and keys to the expected directories" , func () {
243+ It ("updates the config with the expected directories" , func () {
242244 Expect (err ).NotTo (HaveOccurred ())
243245 Expect (cfg .FrontendTLS ).To (HaveLen (2 ))
244246
@@ -253,14 +255,26 @@ var _ = Describe("Config", Serial, func() {
253255 }))
254256 })
255257
256- It ("writes the correct cert and key files" , func () {
258+ It ("writes the cert and key files" , func () {
259+ Expect (tmpDir ).To (BeADirectory ())
260+
257261 for i , name := range []string {"prod" , "dev" } {
258262 certPath := filepath .Join (tmpDir , name , name + ".pem" )
259263 keyPath := filepath .Join (tmpDir , name , name + ".pem.key" )
260264
265+ Expect (path .Join (tmpDir , name )).To (BeADirectory ())
266+
261267 Expect (certPath ).To (BeAnExistingFile ())
262268 Expect (keyPath ).To (BeAnExistingFile ())
263269
270+ certInfo , err := os .Stat (certPath )
271+ Expect (err ).NotTo (HaveOccurred ())
272+ Expect (os .FileMode (0750 )).To (Equal (certInfo .Mode ().Perm ()))
273+
274+ keyInfo , err := os .Stat (keyPath )
275+ Expect (err ).NotTo (HaveOccurred ())
276+ Expect (os .FileMode (0750 )).To (Equal (keyInfo .Mode ().Perm ()))
277+
264278 certData , certErr := os .ReadFile (certPath )
265279 Expect (certErr ).NotTo (HaveOccurred ())
266280 Expect (string (certData )).To (Equal (cfg .FrontendTLSJob [i ].CertChain ))
@@ -272,16 +286,16 @@ var _ = Describe("Config", Serial, func() {
272286 })
273287 })
274288
275- Context ("with invalid cert and key" , func () {
289+ Context ("with valid cert (having san and dnsnames) and key and enableCertCreation set to false " , func () {
276290 BeforeEach (func () {
277- cfg , err = config .New ("fixtures/valid_frontend_cert.yml" , true )
291+ cfg , err = config .New ("fixtures/valid_frontend_cert.yml" , false )
278292 })
279293
280294 It ("loads config without error" , func () {
281295 Expect (err ).NotTo (HaveOccurred ())
282296 })
283297
284- It ("adds the certs and keys to the expected directories" , func () {
298+ It ("updates the config with the expected directories" , func () {
285299 Expect (err ).NotTo (HaveOccurred ())
286300 Expect (cfg .FrontendTLS ).To (HaveLen (2 ))
287301
@@ -296,51 +310,53 @@ var _ = Describe("Config", Serial, func() {
296310 }))
297311 })
298312
299- It ("writes the correct cert and key files with correct permissions " , func () {
300- for i , name := range []string {"prod" , "dev" } {
313+ It ("does not write the cert and key files as enableCertCreation is false " , func () {
314+ for _ , name := range []string {"prod" , "dev" } {
301315 certPath := filepath .Join (tmpDir , name , name + ".pem" )
302316 keyPath := filepath .Join (tmpDir , name , name + ".pem.key" )
303317
304- Expect (certPath ).To (BeAnExistingFile ())
305- Expect (keyPath ).To (BeAnExistingFile ())
306-
307- certData , certErr := os .ReadFile (certPath )
308- Expect (certErr ).NotTo (HaveOccurred ())
309- Expect (string (certData )).To (Equal (cfg .FrontendTLSJob [i ].CertChain ))
310-
311- keyData , keyErr := os .ReadFile (keyPath )
312- Expect (keyErr ).NotTo (HaveOccurred ())
313- Expect (string (keyData )).To (Equal (cfg .FrontendTLSJob [i ].PrivateKey ))
314-
315- certInfo , err := os .Stat (certPath )
316- Expect (err ).NotTo (HaveOccurred ())
317- Expect (os .FileMode (0750 )).To (Equal (certInfo .Mode ().Perm ()))
318-
319- keyInfo , err := os .Stat (keyPath )
320- Expect (err ).NotTo (HaveOccurred ())
321- Expect (os .FileMode (0750 )).To (Equal (keyInfo .Mode ().Perm ()))
318+ Expect (certPath ).ToNot (BeAnExistingFile ())
319+ Expect (keyPath ).ToNot (BeAnExistingFile ())
322320 }
323321 })
324322 })
325323
326324 Context ("with invalid frontend_tls config" , func () {
327- It ("should fail if cert_chain is missing SAN information" , func () {
328- _ , err := config .New ("fixtures/frontend_cert_without_san.yml" , true )
325+ It ("should fail if cert is empty" , func () {
326+ _ , err := config .New ("fixtures/no_frontend_certs.yml" , false )
327+ Expect (err ).To (HaveOccurred ())
328+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: empty cert_chain" ))
329+ })
330+
331+ It ("should fail if key is missing" , func () {
332+ _ , err := config .New ("fixtures/invalid_frontend_key.yml" , false )
329333 Expect (err ).To (HaveOccurred ())
330- Expect (err .Error ()).To (Equal ("frontend_tls[0].cert_chain must include a subjectAltName extension " ))
334+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: empty private_key " ))
331335 })
332- It ("should fail if certs or keys are empty" , func () {
333- _ , err := config .New ("fixtures/no_frontend_certs.yml" , true )
336+
337+ It ("should fail if name is missing" , func () {
338+ _ , err := config .New ("fixtures/invalid_frontend_name.yml" , false )
334339 Expect (err ).To (HaveOccurred ())
335- Expect (err .Error ()).To (Equal ("frontend_tls[0] must include name, cert_chain, and private_key " ))
340+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: empty name" ))
336341 })
342+
337343 It ("should fail if cert is invalid" , func () {
338- _ , err := config .New ("fixtures/invalid_frontend_certs.yml" , true )
344+ _ , err := config .New ("fixtures/invalid_frontend_certs.yml" , false )
345+ Expect (err ).To (HaveOccurred ())
346+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: failed to parse PEM block" ))
347+ })
348+
349+ It ("should fail if cert_chain is missing SAN information and DNSnames" , func () {
350+ _ , err := config .New ("fixtures/frontend_cert_without_san.yml" , false )
339351 Expect (err ).To (HaveOccurred ())
340- Expect (err .Error ()).To (Equal ("failed to parse PEM block " ))
352+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: cert_chain must include either a subjectAltName extension or DNSNames " ))
341353 })
342354
355+ It ("should fail if cert_chain is invalid" , func () {
356+ _ , err := config .New ("fixtures/invalid_frontend_cert_certchain.yml" , false )
357+ Expect (err ).To (HaveOccurred ())
358+ Expect (err .Error ()).To (Equal ("frontend_tls[0]: could not parse certificate: x509: malformed certificate" ))
359+ })
343360 })
344361 })
345-
346362})
0 commit comments