Skip to content

Commit

Permalink
sql: add VIEWSYSTEMTABLE system privilege
Browse files Browse the repository at this point in the history
This privilege is useful for support situations, where an engineer needs
to be able to view system tables without having full admin access.

Release note (sql change): Added the VIEWSYSTEMTABLE system privilege.
Users with this privilege have SELECT privileges for all tables in the
system database.
  • Loading branch information
rafiss committed Aug 24, 2023
1 parent 7164cad commit 081ff6a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
26 changes: 22 additions & 4 deletions pkg/sql/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,32 @@ func (p *planner) CheckPrivilegeForUser(
privilegeKind privilege.Kind,
user username.SQLUsername,
) error {
ok, err := p.HasPrivilege(ctx, privilegeObject, privilegeKind, user)
hasPriv, err := p.HasPrivilege(ctx, privilegeObject, privilegeKind, user)
if err != nil {
return err
}
if !ok {
return insufficientPrivilegeError(user, privilegeKind, privilegeObject)
if hasPriv {
return nil
}
return nil
// Special case for system tables. The VIEWSYSTEMTABLE system privilege is
// equivalent to having SELECT on all system tables. This is because it is not
// possible to dybamically grant SELECT privileges system tables, but in the
// context of support escalations, we need to be able to grant the ability to
// view system tables without granting the entire admin role.
if d, ok := privilegeObject.(catalog.Descriptor); ok {
if catalog.IsSystemDescriptor(d) && privilegeKind == privilege.SELECT {
hasViewSystemTablePriv, err := p.HasPrivilege(
ctx, syntheticprivilege.GlobalPrivilegeObject, privilege.VIEWSYSTEMTABLE, user,
)
if err != nil {
return err
}
if hasViewSystemTablePriv {
return nil
}
}
}
return insufficientPrivilegeError(user, privilegeKind, privilegeObject)
}

// CheckPrivilege implements the AuthorizationAccessor interface.
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/synthetic_privileges
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,29 @@ statement ok
REVOKE SYSTEM ALL FROM testuser

subtest end

subtest view_system_table

user testuser

# Make sure testuser does not have privileges first.
statement error user testuser does not have SELECT privilege on relation users
SELECT * FROM system.users WHERE username = 'testuser'

user root

statement ok
GRANT SYSTEM VIEWSYSTEMTABLE TO testuser

user testuser

query TT
SELECT username, "hashedPassword" FROM system.users WHERE username = 'testuser'
----
testuser NULL

# testuser still should not have write privileges.
statement error user testuser does not have INSERT privilege on relation users
INSERT INTO system.users VALUES ('cat', null, true, 200)

subtest end
3 changes: 3 additions & 0 deletions pkg/sql/privilege/kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/sql/privilege/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (
MODIFYSQLCLUSTERSETTING Kind = 28
REPLICATION Kind = 29
MANAGETENANT Kind = 30
VIEWSYSTEMTABLE Kind = 31
)

// Privilege represents a privilege parsed from an Access Privilege Inquiry
Expand Down Expand Up @@ -149,8 +150,12 @@ var (
// before v22.2 we treated Sequences the same as Tables. This is to avoid making
// certain privileges unavailable after upgrade migration.
// Note that "CREATE, CHANGEFEED, INSERT, DELETE, ZONECONFIG" are no-op privileges on sequences.
SequencePrivileges = List{ALL, USAGE, SELECT, UPDATE, CREATE, CHANGEFEED, DROP, INSERT, DELETE, ZONECONFIG}
GlobalPrivileges = List{ALL, BACKUP, RESTORE, MODIFYCLUSTERSETTING, EXTERNALCONNECTION, VIEWACTIVITY, VIEWACTIVITYREDACTED, VIEWCLUSTERSETTING, CANCELQUERY, NOSQLLOGIN, VIEWCLUSTERMETADATA, VIEWDEBUG, EXTERNALIOIMPLICITACCESS, VIEWJOB, MODIFYSQLCLUSTERSETTING, REPLICATION, MANAGETENANT}
SequencePrivileges = List{ALL, USAGE, SELECT, UPDATE, CREATE, CHANGEFEED, DROP, INSERT, DELETE, ZONECONFIG}
GlobalPrivileges = List{
ALL, BACKUP, RESTORE, MODIFYCLUSTERSETTING, EXTERNALCONNECTION, VIEWACTIVITY, VIEWACTIVITYREDACTED,
VIEWCLUSTERSETTING, CANCELQUERY, NOSQLLOGIN, VIEWCLUSTERMETADATA, VIEWDEBUG, EXTERNALIOIMPLICITACCESS, VIEWJOB,
MODIFYSQLCLUSTERSETTING, REPLICATION, MANAGETENANT, VIEWSYSTEMTABLE,
}
VirtualTablePrivileges = List{ALL, SELECT}
ExternalConnectionPrivileges = List{ALL, USAGE, DROP}
)
Expand Down Expand Up @@ -196,6 +201,7 @@ var ByName = map[string]Kind{
"MODIFYSQLCLUSTERSETTING": MODIFYSQLCLUSTERSETTING,
"REPLICATION": REPLICATION,
"MANAGETENANT": MANAGETENANT,
"VIEWSYSTEMTABLE": VIEWSYSTEMTABLE,
}

// List is a list of privileges.
Expand Down

0 comments on commit 081ff6a

Please sign in to comment.