Skip to content

Commit 6f61c54

Browse files
author
Eric Lee
authored
[Prometheus Remote Write Exporter for Cortex] Fix Panic Issue in MutualTLS Test (#315)
* Add static ECSDA cert files, cert generator script, and update auth_test * Add new line and comment to cert generation script * Remove extra new line from comment * Add test for generated certs and fix file names * Change key generation to ecdsa p256 * Run certificate generator script to retry CI tests * Remove static certificate files and bash script * Update comment to retry tests * Update comment to retry tests
1 parent e497214 commit 6f61c54

File tree

1 file changed

+103
-75
lines changed

1 file changed

+103
-75
lines changed

exporters/metric/cortex/auth_test.go

+103-75
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
package cortex
1919

2020
import (
21+
"crypto/ecdsa"
22+
"crypto/elliptic"
2123
"crypto/rand"
22-
"crypto/rsa"
2324
"crypto/tls"
2425
"crypto/x509"
2526
"crypto/x509/pkix"
@@ -108,7 +109,7 @@ func TestAuthentication(t *testing.T) {
108109
}
109110
for _, test := range tests {
110111
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
112113
// request. The server writes the request's Authorization header to the
113114
// response body.
114115
handler := func(rw http.ResponseWriter, req *http.Request) {
@@ -265,84 +266,111 @@ func TestBuildClient(t *testing.T) {
265266
// successfully verify a server and send a HTTP request and whether a server can
266267
// successfully verify the Exporter client and receive the HTTP request.
267268
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+
}
273323

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)
285331

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()
305340

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)
328354

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+
}
333361
}
334362

335363
// generateCertFiles generates new certificate files from a template that is signed with
336364
// the provided signer certificate and key.
337365
func generateCertFiles(
338366
template *x509.Certificate,
339367
signer *x509.Certificate,
340-
signerKey *rsa.PrivateKey,
368+
signerKey *ecdsa.PrivateKey,
341369
certFilepath string,
342370
keyFilepath string,
343-
) (*x509.Certificate, *rsa.PrivateKey, error) {
371+
) (*x509.Certificate, *ecdsa.PrivateKey, error) {
344372
// 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)
346374
if err != nil {
347375
return nil, nil, err
348376
}
@@ -400,7 +428,7 @@ func generateCertFiles(
400428

401429
// generateCACertFiles creates a CA certificate and key in the local directory. This
402430
// 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) {
404432
// Create a template for CA certificates.
405433
certTemplate := &x509.Certificate{
406434
SerialNumber: big.NewInt(123),
@@ -435,10 +463,10 @@ func generateCACertFiles(certFilepath string, keyFilepath string) (*x509.Certifi
435463
// authority.
436464
func generateServingCertFiles(
437465
caCert *x509.Certificate,
438-
caPrivateKey *rsa.PrivateKey,
466+
caPrivateKey *ecdsa.PrivateKey,
439467
certFilepath string,
440468
keyFilepath string,
441-
) (*x509.Certificate, *rsa.PrivateKey, error) {
469+
) (*x509.Certificate, *ecdsa.PrivateKey, error) {
442470
certTemplate := &x509.Certificate{
443471
SerialNumber: big.NewInt(456),
444472
Subject: pkix.Name{
@@ -457,8 +485,8 @@ func generateServingCertFiles(
457485
certTemplate,
458486
caCert,
459487
caPrivateKey,
460-
"./serving_cert.pem",
461-
"./serving_key.pem",
488+
certFilepath,
489+
keyFilepath,
462490
)
463491
if err != nil {
464492
return nil, nil, err
@@ -472,10 +500,10 @@ func generateServingCertFiles(
472500
// authority.
473501
func generateClientCertFiles(
474502
caCert *x509.Certificate,
475-
caPrivateKey *rsa.PrivateKey,
503+
caPrivateKey *ecdsa.PrivateKey,
476504
certFilepath string,
477505
keyFilepath string,
478-
) (*x509.Certificate, *rsa.PrivateKey, error) {
506+
) (*x509.Certificate, *ecdsa.PrivateKey, error) {
479507
certTemplate := &x509.Certificate{
480508
SerialNumber: big.NewInt(789),
481509
Subject: pkix.Name{
@@ -493,8 +521,8 @@ func generateClientCertFiles(
493521
certTemplate,
494522
caCert,
495523
caPrivateKey,
496-
"./client_cert.pem",
497-
"./client_key.pem",
524+
certFilepath,
525+
keyFilepath,
498526
)
499527
if err != nil {
500528
return nil, nil, err

0 commit comments

Comments
 (0)