Skip to content

Commit 330ebd8

Browse files
committed
catalog/replication: make system.privileges a view too
Release note (enterprise change): 'SYSTEM' privileges are now also inherited in read-only mode standby PCR clusters. Epic: CRDB-50820.
1 parent 2a6e468 commit 330ebd8

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

pkg/sql/catalog/replication/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
"//pkg/sql/catalog/schemadesc",
2020
"//pkg/sql/catalog/tabledesc",
2121
"//pkg/sql/catalog/typedesc",
22+
"//pkg/sql/sem/catconstants",
2223
"//pkg/util/hlc",
2324
"//pkg/util/protoutil",
2425
"@com_github_cockroachdb_errors//:errors",

pkg/sql/catalog/replication/reader_catalog.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
2222
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
2323
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
24+
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
2425
"github.com/cockroachdb/cockroach/pkg/util/hlc"
2526
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
2627
"github.com/cockroachdb/errors"
@@ -64,7 +65,7 @@ func SetupOrAdvanceStandbyReaderCatalog(
6465
// below.
6566
descriptorsToWrite := make([]catalog.MutableDescriptor, 0, len(allExistingDescs.OrderedDescriptorIDs()))
6667
if err := extracted.ForEachDescriptor(func(fromDesc catalog.Descriptor) error {
67-
if !shouldSetupForReader(fromDesc.GetID(), fromDesc.GetParentID()) {
68+
if !shouldSetupForReader(fromDesc.GetID(), fromDesc.GetName(), fromDesc.GetParentID()) {
6869
return nil
6970
}
7071
// Track this descriptor was updated.
@@ -138,7 +139,7 @@ func SetupOrAdvanceStandbyReaderCatalog(
138139
}
139140
}
140141
if err := extracted.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
141-
if !shouldSetupForReader(e.GetID(), e.GetParentID()) {
142+
if !shouldSetupForReader(e.GetID(), e.GetName(), e.GetParentID()) {
142143
return nil
143144
}
144145
// Do not upsert entries if one already exists.
@@ -153,7 +154,7 @@ func SetupOrAdvanceStandbyReaderCatalog(
153154
// Figure out which descriptors should be deleted.
154155
if err := allExistingDescs.ForEachDescriptor(func(desc catalog.Descriptor) error {
155156
// Skip descriptors that were updated above
156-
if !shouldSetupForReader(desc.GetID(), desc.GetParentID()) ||
157+
if !shouldSetupForReader(desc.GetID(), desc.GetName(), desc.GetParentID()) ||
157158
descriptorsUpdated.Contains(desc.GetID()) {
158159
return nil
159160
}
@@ -167,7 +168,7 @@ func SetupOrAdvanceStandbyReaderCatalog(
167168
if err := allExistingDescs.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
168169
// Skip descriptors that were updated above that were
169170
// not renamed.
170-
if !shouldSetupForReader(e.GetID(), e.GetParentID()) ||
171+
if !shouldSetupForReader(e.GetID(), e.GetName(), e.GetParentID()) ||
171172
(descriptorsUpdated.Contains(e.GetID()) &&
172173
!descriptorsRenamed.Contains(e.GetID())) {
173174
return nil
@@ -292,15 +293,23 @@ func replicateDescriptorForReader(
292293
}
293294

294295
// shouldSetupForReader determines if a descriptor should be setup
295-
// access via external row data.
296-
func shouldSetupForReader(id descpb.ID, parentID descpb.ID) bool {
296+
// access via external row data, based on the ID for tables with fixed IDs or on
297+
// the name and parentID for tables with dynamic IDs.
298+
func shouldSetupForReader(id descpb.ID, name string, parentID descpb.ID) bool {
297299
switch id {
298300
case keys.UsersTableID, keys.RoleMembersTableID, keys.RoleOptionsTableID,
299301
keys.DatabaseRoleSettingsTableID, keys.TableStatisticsTableID:
300302
return true
301303
default:
302-
return parentID != keys.SystemDatabaseID &&
303-
id != keys.SystemDatabaseID
304+
if parentID == keys.SystemDatabaseID {
305+
switch name {
306+
case string(catconstants.SystemPrivilegeTableName):
307+
return true
308+
default:
309+
return false
310+
}
311+
}
312+
return id != keys.SystemDatabaseID
304313
}
305314
}
306315

pkg/sql/catalog/replication/reader_catalog_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func TestReaderCatalog(t *testing.T) {
185185
r.srcRunner.Exec(t, `
186186
CREATE USER roacher WITH CREATEROLE;
187187
GRANT ADMIN TO roacher;
188+
GRANT SYSTEM VIEWACTIVITY TO roacher;
188189
ALTER USER roacher SET timezone='America/New_York';
189190
CREATE DATABASE db1;
190191
CREATE SCHEMA db1.sc1;
@@ -226,6 +227,7 @@ INSERT INTO t3(n) VALUES (3);
226227
"INSERT INTO t1(val) VALUES('inactive');",
227228
"CREATE USER roacher2 WITH CREATEROLE;",
228229
"GRANT ADMIN TO roacher2;",
230+
"GRANT SYSTEM VIEWACTIVITY TO roacher2;",
229231
"ALTER USER roacher2 SET timezone='America/New_York';",
230232
"CREATE TABLE t4(n int)",
231233
"INSERT INTO t4 VALUES (32)",
@@ -239,6 +241,7 @@ INSERT INTO t3(n) VALUES (3);
239241
r.compareEqual(t, "SELECT * FROM t1 ORDER BY n")
240242
r.compareEqual(t, "SELECT * FROM v1 ORDER BY 1")
241243
r.compareEqual(t, "SELECT * FROM system.users")
244+
r.compareEqual(t, "SHOW SYSTEM GRANTS FOR roacher")
242245
r.compareEqual(t, "SELECT * FROM system.table_statistics")
243246
r.compareEqual(t, "SELECT * FROM system.role_options")
244247
r.compareEqual(t, "SELECT * FROM system.database_role_settings")
@@ -266,6 +269,8 @@ INSERT INTO t3(n) VALUES (3);
266269
r.compareEqual(t, "SELECT * FROM system.table_statistics")
267270
r.compareEqual(t, "SELECT * FROM system.role_options")
268271
r.compareEqual(t, "SELECT * FROM system.database_role_settings")
272+
r.compareEqual(t, "SHOW SYSTEM GRANTS FOR roacher")
273+
r.compareEqual(t, "SHOW SYSTEM GRANTS FOR roacher2")
269274
r.compareEqual(t, "SELECT * FROM t4 ORDER BY n")
270275
r.compareEqual(t, "SELECT * FROM t5 ORDER BY n")
271276
r.compareEqual(t, "SELECT name FROM system.namespace ORDER BY name")

0 commit comments

Comments
 (0)