Skip to content

Commit c4bef7a

Browse files
committed
MDEV-9443: Roles aren't supported in prepared statements
Make role statements work with the PREPARE keyword.
1 parent 16ddd18 commit c4bef7a

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# Test user to check if we can grant the created role to it.
3+
#
4+
create user test_user;
5+
#
6+
# First create the role.
7+
#
8+
SET @createRole = 'CREATE ROLE developers';
9+
PREPARE stmtCreateRole FROM @createRole;
10+
EXECUTE stmtCreateRole;
11+
#
12+
# Test to see if the role is created.
13+
#
14+
SELECT user, host,is_role FROM mysql.user
15+
WHERE user = 'developers';
16+
user host is_role
17+
developers Y
18+
SHOW GRANTS;
19+
Grants for root@localhost
20+
GRANT developers TO 'root'@'localhost' WITH ADMIN OPTION
21+
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
22+
GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION
23+
#
24+
# Now grant the role to the test user.
25+
#
26+
SET @grantRole = 'GRANT developers to test_user';
27+
PREPARE stmtGrantRole FROM @grantRole;
28+
EXECUTE stmtGrantRole;
29+
#
30+
# We should see 2 entries in the roles_mapping table.
31+
#
32+
SELECT * FROM mysql.roles_mapping;
33+
Host User Role Admin_option
34+
% test_user developers N
35+
localhost root developers Y
36+
SHOW GRANTS FOR test_user;
37+
Grants for test_user@%
38+
GRANT developers TO 'test_user'@'%'
39+
GRANT USAGE ON *.* TO 'test_user'@'%'
40+
#
41+
# Now drop the role.
42+
#
43+
SET @dropRole = 'DROP ROLE developers';
44+
PREPARE stmtDropRole FROM @dropRole;
45+
EXECUTE stmtDropRole;
46+
#
47+
# Check both user and roles_mapping table for traces of our role.
48+
#
49+
SELECT user, host,is_role FROM mysql.user
50+
WHERE user = 'developers';
51+
user host is_role
52+
SELECT * FROM mysql.roles_mapping;
53+
Host User Role Admin_option
54+
SHOW GRANTS;
55+
Grants for root@localhost
56+
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
57+
GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION
58+
SHOW GRANTS FOR test_user;
59+
Grants for test_user@%
60+
GRANT USAGE ON *.* TO 'test_user'@'%'
61+
# Cleanup.
62+
DROP USER test_user;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--source include/not_embedded.inc
2+
3+
4+
--echo #
5+
--echo # Test user to check if we can grant the created role to it.
6+
--echo #
7+
create user test_user;
8+
--echo #
9+
--echo # First create the role.
10+
--echo #
11+
SET @createRole = 'CREATE ROLE developers';
12+
PREPARE stmtCreateRole FROM @createRole;
13+
EXECUTE stmtCreateRole;
14+
--echo #
15+
--echo # Test to see if the role is created.
16+
--echo #
17+
SELECT user, host,is_role FROM mysql.user
18+
WHERE user = 'developers';
19+
SHOW GRANTS;
20+
21+
--echo #
22+
--echo # Now grant the role to the test user.
23+
--echo #
24+
SET @grantRole = 'GRANT developers to test_user';
25+
PREPARE stmtGrantRole FROM @grantRole;
26+
EXECUTE stmtGrantRole;
27+
28+
--echo #
29+
--echo # We should see 2 entries in the roles_mapping table.
30+
--echo #
31+
--sorted_result
32+
SELECT * FROM mysql.roles_mapping;
33+
SHOW GRANTS FOR test_user;
34+
35+
--echo #
36+
--echo # Now drop the role.
37+
--echo #
38+
SET @dropRole = 'DROP ROLE developers';
39+
PREPARE stmtDropRole FROM @dropRole;
40+
EXECUTE stmtDropRole;
41+
42+
--echo #
43+
--echo # Check both user and roles_mapping table for traces of our role.
44+
--echo #
45+
SELECT user, host,is_role FROM mysql.user
46+
WHERE user = 'developers';
47+
SELECT * FROM mysql.roles_mapping;
48+
SHOW GRANTS;
49+
SHOW GRANTS FOR test_user;
50+
51+
--echo # Cleanup.
52+
DROP USER test_user;

sql/sql_prepare.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,9 +2458,12 @@ static bool check_prepared_statement(Prepared_statement *stmt)
24582458
case SQLCOM_CREATE_USER:
24592459
case SQLCOM_RENAME_USER:
24602460
case SQLCOM_DROP_USER:
2461+
case SQLCOM_CREATE_ROLE:
2462+
case SQLCOM_DROP_ROLE:
24612463
case SQLCOM_ASSIGN_TO_KEYCACHE:
24622464
case SQLCOM_PRELOAD_KEYS:
24632465
case SQLCOM_GRANT:
2466+
case SQLCOM_GRANT_ROLE:
24642467
case SQLCOM_REVOKE:
24652468
case SQLCOM_KILL:
24662469
case SQLCOM_COMPOUND:

0 commit comments

Comments
 (0)