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

Add SHOW GRANTS [FOR xxx] statement #3366

Merged
merged 15 commits into from
Dec 14, 2021

Conversation

flaneur2020
Copy link
Member

@flaneur2020 flaneur2020 commented Dec 10, 2021

I hereby agree to the terms of the CLA available at: https://databend.rs/policies/cla/

Summary

  • rename UserPrivilege to UserPrivilegeSet
  • add UserIdentity struct which includes username and hostname
  • add Display for UserPrivilegeType, UserPrivilegeSet, GrantObject, GrantEntry
  • add parser and interpreter for SHOW GRANTS [FOR xxx] statement

Changelog

  • New Feature

Related Issues

Fixes #3262

Test Plan

Unit Tests
Stateless Tests

@databend-bot databend-bot added the pr-feature this PR introduces a new feature to the codebase label Dec 10, 2021
@databend-bot
Copy link
Member

Thanks for the contribution!
I have applied any labels matching special text in your PR Changelog.

Please review the labels and make any necessary changes.

@vercel
Copy link

vercel bot commented Dec 10, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/databend/databend/HKoiinGdDE6vpyrRmWULdAhyWySH
✅ Preview: https://databend-git-fork-flaneur2020-add-show-grants-databend.vercel.app

[Deployment for 8537e88 canceled]

@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2021

Codecov Report

Merging #3366 (8537e88) into main (1f29cfc) will decrease coverage by 0%.
The diff coverage is 30%.

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #3366   +/-   ##
=====================================
- Coverage     61%     61%   -1%     
=====================================
  Files        610     615    +5     
  Lines      34185   34256   +71     
=====================================
+ Hits       20992   21007   +15     
- Misses     13193   13249   +56     
Impacted Files Coverage Δ
common/meta/types/src/user_grant.rs 50% <ø> (ø)
common/meta/types/src/user_privilege.rs 33% <ø> (ø)
common/planners/src/plan_node.rs 45% <0%> (-1%) ⬇️
common/planners/src/plan_rewriter.rs 45% <0%> (-1%) ⬇️
common/planners/src/plan_show_grants.rs 0% <0%> (ø)
query/src/interpreters/interpreter_factory.rs 33% <0%> (-2%) ⬇️
query/src/interpreters/interpreter_show_grants.rs 0% <0%> (ø)
query/src/sql/sql_statement.rs 34% <0%> (-1%) ⬇️
query/src/sql/statements/analyzer_statement.rs 90% <0%> (-1%) ⬇️
query/src/sql/statements/statement_grant.rs 39% <0%> (ø)
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1f29cfc...8537e88. Read the comment docs.

@mergify
Copy link
Contributor

mergify bot commented Dec 10, 2021

This pull request has merge conflicts that must be resolved before it can be merged. @flaneur2020 please rebase it 🙏

@mergify
Copy link
Contributor

mergify bot commented Dec 12, 2021

This pull request has merge conflicts that must be resolved before it can be merged. @flaneur2020 please rebase it 🙏

@flaneur2020
Copy link
Member Author

some test in mysql:

mysql> GRANT SELECT ON db01.tb1 TO 'test-grant'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show grants for 'test-grant'@'localhost';
+-------------------------------------------------------------+
| Grants for test-grant@localhost                             |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test-grant'@'localhost'              |
| GRANT SELECT ON `db01`.* TO 'test-grant'@'localhost'        |
| GRANT SELECT ON `dbnotexists`.* TO 'test-grant'@'localhost' |
| GRANT SELECT ON `db01`.`tb1` TO 'test-grant'@'localhost'    |
+-------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> drop database db01;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW GRANTS FOR 'test-grant'@'localhost';
+--------------------------------------------------------------+
| Grants for test-grant@localhost                              |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test-grant'@'localhost'               |
| GRANT ALL PRIVILEGES ON `db01`.* TO 'test-grant'@'localhost' |
| GRANT SELECT ON `dbnotexists`.* TO 'test-grant'@'localhost'  |
+--------------------------------------------------------------+
3 rows in set (0.00 sec)

it seems mysql would not cleanup the orphan grants after deleting the database.

@flaneur2020 flaneur2020 marked this pull request as ready for review December 14, 2021 02:33
@flaneur2020 flaneur2020 changed the title WIP: Add show grants Add SHOW GRANTS [FOR xxx] statement Dec 14, 2021
@@ -15,6 +15,8 @@ GRANT SELECT ON `db01`.'tb1' TO 'test-grant'@'localhost';
GRANT SELECT ON db01.tbnotexists TO 'test-grant'@'localhost'; -- {ErrorCode 25}
GRANT SELECT ON dbnotexists.* TO 'test-grant'@'localhost'; -- {ErrorCode 3}

SHOW GRANTS FOR 'test-grant'@'localhost';

REVOKE SELECT ON db01.* FROM 'test-grant'@'localhost';
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to check the privileges again after REVOKE:
SHOW GRANTS FOR 'test-grant'@'localhost';

Copy link
Member Author

Choose a reason for hiding this comment

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

thank you, i've add two show grant statements after each revoke statement

Copy link
Member

Choose a reason for hiding this comment

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

After REVOKE SELECT ON db01.* FROM 'test-grant'@'localhost';
Seems like the grants result is not correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

after revoke SELECT ON db01.* FROM 'test-grant'@'localhost', the rest grants is

GRANT CREATE,SELECT,INSERT,SET ON 'default'.* TO 'test-grant'@'localhost'
GRANT SELECT ON 'db01'.'tb1' TO 'test-grant'@'localhost'

the privilege on 'db01'.'tb1' is not revoked by the statement REVOKE .. ON 'db01'.'*', the behaviour is counter-intuitive, but the same as mysql:

╰─$ mysql -uroot --port 3306 -hlocalhost --protocol=TCP                                                                     
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE IF NOT EXISTS `db01`;
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> CREATE TABLE IF NOT EXISTS `db01`.`tb1` (`id` NOT NULL);
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> CREATE USER 'test-grant'@'localhost' IDENTIFIED BY 'password';

mysql> GRANT SELECT ON db01.* TO 'test-grant'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT ON db01.tb1 TO 'test-grant'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'test-grant'@'localhost';
+-------------------------------------------------------------+
| Grants for test-grant@localhost                             |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test-grant'@'localhost'              |
| GRANT SELECT ON `dbnotexists`.* TO 'test-grant'@'localhost' |
| GRANT SELECT ON `db01`.* TO 'test-grant'@'localhost'        |
| GRANT SELECT ON `db01`.`tb1` TO 'test-grant'@'localhost'    |
+-------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> REVOKE SELECT ON db01.* FROM 'test-grant'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'test-grant'@'localhost';
+-------------------------------------------------------------+
| Grants for test-grant@localhost                             |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test-grant'@'localhost'              |
| GRANT SELECT ON `dbnotexists`.* TO 'test-grant'@'localhost' |
| GRANT SELECT ON `db01`.`tb1` TO 'test-grant'@'localhost'    |
+-------------------------------------------------------------+
3 rows in set (0.00 sec)

it seems grant & revoke statement matches the grant objects as key by key, but not taking the relationship between the grant objects into account. (db01.* does include db01.tb1, but revoking privileges on db01.* not revoking db01.tb1)

@BohuTANG
Copy link
Member

/lgtm

@BohuTANG BohuTANG merged commit e145aad into databendlabs:main Dec 14, 2021
@flaneur2020 flaneur2020 deleted the add-show-grants branch December 14, 2021 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-review pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add SHOW GRANTS statement
4 participants