Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to authenticate using ca certificate, access certificate, access key #653

Open
vizvasrj opened this issue Feb 5, 2024 · 1 comment

Comments

@vizvasrj
Copy link

vizvasrj commented Feb 5, 2024

please i need rust equivalent for this
here is go equivalent for producer

package main

import (
    "context"
    "crypto/tls"
    "crypto/x509"
    "fmt"
    "io/ioutil"
    "log"
    "time"

    "github.com/segmentio/kafka-go"
)

func main() {
    TOPIC_NAME := "default_topic"

    keypair, err := tls.LoadX509KeyPair("service.cert", "service.key")
    if err != nil {
        log.Fatalf("Failed to load access key and/or access certificate: %s", err)
    }

    caCert, err := ioutil.ReadFile("ca.pem")
    if err != nil {
        log.Fatalf("Failed to read CA certificate file: %s", err)
    }

    caCertPool := x509.NewCertPool()
    ok := caCertPool.AppendCertsFromPEM(caCert)
    if !ok {
        log.Fatalf("Failed to parse CA certificate file: %s", err)
    }

    dialer := &kafka.Dialer{
        Timeout:   10 * time.Second,
        DualStack: true,
        TLS: &tls.Config{
            Certificates: []tls.Certificate{keypair},
            RootCAs:      caCertPool,
        },
    }

    // init producer
    producer := kafka.NewWriter(kafka.WriterConfig{
        Brokers: []string{"kafka-257bfc54-lakjos-f2b6.a.aivencloud.com:19190"},
        Topic:   TOPIC_NAME,
        Dialer:  dialer,
    })

    // produce 100 messages
    for i := 0; i < 100; i++ {
        message := fmt.Sprint("Hello from Go using SSL ", i+1, "!")
        producer.WriteMessages(context.Background(), kafka.Message{Value: []byte(message)})
        log.Printf("Message sent: " + message)
        time.Sleep(time.Second)
    }

    producer.Close()
}

and consumer

package main

import (
    "context"
    "crypto/tls"
    "crypto/x509"
    "io/ioutil"
    "log"
    "time"

    "github.com/segmentio/kafka-go"
)

func main() {
    TOPIC_NAME := "default_topic"

    keypair, err := tls.LoadX509KeyPair("service.cert", "service.key")
    if err != nil {
        log.Fatalf("Failed to load access key and/or access certificate: %s", err)
    }

    caCert, err := ioutil.ReadFile("ca.pem")
    if err != nil {
        log.Fatalf("Failed to read CA certificate file: %s", err)
    }

    caCertPool := x509.NewCertPool()
    ok := caCertPool.AppendCertsFromPEM(caCert)
    if !ok {
        log.Fatalf("Failed to parse CA certificate file: %s", err)
    }

    dialer := &kafka.Dialer{
        Timeout:   10 * time.Second,
        DualStack: true,
        TLS: &tls.Config{
            Certificates: []tls.Certificate{keypair},
            RootCAs:      caCertPool,
        },
    }

    // init consumer
    consumer := kafka.NewReader(kafka.ReaderConfig{
        Brokers: []string{"kafka-257bfc54-lakjos-f2b6.a.aivencloud.com:19190"},
        Topic:   TOPIC_NAME,
        Dialer:  dialer,
    })

    for {
        message, err := consumer.ReadMessage(context.Background())

        if err != nil {
            log.Printf("Could not read message: %s", err)
        } else {
            log.Printf("Got message using SSL: %s", message.Value)
        }
    }
}
@conradogarciaberrotaran

did you find any way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants