Skip to content

Commit

Permalink
Fix bug in TLS config handling (#170)
Browse files Browse the repository at this point in the history
* Decouple client and server certificate processing to prevent mTLS enforcement on everything
* Update tests to reflect changes
  • Loading branch information
mostafa authored Oct 19, 2022
1 parent 293f1f0 commit 5ce7074
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
45 changes: 23 additions & 22 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ func GetTLSConfig(tlsConfig TLSConfig) (*tls.Config, *Xk6KafkaError) {
tlsObject := newTLSObject(tlsConfig)

if tlsConfig.EnableTLS {
if tlsConfig.ClientCertPem == "" &&
tlsConfig.ClientKeyPem == "" &&
tlsConfig.ServerCaPem == "" {
if tlsConfig.ServerCaPem == "" {
return tlsObject, nil
}
} else {
Expand All @@ -130,28 +128,31 @@ func GetTLSConfig(tlsConfig TLSConfig) (*tls.Config, *Xk6KafkaError) {
noTLSConfig, "No TLS config provided. Continuing with TLS disabled.", nil)
}

// Load the client cert and key if they are provided
if err := fileExists(tlsConfig.ClientCertPem); err != nil {
return nil, err
}
if tlsConfig.ClientCertPem != "" && tlsConfig.ClientKeyPem != "" {
// Load the client certificate and key if provided
if err := fileExists(tlsConfig.ClientCertPem); err != nil {
return nil, err
}

if err := fileExists(tlsConfig.ClientKeyPem); err != nil {
return nil, err
}
if err := fileExists(tlsConfig.ClientKeyPem); err != nil {
return nil, err
}

var cert tls.Certificate
cert, err := tls.LoadX509KeyPair(tlsConfig.ClientCertPem, tlsConfig.ClientKeyPem)
if err != nil {
return nil, NewXk6KafkaError(
failedLoadX509KeyPair,
fmt.Sprintf(
"Error creating x509 key pair from \"%s\" and \"%s\".",
tlsConfig.ClientCertPem,
tlsConfig.ClientKeyPem),
err)
cert, err := tls.LoadX509KeyPair(tlsConfig.ClientCertPem, tlsConfig.ClientKeyPem)
if err != nil {
return nil, NewXk6KafkaError(
failedLoadX509KeyPair,
fmt.Sprintf(
"Error creating x509 key pair from \"%s\" and \"%s\".",
tlsConfig.ClientCertPem,
tlsConfig.ClientKeyPem),
err)
}

tlsObject.Certificates = []tls.Certificate{cert}
}

// Load the CA cert if it is provided
// Load the CA certificate if provided
if err := fileExists(tlsConfig.ServerCaPem); err != nil {
return nil, err
}
Expand All @@ -166,6 +167,7 @@ func GetTLSConfig(tlsConfig TLSConfig) (*tls.Config, *Xk6KafkaError) {
tlsConfig.ServerCaPem),
err)
}

caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
return nil, NewXk6KafkaError(
Expand All @@ -176,7 +178,6 @@ func GetTLSConfig(tlsConfig TLSConfig) (*tls.Config, *Xk6KafkaError) {
nil)
}

tlsObject.Certificates = []tls.Certificate{cert}
tlsObject.RootCAs = caCertPool
return tlsObject, nil
}
Expand Down
34 changes: 26 additions & 8 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kafka

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -146,41 +145,60 @@ func TestTlsConfigFails(t *testing.T) {
saslConfig := []*SimpleTLSConfig{
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{EnableTLS: true, ClientCertPem: "test.cer"},
tlsConfig: TLSConfig{},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "File not found: test.cer",
Code: noTLSConfig,
Message: "No TLS config provided. Continuing with TLS disabled.",
OriginalError: nil,
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ServerCaPem: "server.cer",
ClientCertPem: "client.cer",
ClientKeyPem: "client.pem",
},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "File not found: client.cer",
OriginalError: nil,
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ServerCaPem: "server.cer",
ClientCertPem: "fixtures/client.cer",
ClientKeyPem: "test.pem",
},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "File not found: ",
OriginalError: &os.PathError{},
Message: "File not found: test.pem",
OriginalError: nil,
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ServerCaPem: "server.cer",
ClientCertPem: "fixtures/client.cer",
ClientKeyPem: "fixtures/client.pem",
},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "File not found: ",
OriginalError: &os.PathError{},
Message: "File not found: server.cer",
OriginalError: nil,
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ServerCaPem: "fixtures/caroot.cer",
ClientCertPem: "fixtures/invalid-client.cer",
ClientKeyPem: "fixtures/invalid-client.pem",
},
Expand Down

0 comments on commit 5ce7074

Please sign in to comment.