18
18
package cortex
19
19
20
20
import (
21
+ "crypto/ecdsa"
22
+ "crypto/elliptic"
21
23
"crypto/rand"
22
- "crypto/rsa"
23
24
"crypto/tls"
24
25
"crypto/x509"
25
26
"crypto/x509/pkix"
@@ -108,7 +109,7 @@ func TestAuthentication(t *testing.T) {
108
109
}
109
110
for _ , test := range tests {
110
111
t .Run (test .testName , func (t * testing.T ) {
111
- // Set up a test server that runs a handler function when it receives a http
112
+ // Set up a test server that runs a handler function when it receives a HTTP
112
113
// request. The server writes the request's Authorization header to the
113
114
// response body.
114
115
handler := func (rw http.ResponseWriter , req * http.Request ) {
@@ -265,84 +266,111 @@ func TestBuildClient(t *testing.T) {
265
266
// successfully verify a server and send a HTTP request and whether a server can
266
267
// successfully verify the Exporter client and receive the HTTP request.
267
268
func TestMutualTLS (t * testing.T ) {
268
- // Generate certificate authority certificate to sign other certificates.
269
- caCert , caPrivateKey , err := generateCACertFiles ("./ca_cert.pem" , "./ca_key.pem" )
270
- require .NoError (t , err )
271
- defer os .Remove ("./ca_cert.pem" )
272
- defer os .Remove ("./ca_key.pem" )
269
+ tests := []struct {
270
+ testName string
271
+ generateCerts bool
272
+ caCert string
273
+ caKey string
274
+ servingCert string
275
+ servingKey string
276
+ clientCert string
277
+ clientKey string
278
+ }{
279
+ {
280
+ testName : "Generated ECDSA Certs" ,
281
+ generateCerts : true ,
282
+ caCert : "ca.crt" ,
283
+ caKey : "ca.key" ,
284
+ servingCert : "server.crt" ,
285
+ servingKey : "server.key" ,
286
+ clientCert : "client.crt" ,
287
+ clientKey : "client.key" ,
288
+ },
289
+ }
290
+ for _ , test := range tests {
291
+ t .Run (test .testName , func (t * testing.T ) {
292
+ if test .generateCerts {
293
+ // Generate certificate authority certificate to sign other certificates.
294
+ caCert , caPrivateKey , err := generateCACertFiles (test .caCert , test .caKey )
295
+ require .NoError (t , err )
296
+ defer os .Remove (test .caCert )
297
+ defer os .Remove (test .caKey )
298
+
299
+ // Generate certificate for the server. The client will check this
300
+ // certificate against its certificate authority to verify the server.
301
+ _ , _ , err = generateServingCertFiles (
302
+ caCert ,
303
+ caPrivateKey ,
304
+ test .servingCert ,
305
+ test .servingKey ,
306
+ )
307
+ require .NoError (t , err )
308
+ defer os .Remove (test .servingCert )
309
+ defer os .Remove (test .servingKey )
310
+
311
+ // Generate certificate for the client. The server will check this
312
+ // certificate against its certificate authority to verify the client.
313
+ _ , _ , err = generateClientCertFiles (
314
+ caCert ,
315
+ caPrivateKey ,
316
+ test .clientCert ,
317
+ test .clientKey ,
318
+ )
319
+ require .NoError (t , err )
320
+ defer os .Remove (test .clientCert )
321
+ defer os .Remove (test .clientKey )
322
+ }
273
323
274
- // Generate certificate for the server. The client will check this certificate against
275
- // its certificate authority to verify the server.
276
- _ , _ , err = generateServingCertFiles (
277
- caCert ,
278
- caPrivateKey ,
279
- "./serving_cert.pem" ,
280
- "./serving_key.pem" ,
281
- )
282
- require .NoError (t , err )
283
- defer os .Remove ("./serving_cert.pem" )
284
- defer os .Remove ("./serving_key.pem" )
324
+ // Generate the TLS Config to set up mutual TLS on the server.
325
+ serverTLSConfig , err := generateServerTLSConfig (
326
+ test .caCert ,
327
+ test .servingCert ,
328
+ test .servingKey ,
329
+ )
330
+ require .NoError (t , err )
285
331
286
- // Generate certificate for the client. The server will check this certificate against
287
- // its certificate authority to verify the client.
288
- _ , _ , err = generateClientCertFiles (
289
- caCert ,
290
- caPrivateKey ,
291
- "./client_cert.pem" ,
292
- "./client_key.pem" ,
293
- )
294
- require .NoError (t , err )
295
- defer os .Remove ("./client_cert.pem" )
296
- defer os .Remove ("./client_key.pem" )
297
-
298
- // Generate the tls Config to set up mutual TLS on the server.
299
- serverTLSConfig , err := generateServerTLSConfig (
300
- "ca_cert.pem" ,
301
- "serving_cert.pem" ,
302
- "serving_key.pem" ,
303
- )
304
- require .NoError (t , err )
332
+ // Create and start the TLS server.
333
+ handler := func (rw http.ResponseWriter , req * http.Request ) {
334
+ fmt .Fprint (rw , "Successfully verified client and received request!" )
335
+ }
336
+ server := httptest .NewUnstartedServer (http .HandlerFunc (handler ))
337
+ server .TLS = serverTLSConfig
338
+ server .StartTLS ()
339
+ defer server .Close ()
305
340
306
- // Create and start the TLS server.
307
- handler := func (rw http.ResponseWriter , req * http.Request ) {
308
- fmt .Fprint (rw , "Successfully verified client and received request!" )
309
- }
310
- server := httptest .NewUnstartedServer (http .HandlerFunc (handler ))
311
- server .TLS = serverTLSConfig
312
- server .StartTLS ()
313
- defer server .Close ()
314
-
315
- // Create an Exporter client with the client and CA certificate files.
316
- exporter := Exporter {
317
- Config {
318
- TLSConfig : map [string ]string {
319
- "ca_file" : "./ca_cert.pem" ,
320
- "cert_file" : "./client_cert.pem" ,
321
- "key_file" : "./client_key.pem" ,
322
- "insecure_skip_verify" : "0" ,
323
- },
324
- },
325
- }
326
- client , err := exporter .buildClient ()
327
- require .NoError (t , err )
341
+ // Create an Exporter client with the client and CA certificate files.
342
+ exporter := Exporter {
343
+ Config {
344
+ TLSConfig : map [string ]string {
345
+ "ca_file" : test .caCert ,
346
+ "cert_file" : test .clientCert ,
347
+ "key_file" : test .clientKey ,
348
+ "insecure_skip_verify" : "0" ,
349
+ },
350
+ },
351
+ }
352
+ client , err := exporter .buildClient ()
353
+ require .NoError (t , err )
328
354
329
- // Send the request and verify that the request was successfully received.
330
- res , err := client .Get (server .URL )
331
- require .NoError (t , err )
332
- defer res .Body .Close ()
355
+ // Send the request and verify that the request was successfully received.
356
+ res , err := client .Get (server .URL )
357
+ require .NoError (t , err )
358
+ defer res .Body .Close ()
359
+ })
360
+ }
333
361
}
334
362
335
363
// generateCertFiles generates new certificate files from a template that is signed with
336
364
// the provided signer certificate and key.
337
365
func generateCertFiles (
338
366
template * x509.Certificate ,
339
367
signer * x509.Certificate ,
340
- signerKey * rsa .PrivateKey ,
368
+ signerKey * ecdsa .PrivateKey ,
341
369
certFilepath string ,
342
370
keyFilepath string ,
343
- ) (* x509.Certificate , * rsa .PrivateKey , error ) {
371
+ ) (* x509.Certificate , * ecdsa .PrivateKey , error ) {
344
372
// Generate a private key for the new certificate. This does not have to be rsa 4096.
345
- privateKey , err := rsa .GenerateKey (rand . Reader , 4096 )
373
+ privateKey , err := ecdsa .GenerateKey (elliptic . P256 (), rand . Reader )
346
374
if err != nil {
347
375
return nil , nil , err
348
376
}
@@ -400,7 +428,7 @@ func generateCertFiles(
400
428
401
429
// generateCACertFiles creates a CA certificate and key in the local directory. This
402
430
// certificate is used to sign other certificates.
403
- func generateCACertFiles (certFilepath string , keyFilepath string ) (* x509.Certificate , * rsa .PrivateKey , error ) {
431
+ func generateCACertFiles (certFilepath string , keyFilepath string ) (* x509.Certificate , * ecdsa .PrivateKey , error ) {
404
432
// Create a template for CA certificates.
405
433
certTemplate := & x509.Certificate {
406
434
SerialNumber : big .NewInt (123 ),
@@ -435,10 +463,10 @@ func generateCACertFiles(certFilepath string, keyFilepath string) (*x509.Certifi
435
463
// authority.
436
464
func generateServingCertFiles (
437
465
caCert * x509.Certificate ,
438
- caPrivateKey * rsa .PrivateKey ,
466
+ caPrivateKey * ecdsa .PrivateKey ,
439
467
certFilepath string ,
440
468
keyFilepath string ,
441
- ) (* x509.Certificate , * rsa .PrivateKey , error ) {
469
+ ) (* x509.Certificate , * ecdsa .PrivateKey , error ) {
442
470
certTemplate := & x509.Certificate {
443
471
SerialNumber : big .NewInt (456 ),
444
472
Subject : pkix.Name {
@@ -457,8 +485,8 @@ func generateServingCertFiles(
457
485
certTemplate ,
458
486
caCert ,
459
487
caPrivateKey ,
460
- "./serving_cert.pem" ,
461
- "./serving_key.pem" ,
488
+ certFilepath ,
489
+ keyFilepath ,
462
490
)
463
491
if err != nil {
464
492
return nil , nil , err
@@ -472,10 +500,10 @@ func generateServingCertFiles(
472
500
// authority.
473
501
func generateClientCertFiles (
474
502
caCert * x509.Certificate ,
475
- caPrivateKey * rsa .PrivateKey ,
503
+ caPrivateKey * ecdsa .PrivateKey ,
476
504
certFilepath string ,
477
505
keyFilepath string ,
478
- ) (* x509.Certificate , * rsa .PrivateKey , error ) {
506
+ ) (* x509.Certificate , * ecdsa .PrivateKey , error ) {
479
507
certTemplate := & x509.Certificate {
480
508
SerialNumber : big .NewInt (789 ),
481
509
Subject : pkix.Name {
@@ -493,8 +521,8 @@ func generateClientCertFiles(
493
521
certTemplate ,
494
522
caCert ,
495
523
caPrivateKey ,
496
- "./client_cert.pem" ,
497
- "./client_key.pem" ,
524
+ certFilepath ,
525
+ keyFilepath ,
498
526
)
499
527
if err != nil {
500
528
return nil , nil , err
0 commit comments