Skip to content

Commit

Permalink
Merge #91517
Browse files Browse the repository at this point in the history
91517: sql: add role_id and member_id columns to system.role_members table r=rafiss a=andyyang890

This patch adds user ID columns to the system.role_members table.
The new `role_id` column corresponds to the existing `role` column
and the new `member_id` column corresponds to the existing `member`
column. Migrations are also added to alter and backfill the table
in older clusters.

Part of #87079

Release note: None

Co-authored-by: Andy Yang <yang@cockroachlabs.com>
  • Loading branch information
craig[bot] and andyyang890 committed Dec 9, 2022
2 parents c471947 + 7adcd34 commit e1e3c3d
Show file tree
Hide file tree
Showing 17 changed files with 629 additions and 144 deletions.
2 changes: 1 addition & 1 deletion docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,4 @@ trace.jaeger.agent string the address of a Jaeger agent to receive traces using
trace.opentelemetry.collector string address of an OpenTelemetry trace collector to receive traces using the otel gRPC protocol, as <host>:<port>. If no port is specified, 4317 will be used.
trace.span_registry.enabled boolean true if set, ongoing traces can be seen at https://<ui>/#/debug/tracez
trace.zipkin.collector string the address of a Zipkin instance to receive traces, as <host>:<port>. If no port is specified, 9411 will be used.
version version 1000022.2-10 set the active cluster version in the format '<major>.<minor>'
version version 1000022.2-14 set the active cluster version in the format '<major>.<minor>'
2 changes: 1 addition & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@
<tr><td><code>trace.opentelemetry.collector</code></td><td>string</td><td><code></code></td><td>address of an OpenTelemetry trace collector to receive traces using the otel gRPC protocol, as <host>:<port>. If no port is specified, 4317 will be used.</td></tr>
<tr><td><code>trace.span_registry.enabled</code></td><td>boolean</td><td><code>true</code></td><td>if set, ongoing traces can be seen at https://<ui>/#/debug/tracez</td></tr>
<tr><td><code>trace.zipkin.collector</code></td><td>string</td><td><code></code></td><td>the address of a Zipkin instance to receive traces, as <host>:<port>. If no port is specified, 9411 will be used.</td></tr>
<tr><td><code>version</code></td><td>version</td><td><code>1000022.2-10</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
<tr><td><code>version</code></td><td>version</td><td><code>1000022.2-14</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
</tbody>
</table>
4 changes: 2 additions & 2 deletions pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ exp,benchmark
11,Grant/grant_all_on_1_table
12,Grant/grant_all_on_2_tables
12,Grant/grant_all_on_3_tables
9,GrantRole/grant_1_role
11,GrantRole/grant_2_roles
11,GrantRole/grant_1_role
15,GrantRole/grant_2_roles
3,ORMQueries/activerecord_type_introspection_query
3,ORMQueries/django_table_introspection_1_table
3,ORMQueries/django_table_introspection_4_tables
Expand Down
34 changes: 17 additions & 17 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9468,19 +9468,19 @@ func TestBackupRestoreSystemUsers(t *testing.T) {

// Role 'app_role' and user 'app' will be added, and 'app' is granted with 'app_role'
// User test will remain untouched with no role granted
sqlDBRestore.CheckQueryResults(t, "SELECT username, \"hashedPassword\", \"isRole\" FROM system.users", [][]string{
{"admin", "", "true"},
{"app", "NULL", "false"},
{"app_role", "NULL", "true"},
{"root", "", "false"},
{"test", "NULL", "false"},
{"test_role", "NULL", "true"},
sqlDBRestore.CheckQueryResults(t, "SELECT * FROM system.users", [][]string{
{"admin", "", "true", "2"},
{"app", "NULL", "false", "101"},
{"app_role", "NULL", "true", "102"},
{"root", "", "false", "1"},
{"test", "NULL", "false", "100"},
{"test_role", "NULL", "true", "103"},
})
sqlDBRestore.CheckQueryResults(t, "SELECT * FROM system.role_members", [][]string{
{"admin", "app", "false"},
{"admin", "root", "true"},
{"app_role", "app", "false"},
{"app_role", "test_role", "false"},
{"admin", "app", "false", "NULL", "NULL"},
{"admin", "root", "true", "2", "1"},
{"app_role", "app", "false", "NULL", "NULL"},
{"app_role", "test_role", "false", "NULL", "NULL"},
})
sqlDBRestore.CheckQueryResults(t, "SHOW USERS", [][]string{
{"admin", "", "{}"},
Expand All @@ -9501,10 +9501,10 @@ func TestBackupRestoreSystemUsers(t *testing.T) {
defer cleanupEmptyCluster1()
t.Run("restore-from-backup-with-no-system-role-members", func(t *testing.T) {
sqlDBRestore1.Exec(t, "RESTORE SYSTEM USERS FROM $1", localFoo+"/3")
sqlDBRestore1.CheckQueryResults(t, "SELECT \"role\", \"member\", \"isAdmin\" FROM system.role_members", [][]string{
{"admin", "root", "true"},
sqlDBRestore1.CheckQueryResults(t, "SELECT * FROM system.role_members", [][]string{
{"admin", "root", "true", "2", "1"},
})
sqlDBRestore1.CheckQueryResults(t, "SELECT username, \"hashedPassword\", \"isRole\", \"user_id\" FROM system.users", [][]string{
sqlDBRestore1.CheckQueryResults(t, "SELECT * FROM system.users", [][]string{
{"admin", "", "true", "2"},
{"app", "NULL", "false", "100"},
{"app_role", "NULL", "true", "101"},
Expand All @@ -9528,10 +9528,10 @@ func TestBackupRestoreSystemUsers(t *testing.T) {
// allocated properly in the restore.
sqlDBRestore2.Exec(t, "CREATE USER testuser")
sqlDBRestore2.Exec(t, "RESTORE SYSTEM USERS FROM $1", localFoo+"/3")
sqlDBRestore2.CheckQueryResults(t, "SELECT \"role\", \"member\", \"isAdmin\" FROM system.role_members", [][]string{
{"admin", "root", "true"},
sqlDBRestore2.CheckQueryResults(t, "SELECT * FROM system.role_members", [][]string{
{"admin", "root", "true", "2", "1"},
})
sqlDBRestore2.CheckQueryResults(t, "SELECT username, \"hashedPassword\", \"isRole\", \"user_id\" FROM system.users", [][]string{
sqlDBRestore2.CheckQueryResults(t, "SELECT * FROM system.users", [][]string{
{"admin", "", "true", "2"},
{"app", "NULL", "false", "101"},
{"app_role", "NULL", "true", "102"},
Expand Down
16 changes: 16 additions & 0 deletions pkg/clusterversion/cockroach_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ const (
// V23_1_CreateSystemJobInfoTable creates the system.job_info table.
V23_1CreateSystemJobInfoTable

// V23_1RoleMembersTableHasIDColumns is the version where the role_members
// system table has columns for ids.
V23_1RoleMembersTableHasIDColumns

// V23_1RoleMembersIDColumnsBackfilled is the version where the columns for
// ids in the role_members system table have been backfilled.
V23_1RoleMembersIDColumnsBackfilled

// *************************************************
// Step (1): Add new versions here.
// Do not add new versions to a patch release.
Expand Down Expand Up @@ -588,6 +596,14 @@ var rawVersionsSingleton = keyedVersions{
Key: V23_1CreateSystemJobInfoTable,
Version: roachpb.Version{Major: 22, Minor: 2, Internal: 10},
},
{
Key: V23_1RoleMembersTableHasIDColumns,
Version: roachpb.Version{Major: 22, Minor: 2, Internal: 12},
},
{
Key: V23_1RoleMembersIDColumnsBackfilled,
Version: roachpb.Version{Major: 22, Minor: 2, Internal: 14},
},

// *************************************************
// Step (2): Add new versions here.
Expand Down
59 changes: 55 additions & 4 deletions pkg/sql/catalog/systemschema/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,17 @@ CREATE TABLE system.locations (
// role_members stores relationships between roles (role->role and role->user).
RoleMembersTableSchema = `
CREATE TABLE system.role_members (
"role" STRING NOT NULL,
"member" STRING NOT NULL,
"isAdmin" BOOL NOT NULL,
"role" STRING NOT NULL,
"member" STRING NOT NULL,
"isAdmin" BOOL NOT NULL,
role_id OID,
member_id OID,
CONSTRAINT "primary" PRIMARY KEY ("role", "member"),
INDEX ("role"),
INDEX ("member")
INDEX ("member"),
INDEX (role_id),
INDEX (member_id),
UNIQUE INDEX (role_id, member_id)
);`

// comments stores comments(database, table, column...).
Expand Down Expand Up @@ -1579,6 +1584,8 @@ var (
{Name: "role", ID: 1, Type: types.String},
{Name: "member", ID: 2, Type: types.String},
{Name: "isAdmin", ID: 3, Type: types.Bool},
{Name: "role_id", ID: 4, Type: types.Oid, Nullable: true},
{Name: "member_id", ID: 5, Type: types.Oid, Nullable: true},
},
[]descpb.ColumnFamilyDescriptor{
{
Expand All @@ -1594,6 +1601,20 @@ var (
ColumnIDs: []descpb.ColumnID{3},
DefaultColumnID: 3,
},
{
Name: "fam_4_role_id",
ID: 4,
ColumnNames: []string{"role_id"},
ColumnIDs: []descpb.ColumnID{4},
DefaultColumnID: 4,
},
{
Name: "fam_5_member_id",
ID: 5,
ColumnNames: []string{"member_id"},
ColumnIDs: []descpb.ColumnID{5},
DefaultColumnID: 5,
},
},
descpb.IndexDescriptor{
Name: "primary",
Expand Down Expand Up @@ -1623,6 +1644,36 @@ var (
KeySuffixColumnIDs: []descpb.ColumnID{1},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
descpb.IndexDescriptor{
Name: "role_members_role_id_idx",
ID: 4,
Unique: false,
KeyColumnNames: []string{"role_id"},
KeyColumnDirections: []catpb.IndexColumn_Direction{catpb.IndexColumn_ASC},
KeyColumnIDs: []descpb.ColumnID{4},
KeySuffixColumnIDs: []descpb.ColumnID{1, 2},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
descpb.IndexDescriptor{
Name: "role_members_member_id_idx",
ID: 5,
Unique: false,
KeyColumnNames: []string{"member_id"},
KeyColumnDirections: []catpb.IndexColumn_Direction{catpb.IndexColumn_ASC},
KeyColumnIDs: []descpb.ColumnID{5},
KeySuffixColumnIDs: []descpb.ColumnID{1, 2},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
descpb.IndexDescriptor{
Name: "role_members_role_id_member_id_key",
ID: 6,
Unique: true,
KeyColumnNames: []string{"role_id", "member_id"},
KeyColumnDirections: []catpb.IndexColumn_Direction{catpb.IndexColumn_ASC, catpb.IndexColumn_ASC},
KeyColumnIDs: []descpb.ColumnID{4, 5},
KeySuffixColumnIDs: []descpb.ColumnID{1, 2},
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
},
))

// CommentsTable is the descriptor for the comments table.
Expand Down
Loading

0 comments on commit e1e3c3d

Please sign in to comment.