Skip to content

Commit

Permalink
Merge pull request #65810 from RichardJCai/backport21.1-65722
Browse files Browse the repository at this point in the history
release-21.1: sql: ensure schema only has valid privileges after reparent database is done
  • Loading branch information
RichardJCai authored May 28, 2021
2 parents e6512ad + 55986b3 commit 506159b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/reparent_database
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ CREATE VIEW with_views.v AS SELECT x FROM with_views.t

statement error pq: could not convert database "with_views" into schema because "with_views.public.t" has dependent objects \[with_views.public.v\]
ALTER DATABASE with_views CONVERT TO SCHEMA WITH PARENT pgdatabase

# Ensure only valid privileges are copied over to the schema.
statement ok
CREATE DATABASE to_schema;
GRANT CREATE, DROP, SELECT, INSERT, DELETE, UPDATE ON DATABASE to_schema TO testuser;
CREATE DATABASE parent2;

# No privilege validation error should occur.
statement ok
ALTER DATABASE to_schema CONVERT TO SCHEMA WITH PARENT parent2;

statement ok
GRANT USAGE ON SCHEMA parent2.to_schema TO testuser;
11 changes: 11 additions & 0 deletions pkg/sql/reparent_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
Expand Down Expand Up @@ -114,6 +115,16 @@ func (n *reparentDatabaseNode) startExec(params runParams) error {
if err != nil {
return err
}

// Not all Privileges on databases are valid on schemas.
// Remove any privileges that are not valid for schemas.
schemaPrivs := privilege.GetValidPrivilegesForObject(privilege.Schema).ToBitField()
privs := n.db.GetPrivileges()
for i, u := range privs.Users {
// Remove privileges that are valid for databases but not for schemas.
privs.Users[i].Privileges = u.Privileges & schemaPrivs
}

schema := schemadesc.NewBuilder(&descpb.SchemaDescriptor{
ParentID: n.newParent.ID,
Name: n.db.Name,
Expand Down

0 comments on commit 506159b

Please sign in to comment.