@@ -16,6 +16,37 @@ import (
1616 "time"
1717)
1818
19+ func Test_loadPemOrFile (t * testing.T ) {
20+ cert , _ , err := generateCert ("cert" , generateCertOptions {})
21+ require .NoError (t , err )
22+ certPem := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : cert .Raw })
23+
24+ certFile , err := os .CreateTemp ("" , "cert-*.pem" )
25+ require .NoError (t , err )
26+ defer func (name string ) {
27+ _ = os .Remove (name )
28+ }(certFile .Name ())
29+ _ , err = certFile .Write (certPem )
30+ require .NoError (t , err )
31+
32+ t .Run ("Load raw PEM" , func (t * testing.T ) {
33+ out , err := loadPemOrFile (string (certPem ))
34+ require .NoError (t , err )
35+ require .Equal (t , certPem , out )
36+ })
37+
38+ t .Run ("Load file" , func (t * testing.T ) {
39+ out , err := loadPemOrFile (certFile .Name ())
40+ require .NoError (t , err )
41+ require .Equal (t , certPem , out )
42+ })
43+
44+ t .Run ("Invalid file" , func (t * testing.T ) {
45+ _ , err := loadPemOrFile ("/dev/null/nonexistent" )
46+ require .Error (t , err )
47+ })
48+ }
49+
1950func TestTLS_MakeConfig (t * testing.T ) {
2051 t .Run ("TLS disabled" , func (t * testing.T ) {
2152 tlsConfig := & TLS {Enable : false }
@@ -48,13 +79,13 @@ func TestTLS_MakeConfig(t *testing.T) {
4879 t .Run ("Missing client certificate" , func (t * testing.T ) {
4980 tlsConfig := & TLS {Enable : true , Key : "test.key" }
5081 _ , err := tlsConfig .MakeConfig ("icinga.com" )
51- require .Error (t , err )
82+ require .ErrorContains (t , err , "client certificate missing" )
5283 })
5384
5485 t .Run ("Missing private key" , func (t * testing.T ) {
5586 tlsConfig := & TLS {Enable : true , Cert : "test.crt" }
5687 _ , err := tlsConfig .MakeConfig ("icinga.com" )
57- require .Error (t , err )
88+ require .ErrorContains (t , err , "private key missing" )
5889 })
5990
6091 t .Run ("x509" , func (t * testing.T ) {
@@ -93,7 +124,7 @@ func TestTLS_MakeConfig(t *testing.T) {
93124 defer func (name string ) {
94125 _ = os .Remove (name )
95126 }(corruptFile .Name ())
96- err = os .WriteFile (corruptFile .Name (), []byte ("corrupt PEM " ), 0600 )
127+ err = os .WriteFile (corruptFile .Name (), []byte ("-----BEGIN CORRUPT----- \n OOPS \n -----END CORRUPT----- " ), 0600 )
97128 require .NoError (t , err )
98129
99130 t .Run ("Valid certificate and key" , func (t * testing.T ) {
@@ -104,6 +135,30 @@ func TestTLS_MakeConfig(t *testing.T) {
104135 require .Len (t , config .Certificates , 1 )
105136 })
106137
138+ t .Run ("Valid certificate and key as PEM" , func (t * testing.T ) {
139+ certRaw , err := os .ReadFile (certFile .Name ())
140+ require .NoError (t , err )
141+ keyRaw , err := os .ReadFile (keyFile .Name ())
142+ require .NoError (t , err )
143+
144+ tlsConfig := & TLS {Enable : true , Cert : string (certRaw ), Key : string (keyRaw )}
145+ config , err := tlsConfig .MakeConfig ("icinga.com" )
146+ require .NoError (t , err )
147+ require .NotNil (t , config )
148+ require .Len (t , config .Certificates , 1 )
149+ })
150+
151+ t .Run ("Valid certificate and key, mixed file and PEM" , func (t * testing.T ) {
152+ keyRaw , err := os .ReadFile (keyFile .Name ())
153+ require .NoError (t , err )
154+
155+ tlsConfig := & TLS {Enable : true , Cert : certFile .Name (), Key : string (keyRaw )}
156+ config , err := tlsConfig .MakeConfig ("icinga.com" )
157+ require .NoError (t , err )
158+ require .NotNil (t , config )
159+ require .Len (t , config .Certificates , 1 )
160+ })
161+
107162 t .Run ("Mismatched certificate and key" , func (t * testing.T ) {
108163 _key , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
109164 require .NoError (t , err )
@@ -149,6 +204,17 @@ func TestTLS_MakeConfig(t *testing.T) {
149204 require .Error (t , err )
150205 })
151206
207+ t .Run ("Corrupt certificate as PEM" , func (t * testing.T ) {
208+ corruptRaw , err := os .ReadFile (corruptFile .Name ())
209+ require .NoError (t , err )
210+ keyRaw , err := os .ReadFile (keyFile .Name ())
211+ require .NoError (t , err )
212+
213+ tlsConfig := & TLS {Enable : true , Cert : string (corruptRaw ), Key : string (keyRaw )}
214+ _ , err = tlsConfig .MakeConfig ("icinga.com" )
215+ require .Error (t , err )
216+ })
217+
152218 t .Run ("Invalid key path" , func (t * testing.T ) {
153219 tlsConfig := & TLS {Enable : true , Cert : certFile .Name (), Key : "nonexistent.key" }
154220 _ , err := tlsConfig .MakeConfig ("icinga.com" )
@@ -184,6 +250,17 @@ func TestTLS_MakeConfig(t *testing.T) {
184250 require .NotNil (t , config .RootCAs )
185251 })
186252
253+ t .Run ("Valid CA as PEM" , func (t * testing.T ) {
254+ caRaw , err := os .ReadFile (caFile .Name ())
255+ require .NoError (t , err )
256+
257+ tlsConfig := & TLS {Enable : true , Ca : string (caRaw )}
258+ config , err := tlsConfig .MakeConfig ("icinga.com" )
259+ require .NoError (t , err )
260+ require .NotNil (t , config )
261+ require .NotNil (t , config .RootCAs )
262+ })
263+
187264 t .Run ("Invalid CA path" , func (t * testing.T ) {
188265 tlsConfig := & TLS {Enable : true , Ca : "nonexistent.ca" }
189266 _ , err := tlsConfig .MakeConfig ("icinga.com" )
0 commit comments