Skip to content

Commit bcb6150

Browse files
committed
Merge pull request #640 from dveeden/auth-plugins
Support for authentication plugins
2 parents 9f5539c + 305b0d2 commit bcb6150

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

lib/puppet/provider/mysql_user/mysql.rb

+17-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ def self.instances
1212
# To reduce the number of calls to MySQL we collect all the properties in
1313
# one big swoop.
1414
users.collect do |name|
15-
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
15+
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD, PLUGIN FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
1616
@max_user_connections, @max_connections_per_hour, @max_queries_per_hour,
17-
@max_updates_per_hour, @password = mysql([defaults_file, "-NBe", query].compact).split(/\s/)
17+
@max_updates_per_hour, @password, @plugin = mysql([defaults_file, "-NBe", query].compact).split(/\s/)
1818

1919
new(:name => name,
2020
:ensure => :present,
2121
:password_hash => @password,
22+
:plugin => @plugin,
2223
:max_user_connections => @max_user_connections,
2324
:max_connections_per_hour => @max_connections_per_hour,
2425
:max_queries_per_hour => @max_queries_per_hour,
@@ -39,17 +40,26 @@ def self.prefetch(resources)
3940
end
4041

4142
def create
42-
merged_name = @resource[:name].sub('@', "'@'")
43+
merged_name = @resource[:name].sub('@', "'@'")
4344
password_hash = @resource.value(:password_hash)
45+
plugin = @resource.value(:plugin)
4446
max_user_connections = @resource.value(:max_user_connections) || 0
4547
max_connections_per_hour = @resource.value(:max_connections_per_hour) || 0
4648
max_queries_per_hour = @resource.value(:max_queries_per_hour) || 0
4749
max_updates_per_hour = @resource.value(:max_updates_per_hour) || 0
4850

49-
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' IDENTIFIED BY PASSWORD '#{password_hash}' WITH MAX_USER_CONNECTIONS #{max_user_connections} MAX_CONNECTIONS_PER_HOUR #{max_connections_per_hour} MAX_QUERIES_PER_HOUR #{max_queries_per_hour} MAX_UPDATES_PER_HOUR #{max_updates_per_hour}"].compact)
50-
51-
@property_hash[:ensure] = :present
52-
@property_hash[:password_hash] = password_hash
51+
# Use CREATE USER to be compatible with NO_AUTO_CREATE_USER sql_mode
52+
# This is also required if you want to specify a authentication plugin
53+
if !plugin.nil?
54+
mysql([defaults_file, '-e', "CREATE USER '#{merged_name}' IDENTIFIED WITH '#{plugin}'"].compact)
55+
@property_hash[:ensure] = :present
56+
@property_hash[:plugin] = plugin
57+
else
58+
mysql([defaults_file, '-e', "CREATE USER '#{merged_name}' IDENTIFIED BY PASSWORD '#{password_hash}'"].compact)
59+
@property_hash[:ensure] = :present
60+
@property_hash[:password_hash] = password_hash
61+
end
62+
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_USER_CONNECTIONS #{max_user_connections} MAX_CONNECTIONS_PER_HOUR #{max_connections_per_hour} MAX_QUERIES_PER_HOUR #{max_queries_per_hour} MAX_UPDATES_PER_HOUR #{max_updates_per_hour}"].compact)
5363
@property_hash[:max_user_connections] = max_user_connections
5464
@property_hash[:max_connections_per_hour] = max_connections_per_hour
5565
@property_hash[:max_queries_per_hour] = max_queries_per_hour

lib/puppet/type/mysql_user.rb

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
newvalue(/\w+/)
4141
end
4242

43+
newproperty(:plugin) do
44+
desc 'The authentication plugin of the user.'
45+
newvalue(/\w+/)
46+
end
47+
4348
newproperty(:max_user_connections) do
4449
desc "Max concurrent connections for the user. 0 means no (or global) limit."
4550
newvalue(/\d+/)

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
Puppet::Util.stubs(:which).with('mysql').returns('/usr/bin/mysql')
3838
File.stubs(:file?).with('/root/.my.cnf').returns(true)
3939
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT CONCAT(User, '@',Host) AS User FROM mysql.user"]).returns('joe@localhost')
40-
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD FROM mysql.user WHERE CONCAT(user, '@', host) = 'joe@localhost'"]).returns('10 10 10 10 *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4')
40+
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD, PLUGIN FROM mysql.user WHERE CONCAT(user, '@', host) = 'joe@localhost'"]).returns('10 10 10 10 *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4')
4141
end
4242

4343
let(:instance) { provider.class.instances.first }
@@ -46,7 +46,7 @@
4646
it 'returns an array of users' do
4747
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT CONCAT(User, '@',Host) AS User FROM mysql.user"]).returns(raw_users)
4848
parsed_users.each do |user|
49-
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD FROM mysql.user WHERE CONCAT(user, '@', host) = '#{user}'"]).returns('10 10 10 10 ')
49+
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD, PLUGIN FROM mysql.user WHERE CONCAT(user, '@', host) = '#{user}'"]).returns('10 10 10 10 ')
5050
end
5151

5252
usernames = provider.class.instances.collect {|x| x.name }
@@ -63,7 +63,8 @@
6363

6464
describe 'create' do
6565
it 'makes a user' do
66-
provider.expects(:mysql).with([defaults_file, '-e', "GRANT USAGE ON *.* TO 'joe'@'localhost' IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' WITH MAX_USER_CONNECTIONS 10 MAX_CONNECTIONS_PER_HOUR 10 MAX_QUERIES_PER_HOUR 10 MAX_UPDATES_PER_HOUR 10"])
66+
provider.expects(:mysql).with([defaults_file, '-e', "CREATE USER 'joe'@'localhost' IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"])
67+
provider.expects(:mysql).with([defaults_file, '-e', "GRANT USAGE ON *.* TO 'joe'@'localhost' WITH MAX_USER_CONNECTIONS 10 MAX_CONNECTIONS_PER_HOUR 10 MAX_QUERIES_PER_HOUR 10 MAX_UPDATES_PER_HOUR 10"])
6768
provider.expects(:exists?).returns(true)
6869
expect(provider.create).to be_truthy
6970
end

tests/mysql_user.pp

+11
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@
1919
ensure => present,
2020
password_hash => mysql_password('blah'),
2121
}
22+
23+
mysql_user{ 'socketplugin@%':
24+
ensure => present,
25+
plugin => 'unix_socket',
26+
}
27+
28+
mysql_user{ 'socketplugin@%':
29+
ensure => present,
30+
password_hash => mysql_password('blah'),
31+
plugin => 'mysql_native_password',
32+
}

0 commit comments

Comments
 (0)