Skip to content

Commit a897718

Browse files
authored
add some TLS options to kafka.json (#127)
New options: * tls_client_cert_file * tls_client_key_file * tls_server_ca_cert_file * tls_server_insecure_skip_verify
1 parent b3e62c3 commit a897718

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ addresses and access credentials.
9797
This schema registry config file contains the URI of the service and
9898
access credentials.
9999

100-
> We currently support just SASL authentication though it will be easy
101-
> to add other authentication options (or no auth). Please let us know if
102-
> you have a requirement here.
100+
> We currently support no authentication, SASL/PLAIN authentication, and
101+
> TLS client authentication, though it will be easy to add other
102+
> authentication options. Please let us know if you have a requirement
103+
> here.
103104
104105
## Description
105106

cli/flags.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"crypto/tls"
5+
"crypto/x509"
56
"encoding/json"
67
"flag"
78
"fmt"
@@ -62,11 +63,15 @@ func getKey() (apiKey, error) {
6263
}
6364

6465
type config struct {
65-
BootstrapServers string `json:"bootstrap_servers"`
66-
SecurityProtocol string `json:"security_protocol"`
67-
SaslMechanisms string `json:"sasl_mechanisms"`
68-
SaslUsername string `json:"sasl_username"`
69-
SaslPassword string `json:"sasl_password"`
66+
BootstrapServers string `json:"bootstrap_servers"`
67+
SecurityProtocol string `json:"security_protocol"`
68+
SaslMechanisms string `json:"sasl_mechanisms"`
69+
SaslUsername string `json:"sasl_username"`
70+
SaslPassword string `json:"sasl_password"`
71+
TLSClientCertFile string `json:"tls_client_cert_file"`
72+
TLSClientKeyFile string `json:"tls_client_key_file"`
73+
TLSServerCACertFile string `json:"tls_server_ca_cert_file"`
74+
TLSServerInsecureSkipVerify bool `json:"tls_server_insecure_skip_verify"`
7075
}
7176

7277
func LoadKafkaConfig() ([]kgo.Opt, error) {
@@ -90,10 +95,31 @@ func LoadKafkaConfig() ([]kgo.Opt, error) {
9095
switch c.SecurityProtocol {
9196
case "", "PLAINTEXT", "SASL_PLAINTEXT":
9297
case "SSL", "SASL_SSL":
98+
var tlsConfig tls.Config
99+
if c.TLSClientCertFile != "" && c.TLSClientKeyFile != "" {
100+
cert, err := tls.LoadX509KeyPair(c.TLSClientCertFile, c.TLSClientKeyFile)
101+
if err != nil {
102+
return nil, fmt.Errorf("failed to load key pair from tls_client_cert_file and tls_client_key_file: %w", err)
103+
}
104+
tlsConfig.Certificates = []tls.Certificate{cert}
105+
}
106+
if c.TLSServerCACertFile != "" {
107+
caCert, err := os.ReadFile(c.TLSServerCACertFile)
108+
if err != nil {
109+
return nil, fmt.Errorf("failed to read tls_server_ca_cert_file: %w", err)
110+
}
111+
p := x509.NewCertPool()
112+
if !p.AppendCertsFromPEM(caCert) {
113+
return nil, fmt.Errorf("failed to append certificates from tls_server_ca_cert_file")
114+
}
115+
tlsConfig.RootCAs = p
116+
}
117+
tlsConfig.InsecureSkipVerify = c.TLSServerInsecureSkipVerify
93118
d := &tls.Dialer{
94119
NetDialer: &net.Dialer{
95120
Timeout: 10 * time.Second,
96121
},
122+
Config: &tlsConfig,
97123
}
98124
opts = append(opts, kgo.Dialer(d.DialContext))
99125
default:

kafka.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2-
"bootstrap_servers": "<YOUR KAFKA BOOTSTRAP SERVERS>",
3-
"security_protocol": "SASL_SSL",
4-
"sasl_mechanisms": "PLAIN",
5-
"sasl_username": "<YOUR KAFKA USERNAME>",
6-
"sasl_password": "<YOUR KAFKA PASSWORD>"
2+
"bootstrap_servers": "comma-separated list of Kafka bootstrap servers",
3+
"security_protocol": "PLAINTEXT, SASL_PLAINTEXT, SASL_SSL, or SSL (default: PLAINTEXT)",
4+
"sasl_mechanisms": "must be PLAIN for SASL_PLAINTEXT and SASL_SSL",
5+
"sasl_username": "Kafka username for SASL_PLAINTEXT and SASL_SSL",
6+
"sasl_password": "Kafka password for SASL_PLAINTEXT and SASL_SSL",
7+
"tls_client_cert_file": "path to certificate file for client authentication for SASL_SSL and SSL",
8+
"tls_client_key_file": "path to private key file for client authentication for SASL_SSL and SSL",
9+
"tls_server_ca_cert_file": "path to root CA certificate file for server certificate verification for SASL_SSL and SSL",
10+
"tls_server_insecure_skip_verify": "set to true to skip server certificate verification for SASL_SSL and SSL (default: false)"
711
}

0 commit comments

Comments
 (0)