Browse Source

bug fixes

Shriram Rajagopalan 9 years ago
parent
commit
9d69627aa9
2 changed files with 18 additions and 14 deletions
  1. 16 12
      broker.go
  2. 2 2
      config.go

+ 16 - 12
broker.go

@@ -85,13 +85,17 @@ func (b *Broker) Open(conf *Config) error {
 		b.conf = conf
 
 		if conf.Net.SASL.Enable {
-			err = b.doSASLPlainAuth()
-			if err != nil {
-				Logger.Printf("SASL authentication with broker %s failed\n", b.addr)
-				_ = b.Close()
+			b.connErr = b.doSASLPlainAuth()
+			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
-			} else {
-				Logger.Printf("SASL authentication with broker %s succeeded\n", b.addr)
 			}
 		}
 
@@ -494,14 +498,14 @@ func (b *Broker) doSASLPlainAuth() error {
 
 	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: %s\n", err.Error())
-		return 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\n", err.Error())
-		return nil
+		Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
+		return err
 	}
 
 	header := make([]byte, 4)
@@ -509,10 +513,10 @@ func (b *Broker) doSASLPlainAuth() error {
 	// 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: %s\n", err.Error())
+		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:%v - %v\n", n, header)
+	Logger.Printf("SASL authentication successful with broker %s:%v - %v\n", b.addr, n, header)
 	return nil
 }

+ 2 - 2
config.go

@@ -313,9 +313,9 @@ func (c *Config) Validate() error {
 		return ConfigurationError("Net.WriteTimeout must be > 0")
 	case c.Net.KeepAlive < 0:
 		return ConfigurationError("Net.KeepAlive must be >= 0")
-	case (c.Net.SASL.Enable == true) && (c.Net.SASL.User == ""):
+	case c.Net.SASL.Enable == true && c.Net.SASL.User == "":
 		return ConfigurationError("Net.SASL.User must not be empty when SASL is enabled")
-	case (c.Net.SASL.Enable == true) && (c.Net.SASL.Password == ""):
+	case c.Net.SASL.Enable == true && c.Net.SASL.Password == "":
 		return ConfigurationError("Net.SASL.Password must not be empty when SASL is enabled")
 	}