Skip to content

Commit 59a98cf

Browse files
Kafka TLS: allow CA(RootCAs) or cert/key(certificate chains) only
1 parent d39d428 commit 59a98cf

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

common/messaging/kafka/clientImpl.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,32 +169,34 @@ func (c *clientImpl) initAuth(saramaConfig *sarama.Config) error {
169169
return nil
170170
}
171171

172-
// convertTLSConfig convert tls config
173-
func convertTLSConfig(tlsConfig auth.TLS) (*tls.Config, error) {
174-
if !tlsConfig.Enabled {
172+
// convertTLSConfig converts tls config
173+
func convertTLSConfig(authConfig auth.TLS) (*tls.Config, error) {
174+
if !authConfig.Enabled {
175175
return nil, nil
176176
}
177177

178-
if tlsConfig.CertFile != "" && tlsConfig.CaFile != "" && tlsConfig.KeyFile != "" {
179-
cert, err := tls.LoadX509KeyPair(tlsConfig.CertFile, tlsConfig.KeyFile)
178+
tlsConfig := &tls.Config{
179+
InsecureSkipVerify: !authConfig.EnableHostVerification,
180+
}
181+
182+
if authConfig.CaFile != "" {
183+
caCertPool := x509.NewCertPool()
184+
pemData, err := ioutil.ReadFile(authConfig.CaFile)
180185
if err != nil {
181186
return nil, err
182187
}
183-
caCertPool := x509.NewCertPool()
184-
pemData, err := ioutil.ReadFile(tlsConfig.CaFile)
188+
caCertPool.AppendCertsFromPEM(pemData)
189+
190+
tlsConfig.RootCAs = caCertPool
191+
}
192+
193+
if authConfig.CertFile != "" && authConfig.KeyFile != "" {
194+
cert, err := tls.LoadX509KeyPair(authConfig.CertFile, authConfig.KeyFile)
185195
if err != nil {
186196
return nil, err
187197
}
188-
caCertPool.AppendCertsFromPEM(pemData)
189198

190-
return &tls.Config{
191-
Certificates: []tls.Certificate{cert},
192-
RootCAs: caCertPool,
193-
InsecureSkipVerify: !tlsConfig.EnableHostVerification,
194-
}, nil
195-
} else {
196-
return &tls.Config{
197-
InsecureSkipVerify: !tlsConfig.EnableHostVerification,
198-
}, nil
199+
tlsConfig.Certificates = []tls.Certificate{cert}
199200
}
201+
return tlsConfig, nil
200202
}

0 commit comments

Comments
 (0)