|
@@ -2,6 +2,7 @@ package sarama
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
|
|
|
+ "encoding/binary"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"io"
|
|
"io"
|
|
|
"net"
|
|
"net"
|
|
@@ -82,6 +83,22 @@ func (b *Broker) Open(conf *Config) error {
|
|
|
b.conn = newBufConn(b.conn)
|
|
b.conn = newBufConn(b.conn)
|
|
|
|
|
|
|
|
b.conf = conf
|
|
b.conf = conf
|
|
|
|
|
+
|
|
|
|
|
+ if conf.Net.SASL.Enable {
|
|
|
|
|
+ b.connErr = b.sendAndReceiveSASLPlainAuth()
|
|
|
|
|
+ if b.connErr != nil {
|
|
|
|
|
+ err = b.conn.Close()
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ Logger.Printf("Closed connection to broker %s\n", b.addr)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Logger.Printf("Error while closing connection to broker %s: %s\n", b.addr, err)
|
|
|
|
|
+ }
|
|
|
|
|
+ b.conn = nil
|
|
|
|
|
+ atomic.StoreInt32(&b.opened, 0)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
b.done = make(chan bool)
|
|
b.done = make(chan bool)
|
|
|
b.responses = make(chan responsePromise, b.conf.Net.MaxOpenRequests-1)
|
|
b.responses = make(chan responsePromise, b.conf.Net.MaxOpenRequests-1)
|
|
|
|
|
|
|
@@ -454,3 +471,52 @@ func (b *Broker) responseReceiver() {
|
|
|
}
|
|
}
|
|
|
close(b.done)
|
|
close(b.done)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// Kafka 0.10.0 plans to support SASL Plain and Kerberos as per PR #812 (KIP-43)/(JIRA KAFKA-3149)
|
|
|
|
|
+// Some hosted kafka services such as IBM Message Hub already offer SASL/PLAIN auth with Kafka 0.9
|
|
|
|
|
+//
|
|
|
|
|
+// In SASL Plain, Kafka expects the auth header to be in the following format
|
|
|
|
|
+// Message format (from https://tools.ietf.org/html/rfc4616):
|
|
|
|
|
+//
|
|
|
|
|
+// message = [authzid] UTF8NUL authcid UTF8NUL passwd
|
|
|
|
|
+// authcid = 1*SAFE ; MUST accept up to 255 octets
|
|
|
|
|
+// authzid = 1*SAFE ; MUST accept up to 255 octets
|
|
|
|
|
+// passwd = 1*SAFE ; MUST accept up to 255 octets
|
|
|
|
|
+// UTF8NUL = %x00 ; UTF-8 encoded NUL character
|
|
|
|
|
+//
|
|
|
|
|
+// SAFE = UTF1 / UTF2 / UTF3 / UTF4
|
|
|
|
|
+// ;; any UTF-8 encoded Unicode character except NUL
|
|
|
|
|
+//
|
|
|
|
|
+// When credentials are valid, Kafka returns a 4 byte array of null characters.
|
|
|
|
|
+// When credentials are invalid, Kafka closes the connection. This does not seem to be the ideal way
|
|
|
|
|
+// of responding to bad credentials but thats how its being done today.
|
|
|
|
|
+func (b *Broker) sendAndReceiveSASLPlainAuth() error {
|
|
|
|
|
+ length := 1 + len(b.conf.Net.SASL.User) + 1 + len(b.conf.Net.SASL.Password)
|
|
|
|
|
+ authBytes := make([]byte, length+4) //4 byte length header + auth data
|
|
|
|
|
+ binary.BigEndian.PutUint32(authBytes, uint32(length))
|
|
|
|
|
+ copy(authBytes[4:], []byte("\x00"+b.conf.Net.SASL.User+"\x00"+b.conf.Net.SASL.Password))
|
|
|
|
|
+
|
|
|
|
|
+ err := b.conn.SetWriteDeadline(time.Now().Add(b.conf.Net.WriteTimeout))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ Logger.Printf("Failed to set write deadline when doing SASL auth with broker %s: %s\n", b.addr, err.Error())
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _, err = b.conn.Write(authBytes)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ header := make([]byte, 4)
|
|
|
|
|
+ n, err := io.ReadFull(b.conn, header)
|
|
|
|
|
+ // If the credentials are valid, we would get a 4 byte response filled with null characters.
|
|
|
|
|
+ // Otherwise, the broker closes the connection and we get an EOF
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ Logger.Printf("Failed to read response while authenticating with SASL to broker %s: %s\n", b.addr, err.Error())
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Logger.Printf("SASL authentication successful with broker %s:%v - %v\n", b.addr, n, header)
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|