Skip to content

Commit c9dea6d

Browse files
tls_manager_test.go: reproduce partial tls files handling
When there is only one of the tls pairs (key/certificate) and the other is missing, the TLS manager currently assumes it exists and ignore generating them. This results in error propgated to user that the other tls pair file is missing/not found.
1 parent 85a5bf2 commit c9dea6d

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tls_manager_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,92 @@ func newTestDirectory(t *testing.T) (string, string, string) {
369369

370370
return tempDir, certPath, keyPath
371371
}
372+
373+
// TestGenerateCertPairWithPartialFiles tests that generateCertPair regenerates
374+
// a cert/key pair when only one file exists.
375+
func TestGenerateCertPairWithPartialFiles(t *testing.T) {
376+
t.Parallel()
377+
378+
keyRing := &mock.SecretKeyRing{
379+
RootKey: privKey,
380+
}
381+
382+
testCases := []struct {
383+
name string
384+
setup func(t *testing.T, certPath, keyPath string)
385+
}{
386+
{
387+
name: "only key exists",
388+
setup: func(t *testing.T, certPath, keyPath string) {
389+
// Create only a key file. It simulates leftover
390+
// from previous run.
391+
_, keyBytes := genCertPair(t, false)
392+
keyBuf := &bytes.Buffer{}
393+
err := pem.Encode(
394+
keyBuf, &pem.Block{
395+
Type: "EC PRIVATE KEY",
396+
Bytes: keyBytes,
397+
},
398+
)
399+
require.NoError(t, err)
400+
401+
err = os.WriteFile(
402+
keyPath, keyBuf.Bytes(), 0600,
403+
)
404+
require.NoError(t, err)
405+
},
406+
},
407+
{
408+
name: "only cert exists",
409+
setup: func(t *testing.T, certPath, keyPath string) {
410+
// Create only a cert file. It simulates
411+
// leftover from previous run.
412+
certBytes, _ := genCertPair(t, false)
413+
certBuf := &bytes.Buffer{}
414+
err := pem.Encode(
415+
certBuf, &pem.Block{
416+
Type: "CERTIFICATE",
417+
Bytes: certBytes,
418+
},
419+
)
420+
require.NoError(t, err)
421+
422+
err = os.WriteFile(
423+
certPath, certBuf.Bytes(), 0644,
424+
)
425+
require.NoError(t, err)
426+
},
427+
},
428+
}
429+
430+
for _, tc := range testCases {
431+
tc := tc
432+
t.Run(tc.name, func(t *testing.T) {
433+
t.Parallel()
434+
435+
tempDir := t.TempDir()
436+
certPath := tempDir + "/tls.cert"
437+
keyPath := tempDir + "/tls.key"
438+
439+
tc.setup(t, certPath, keyPath)
440+
441+
cfg := &TLSManagerCfg{
442+
TLSCertPath: certPath,
443+
TLSKeyPath: keyPath,
444+
TLSCertDuration: testTLSCertDuration,
445+
}
446+
tlsManager := NewTLSManager(cfg)
447+
448+
err := tlsManager.generateCertPair(keyRing)
449+
require.NoError(
450+
t, err, "should generate new cert pair when %s",
451+
tc.name,
452+
)
453+
454+
_, _, err = cert.GetCertBytesFromPath(certPath, keyPath)
455+
require.NoError(
456+
t, err, "should be able to load cert pair",
457+
)
458+
})
459+
}
460+
}

0 commit comments

Comments
 (0)