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

Ignore zero addresses in CLUSTER SLOTS response #31

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions rediscluster/redisclusterutil/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ func ParseSlotsInfo(res interface{}) ([]SlotsRange, error) {
for j := 2; j < len(rawrange); j++ {
rawaddr, ok := rawrange[j].([]interface{})
if !ok || len(rawaddr) < 2 {
return errf("address format mismatch: res[%d][%d] = %+v",
return errf("address format mismatch: res[%d][%d] = %+v, missing lines",
i, j, rawrange[j])
}
host, ok := rawaddr[0].([]byte)
port, ok2 := rawaddr[1].(int64)
if !ok || !ok2 || port <= 0 || port+10000 > 65535 {
host, hasHost := rawaddr[0].([]byte)
port, hasPort := rawaddr[1].(int64)
if !hasHost && hasPort && port == 0 {
// fallback to skip zero address
Copy link
Contributor

@g7r g7r Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave a message explaining the source of this fallback:

Due to possible Redis cluster misconfiguration we can receive zero address for one of the replicas.
It is totally fine to skip the misconfigured replica and go with the remaining ones without inducing denial of service.

arr, isArray := rawaddr[0].([]interface{})
if isArray && len(arr) == 0 {
continue
}
}
if !hasHost || !hasPort || port <= 0 || port+10000 > 65535 {
return errf("address format mismatch: res[%d][%d] = %+v",
i, j, rawaddr)
}
Expand Down
182 changes: 182 additions & 0 deletions rediscluster/redisclusterutil/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package redisclusterutil

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseSlotsInfo(t *testing.T) {
clusterSlotsResponse := []interface{}{
[]interface{}{
int64(0),
int64(5460),
[]interface{}{
[]byte("127.0.0.1"),
int64(30001),
[]byte("09dbe9720cda62f7865eabc5fd8857c5d2678366"),
[]interface{}{
"hostname",
"host-1.redis.example.com",
},
},
[]interface{}{
[]byte("127.0.0.1"),
int64(30004),
[]byte("821d8ca00d7ccf931ed3ffc7e3db0599d2271abf"),
[]interface{}{
"hostname",
"host-2.redis.example.com",
},
},
},
[]interface{}{
int64(5461),
int64(10922),
[]interface{}{
[]byte("127.0.0.1"),
int64(30002),
[]byte("c9d93d9f2c0c524ff34cc11838c2003d8c29e013"),
[]interface{}{
"hostname",
"host-3.redis.example.com",
},
},
[]interface{}{
[]byte("127.0.0.1"),
int64(30005),
[]byte("faadb3eb99009de4ab72ad6b6ed87634c7ee410f"),
[]interface{}{
"hostname",
"host-4.redis.example.com",
},
},
},
[]interface{}{
int64(10923),
int64(16383),
[]interface{}{
[]byte("192.168.11.131"),
int64(30003),
[]byte("044ec91f325b7595e76dbcb18cc688b6a5b434a1"),
[]interface{}{
"hostname",
"host-5.redis.example.com",
},
},
[]interface{}{
[]byte("127.0.0.1"),
int64(30006),
[]byte("58e6e48d41228013e5d9c1c37c5060693925e97e"),
[]interface{}{
"hostname",
"host-6.redis.example.com",
},
},
},
}

expectedSlots := []SlotsRange{
{
From: 0,
To: 5460,
Addrs: []string{
"127.0.0.1:30001",
"127.0.0.1:30004",
},
},
{
From: 5461,
To: 10922,
Addrs: []string{
"127.0.0.1:30002",
"127.0.0.1:30005",
},
},
{
From: 10923,
To: 16383,
Addrs: []string{
"192.168.11.131:30003",
"127.0.0.1:30006",
},
},
}

slots, err := ParseSlotsInfo(clusterSlotsResponse)
require.NoError(t, err)

assert.Equal(t, expectedSlots, slots)
}

func TestParseSlotsInfo_EmptyAddress(t *testing.T) {
clusterSlotsResponse := []interface{}{
[]interface{}{
int64(0),
int64(5460),
[]interface{}{
[]byte("127.0.0.1"),
int64(30001),
[]byte("09dbe9720cda62f7865eabc5fd8857c5d2678366"),
[]interface{}{
"hostname",
"host-1.redis.example.com",
},
},
[]interface{}{
[]interface{}{},
int64(0),
[]byte("821d8ca00d7ccf931ed3ffc7e3db0599d2271abf"),
[]interface{}{
"hostname",
"host-2.redis.example.com",
},
},
},
[]interface{}{
int64(5461),
int64(10922),
[]interface{}{
[]interface{}{},
int64(0),
[]byte("c9d93d9f2c0c524ff34cc11838c2003d8c29e013"),
[]interface{}{
"hostname",
"host-3.redis.example.com",
},
},
[]interface{}{
[]byte("127.0.0.1"),
int64(30005),
[]byte("faadb3eb99009de4ab72ad6b6ed87634c7ee410f"),
[]interface{}{
"hostname",
"host-4.redis.example.com",
},
},
},
}

expectedSlots := []SlotsRange{
{
From: 0,
To: 5460,
Addrs: []string{
"127.0.0.1:30001",
},
},
{
From: 5461,
To: 10922,
Addrs: []string{
"127.0.0.1:30005",
},
},
}

slots, err := ParseSlotsInfo(clusterSlotsResponse)
require.NoError(t, err)

assert.Equal(t, expectedSlots, slots)
}
Loading