瀏覽代碼

Merge pull request #633 from Shopify/validate-client-id

Validate client ID
Evan Huus 9 年之前
父節點
當前提交
fdea37926f
共有 2 個文件被更改,包括 13 次插入0 次删除
  1. 5 0
      config.go
  2. 8 0
      config_test.go

+ 5 - 0
config.go

@@ -2,9 +2,12 @@ package sarama
 
 
 import (
 import (
 	"crypto/tls"
 	"crypto/tls"
+	"regexp"
 	"time"
 	"time"
 )
 )
 
 
+var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]*\z`)
+
 // Config is used to pass multiple configuration options to Sarama's constructors.
 // Config is used to pass multiple configuration options to Sarama's constructors.
 type Config struct {
 type Config struct {
 	// Net is the namespace for network-level properties used by the Broker, and
 	// Net is the namespace for network-level properties used by the Broker, and
@@ -353,6 +356,8 @@ func (c *Config) Validate() error {
 	switch {
 	switch {
 	case c.ChannelBufferSize < 0:
 	case c.ChannelBufferSize < 0:
 		return ConfigurationError("ChannelBufferSize must be >= 0")
 		return ConfigurationError("ChannelBufferSize must be >= 0")
+	case !validID.MatchString(c.ClientID):
+		return ConfigurationError("ClientID is invalid")
 	}
 	}
 
 
 	return nil
 	return nil

+ 8 - 0
config_test.go

@@ -8,3 +8,11 @@ func TestDefaultConfigValidates(t *testing.T) {
 		t.Error(err)
 		t.Error(err)
 	}
 	}
 }
 }
+
+func TestClientIDValidates(t *testing.T) {
+	config := NewConfig()
+	config.ClientID = "foo:bar"
+	if err := config.Validate(); string(err.(ConfigurationError)) != "ClientID is invalid" {
+		t.Error("Expected invalid ClientID, got ", err)
+	}
+}