Skip to content

Commit

Permalink
[topo] Disallow the slash character in shard names (vitessio#12843)
Browse files Browse the repository at this point in the history
* Disallow the slash character in shard names

Fixes vitessio#12842.

Signed-off-by: Andrew Mason <[email protected]>

* add release note

Signed-off-by: Andrew Mason <[email protected]>

* Update go/vt/topo/shard.go

Co-authored-by: Deepthi Sigireddi <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>

* Update changelog/17.0/17.0.0/summary.md

Co-authored-by: Deepthi Sigireddi <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>

---------

Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
Co-authored-by: Deepthi Sigireddi <[email protected]>
  • Loading branch information
Andrew Mason and deepthi committed Apr 6, 2023
1 parent d6c92cb commit 26cf001
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go/vt/topo/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func IsShardUsingRangeBasedSharding(shard string) bool {
// ValidateShardName takes a shard name and sanitizes it, and also returns
// the KeyRange.
func ValidateShardName(shard string) (string, *topodatapb.KeyRange, error) {
if strings.Contains(shard, "/") {
return "", nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "invalid shardId, may not contain '/': %v", shard)
}

if !IsShardUsingRangeBasedSharding(shard) {
return shard, nil, nil
}
Expand Down
60 changes: 58 additions & 2 deletions go/vt/topo/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package topo

import (
"context"
"reflect"
"testing"

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

"context"

"vitess.io/vitess/go/test/utils"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

Expand Down Expand Up @@ -222,3 +223,58 @@ func TestUpdateSourceDeniedTables(t *testing.T) {
t.Fatalf("one cell removal from all failed: %v", si)
}
}

func TestValidateShardName(t *testing.T) {
t.Parallel()

cases := []struct {
name string
expectedRange *topodatapb.KeyRange
valid bool
}{
{
name: "0",
valid: true,
},
{
name: "-80",
expectedRange: &topodatapb.KeyRange{
Start: nil,
End: []byte{0x80},
},
valid: true,
},
{
name: "40-80",
expectedRange: &topodatapb.KeyRange{
Start: []byte{0x40},
End: []byte{0x80},
},
valid: true,
},
{
name: "foo-bar",
valid: false,
},
{
name: "a/b",
valid: false,
},
}

for _, tcase := range cases {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
t.Parallel()

_, kr, err := ValidateShardName(tcase.name)
if !tcase.valid {
assert.Error(t, err, "expected %q to be an invalid shard name", tcase.name)
return
}

require.NoError(t, err, "expected %q to be a valid shard name, got error: %v", tcase.name, err)
utils.MustMatch(t, tcase.expectedRange, kr)
})
}
}

0 comments on commit 26cf001

Please sign in to comment.