Skip to content

Commit e9572e5

Browse files
HugoWenTDgrooverdan
authored andcommitted
MDEV-27124: Update definer of Add/DropGeometryColumn procedures from 'root' to 'mariadb.sys'
From 10.4.13, the `mariadb.sys` user was created to replace `root` definers. - In commit 0253ea7, definer of Add/DropGeometryColumn procedures was changed to `mariadb.sys`, in `scripts/maria_add_gis_sp.sql.in`. However, maria_add_gis_sp.sql only applies to new databases created by installation script. Databases upgraded from old versions will miss this change. - In addition, according to commit 0d6d801(MDEV-23102), in some scenarios when root user is replaced it will skip creating `mariadb.sys` user. This commit is to update the definer from `root` to `mariadb.sys` during upgrade. It only makes the change if the original definers are root. Doesn't choose to execute `maria_add_gis_sp.sql` in upgrade script to recreate the procedures is because of considering the scenarios of MDEV-23102 that `root` user is replaced and `mariadb.sys` is not created. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.
1 parent b53ee76 commit e9572e5

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#
2+
# The definer of Add/DropGeometryColumn procedures should be updated to mariadb.sys during upgrade
3+
#
4+
use mysql;
5+
create table save_proc like proc;
6+
insert into save_proc select * from proc;
7+
set @save_sql_mode= @@sql_mode;
8+
#
9+
# If the definer is root before the upgrade:
10+
# Drop the procedures if exists and recreate with root definer
11+
#
12+
DROP PROCEDURE IF EXISTS AddGeometryColumn;
13+
DROP PROCEDURE IF EXISTS DropGeometryColumn;
14+
CREATE DEFINER=`root`@`localhost` PROCEDURE AddGeometryColumn(catalog varchar(64), t_schema varchar(64),
15+
t_name varchar(64), geometry_column varchar(64), t_srid int) SQL SECURITY INVOKER
16+
begin
17+
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; execute ls; deallocate prepare ls; end |
18+
CREATE DEFINER=`root`@`localhost` PROCEDURE DropGeometryColumn(catalog varchar(64), t_schema varchar(64),
19+
t_name varchar(64), geometry_column varchar(64)) SQL SECURITY INVOKER
20+
begin
21+
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; end |
22+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'root@localhost';
23+
count(*)
24+
1
25+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'root@localhost';
26+
count(*)
27+
1
28+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
29+
count(*)
30+
0
31+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
32+
count(*)
33+
0
34+
#
35+
# Run mysql_upgrade
36+
#
37+
Phase 1/7: Checking and upgrading mysql database
38+
Processing databases
39+
mysql
40+
mysql.column_stats OK
41+
mysql.columns_priv OK
42+
mysql.db OK
43+
mysql.event OK
44+
mysql.func OK
45+
mysql.global_priv OK
46+
mysql.gtid_slave_pos OK
47+
mysql.help_category OK
48+
mysql.help_keyword OK
49+
mysql.help_relation OK
50+
mysql.help_topic OK
51+
mysql.index_stats OK
52+
mysql.innodb_index_stats
53+
Error : Unknown storage engine 'InnoDB'
54+
error : Corrupt
55+
mysql.innodb_table_stats
56+
Error : Unknown storage engine 'InnoDB'
57+
error : Corrupt
58+
mysql.plugin OK
59+
mysql.proc OK
60+
mysql.procs_priv OK
61+
mysql.proxies_priv OK
62+
mysql.roles_mapping OK
63+
mysql.save_proc OK
64+
mysql.servers OK
65+
mysql.table_stats OK
66+
mysql.tables_priv OK
67+
mysql.time_zone OK
68+
mysql.time_zone_leap_second OK
69+
mysql.time_zone_name OK
70+
mysql.time_zone_transition OK
71+
mysql.time_zone_transition_type OK
72+
mysql.transaction_registry
73+
Error : Unknown storage engine 'InnoDB'
74+
error : Corrupt
75+
76+
Repairing tables
77+
mysql.innodb_index_stats
78+
Error : Unknown storage engine 'InnoDB'
79+
error : Corrupt
80+
mysql.innodb_table_stats
81+
Error : Unknown storage engine 'InnoDB'
82+
error : Corrupt
83+
mysql.transaction_registry
84+
Error : Unknown storage engine 'InnoDB'
85+
error : Corrupt
86+
Phase 2/7: Installing used storage engines... Skipped
87+
Phase 3/7: Fixing views
88+
mysql.user OK
89+
Phase 4/7: Running 'mysql_fix_privilege_tables'
90+
Phase 5/7: Fixing table and database names
91+
Phase 6/7: Checking and upgrading tables
92+
Processing databases
93+
information_schema
94+
mtr
95+
mtr.global_suppressions OK
96+
mtr.test_suppressions OK
97+
performance_schema
98+
test
99+
Phase 7/7: Running 'FLUSH PRIVILEGES'
100+
OK
101+
#
102+
# check new definers of Add/DropGeometryColumn
103+
#
104+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'root@localhost';
105+
count(*)
106+
0
107+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'root@localhost';
108+
count(*)
109+
0
110+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
111+
count(*)
112+
1
113+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
114+
count(*)
115+
1
116+
#
117+
# restore environment
118+
#
119+
delete from proc;
120+
rename table proc to bad_proc;
121+
rename table save_proc to proc;
122+
drop table bad_proc;
123+
flush privileges;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--echo #
2+
--echo # The definer of Add/DropGeometryColumn procedures should be updated to mariadb.sys during upgrade
3+
--echo #
4+
5+
--source include/mysql_upgrade_preparation.inc
6+
7+
use mysql;
8+
9+
create table save_proc like proc;
10+
insert into save_proc select * from proc;
11+
set @save_sql_mode= @@sql_mode;
12+
13+
--echo #
14+
--echo # If the definer is root before the upgrade:
15+
--echo # Drop the procedures if exists and recreate with root definer
16+
--echo #
17+
DROP PROCEDURE IF EXISTS AddGeometryColumn;
18+
DROP PROCEDURE IF EXISTS DropGeometryColumn;
19+
20+
DELIMITER |;
21+
22+
CREATE DEFINER=`root`@`localhost` PROCEDURE AddGeometryColumn(catalog varchar(64), t_schema varchar(64),
23+
t_name varchar(64), geometry_column varchar(64), t_srid int) SQL SECURITY INVOKER
24+
begin
25+
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; execute ls; deallocate prepare ls; end |
26+
27+
CREATE DEFINER=`root`@`localhost` PROCEDURE DropGeometryColumn(catalog varchar(64), t_schema varchar(64),
28+
t_name varchar(64), geometry_column varchar(64)) SQL SECURITY INVOKER
29+
begin
30+
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; end |
31+
32+
DELIMITER ;|
33+
34+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'root@localhost';
35+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'root@localhost';
36+
37+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
38+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
39+
40+
let $MYSQLD_DATADIR= `select @@datadir`;
41+
42+
--echo #
43+
--echo # Run mysql_upgrade
44+
--echo #
45+
--exec $MYSQL_UPGRADE 2>&1
46+
--file_exists $MYSQLD_DATADIR/mysql_upgrade_info
47+
--remove_file $MYSQLD_DATADIR/mysql_upgrade_info
48+
49+
--echo #
50+
--echo # check new definers of Add/DropGeometryColumn
51+
--echo #
52+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'root@localhost';
53+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'root@localhost';
54+
55+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='AddGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
56+
SELECT count(*) FROM information_schema.ROUTINES WHERE ROUTINE_CATALOG = 'def' and ROUTINE_SCHEMA = 'mysql' and ROUTINE_NAME='DropGeometryColumn' and DEFINER = 'mariadb.sys@localhost';
57+
58+
--echo #
59+
--echo # restore environment
60+
--echo #
61+
delete from proc;
62+
rename table proc to bad_proc;
63+
rename table save_proc to proc;
64+
drop table bad_proc;
65+
flush privileges;
66+

scripts/mysql_system_tables_fix.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ ALTER TABLE proc MODIFY comment
510510
ALTER TABLE proc ADD aggregate enum('NONE', 'GROUP') DEFAULT 'NONE' NOT NULL
511511
AFTER body_utf8;
512512

513+
# Update definer of Add/DropGeometryColumn procedures to 'mariadb.sys'
514+
# To consider the scenarios in MDEV-23102, only update the definer when it's 'root'
515+
UPDATE proc SET Definer = 'mariadb.sys@localhost' WHERE Definer = 'root@localhost' AND Name = 'AddGeometryColumn';
516+
UPDATE proc SET Definer = 'mariadb.sys@localhost' WHERE Definer = 'root@localhost' AND Name = 'DropGeometryColumn';
517+
513518
#
514519
# EVENT privilege
515520
#

0 commit comments

Comments
 (0)