@@ -48,13 +48,47 @@ func TestTLS_MakeConfig(t *testing.T) {
48
48
t .Run ("Missing client certificate" , func (t * testing.T ) {
49
49
tlsConfig := & TLS {Enable : true , Key : "test.key" }
50
50
_ , err := tlsConfig .MakeConfig ("icinga.com" )
51
- require .Error (t , err )
51
+ require .ErrorContains (t , err , "client certificate missing" )
52
52
})
53
53
54
54
t .Run ("Missing private key" , func (t * testing.T ) {
55
55
tlsConfig := & TLS {Enable : true , Cert : "test.crt" }
56
56
_ , err := tlsConfig .MakeConfig ("icinga.com" )
57
- require .Error (t , err )
57
+ require .ErrorContains (t , err , "private key missing" )
58
+ })
59
+
60
+ t .Run ("Cert is file, Key is PEM" , func (t * testing.T ) {
61
+ tlsConfig := & TLS {
62
+ Enable : true ,
63
+ Cert : "test.crt" ,
64
+ Key : `-----BEGIN EC PRIVATE KEY-----
65
+ MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
66
+ AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
67
+ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
68
+ -----END EC PRIVATE KEY----` ,
69
+ }
70
+ _ , err := tlsConfig .MakeConfig ("icinga.com" )
71
+ require .ErrorContains (t , err , "either both certificate and key are PEM or none; is PEM certificate=false, key=true" )
72
+ })
73
+
74
+ t .Run ("Cert is PEM, Key is file" , func (t * testing.T ) {
75
+ tlsConfig := & TLS {
76
+ Enable : true ,
77
+ Cert : `-----BEGIN CERTIFICATE-----
78
+ MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
79
+ DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
80
+ EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
81
+ 7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
82
+ 5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
83
+ BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
84
+ NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
85
+ Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
86
+ 6MF9+Yw1Yy0t
87
+ -----END CERTIFICATE-----` ,
88
+ Key : "test.key" ,
89
+ }
90
+ _ , err := tlsConfig .MakeConfig ("icinga.com" )
91
+ require .ErrorContains (t , err , "either both certificate and key are PEM or none; is PEM certificate=true, key=false" )
58
92
})
59
93
60
94
t .Run ("x509" , func (t * testing.T ) {
@@ -93,7 +127,7 @@ func TestTLS_MakeConfig(t *testing.T) {
93
127
defer func (name string ) {
94
128
_ = os .Remove (name )
95
129
}(corruptFile .Name ())
96
- err = os .WriteFile (corruptFile .Name (), []byte ("corrupt PEM " ), 0600 )
130
+ err = os .WriteFile (corruptFile .Name (), []byte ("-----BEGIN CORRUPT----- \n OOPS \n -----END CORRUPT----- " ), 0600 )
97
131
require .NoError (t , err )
98
132
99
133
t .Run ("Valid certificate and key" , func (t * testing.T ) {
@@ -104,6 +138,19 @@ func TestTLS_MakeConfig(t *testing.T) {
104
138
require .Len (t , config .Certificates , 1 )
105
139
})
106
140
141
+ t .Run ("Valid certificate and key as PEM" , func (t * testing.T ) {
142
+ certRaw , err := os .ReadFile (certFile .Name ())
143
+ require .NoError (t , err )
144
+ keyRaw , err := os .ReadFile (keyFile .Name ())
145
+ require .NoError (t , err )
146
+
147
+ tlsConfig := & TLS {Enable : true , Cert : string (certRaw ), Key : string (keyRaw )}
148
+ config , err := tlsConfig .MakeConfig ("icinga.com" )
149
+ require .NoError (t , err )
150
+ require .NotNil (t , config )
151
+ require .Len (t , config .Certificates , 1 )
152
+ })
153
+
107
154
t .Run ("Mismatched certificate and key" , func (t * testing.T ) {
108
155
_key , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
109
156
require .NoError (t , err )
@@ -149,6 +196,17 @@ func TestTLS_MakeConfig(t *testing.T) {
149
196
require .Error (t , err )
150
197
})
151
198
199
+ t .Run ("Corrupt certificate as PEM" , func (t * testing.T ) {
200
+ corruptRaw , err := os .ReadFile (corruptFile .Name ())
201
+ require .NoError (t , err )
202
+ keyRaw , err := os .ReadFile (keyFile .Name ())
203
+ require .NoError (t , err )
204
+
205
+ tlsConfig := & TLS {Enable : true , Cert : string (corruptRaw ), Key : string (keyRaw )}
206
+ _ , err = tlsConfig .MakeConfig ("icinga.com" )
207
+ require .Error (t , err )
208
+ })
209
+
152
210
t .Run ("Invalid key path" , func (t * testing.T ) {
153
211
tlsConfig := & TLS {Enable : true , Cert : certFile .Name (), Key : "nonexistent.key" }
154
212
_ , err := tlsConfig .MakeConfig ("icinga.com" )
@@ -184,6 +242,17 @@ func TestTLS_MakeConfig(t *testing.T) {
184
242
require .NotNil (t , config .RootCAs )
185
243
})
186
244
245
+ t .Run ("Valid CA as PEM" , func (t * testing.T ) {
246
+ caRaw , err := os .ReadFile (caFile .Name ())
247
+ require .NoError (t , err )
248
+
249
+ tlsConfig := & TLS {Enable : true , Ca : string (caRaw )}
250
+ config , err := tlsConfig .MakeConfig ("icinga.com" )
251
+ require .NoError (t , err )
252
+ require .NotNil (t , config )
253
+ require .NotNil (t , config .RootCAs )
254
+ })
255
+
187
256
t .Run ("Invalid CA path" , func (t * testing.T ) {
188
257
tlsConfig := & TLS {Enable : true , Ca : "nonexistent.ca" }
189
258
_ , err := tlsConfig .MakeConfig ("icinga.com" )
0 commit comments