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: Implement grant and revoke privilege #18827

Merged
merged 9 commits into from
Apr 20, 2023

Conversation

jkosh44
Copy link
Contributor

@jkosh44 jkosh44 commented Apr 18, 2023

This commit implements the privilege variants of the SQL commands GRANT and REVOKE.

Part of #11579

Motivation

This PR adds a known-desirable feature.

Tips for reviewer

  • Over half the added lines are tests.

Checklist

  • This PR has adequate test coverage / QA involvement has been duly considered.
  • This PR has an associated up-to-date design doc, is a design doc (template), or is sufficiently small to not require a design.
  • This PR evolves an existing $T ⇔ Proto$T mapping (possibly in a backwards-incompatible way) and therefore is tagged with a T-proto label.
  • If this PR will require changes to cloud orchestration, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label (example).
  • This PR includes the following user-facing behavior changes:
    • This release adds the GRANT and REVOKE commands for adding and removing object privileges.

@jkosh44 jkosh44 force-pushed the grant-revoke-privilege branch 4 times, most recently from 062a26a to 869dc0a Compare April 18, 2023 23:19
This commit implements the privilege variants of the SQL commands
`GRANT` and `REVOKE`.

Part of MaterializeInc#11579
@jkosh44 jkosh44 marked this pull request as ready for review April 18, 2023 23:52
@jkosh44 jkosh44 requested a review from a team as a code owner April 18, 2023 23:52
@jkosh44 jkosh44 requested review from a team and def- April 18, 2023 23:52
Copy link
Contributor

@def- def- left a comment

Choose a reason for hiding this comment

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

Thanks for the tests

@@ -723,6 +734,16 @@ pub enum DatabaseId {
System(u64),
}

impl DatabaseId {
pub fn is_user(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is unused, but it's fairly small and I think since we're adding is_system it's useful to have this around for the future.

@@ -685,6 +686,16 @@ pub enum SchemaId {
System(u64),
}

impl SchemaId {
pub fn is_user(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

unused?

Copy link
Contributor

Choose a reason for hiding this comment

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

Overall looks good to me!

src/adapter/src/catalog.rs Outdated Show resolved Hide resolved
src/adapter/src/catalog.rs Outdated Show resolved Hide resolved
src/adapter/src/catalog.rs Outdated Show resolved Hide resolved
src/adapter/src/catalog.rs Outdated Show resolved Hide resolved
);
existing_privilege.acl_mode =
existing_privilege.acl_mode.difference(privilege.acl_mode);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need an else block here like above because it's not a problem to attempt to revoke a privilege that already doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that just ends up as a no-op.

ObjectId::Cluster(cluster_id) | ObjectId::ClusterReplica((cluster_id, _)) => {
if cluster_id.is_system() {
let cluster = self.get_cluster(*cluster_id);
Err(Error::new(ErrorKind::ReadOnlyCluster(
Copy link
Contributor

Choose a reason for hiding this comment

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

Some of this logic is duplicated above in Op::GrantPrivilege. Should that call this function instead?

Copy link
Contributor Author

@jkosh44 jkosh44 Apr 20, 2023

Choose a reason for hiding this comment

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

To be honest, almost all of planning, sequencing, and the catalog commit are identical except for a few lines. I was tempted to implement Revoke and Grant as a single statement with a boolean flag/enum, to avoid duplication. That's actually how PostgreSQL implements Grant/Revoke.

What do you think? Is that going too far?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I just massively cleaned this up in b04f54c

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I completely misunderstood this commit. This function is actually called in the sequencer, so I think it's safe to remove these checks from Op::GrantPrivilege.

src/adapter/src/coord/sequencer/inner.rs Outdated Show resolved Hide resolved
@jkosh44 jkosh44 enabled auto-merge (squash) April 20, 2023 10:17
@jkosh44 jkosh44 merged commit a8aa3be into MaterializeInc:main Apr 20, 2023
@jkosh44 jkosh44 deleted the grant-revoke-privilege branch April 20, 2023 13:51
@jkosh44 jkosh44 mentioned this pull request Apr 24, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants