Skip to content

sql: delegate SHOW SYSTEM GRANTS to new crdb_internal.kv_system_privileges vtable#104565

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
ecwall:sql_show_system_grants
Jun 8, 2023
Merged

sql: delegate SHOW SYSTEM GRANTS to new crdb_internal.kv_system_privileges vtable#104565
craig[bot] merged 1 commit intocockroachdb:masterfrom
ecwall:sql_show_system_grants

Conversation

@ecwall
Copy link
Copy Markdown
Contributor

@ecwall ecwall commented Jun 8, 2023

Fixes #104412

SHOW SYSTEM GRANTS previously delegated to system.privileges which requires
the admin or root user privileges.

This change adds a new crdb_internal.kv_system_privileges vtable that is
accessible to anyone.

Release note (bug fix): Fixes issue requiring admin or root user privileges to
use SHOW SYSTEM GRANTS.

@ecwall ecwall requested a review from rafiss June 8, 2023 00:30
@ecwall ecwall requested a review from a team as a code owner June 8, 2023 00:30
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@ecwall ecwall added backport-22.2.x backport-23.1.x PAST MAINTENANCE SUPPORT: 23.1 patch releases via ER request only labels Jun 8, 2023
@ecwall ecwall requested a review from a team as a code owner June 8, 2023 00:48
@ecwall ecwall requested review from a team and DrewKimball June 8, 2023 00:48
@ecwall ecwall requested a review from a team as a code owner June 8, 2023 02:21
rows, err := p.ExecCfg().InternalDB.Executor().QueryIteratorEx(
ctx,
op,
nil,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: decorate with comment

ctx,
op,
nil,
sessiondata.RootUserSessionDataOverride,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a parameter of populateFromQuery instead of hard-coding it to the root user, please? I have another use-case in mind where populateFromQuery might be useful but it would be for the node user.

},
}

func populateFromQuery(query string, op string) populateFunc {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: s/op/opName? also have it precede the query arg perhaps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced this func with virtualSchemaView{}.

}

var crdbInternalSystemPrivileges = virtualSchemaTable{
comment: `vtable backing SHOW SYSTEM GRANTS`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this comment deserves to be improved, how about something along the lines of "this vtable is a view on system.privileges with the root user and is used to back SHOW SYSTEM GRANTS"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

var crdbInternalSystemPrivileges = virtualSchemaTable{
comment: `vtable backing SHOW SYSTEM GRANTS`,
schema: `
CREATE TABLE crdb_internal.system_privileges (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a naming scheme in force for this virtual schema which requires new tables to have a prefix depending on what they do, in this case this prefix is kv_.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do user privileges get a kv_ prefix?

Copy link
Copy Markdown

@postamar postamar Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/crdb_internal.go#L107 this vtable needs the kv_ prefix because it's populated (ultimately) by performing a KV scan on a physical table. That's my understanding, at least, which so far hasn't been challenged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sounds good to me. I never noticed that comment.

if err != nil {
return err
}
defer func() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation for the existence of this defer deserves a comment.

}
}

var crdbInternalSystemPrivileges = virtualSchemaTable{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (but I'm not sure) that you could define this as a virtualSchemaView and this way you would not need to introduce populateFromQuery at all!

I investigated this a bit. In master we already have crdb_internal.transaction_statistics_persisted which is the same thing for system.transaction_statistics; I set up a logictest in which testuser was non-admin and querying the latter would correctly raise an error however it totally can query the vtable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I was looking for something like this, but did not know what to search for.

@dhartunian dhartunian removed the request for review from a team June 8, 2023 14:22
@ecwall ecwall requested review from a team and postamar and removed request for a team and DrewKimball June 8, 2023 15:15
…s vtable

Fixes #104412

SHOW SYSTEM GRANTS previously delegated to system.privileges which requires
the admin or root user privileges.

This change adds a new crdb_internal.kv_system_privileges vtable that is
accessible to anyone.

Release note (bug fix): Fixes issue requiring admin or root user privileges to
use SHOW SYSTEM GRANTS.
Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! glad the view works

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ecwall and @postamar)

@ecwall
Copy link
Copy Markdown
Contributor Author

ecwall commented Jun 8, 2023

bors r=rafiss

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jun 8, 2023

Build succeeded:

@craig craig bot merged commit 7fb4c16 into cockroachdb:master Jun 8, 2023
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Jun 8, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from f8c7d67 to blathers/backport-release-22.2-104565: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


error creating merge commit from f8c7d67 to blathers/backport-release-23.1-104565: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@ecwall ecwall changed the title sql: delegate SHOW SYSTEM GRANTS to new crdb_internal.system_privileges vtable sql: delegate SHOW SYSTEM GRANTS to new crdb_internal.kv_system_privileges vtable Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-23.1.x PAST MAINTENANCE SUPPORT: 23.1 patch releases via ER request only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show System Grants Errors - User Lacks SELECT Privilege on 'privileges' Relation

4 participants