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

feature: support sasl SCRAM-SHA-256 and SCRAM-SHA-512 authentication #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example/kq/producer/produce.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type message struct {
}

func main() {
pusher := kq.NewPusher([]string{
"127.0.0.1:19092",
"127.0.0.1:19092",
"127.0.0.1:19092",
}, "kq")
pusher := kq.NewPusher(kq.KqConf{
Brokers: []string{"127.0.0.1:19092", "127.0.0.1:19092", "127.0.0.1:19092"},
Topic: "kq",
},
)

ticker := time.NewTicker(time.Millisecond)
for round := 0; round < 3; round++ {
Expand Down
1 change: 1 addition & 0 deletions kq/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type KqConf struct {
Username string `json:",optional"`
Password string `json:",optional"`
ForceCommit bool `json:",default=true"`
Mechanism string `json:",optional"`
}
72 changes: 64 additions & 8 deletions kq/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"time"

"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
"github.com/zeromicro/go-zero/core/executors"
"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -25,17 +28,70 @@ type (
}
)

func NewPusher(addrs []string, topic string, opts ...PushOption) *Pusher {
producer := &kafka.Writer{
Addr: kafka.TCP(addrs...),
Topic: topic,
Balancer: &kafka.LeastBytes{},
Compression: kafka.Snappy,
func NewPusher(config KqConf, opts ...PushOption) *Pusher {
var producer *kafka.Writer
if config.Username != "" && config.Password != "" {
if config.Mechanism == "" {
// 使用SASL 明文认证
mechanism := plain.Mechanism{
Username: config.Username,
Password: config.Password,
}

sharedTransport := &kafka.Transport{
SASL: mechanism,
}

producer = &kafka.Writer{
Addr: kafka.TCP(config.Brokers...),
Topic: config.Topic,
Balancer: &kafka.Hash{},
Transport: sharedTransport,
}
} else {
// 使用SASL SCRAM-SHA认证
var mechanism sasl.Mechanism
var err error
if config.Mechanism == "SCRAM-SHA-512" {
mechanism, err = scram.Mechanism(scram.SHA512, config.Username, config.Password)
if err != nil {
panic(err)
}
} else if config.Mechanism == "SCRAM-SHA-256" {
mechanism, err = scram.Mechanism(scram.SHA256, config.Username, config.Password)
if err != nil {
panic(err)
}
}
// Transports are responsible for managing connection pools and other resources,
// it's generally best to create a few of these and share them across your
// application.
sharedTransport := &kafka.Transport{
SASL: mechanism,
}

producer = &kafka.Writer{
Addr: kafka.TCP(config.Brokers...),
Topic: config.Topic,
Balancer: &kafka.Hash{},
Transport: sharedTransport,
}
}
} else {
// 不使用认证
producer = &kafka.Writer{
Addr: kafka.TCP(config.Brokers...),
Topic: config.Topic,
Balancer: &kafka.LeastBytes{},
Compression: kafka.Snappy,
}
}

pusher := &Pusher{
produer: producer,
topic: topic,
topic: config.Topic,
}

pusher.executor = executors.NewChunkExecutor(func(tasks []interface{}) {
chunk := make([]kafka.Message, len(tasks))
for i := range tasks {
Expand All @@ -53,7 +109,7 @@ func (p *Pusher) Close() error {
if p.executor != nil {
p.executor.Flush()
}

return p.produer.Close()
}

Expand Down
35 changes: 30 additions & 5 deletions kq/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/segmentio/kafka-go"
_ "github.com/segmentio/kafka-go/gzip"
_ "github.com/segmentio/kafka-go/lz4"
"github.com/segmentio/kafka-go/sasl"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
_ "github.com/segmentio/kafka-go/snappy"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/queue"
Expand Down Expand Up @@ -114,11 +116,34 @@ func newKafkaQueue(c KqConf, handler ConsumeHandler, options queueOptions) queue
QueueCapacity: options.queueCapacity,
}
if len(c.Username) > 0 && len(c.Password) > 0 {
readerConfig.Dialer = &kafka.Dialer{
SASLMechanism: plain.Mechanism{
Username: c.Username,
Password: c.Password,
},
if len(c.Mechanism) > 0 {
// 使用SASL SCRAM-SHA认证
var mechanism sasl.Mechanism
var err error
if c.Mechanism == "SCRAM-SHA-512" {
mechanism, err = scram.Mechanism(scram.SHA512, c.Username, c.Password)
if err != nil {
panic(err)
}
} else if c.Mechanism == "SCRAM-SHA-256" {
mechanism, err = scram.Mechanism(scram.SHA256, c.Username, c.Password)
if err != nil {
panic(err)
}
}
readerConfig.Dialer = &kafka.Dialer{
Timeout: 10 * time.Second,
DualStack: true,
SASLMechanism: mechanism,
}
} else {
// 使用SASL PLAIN认证
readerConfig.Dialer = &kafka.Dialer{
SASLMechanism: plain.Mechanism{
Username: c.Username,
Password: c.Password,
},
}
}
}
consumer := kafka.NewReader(readerConfig)
Expand Down