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

fix(config): relax ClientID validation after 1.0.0 #2706

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
11 changes: 8 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (

const defaultClientID = "sarama"

var validID = regexp.MustCompile(`\A[A-Za-z0-9._-]+\z`)
// validClientID specifies the permitted characters for a client.id when
// connecting to Kafka versions before 1.0.0 (KIP-190)
var validClientID = regexp.MustCompile(`\A[A-Za-z0-9._-]+\z`)

// Config is used to pass multiple configuration options to Sarama's constructors.
type Config struct {
Expand Down Expand Up @@ -846,8 +848,11 @@ func (c *Config) Validate() error {
switch {
case c.ChannelBufferSize < 0:
return ConfigurationError("ChannelBufferSize must be >= 0")
case !validID.MatchString(c.ClientID):
return ConfigurationError("ClientID is invalid")
}

// only validate clientID locally for Kafka versions before KIP-190 was implemented
if !c.Version.IsAtLeast(V1_0_0_0) && !validClientID.MatchString(c.ClientID) {
return ConfigurationError(fmt.Sprintf("ClientID value %q is not valid for Kafka versions before 1.0.0", c.ClientID))
}

return nil
Expand Down
36 changes: 19 additions & 17 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package sarama

import (
"errors"
"fmt"
"os"
"strings"
"testing"

"github.com/rcrowley/go-metrics"
assert "github.com/stretchr/testify/require"
)

// NewTestConfig returns a config meant to be used by tests.
Expand All @@ -30,23 +32,23 @@ func TestDefaultConfigValidates(t *testing.T) {
}
}

func TestInvalidClientIDConfigValidates(t *testing.T) {
config := NewTestConfig()
config.ClientID = "foo:bar"
err := config.Validate()
var target ConfigurationError
if !errors.As(err, &target) || string(target) != "ClientID is invalid" {
t.Error("Expected invalid ClientID, got ", err)
}
}

func TestEmptyClientIDConfigValidates(t *testing.T) {
config := NewTestConfig()
config.ClientID = ""
err := config.Validate()
var target ConfigurationError
if !errors.As(err, &target) || string(target) != "ClientID is invalid" {
t.Error("Expected invalid ClientID, got ", err)
// TestInvalidClientIDValidated ensures that the ClientID field is checked
// when Version is set to anything less than 1_0_0_0, but otherwise accepted
func TestInvalidClientIDValidated(t *testing.T) {
for _, version := range SupportedVersions {
for _, clientID := range []string{"", "foo:bar", "foo|bar"} {
config := NewTestConfig()
config.ClientID = clientID
config.Version = version
err := config.Validate()
if config.Version.IsAtLeast(V1_0_0_0) {
assert.NoError(t, err)
continue
}
var target ConfigurationError
assert.ErrorAs(t, err, &target)
assert.ErrorContains(t, err, fmt.Sprintf("ClientID value %q is not valid for Kafka versions before 1.0.0", clientID))
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions mocks/async_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ func (brokePartitioner) RequiresConsistency() bool { return false }
func TestProducerWithInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
config.Version = sarama.V0_11_0_2
config.ClientID = "not a valid producer ID"
mp := NewAsyncProducer(trm, config)
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
} else if !strings.Contains(trm.errors[0], `ClientID value "not a valid producer ID" is not valid for Kafka versions before 1.0.0`) {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
5 changes: 3 additions & 2 deletions mocks/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,16 @@ func TestConsumerOffsetsAreManagedCorrectlyWithSpecifiedOffset(t *testing.T) {
func TestConsumerInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
config.Version = sarama.V0_11_0_2
config.ClientID = "not a valid consumer ID"
consumer := NewConsumer(trm, config)
if err := consumer.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
} else if !strings.Contains(trm.errors[0], `ClientID value "not a valid consumer ID" is not valid for Kafka versions before 1.0.0`) {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
5 changes: 3 additions & 2 deletions mocks/sync_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,16 @@ func (f faultyEncoder) Length() int {
func TestSyncProducerInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
config.Version = sarama.V0_11_0_2
config.ClientID = "not a valid producer ID"
mp := NewSyncProducer(trm, config)
if err := mp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
} else if !strings.Contains(trm.errors[0], `ClientID value "not a valid producer ID" is not valid for Kafka versions before 1.0.0`) {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
Loading