Skip to content

Commit 760e859

Browse files
committed
Merge pull request #1004 from joshuaspence/changeplugin
Allow authentication plugin to be changed
2 parents d2f3899 + 99c76c1 commit 760e859

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

lib/puppet/provider/mysql_user/mysql.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ def max_updates_per_hour=(int)
158158
(max_updates_per_hour == int) ? (return true) : (return false)
159159
end
160160

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

spec/acceptance/types/mysql_user_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ class { 'mysql::server': }
4343
end
4444
end
4545
end
46+
47+
describe 'changing authentication plugin' do
48+
it 'should work without errors' do
49+
pp = <<-EOS
50+
mysql_user { 'ashp@localhost':
51+
plugin => 'auth_socket',
52+
}
53+
EOS
54+
55+
apply_manifest(pp, :catch_failures => true)
56+
end
57+
58+
it 'should have correct plugin' do
59+
shell("mysql -NBe \"select plugin from mysql.user where CONCAT(user, '@', host) = 'ashp@localhost'\"") do |r|
60+
expect(r.stdout.rstrip).to eq('auth_socket')
61+
expect(r.stderr).to be_empty
62+
end
63+
end
64+
65+
it 'should not have a password' do
66+
shell("mysql -NBe \"select password from mysql.user where CONCAT(user, '@', host) = 'ashp@localhost'\"") do |r|
67+
expect(r.stdout.rstrip).to be_empty
68+
expect(r.stderr).to be_empty
69+
end
70+
end
71+
end
4672
end
4773

4874
context 'using ashp-dash@localhost' do

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,52 @@
277277
end
278278
end
279279

280+
describe 'plugin=' do
281+
context 'auth_socket' do
282+
context 'MySQL < 5.7.6' do
283+
it 'changes the authentication plugin' do
284+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.1'][:string])
285+
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')
286+
287+
provider.expects(:plugin).returns('auth_socket')
288+
provider.plugin = 'auth_socket'
289+
end
290+
end
291+
292+
context 'MySQL >= 5.7.6' do
293+
it 'changes the authentication plugin' do
294+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.6'][:string])
295+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "ALTER USER 'joe'@'localhost' IDENTIFIED WITH 'auth_socket'"]).returns('0')
296+
297+
provider.expects(:plugin).returns('auth_socket')
298+
provider.plugin = 'auth_socket'
299+
end
300+
end
301+
end
302+
303+
context 'mysql_native_password' do
304+
context 'MySQL < 5.7.6' do
305+
it 'changes the authentication plugin' do
306+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.1'][:string])
307+
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')
308+
309+
provider.expects(:plugin).returns('mysql_native_password')
310+
provider.plugin = 'mysql_native_password'
311+
end
312+
end
313+
314+
context 'MySQL >= 5.7.6' do
315+
it 'changes the authentication plugin' do
316+
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.7.6'][:string])
317+
provider.expects(:mysql).with([defaults_file, system_database, '-e', "ALTER USER 'joe'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"]).returns('0')
318+
319+
provider.expects(:plugin).returns('mysql_native_password')
320+
provider.plugin = 'mysql_native_password'
321+
end
322+
end
323+
end
324+
end
325+
280326
describe 'tls_options=' do
281327
it 'adds SSL option grant in mysql 5.5' do
282328
provider.class.instance_variable_set(:@mysqld_version_string, mysql_version_string_hash['mysql-5.5'][:string])

0 commit comments

Comments
 (0)