-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[Backport] Update topo {Get,Create}Keyspace to prevent invalid keyspace names (#12732) #12771
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package topotests | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "vitess.io/vitess/go/vt/topo/memorytopo" | ||
| "vitess.io/vitess/go/vt/vterrors" | ||
|
|
||
| topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
| "vitess.io/vitess/go/vt/proto/vtrpc" | ||
| ) | ||
|
|
||
| func TestCreateKeyspace(t *testing.T) { | ||
| ts := memorytopo.NewServer("zone1") | ||
| ctx := context.Background() | ||
|
|
||
| t.Run("valid name", func(t *testing.T) { | ||
| err := ts.CreateKeyspace(ctx, "ks", &topodatapb.Keyspace{}) | ||
| require.NoError(t, err) | ||
| }) | ||
| t.Run("invalid name", func(t *testing.T) { | ||
| err := ts.CreateKeyspace(ctx, "no/slashes/allowed", &topodatapb.Keyspace{}) | ||
| assert.Error(t, err) | ||
| assert.Equal(t, vtrpc.Code_INVALID_ARGUMENT, vterrors.Code(err), "%+v", err) | ||
| }) | ||
| } | ||
|
|
||
| func TestGetKeyspace(t *testing.T) { | ||
| ts := memorytopo.NewServer("zone1") | ||
| ctx := context.Background() | ||
|
|
||
| t.Run("valid name", func(t *testing.T) { | ||
| // First, create the keyspace. | ||
| err := ts.CreateKeyspace(ctx, "ks", &topodatapb.Keyspace{}) | ||
| require.NoError(t, err) | ||
|
|
||
| // Now, get it. | ||
| ks, err := ts.GetKeyspace(ctx, "ks") | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, ks) | ||
| }) | ||
|
|
||
| t.Run("invalid name", func(t *testing.T) { | ||
| // We can't create the keyspace (because we can't create a keyspace | ||
| // with an invalid name), so we'll validate the error we get is *not* | ||
| // NOT_FOUND. | ||
| ks, err := ts.GetKeyspace(ctx, "no/slashes/allowed") | ||
| assert.Error(t, err) | ||
| assert.Equal(t, vtrpc.Code_INVALID_ARGUMENT, vterrors.Code(err), "%+v", err) | ||
| assert.Nil(t, ks) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,10 @@ var ErrKeyspaceNotFound = errors.New("keyspace not found") | |
|
|
||
| // ReadKeyspace reads the vitess keyspace record. | ||
| func ReadKeyspace(keyspaceName string) (*topo.KeyspaceInfo, error) { | ||
| if err := topo.ValidateKeyspaceName(keyspaceName); err != nil { | ||
| return nil, err | ||
| } | ||
|
Comment on lines
+33
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the upstream PR we documented this as a breaking change. And here I can see that it could be. Do we really want to backport this? If so, don't we want the same release notes summary update?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we want the backport, i'll update the notes! |
||
|
|
||
| query := ` | ||
| select | ||
| keyspace_type, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be I am missing something here. What will happen to existing keyspaces with "/" ??