Skip to content

Commit 99c76c1

Browse files
author
Joshua Spence
committed
Allow authentication plugin to be changed
Currently changing the `plugin` property of the `mysql_user` resource has no effect.
1 parent 9419870 commit 99c76c1

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

lib/puppet/provider/mysql_user/mysql.rb

+17
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ def max_updates_per_hour=(int)
159159
(max_updates_per_hour == int) ? (return true) : (return false)
160160
end
161161

162+
def plugin=(string)
163+
merged_name = self.class.cmd_user(@resource[:name])
164+
165+
if (mysqld_type == 'mysql' || mysqld_type == 'percona') && Puppet::Util::Package.versioncmp(mysqld_version, '5.7.6') >= 0
166+
sql = "ALTER USER #{merged_name} IDENTIFIED WITH '#{string}'"
167+
sql << " AS '#{@resource[:password_hash]}'" if string == 'mysql_native_password'
168+
else
169+
# See https://bugs.mysql.com/bug.php?id=67449
170+
sql = "UPDATE mysql.user SET plugin = '#{string}'"
171+
sql << ((string == 'mysql_native_password') ? ", password = '#{@resource[:password_hash]}'" : ", password = ''")
172+
sql << " WHERE CONCAT(user, '@', host) = '#{@resource[:name]}'"
173+
end
174+
175+
mysql([defaults_file, system_database, '-e', sql].compact)
176+
(plugin == string) ? (return true) : (return false)
177+
end
178+
162179
def tls_options=(array)
163180
merged_name = self.class.cmd_user(@resource[:name])
164181
merged_tls_options = array.join(' AND ')

spec/acceptance/types/mysql_user_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ class { 'mysql::server': }
3636
end
3737
end
3838
end
39+
40+
describe 'changing authentication plugin' do
41+
it 'should work without errors' do
42+
pp = <<-EOS
43+
mysql_user { 'ashp@localhost':
44+
plugin => 'auth_socket',
45+
}
46+
EOS
47+
48+
apply_manifest(pp, :catch_failures => true)
49+
end
50+
51+
it 'should have correct plugin' do
52+
shell("mysql -NBe \"select plugin from mysql.user where CONCAT(user, '@', host) = 'ashp@localhost'\"") do |r|
53+
expect(r.stdout.rstrip).to eq('auth_socket')
54+
expect(r.stderr).to be_empty
55+
end
56+
end
57+
58+
it 'should not have a password' do
59+
shell("mysql -NBe \"select password from mysql.user where CONCAT(user, '@', host) = 'ashp@localhost'\"") do |r|
60+
expect(r.stdout.rstrip).to be_empty
61+
expect(r.stderr).to be_empty
62+
end
63+
end
64+
end
3965
end
4066

4167
context 'using ashp-dash@localhost' do

spec/unit/puppet/provider/mysql_user/mysql_spec.rb

+46
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,52 @@
286286
end
287287
end
288288

289+
describe 'plugin=' do
290+
context 'auth_socket' do
291+
context 'MySQL < 5.7.6' do
292+
it 'changes the authentication plugin' do
293+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.1'][:string])
294+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "UPDATE mysql.user SET plugin = 'auth_socket', password = '' WHERE CONCAT(user, '@', host) = 'joe@localhost'"]).returns('0')
295+
296+
provider.expects(:plugin).returns('auth_socket')
297+
provider.plugin = 'auth_socket'
298+
end
299+
end
300+
301+
context 'MySQL >= 5.7.6' do
302+
it 'changes the authentication plugin' do
303+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.6'][:string])
304+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "ALTER USER 'joe'@'localhost' IDENTIFIED WITH 'auth_socket'"]).returns('0')
305+
306+
provider.expects(:plugin).returns('auth_socket')
307+
provider.plugin = 'auth_socket'
308+
end
309+
end
310+
end
311+
312+
context 'mysql_native_password' do
313+
context 'MySQL < 5.7.6' do
314+
it 'changes the authentication plugin' do
315+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.1'][:string])
316+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "UPDATE mysql.user SET plugin = 'mysql_native_password', password = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' WHERE CONCAT(user, '@', host) = 'joe@localhost'"]).returns('0')
317+
318+
provider.expects(:plugin).returns('mysql_native_password')
319+
provider.plugin = 'mysql_native_password'
320+
end
321+
end
322+
323+
context 'MySQL >= 5.7.6' do
324+
it 'changes the authentication plugin' do
325+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.6'][:string])
326+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "ALTER USER 'joe'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"]).returns('0')
327+
328+
provider.expects(:plugin).returns('mysql_native_password')
329+
provider.plugin = 'mysql_native_password'
330+
end
331+
end
332+
end
333+
end
334+
289335
describe 'tls_options=' do
290336
it 'adds SSL option grant in mysql 5.5' do
291337
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.5'][:string])

0 commit comments

Comments
 (0)