2017-03-06 23:18:59 +03:00
|
|
|
package endpoint
|
|
|
|
|
|
|
|
import (
|
2021-02-15 20:04:37 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-03-06 23:18:59 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-08 11:20:45 +03:00
|
|
|
"os"
|
2017-03-06 23:18:59 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-02-15 20:04:37 +03:00
|
|
|
lg "log"
|
|
|
|
|
2017-03-06 23:18:59 +03:00
|
|
|
"github.com/Shopify/sarama"
|
2019-10-30 20:17:59 +03:00
|
|
|
"github.com/tidwall/gjson"
|
2021-02-15 20:04:37 +03:00
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2017-03-06 23:18:59 +03:00
|
|
|
)
|
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
const kafkaExpiresAfter = time.Second * 30
|
2017-03-06 23:18:59 +03:00
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// KafkaConn is an endpoint connection
|
|
|
|
type KafkaConn struct {
|
2017-03-06 23:18:59 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
ep Endpoint
|
|
|
|
conn sarama.SyncProducer
|
2021-07-01 00:18:44 +03:00
|
|
|
cfg *sarama.Config
|
2017-03-06 23:18:59 +03:00
|
|
|
ex bool
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Expired returns true if the connection has expired
|
|
|
|
func (conn *KafkaConn) Expired() bool {
|
2017-03-06 23:18:59 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
if !conn.ex {
|
2021-03-31 18:13:44 +03:00
|
|
|
if time.Since(conn.t) > kafkaExpiresAfter {
|
2022-09-25 16:28:17 +03:00
|
|
|
conn.close()
|
2017-03-06 23:18:59 +03:00
|
|
|
conn.ex = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn.ex
|
|
|
|
}
|
|
|
|
|
2022-09-25 16:28:17 +03:00
|
|
|
// ExpireNow forces the connection to expire
|
|
|
|
func (conn *KafkaConn) ExpireNow() {
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
conn.close()
|
|
|
|
conn.ex = true
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func (conn *KafkaConn) close() {
|
2017-03-06 23:18:59 +03:00
|
|
|
if conn.conn != nil {
|
|
|
|
conn.conn.Close()
|
|
|
|
conn.conn = nil
|
2021-07-01 00:18:44 +03:00
|
|
|
conn.cfg.MetricRegistry.UnregisterAll()
|
|
|
|
conn.cfg = nil
|
2017-03-06 23:18:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Send sends a message
|
|
|
|
func (conn *KafkaConn) Send(msg string) error {
|
2017-03-06 23:18:59 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
if conn.ex {
|
|
|
|
return errExpired
|
|
|
|
}
|
|
|
|
conn.t = time.Now()
|
|
|
|
|
2022-09-26 20:02:02 +03:00
|
|
|
if log.Level() > 2 {
|
2021-02-19 01:34:01 +03:00
|
|
|
sarama.Logger = lg.New(log.Output(), "[sarama] ", 0)
|
2021-02-15 20:04:37 +03:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:18:59 +03:00
|
|
|
uri := fmt.Sprintf("%s:%d", conn.ep.Kafka.Host, conn.ep.Kafka.Port)
|
|
|
|
if conn.conn == nil {
|
2018-04-17 18:30:49 +03:00
|
|
|
cfg := sarama.NewConfig()
|
2021-02-15 20:04:37 +03:00
|
|
|
|
2021-08-03 15:46:40 +03:00
|
|
|
cfg.Net.DialTimeout = time.Second
|
|
|
|
cfg.Net.ReadTimeout = time.Second * 5
|
|
|
|
cfg.Net.WriteTimeout = time.Second * 5
|
|
|
|
// Fix #333 : fix backward incompatibility introduced by sarama library
|
|
|
|
cfg.Producer.Return.Successes = true
|
|
|
|
cfg.Version = sarama.V0_10_0_0
|
|
|
|
|
|
|
|
switch conn.ep.Kafka.Auth {
|
|
|
|
case "sasl":
|
2022-09-07 11:34:24 +03:00
|
|
|
// This path allows to either provide a custom ca certificate
|
|
|
|
// or, because RootCAs is nil, is using the hosts ca set
|
|
|
|
// to verify the server certificate
|
2021-08-03 15:46:40 +03:00
|
|
|
if conn.ep.Kafka.SSL {
|
|
|
|
tlsConfig := tls.Config{}
|
|
|
|
|
2022-09-07 11:34:24 +03:00
|
|
|
if conn.ep.Kafka.CACertFile != "" {
|
|
|
|
caCertPool, err := loadRootTLSCert(conn.ep.Kafka.CACertFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = &caCertPool
|
2021-08-03 15:46:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Net.TLS.Enable = true
|
|
|
|
cfg.Net.TLS.Config = &tlsConfig
|
2021-02-15 20:04:37 +03:00
|
|
|
}
|
2021-07-08 13:05:16 +03:00
|
|
|
|
2021-07-08 11:20:45 +03:00
|
|
|
cfg.Net.SASL.Enable = true
|
|
|
|
cfg.Net.SASL.User = os.Getenv("KAFKA_USERNAME")
|
|
|
|
cfg.Net.SASL.Password = os.Getenv("KAFKA_PASSWORD")
|
|
|
|
cfg.Net.SASL.Handshake = true
|
2022-09-07 11:34:24 +03:00
|
|
|
cfg.Net.SASL.Mechanism = sarama.SASLTypePlaintext
|
|
|
|
|
2021-07-08 13:52:16 +03:00
|
|
|
if conn.ep.Kafka.SASLSHA256 {
|
|
|
|
cfg.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA256} }
|
|
|
|
cfg.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA256
|
|
|
|
}
|
|
|
|
if conn.ep.Kafka.SASLSHA512 {
|
|
|
|
cfg.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
|
|
|
|
cfg.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
|
|
|
|
}
|
2021-08-03 15:46:40 +03:00
|
|
|
case "tls":
|
|
|
|
tlsConfig := tls.Config{}
|
2022-09-07 11:34:24 +03:00
|
|
|
cfg.Net.TLS.Enable = true
|
2021-02-15 20:04:37 +03:00
|
|
|
|
2021-08-03 15:46:40 +03:00
|
|
|
certificates, err := loadClientTLSCert(conn.ep.Kafka.KeyFile, conn.ep.Kafka.CertFile)
|
|
|
|
if err != nil {
|
|
|
|
cfg.MetricRegistry.UnregisterAll()
|
|
|
|
return err
|
|
|
|
}
|
2022-09-07 11:34:24 +03:00
|
|
|
tlsConfig.Certificates = certificates
|
2021-08-03 15:46:40 +03:00
|
|
|
|
2022-09-07 11:34:24 +03:00
|
|
|
// This path allows to either provide a custom ca certificate
|
|
|
|
// or, because RootCAs is nil, is using the hosts ca set
|
|
|
|
// to verify server certificate
|
|
|
|
if conn.ep.Kafka.CACertFile != "" {
|
|
|
|
caCertPool, err := loadRootTLSCert(conn.ep.Kafka.CACertFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = &caCertPool
|
2021-08-03 15:46:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Net.TLS.Config = &tlsConfig
|
|
|
|
}
|
2018-07-03 15:27:01 +03:00
|
|
|
|
2018-04-17 18:30:49 +03:00
|
|
|
c, err := sarama.NewSyncProducer([]string{uri}, cfg)
|
2017-03-06 23:18:59 +03:00
|
|
|
if err != nil {
|
2021-07-01 00:18:44 +03:00
|
|
|
cfg.MetricRegistry.UnregisterAll()
|
2017-03-06 23:18:59 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.conn = c
|
2021-07-01 00:18:44 +03:00
|
|
|
conn.cfg = cfg
|
2017-03-06 23:18:59 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:59:08 +03:00
|
|
|
// parse json again to get out info for our kafka key
|
|
|
|
key := gjson.Get(msg, "key")
|
|
|
|
id := gjson.Get(msg, "id")
|
|
|
|
keyValue := fmt.Sprintf("%s-%s", key.String(), id.String())
|
|
|
|
|
2017-03-06 23:18:59 +03:00
|
|
|
message := &sarama.ProducerMessage{
|
2018-11-15 13:59:08 +03:00
|
|
|
Topic: conn.ep.Kafka.TopicName,
|
|
|
|
Key: sarama.StringEncoder(keyValue),
|
2017-03-06 23:18:59 +03:00
|
|
|
Value: sarama.StringEncoder(msg),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, offset, err := conn.conn.SendMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
conn.close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
conn.close()
|
|
|
|
return errors.New("invalid kafka reply")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func newKafkaConn(ep Endpoint) *KafkaConn {
|
|
|
|
return &KafkaConn{
|
2017-03-06 23:18:59 +03:00
|
|
|
ep: ep,
|
|
|
|
t: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 20:04:37 +03:00
|
|
|
|
2021-08-03 15:46:40 +03:00
|
|
|
func loadClientTLSCert(KeyFile, CertFile string) ([]tls.Certificate, error) {
|
|
|
|
// load client cert
|
2021-02-15 20:04:37 +03:00
|
|
|
cert, err := tls.LoadX509KeyPair(CertFile, KeyFile)
|
2021-08-03 15:46:40 +03:00
|
|
|
|
2021-02-15 20:04:37 +03:00
|
|
|
if err != nil {
|
2021-08-03 15:46:40 +03:00
|
|
|
return []tls.Certificate{cert}, err
|
2021-02-15 20:04:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 15:46:40 +03:00
|
|
|
return []tls.Certificate{cert}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadRootTLSCert(CACertFile string) (x509.CertPool, error) {
|
2021-02-15 20:04:37 +03:00
|
|
|
// Load CA cert
|
2022-09-13 03:06:27 +03:00
|
|
|
caCert, err := os.ReadFile(CACertFile)
|
2021-08-03 15:46:40 +03:00
|
|
|
|
2021-02-15 20:04:37 +03:00
|
|
|
if err != nil {
|
2021-08-03 15:46:40 +03:00
|
|
|
return x509.CertPool{}, err
|
2021-02-15 20:04:37 +03:00
|
|
|
}
|
2021-08-03 15:46:40 +03:00
|
|
|
|
2021-02-15 20:04:37 +03:00
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
2021-08-03 15:46:40 +03:00
|
|
|
return *caCertPool, err
|
2021-02-15 20:04:37 +03:00
|
|
|
}
|