Skip to content

Commit 69e2905

Browse files
craig[bot]RichardJCaiandreimatei
committed
Merge #65722 #65792
65722: sql: ensure schema only has valid privileges after reparent database is done r=ajwerner a=RichardJCai sql: ensure schema only has valid privileges after reparent database is done Release note (bug fix): Previously, ALTER DATABASE ... CONVERT TO SCHEMA could potentially leave the schema with invalid privileges thus causing the privilege descriptor to be invalid. Fixes #65697 65792: kvserver: skip some long tests under short r=andreimatei a=andreimatei TestResetQuorum - 33s TestLearnerSnapshotFailsRollback - 90s TestReliableIntentCleanup - 80s Release note: None Co-authored-by: richardjcai <[email protected]> Co-authored-by: Andrei Matei <[email protected]>
3 parents 9cf809c + 6ee51fc + 832bcb2 commit 69e2905

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

pkg/kv/kvserver/intent_resolver_integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func TestReliableIntentCleanup(t *testing.T) {
219219
defer leaktest.AfterTest(t)()
220220
defer log.Scope(t).Close(t)
221221
skip.WithIssue(t, 65447, "fixing the flake uncovered additional bugs in #65458")
222+
skip.UnderShort(t) // takes 85s
222223
skip.UnderRace(t, "timing-sensitive test")
223224
skip.UnderStress(t, "multi-node test")
224225

pkg/kv/kvserver/replica_learner_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ func TestLearnerSnapshotFailsRollback(t *testing.T) {
309309
defer leaktest.AfterTest(t)()
310310
defer log.Scope(t).Close(t)
311311

312+
skip.UnderShort(t) // Takes 90s.
313+
312314
runTest := func(t *testing.T, replicaType roachpb.ReplicaType) {
313315
var rejectSnapshots int64
314316
knobs, ltk := makeReplicationTestKnobs()

pkg/kv/kvserver/reset_quorum_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
2828
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
2929
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
30+
"github.com/cockroachdb/cockroach/pkg/util/log"
3031
"github.com/cockroachdb/errors"
3132
"github.com/stretchr/testify/require"
3233
)
@@ -51,9 +52,11 @@ import (
5152
// 6. A meta range (error expected).
5253
func TestResetQuorum(t *testing.T) {
5354
defer leaktest.AfterTest(t)()
55+
defer log.Scope(t).Close(t)
5456

5557
skip.UnderStress(t, "too many nodes")
5658
skip.UnderRace(t, "takes >1m under race")
59+
skip.UnderShort(t)
5760

5861
ctx := context.Background()
5962
livenessDuration := 3000 * time.Millisecond

pkg/sql/logictest/testdata/logic_test/reparent_database

+13
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,16 @@ CREATE VIEW with_views.v AS SELECT x FROM with_views.t
6969

7070
statement error pq: could not convert database "with_views" into schema because "with_views.public.t" has dependent objects \[with_views.public.v\]
7171
ALTER DATABASE with_views CONVERT TO SCHEMA WITH PARENT pgdatabase
72+
73+
# Ensure only valid privileges are copied over to the schema.
74+
statement ok
75+
CREATE DATABASE to_schema;
76+
GRANT CREATE, DROP, SELECT, INSERT, DELETE, UPDATE ON DATABASE to_schema TO testuser;
77+
CREATE DATABASE parent2;
78+
79+
# No privilege validation error should occur.
80+
statement ok
81+
ALTER DATABASE to_schema CONVERT TO SCHEMA WITH PARENT parent2;
82+
83+
statement ok
84+
GRANT USAGE ON SCHEMA parent2.to_schema TO testuser;

pkg/sql/reparent_database.go

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
2626
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2727
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
28+
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
2829
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2930
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
3031
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
@@ -114,6 +115,16 @@ func (n *reparentDatabaseNode) startExec(params runParams) error {
114115
if err != nil {
115116
return err
116117
}
118+
119+
// Not all Privileges on databases are valid on schemas.
120+
// Remove any privileges that are not valid for schemas.
121+
schemaPrivs := privilege.GetValidPrivilegesForObject(privilege.Schema).ToBitField()
122+
privs := n.db.GetPrivileges()
123+
for i, u := range privs.Users {
124+
// Remove privileges that are valid for databases but not for schemas.
125+
privs.Users[i].Privileges = u.Privileges & schemaPrivs
126+
}
127+
117128
schema := schemadesc.NewBuilder(&descpb.SchemaDescriptor{
118129
ParentID: n.newParent.ID,
119130
Name: n.db.Name,

0 commit comments

Comments
 (0)