Skip to content

Commit 7a026a9

Browse files
authored
Merge pull request #1427 from netlogix/bugfix/mysql8-idempodent-grants
MODULES-8373 Fix mysql_grant resource to be idempodent on MySQL 8+
2 parents cb97c87 + 76e901f commit 7a026a9

File tree

7 files changed

+32
-46
lines changed

7 files changed

+32
-46
lines changed

lib/puppet/provider/mysql_grant/mysql.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def self.instances
4545
(priv == 'ALL PRIVILEGES') ? 'ALL' : priv.strip
4646
end
4747
end
48+
sorted_privileges = stripped_privileges.sort
49+
if newer_than('mysql' => '8.0.0') && sorted_privileges == ['ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROLE', 'CREATE ROUTINE', 'CREATE TABLESPACE', 'CREATE TEMPORARY TABLES', 'CREATE USER',
50+
'CREATE VIEW', 'DELETE', 'DROP', 'DROP ROLE', 'EVENT', 'EXECUTE', 'FILE', 'INDEX', 'INSERT', 'LOCK TABLES', 'PROCESS', 'REFERENCES',
51+
'RELOAD', 'REPLICATION CLIENT', 'REPLICATION SLAVE', 'SELECT', 'SHOW DATABASES', 'SHOW VIEW', 'SHUTDOWN', 'SUPER', 'TRIGGER',
52+
'UPDATE']
53+
sorted_privileges = ['ALL']
54+
end
4855
# Same here, but to remove OPTION leaving just GRANT.
4956
options = if %r{WITH\sGRANT\sOPTION}.match?(rest)
5057
['GRANT']
@@ -57,7 +64,7 @@ def self.instances
5764
instances << new(
5865
name: "#{user}@#{host}/#{table}",
5966
ensure: :present,
60-
privileges: stripped_privileges.sort,
67+
privileges: sorted_privileges,
6168
table: table,
6269
user: "#{user}@#{host}",
6370
options: options,

spec/acceptance/00_mysql_server_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class { 'mysql::server':
3939
databases => {
4040
'somedb' => {
4141
ensure => 'present',
42-
charset => 'utf8',
42+
charset => #{fetch_charset},
4343
},
4444
}
4545
}

spec/acceptance/01_mysql_db_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class { 'mysql::server':
1414
mysql::db { 'spec1':
1515
user => 'root1',
1616
password => 'password',
17+
charset => #{fetch_charset},
1718
}
1819
MANIFEST
1920
end
@@ -42,6 +43,7 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
4243
user => 'root1',
4344
password => 'password',
4445
sql => '/tmp/spec.sql',
46+
charset => #{fetch_charset},
4547
}
4648
MANIFEST
4749
end
@@ -66,6 +68,7 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
6668
user => 'root1',
6769
password => 'password',
6870
dbname => 'realdb',
71+
charset => #{fetch_charset},
6972
}
7073
MANIFEST
7174
end

spec/acceptance/04_mysql_backup_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class { 'mysql::server': root_password => 'password' }
1212
]:
1313
user => 'backup',
1414
password => 'secret',
15+
charset => #{fetch_charset},
1516
}
1617
1718
class { 'mysql::server::backup':
@@ -72,6 +73,7 @@ class { 'mysql::server': root_password => 'password' }
7273
]:
7374
user => 'backup',
7475
password => 'secret',
76+
charset => #{fetch_charset},
7577
}
7678
7779
class { 'mysql::server::backup':
@@ -136,6 +138,7 @@ class { 'mysql::server': root_password => 'password' }
136138
]:
137139
user => 'backup',
138140
password => 'secret',
141+
charset => #{fetch_charset},
139142
}
140143
case $facts['os']['family'] {
141144
/Debian/: {
@@ -260,6 +263,7 @@ class { 'mysql::server': root_password => 'password' }
260263
]:
261264
user => 'backup',
262265
password => 'secret',
266+
charset => #{fetch_charset},
263267
}
264268
case $facts['os']['family'] {
265269
/Debian/: {

spec/acceptance/types/mysql_database_spec.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class { 'mysql::server': }
1515
describe 'creating database' do
1616
pp = <<-MANIFEST
1717
mysql_database { 'spec_db':
18-
ensure => present,
18+
ensure => present,
19+
charset => #{fetch_charset},
1920
}
2021
MANIFEST
2122
it 'works without errors' do
@@ -37,7 +38,7 @@ class { 'mysql::server': }
3738
collate => 'latin1_swedish_ci',
3839
}
3940
mysql_database { 'spec_utf8':
40-
charset => 'utf8',
41+
charset => #{fetch_charset},
4142
collate => 'utf8_general_ci',
4243
}
4344
MANIFEST
@@ -54,7 +55,7 @@ class { 'mysql::server': }
5455

5556
it 'finds utf8 db #stdout' do
5657
run_shell("mysql -NBe \"SHOW VARIABLES LIKE '%_database'\" spec_utf8") do |r|
57-
expect(r.stdout).to match(%r{^character_set_database\tutf8\ncollation_database\tutf8_general_ci$})
58+
expect(r.stdout).to match(%r{^character_set_database\tutf8(mb3)?\ncollation_database\tutf8_general_ci$})
5859
expect(r.stderr).to be_empty
5960
end
6061
end

spec/acceptance/types/mysql_grant_spec.rb

+4-41
Original file line numberDiff line numberDiff line change
@@ -268,51 +268,13 @@ class { 'mysql::server':
268268
end
269269
end
270270

271-
# On Ubuntu 20.04 'ALL' now returns as the sum of it's constitute parts and so require a specific test
272-
describe 'ALL privilege on newer MySQL versions', if: os[:family] == 'ubuntu' && os[:release] =~ %r{^20\.04} do
273-
pp_one = <<-MANIFEST
274-
mysql_user { 'all@localhost':
275-
ensure => present,
276-
}
277-
mysql_grant { 'all@localhost/*.*':
278-
user => 'all@localhost',
279-
privileges => ['ALL'],
280-
table => '*.*',
281-
require => Mysql_user['all@localhost'],
282-
}
283-
MANIFEST
284-
it "create ['ALL'] privs" do
285-
apply_manifest(pp_one, catch_failures: true)
286-
end
287-
288-
pp_two = <<-MANIFEST
289-
mysql_user { 'all@localhost':
290-
ensure => present,
291-
}
292-
mysql_grant { 'all@localhost/*.*':
293-
user => 'all@localhost',
294-
privileges => ['ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROLE', 'CREATE ROUTINE', 'CREATE TABLESPACE', 'CREATE TEMPORARY TABLES', 'CREATE USER', 'CREATE VIEW', 'DELETE', 'DROP', 'DROP ROLE', 'EVENT', 'EXECUTE', 'FILE', 'INDEX', 'INSERT', 'LOCK TABLES', 'PROCESS', 'REFERENCES', 'RELOAD', 'REPLICATION CLIENT', 'REPLICATION SLAVE', 'SELECT', 'SHOW DATABASES', 'SHOW VIEW', 'SHUTDOWN', 'SUPER', 'TRIGGER', 'UPDATE'],
295-
table => '*.*',
296-
require => Mysql_user['all@localhost'],
297-
}
298-
MANIFEST
299-
it "create ['ALL'] constitute parts privs" do
300-
apply_manifest(pp_two, catch_changes: true)
301-
end
302-
end
303-
304271
describe 'complex test' do
305-
# On Ubuntu 20.04 'ALL' now returns as the sum of it's constitute parts and so is no longer idempotent when set
306-
privileges = if os[:family] == 'ubuntu' && os[:release] =~ %r{^20\.04}
307-
"['SELECT', 'INSERT', 'UPDATE']"
308-
else
309-
"['ALL']"
310-
end
311272
pp = <<-MANIFEST
312273
$dbSubnet = '10.10.10.%'
313274
314275
mysql_database { 'foo':
315-
ensure => present,
276+
ensure => present,
277+
charset => '#{fetch_charset}',
316278
}
317279
318280
exec { 'mysql-create-table':
@@ -325,7 +287,7 @@ class { 'mysql::server':
325287
Mysql_grant {
326288
ensure => present,
327289
options => ['GRANT'],
328-
privileges => #{privileges},
290+
privileges => ['ALL'],
329291
table => '*.*',
330292
require => [ Mysql_database['foo'], Exec['mysql-create-table'] ],
331293
}
@@ -724,6 +686,7 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
724686
user => 'root1',
725687
password => 'password',
726688
sql => '/tmp/grant_spec_table.sql',
689+
charset => #{fetch_charset},
727690
}
728691
MANIFEST
729692
it 'creates table' do

spec/spec_helper_acceptance_local.rb

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def export_locales
3232
LitmusHelper.instance.run_shell('source ~/.bashrc')
3333
end
3434

35+
def fetch_charset
36+
@charset ||= if os[:family] == 'ubuntu' && os[:release] =~ %r{^20\.04}
37+
'utf8mb3'
38+
else
39+
'utf8'
40+
end
41+
end
42+
3543
RSpec.configure do |c|
3644
c.before :suite do
3745
if os[:family] == 'debian' || os[:family] == 'ubuntu'

0 commit comments

Comments
 (0)