-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
MDEV-31535: Add privilege-based fast path for SHOW DATABASES listing #5457
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
Open
itzanway
wants to merge
1
commit into
MariaDB:main
Choose a base branch
from
itzanway:MDEV-31535
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+393
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # | ||
| # MDEV-31535: Optimize directory listing for SHOW DATABASES / | ||
| # INFORMATION_SCHEMA.SCHEMATA by using a privilege-based lookup | ||
| # (get_acl_databases_for_user) instead of a full filesystem scan, | ||
| # for users without global database-listing privileges. | ||
| # | ||
| drop database if exists mdev31535a; | ||
| drop database if exists mdev31535b; | ||
| drop database if exists mdev31535pub; | ||
| drop database if exists mdev31535roledb; | ||
| drop database if exists mdev31535_wild_a; | ||
| drop database if exists mdev31535_wild_b; | ||
| create database mdev31535a; | ||
| create database mdev31535b; | ||
| create table mdev31535b.t1 (a int); | ||
| create database mdev31535_wild_a; | ||
| create database mdev31535_wild_b; | ||
| # | ||
| # Case 1: user with an explicit mysql.db grant on exactly one database | ||
| # (no wildcard metacharacter in the name) is resolved by the fast path | ||
| # and sees only that database plus information_schema. | ||
| # | ||
| create user mdev31535_u1@localhost; | ||
| grant select on mdev31535a.* to mdev31535_u1@localhost; | ||
| connect con1,localhost,mdev31535_u1,,; | ||
| connection con1; | ||
| show databases; | ||
| Database | ||
| information_schema | ||
| mdev31535a | ||
| select schema_name from information_schema.schemata; | ||
| schema_name | ||
| information_schema | ||
| mdev31535a | ||
| connection default; | ||
| disconnect con1; | ||
| # | ||
| # Case 2: user with only a table-level grant (no mysql.db row) still | ||
| # sees that database, via the column_priv_hash pass of the fast path. | ||
| # | ||
| create user mdev31535_u2@localhost; | ||
| grant select on mdev31535b.t1 to mdev31535_u2@localhost; | ||
| connect con2,localhost,mdev31535_u2,,; | ||
| connection con2; | ||
| show databases; | ||
| Database | ||
| information_schema | ||
| mdev31535b | ||
| select schema_name from information_schema.schemata; | ||
| schema_name | ||
| information_schema | ||
| mdev31535b | ||
| connection default; | ||
| disconnect con2; | ||
| # | ||
| # Case 3: user whose db grant contains an SQL wildcard ('_' or '%') can | ||
| # match an unknown set of on-disk databases, so the fast path declines | ||
| # and falls back to the legacy find_files() scan. | ||
| # | ||
| create user mdev31535_u3@localhost; | ||
| grant select on `mdev31535\_wild_%`.* to mdev31535_u3@localhost; | ||
| connect con3,localhost,mdev31535_u3,,; | ||
| connection con3; | ||
| show databases; | ||
| Database | ||
| information_schema | ||
| mdev31535_wild_a | ||
| mdev31535_wild_b | ||
| connection default; | ||
| disconnect con3; | ||
| # | ||
| # Case 4: a database granted to a role that is the user's default role | ||
| # must be visible, alongside the user's own database. This mirrors | ||
| # acl_get_all3()/check_grant_db(), which also consider the active role. | ||
| # | ||
| create role mdev31535role; | ||
| create database mdev31535roledb; | ||
| grant select on mdev31535roledb.* to mdev31535role; | ||
| create user mdev31535_u4@localhost; | ||
| grant select on mdev31535a.* to mdev31535_u4@localhost; | ||
| grant mdev31535role to mdev31535_u4@localhost; | ||
| set default role mdev31535role for mdev31535_u4@localhost; | ||
| connect con4,localhost,mdev31535_u4,,; | ||
| connection con4; | ||
| select current_role; | ||
| current_role | ||
| mdev31535role | ||
| show databases; | ||
| Database | ||
| information_schema | ||
| mdev31535a | ||
| mdev31535roledb | ||
| select schema_name from information_schema.schemata; | ||
| schema_name | ||
| information_schema | ||
| mdev31535a | ||
| mdev31535roledb | ||
| connection default; | ||
| disconnect con4; | ||
| # | ||
| # Case 5: a database granted via the PUBLIC role must be visible to any | ||
| # user through the fast path, exactly as the legacy per-database ACL | ||
| # filter (acl_get_all3) would admit it. Before this fix the fast path | ||
| # ignored PUBLIC grants and hid such databases. | ||
| # | ||
| create user mdev31535_u5@localhost; | ||
| create database mdev31535pub; | ||
| grant select on mdev31535a.* to mdev31535_u5@localhost; | ||
| grant select on mdev31535pub.* to public; | ||
| connect con5,localhost,mdev31535_u5,,; | ||
| connection con5; | ||
| show databases; | ||
| Database | ||
| information_schema | ||
| mdev31535a | ||
| mdev31535pub | ||
| select schema_name from information_schema.schemata; | ||
| schema_name | ||
| information_schema | ||
| mdev31535a | ||
| mdev31535pub | ||
| connection default; | ||
| disconnect con5; | ||
| revoke select on mdev31535pub.* from public; | ||
| drop role mdev31535role; | ||
| drop user mdev31535_u1@localhost; | ||
| drop user mdev31535_u2@localhost; | ||
| drop user mdev31535_u3@localhost; | ||
| drop user mdev31535_u4@localhost; | ||
| drop user mdev31535_u5@localhost; | ||
| drop database mdev31535a; | ||
| drop database mdev31535b; | ||
| drop database mdev31535pub; | ||
| drop database mdev31535roledb; | ||
| drop database mdev31535_wild_a; | ||
| drop database mdev31535_wild_b; | ||
| # Granting to PUBLIC auto-creates the PUBLIC role row in mysql.global_priv | ||
| # on the minimal test datadir; DROP ROLE PUBLIC is not permitted, so | ||
| # remove the leftover role directly to keep server state unchanged. | ||
| delete from mysql.global_priv where User='PUBLIC' and Host=''; | ||
| flush privileges; | ||
| # End of MDEV-31535 tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| --source include/not_embedded.inc | ||
|
|
||
| --echo # | ||
| --echo # MDEV-31535: Optimize directory listing for SHOW DATABASES / | ||
| --echo # INFORMATION_SCHEMA.SCHEMATA by using a privilege-based lookup | ||
| --echo # (get_acl_databases_for_user) instead of a full filesystem scan, | ||
| --echo # for users without global database-listing privileges. | ||
| --echo # | ||
|
|
||
| --disable_warnings | ||
| drop database if exists mdev31535a; | ||
| drop database if exists mdev31535b; | ||
| drop database if exists mdev31535pub; | ||
| drop database if exists mdev31535roledb; | ||
| drop database if exists mdev31535_wild_a; | ||
| drop database if exists mdev31535_wild_b; | ||
| --enable_warnings | ||
|
|
||
| create database mdev31535a; | ||
| create database mdev31535b; | ||
| create table mdev31535b.t1 (a int); | ||
| create database mdev31535_wild_a; | ||
| create database mdev31535_wild_b; | ||
|
|
||
| --echo # | ||
| --echo # Case 1: user with an explicit mysql.db grant on exactly one database | ||
| --echo # (no wildcard metacharacter in the name) is resolved by the fast path | ||
| --echo # and sees only that database plus information_schema. | ||
| --echo # | ||
| create user mdev31535_u1@localhost; | ||
| grant select on mdev31535a.* to mdev31535_u1@localhost; | ||
|
|
||
| connect (con1,localhost,mdev31535_u1,,); | ||
| connection con1; | ||
| --sorted_result | ||
| show databases; | ||
| --sorted_result | ||
| select schema_name from information_schema.schemata; | ||
| connection default; | ||
| disconnect con1; | ||
|
|
||
| --echo # | ||
| --echo # Case 2: user with only a table-level grant (no mysql.db row) still | ||
| --echo # sees that database, via the column_priv_hash pass of the fast path. | ||
| --echo # | ||
| create user mdev31535_u2@localhost; | ||
| grant select on mdev31535b.t1 to mdev31535_u2@localhost; | ||
|
|
||
| connect (con2,localhost,mdev31535_u2,,); | ||
| connection con2; | ||
| --sorted_result | ||
| show databases; | ||
| --sorted_result | ||
| select schema_name from information_schema.schemata; | ||
| connection default; | ||
| disconnect con2; | ||
|
|
||
| --echo # | ||
| --echo # Case 3: user whose db grant contains an SQL wildcard ('_' or '%') can | ||
| --echo # match an unknown set of on-disk databases, so the fast path declines | ||
| --echo # and falls back to the legacy find_files() scan. | ||
| --echo # | ||
| create user mdev31535_u3@localhost; | ||
| grant select on `mdev31535\_wild_%`.* to mdev31535_u3@localhost; | ||
|
|
||
| connect (con3,localhost,mdev31535_u3,,); | ||
| connection con3; | ||
| --sorted_result | ||
| show databases; | ||
| connection default; | ||
| disconnect con3; | ||
|
|
||
| --echo # | ||
| --echo # Case 4: a database granted to a role that is the user's default role | ||
| --echo # must be visible, alongside the user's own database. This mirrors | ||
| --echo # acl_get_all3()/check_grant_db(), which also consider the active role. | ||
| --echo # | ||
| create role mdev31535role; | ||
| create database mdev31535roledb; | ||
| grant select on mdev31535roledb.* to mdev31535role; | ||
| create user mdev31535_u4@localhost; | ||
| grant select on mdev31535a.* to mdev31535_u4@localhost; | ||
| grant mdev31535role to mdev31535_u4@localhost; | ||
| set default role mdev31535role for mdev31535_u4@localhost; | ||
|
|
||
| connect (con4,localhost,mdev31535_u4,,); | ||
| connection con4; | ||
| select current_role; | ||
| --sorted_result | ||
| show databases; | ||
| --sorted_result | ||
| select schema_name from information_schema.schemata; | ||
| connection default; | ||
| disconnect con4; | ||
|
|
||
| --echo # | ||
| --echo # Case 5: a database granted via the PUBLIC role must be visible to any | ||
| --echo # user through the fast path, exactly as the legacy per-database ACL | ||
| --echo # filter (acl_get_all3) would admit it. Before this fix the fast path | ||
| --echo # ignored PUBLIC grants and hid such databases. | ||
| --echo # | ||
| create user mdev31535_u5@localhost; | ||
| create database mdev31535pub; | ||
| grant select on mdev31535a.* to mdev31535_u5@localhost; | ||
| grant select on mdev31535pub.* to public; | ||
|
|
||
| connect (con5,localhost,mdev31535_u5,,); | ||
| connection con5; | ||
| --sorted_result | ||
| show databases; | ||
| --sorted_result | ||
| select schema_name from information_schema.schemata; | ||
| connection default; | ||
| disconnect con5; | ||
|
|
||
| revoke select on mdev31535pub.* from public; | ||
|
|
||
| drop role mdev31535role; | ||
| drop user mdev31535_u1@localhost; | ||
| drop user mdev31535_u2@localhost; | ||
| drop user mdev31535_u3@localhost; | ||
| drop user mdev31535_u4@localhost; | ||
| drop user mdev31535_u5@localhost; | ||
| drop database mdev31535a; | ||
| drop database mdev31535b; | ||
| drop database mdev31535pub; | ||
| drop database mdev31535roledb; | ||
| drop database mdev31535_wild_a; | ||
| drop database mdev31535_wild_b; | ||
|
|
||
| --echo # Granting to PUBLIC auto-creates the PUBLIC role row in mysql.global_priv | ||
| --echo # on the minimal test datadir; DROP ROLE PUBLIC is not permitted, so | ||
| --echo # remove the leftover role directly to keep server state unchanged. | ||
| delete from mysql.global_priv where User='PUBLIC' and Host=''; | ||
| flush privileges; | ||
|
|
||
| --echo # End of MDEV-31535 tests | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but how we could verify that fast path was used and cases where not it was not used?
Maybe need again after select schema_name from information_schema.schemata; additional
explain format=json ...; so that it would show fast path in some field...
Check also if you do select schema_name from information_schema.schemata more than once does result come from query_cache or is same code executed again.