Skip to content

Commit

Permalink
fix(kafka): improve TLS and plaintext auth configuration
Browse files Browse the repository at this point in the history
- Fix TLS configuration initialization for Kafka auth
- Add proper handling of system CA certs pool
- Set secure defaults for TLS configuration
- Remove redundant code comments

Signed-off-by: Amol Verma <[email protected]>
Signed-off-by: Amol Verma <[email protected]>
  • Loading branch information
Amol Verma authored and Amol Verma committed Feb 23, 2025
1 parent 0bdd1a8 commit d239d32
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 6 additions & 5 deletions pkg/kafka/auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (config *AuthenticationConfig) SetConfiguration(saramaConfig *sarama.Config
if err := setTLSConfiguration(&config.TLS, saramaConfig, logger); err != nil {
return err
}
// saramaConfig.Net.TLS.Config.InsecureSkipVerify = true
// if config.Authentication == tls {
// return nil
// }
}

switch authentication {
Expand Down Expand Up @@ -84,12 +88,9 @@ func (config *AuthenticationConfig) InitFromViper(configPrefix string, v *viper.
if err != nil {
return fmt.Errorf("failed to process Kafka TLS options: %w", err)
}
tlsCfg.IncludeSystemCACertsPool = (config.Authentication == tls)
tlsCfg.Insecure = false
config.TLS = tlsCfg
} else {
// Explicitly set TLS to insecure when disabled
config.TLS = configtls.ClientConfig{
Insecure: true,
}
}

config.PlainText.Username = v.GetString(configPrefix + plainTextPrefix + suffixPlainTextUsername)
Expand Down
14 changes: 8 additions & 6 deletions pkg/kafka/auth/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
)

func setTLSConfiguration(config *configtls.ClientConfig, saramaConfig *sarama.Config, _ *zap.Logger) error {
tlsConfig, err := config.LoadTLSConfig(context.Background())
if err != nil {
return fmt.Errorf("error loading tls config: %w", err)
}
if !config.Insecure {
tlsConfig, err := config.LoadTLSConfig(context.Background())
if err != nil {
return fmt.Errorf("error loading tls config: %w", err)
}

saramaConfig.Net.TLS.Enable = true
saramaConfig.Net.TLS.Config = tlsConfig
saramaConfig.Net.TLS.Enable = true
saramaConfig.Net.TLS.Config = tlsConfig
}
return nil
}

0 comments on commit d239d32

Please sign in to comment.