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

syntheticprivilege: admin always has ALL global privileges #110976

Merged
merged 2 commits into from
Sep 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ REVOKE ADMIN FROM testuser
exec-sql user=testuser
BACKUP INTO 'userfile:///test-nonroot-cluster';
----
pq: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP privilege on global
pq: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP system privilege

# Grant system backup privilege and re-run the backup.
exec-sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CREATE USER testuser
exec-sql user=testuser
BACKUP INTO 'nodelocal://1/test2'
----
pq: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP privilege on global
pq: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP system privilege

# Database backup as a non-admin user should have CONNECT on database and SELECT on tables.
exec-sql user=testuser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ CREATE USER testuser;
exec-sql user=testuser
RESTORE FROM LATEST IN 'nodelocal://1/foo';
----
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE privilege on global
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE system privilege

exec-sql
GRANT SYSTEM RESTORE TO testuser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CREATE USER testuser
exec-sql cluster=s2 user=testuser
RESTORE FROM LATEST IN 'nodelocal://1/test/'
----
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE privilege on global
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE system privilege

exec-sql cluster=s2 user=testuser
RESTORE DATABASE d FROM LATEST IN 'nodelocal://1/test/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CREATE USER testuser
exec-sql user=testuser
RESTORE FROM LATEST IN 'nodelocal://1/full_cluster_backup/' with schema_only
----
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE privilege on global
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE system privilege

# Fail fast using a database backup
exec-sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CREATE USER testuser
exec-sql user=testuser
RESTORE FROM LATEST IN 'nodelocal://1/full_cluster_backup/' with schema_only, verify_backup_table_data
----
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE privilege on global
pq: only users with the admin role or the RESTORE system privilege are allowed to perform a cluster restore: user testuser does not have RESTORE system privilege

# Fail fast using a database backup
exec-sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ REVOKE ADMIN FROM testuser;
exec-sql user=testuser
CREATE SCHEDULE foocluster FOR BACKUP INTO 'external://foo/cluster' RECURRING '@hourly';
----
pq: failed to dry run backup: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP privilege on global
pq: failed to dry run backup: only users with the admin role or the BACKUP system privilege are allowed to perform full cluster backups: user testuser does not have BACKUP system privilege

exec-sql user=testuser
CREATE SCHEDULE foodb FOR BACKUP DATABASE foo INTO 'external://foo/database' RECURRING '@hourly';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func TestReplicationManagerRequiresReplicationPrivilege(t *testing.T) {
{user: "admin", expErr: "", isEnterprise: true},
{user: "root", expErr: "", isEnterprise: true},
{user: "somebody", expErr: "", isEnterprise: true},
{user: "nobody", expErr: "user nobody does not have REPLICATION privilege on global", isEnterprise: true},
{user: "nobody", expErr: "user nobody does not have REPLICATION system privilege", isEnterprise: true},

{user: "admin", expErr: "use of REPLICATION requires an enterprise license", isEnterprise: false},
{user: "root", expErr: " use of REPLICATION requires an enterprise license", isEnterprise: false},
{user: "somebody", expErr: "use of REPLICATION requires an enterprise license", isEnterprise: false},
{user: "nobody", expErr: "user nobody does not have REPLICATION privilege on global", isEnterprise: false},
{user: "nobody", expErr: "user nobody does not have REPLICATION system privilege", isEnterprise: false},
} {
t.Run(fmt.Sprintf("%s/ent=%t", tc.user, tc.isEnterprise), func(t *testing.T) {
if tc.isEnterprise {
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,14 @@ func insufficientPrivilegeError(
user, typeForError, object.GetName())
}

// Make a slightly different message for the global privilege object so that
// it uses more understandable user-facing language.
if object.GetObjectType() == privilege.Global {
return pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s system privilege",
user, kind)
}

return pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s privilege on %s %s",
user, kind, typeForError, object.GetName())
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/tenant
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ set default_transaction_read_only = off;

user testuser

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
CREATE TENANT four

subtest drop_tenant
Expand Down Expand Up @@ -195,13 +195,13 @@ set default_transaction_read_only = off;

user testuser

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
DROP TENANT three

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
SHOW TENANTS

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
SHOW TENANT 'two'

user root
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/tenant_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ SELECT crdb_internal.update_tenant_resource_limits('tenant-number-ten', 1000, 10

user testuser

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
SELECT crdb_internal.create_tenant(314)

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
SELECT crdb_internal.create_tenant('not-allowed')

statement error user testuser does not have MANAGETENANT privilege on global
statement error user testuser does not have MANAGETENANT system privilege
DROP TENANT [1]

statement error only users with the admin role are allowed to gc tenant
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/syntheticprivilege/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ See: `sql/logictest/testdata/logic_test/system_privileges`
```sql
user testuser

statement error pq: user testuser does not have MODIFYCLUSTERSETTING privilege on Global
statement error pq: user testuser does not have MODIFYCLUSTERSETTING system privilege
SELECT * FROM crdb_internal.cluster_settings;

user root
Expand All @@ -386,7 +386,7 @@ REVOKE SYSTEM MODIFYCLUSTERSETTING FROM testuser

user testuser

statement error pq: user testuser does not have MODIFYCLUSTERSETTING privilege on Global
statement error pq: user testuser does not have MODIFYCLUSTERSETTING system privilege
SELECT * FROM crdb_internal.cluster_settings;

user root
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/syntheticprivilegecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func (c *Cache) readFromStorage(
}
}

// Admin always has ALL global privileges.
if spo.GetObjectType() == privilege.Global {
privDesc.Grant(username.AdminRoleName(), privilege.List{privilege.ALL}, true)
}

// We use InvalidID to skip checks on the root/admin roles having
// privileges.
validPrivs, err := privilege.GetValidPrivilegesForObject(spo.GetObjectType())
Expand Down