Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add VIEWSYSTEMTABLE system privilege #109474

Merged
merged 1 commit into from Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions pkg/sql/authorization.go
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 dynamically 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
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
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