Skip to content

Commit 6ee51fc

Browse files
committed
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.
1 parent 4f9d5cd commit 6ee51fc

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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)