-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): Add support for SASL auth to Kafka (#14487)
- Loading branch information
Showing
15 changed files
with
387 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package kafka | ||
package client | ||
|
||
import ( | ||
"github.com/go-kit/log" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/dskit/flagext" | ||
"github.com/stretchr/testify/require" | ||
"github.com/twmb/franz-go/pkg/kfake" | ||
"github.com/twmb/franz-go/pkg/kgo" | ||
"github.com/twmb/franz-go/pkg/kmsg" | ||
|
||
"github.com/grafana/loki/v3/pkg/kafka" | ||
"github.com/grafana/loki/v3/pkg/kafka/testkafka" | ||
) | ||
|
||
func TestNewReaderClient(t *testing.T) { | ||
_, addr := testkafka.CreateClusterWithoutCustomConsumerGroupsSupport(t, 1, "test", kfake.EnableSASL(), kfake.Superuser("PLAIN", "user", "password")) | ||
|
||
tests := []struct { | ||
name string | ||
config kafka.Config | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid config", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
SASLUsername: "user", | ||
SASLPassword: flagext.SecretWithValue("password"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "wrong password", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
SASLUsername: "user", | ||
SASLPassword: flagext.SecretWithValue("wrong wrong wrong"), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "wrong username", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
SASLUsername: "wrong wrong wrong", | ||
SASLPassword: flagext.SecretWithValue("password"), | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client, err := NewReaderClient(tt.config, nil, nil) | ||
require.NoError(t, err) | ||
|
||
err = client.Ping(context.Background()) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSetDefaultNumberOfPartitionsForAutocreatedTopics(t *testing.T) { | ||
cluster, err := kfake.NewCluster(kfake.NumBrokers(1)) | ||
require.NoError(t, err) | ||
t.Cleanup(cluster.Close) | ||
|
||
addrs := cluster.ListenAddrs() | ||
require.Len(t, addrs, 1) | ||
|
||
cfg := kafka.Config{ | ||
Address: addrs[0], | ||
AutoCreateTopicDefaultPartitions: 100, | ||
} | ||
|
||
cluster.ControlKey(kmsg.AlterConfigs.Int16(), func(request kmsg.Request) (kmsg.Response, error, bool) { | ||
r := request.(*kmsg.AlterConfigsRequest) | ||
|
||
require.Len(t, r.Resources, 1) | ||
res := r.Resources[0] | ||
require.Equal(t, kmsg.ConfigResourceTypeBroker, res.ResourceType) | ||
require.Len(t, res.Configs, 1) | ||
cfg := res.Configs[0] | ||
require.Equal(t, "num.partitions", cfg.Name) | ||
require.NotNil(t, *cfg.Value) | ||
require.Equal(t, "100", *cfg.Value) | ||
|
||
return &kmsg.AlterConfigsResponse{}, nil, true | ||
}) | ||
|
||
client, err := kgo.NewClient(commonKafkaClientOptions(cfg, nil, log.NewNopLogger())...) | ||
require.NoError(t, err) | ||
|
||
setDefaultNumberOfPartitionsForAutocreatedTopics(cfg, client, log.NewNopLogger()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/dskit/flagext" | ||
"github.com/stretchr/testify/require" | ||
"github.com/twmb/franz-go/pkg/kfake" | ||
|
||
"github.com/grafana/loki/v3/pkg/kafka" | ||
"github.com/grafana/loki/v3/pkg/kafka/testkafka" | ||
) | ||
|
||
func TestNewWriterClient(t *testing.T) { | ||
_, addr := testkafka.CreateClusterWithoutCustomConsumerGroupsSupport(t, 1, "test", kfake.EnableSASL(), kfake.Superuser("PLAIN", "user", "password")) | ||
|
||
tests := []struct { | ||
name string | ||
config kafka.Config | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid config", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
WriteTimeout: time.Second, | ||
SASLUsername: "user", | ||
SASLPassword: flagext.SecretWithValue("password"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "wrong password", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
WriteTimeout: time.Second, | ||
SASLUsername: "user", | ||
SASLPassword: flagext.SecretWithValue("wrong wrong wrong"), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "wrong username", | ||
config: kafka.Config{ | ||
Address: addr, | ||
Topic: "abcd", | ||
WriteTimeout: time.Second, | ||
SASLUsername: "wrong wrong wrong", | ||
SASLPassword: flagext.SecretWithValue("password"), | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client, err := NewWriterClient(tt.config, 10, nil, nil) | ||
require.NoError(t, err) | ||
|
||
err = client.Ping(context.Background()) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.