Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,33 @@ func newTLSConfig(dir string) (*tls.Config, error) {

tlsConfig := &tls.Config{}

_, certFileErr := os.Stat(certFile)
_, keyFileErr := os.Stat(keyFile)
_, caFileErr := os.Stat(caFile)

if certFileErr != nil && keyFileErr != nil && caFileErr != nil {
return nil, errors.Join(certFileErr, keyFileErr, caFileErr)
}
Comment on lines +329 to +331

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine error handling to account for individual file errors.

Currently, the function returns an error if all files are missing. It might be more useful to handle cases where any of the files are missing individually, allowing for more specific error messages and handling.

- if certFileErr != nil && keyFileErr != nil && caFileErr != nil {
+ if certFileErr != nil || keyFileErr != nil || caFileErr != nil {
+    var errMsgs []string
+    if certFileErr != nil { errMsgs = append(errMsgs, "certificate file missing") }
+    if keyFileErr != nil { errMsgs = append(errMsgs, "key file missing") }
+    if caFileErr != nil { errMsgs = append(errMsgs, "CA file missing") }
+    return nil, fmt.Errorf("file error(s): %s", strings.Join(errMsgs, ", "))
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if certFileErr != nil && keyFileErr != nil && caFileErr != nil {
return nil, errors.Join(certFileErr, keyFileErr, caFileErr)
}
if certFileErr != nil || keyFileErr != nil || caFileErr != nil {
var errMsgs []string
if certFileErr != nil { errMsgs = append(errMsgs, "certificate file missing") }
if keyFileErr != nil { errMsgs = append(errMsgs, "key file missing") }
if caFileErr != nil { errMsgs = append(errMsgs, "CA file missing") }
return nil, fmt.Errorf("file error(s): %s", strings.Join(errMsgs, ", "))
}

Comment on lines +325 to +331

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error aggregation.

The current implementation attempts to aggregate errors using errors.Join, which is not a standard function in Go's errors package. Consider using a custom error aggregation approach or a package like github.com/hashicorp/go-multierror for error handling.

- return nil, errors.Join(certFileErr, keyFileErr, caFileErr)
+ var multiErr *multierror.Error
+ multiErr = multierror.Append(multiErr, certFileErr, keyFileErr, caFileErr)
+ return nil, multiErr.ErrorOrNil()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_, certFileErr := os.Stat(certFile)
_, keyFileErr := os.Stat(keyFile)
_, caFileErr := os.Stat(caFile)
if certFileErr != nil && keyFileErr != nil && caFileErr != nil {
return nil, errors.Join(certFileErr, keyFileErr, caFileErr)
}
_, certFileErr := os.Stat(certFile)
_, keyFileErr := os.Stat(keyFile)
_, caFileErr := os.Stat(caFile)
if certFileErr != nil && keyFileErr != nil && caFileErr != nil {
var multiErr *multierror.Error
multiErr = multierror.Append(multiErr, certFileErr, keyFileErr, caFileErr)
return nil, multiErr.ErrorOrNil()
}


// Load client cert
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
if certFileErr == nil && keyFileErr == nil {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
tlsConfig.Certificates = []tls.Certificate{cert}

// Load CA cert
caCert, err := os.ReadFile(caFile)
if err != nil {
return nil, err
if caFileErr == nil {
caCert, err := os.ReadFile(caFile)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool

return tlsConfig, nil
}
Expand Down