Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions lib/puppet/provider/mysql_user/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ def self.instances
users.collect do |name|
if mysqld_version.nil?
## Default ...
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD /*!50508 , PLUGIN */ FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD, SSL_TYPE, SSL_CIPHER /*!50508 , PLUGIN */ FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this need an annotation like PLUGIN? since when has that been available?

else
if mysqld_type == "mysql" and Puppet::Util::Package.versioncmp(mysqld_version, '5.7.6') >= 0
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, AUTHENTICATION_STRING, PLUGIN FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, AUTHENTICATION_STRING, SSL_TYPE, SSL_CIPHER, PLUGIN FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
else
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD /*!50508 , PLUGIN */ FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
query = "SELECT MAX_USER_CONNECTIONS, MAX_CONNECTIONS, MAX_QUESTIONS, MAX_UPDATES, PASSWORD, SSL_TYPE, SSL_CIPHER /*!50508 , PLUGIN */ FROM mysql.user WHERE CONCAT(user, '@', host) = '#{name}'"
end
end
@max_user_connections, @max_connections_per_hour, @max_queries_per_hour,
@max_updates_per_hour, @password, @plugin = mysql([defaults_file, "-NBe", query].compact).split(/\s/)
@max_updates_per_hour, @password, @ssl_type, @ssl_cipher, @plugin = mysql([defaults_file, "-NBe", query].compact).split(/\s/)

new(:name => name,
:ensure => :present,
Expand All @@ -32,7 +32,9 @@ def self.instances
: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
:max_updates_per_hour => @max_updates_per_hour,
:ssl_type => @ssl_type,
:ssl_cipher => @ssl_cipher
)
end
end
Expand All @@ -56,6 +58,8 @@ def create
max_connections_per_hour = @resource.value(:max_connections_per_hour) || 0
max_queries_per_hour = @resource.value(:max_queries_per_hour) || 0
max_updates_per_hour = @resource.value(:max_updates_per_hour) || 0
ssl_type = @resource.value(:ssl_type)
ssl_cipher = @resource.value(:ssl_cipher)

# Use CREATE USER to be compatible with NO_AUTO_CREATE_USER sql_mode
# This is also required if you want to specify a authentication plugin
Expand All @@ -72,11 +76,24 @@ def create
@property_hash[:ensure] = :present
@property_hash[:password_hash] = password_hash
end
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)
grant_require = case ssl_type
when 'ANY' then 'SSL'
when 'SPECIFIED' then 'SSL'
when 'X509' then 'X509'
when '' then 'NONE'
else raise Puppet::Error, "Non-supported SSL-type #{ssl_type}"
end
if !ssl_cipher.empty?
grant_require << " AND CIPHER '#{ssl_cipher}'"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand why you're validating this here, rather than in the type.


mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' REQUIRE #{grant_require} 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)
@property_hash[:max_user_connections] = max_user_connections
@property_hash[:max_connections_per_hour] = max_connections_per_hour
@property_hash[:max_queries_per_hour] = max_queries_per_hour
@property_hash[:max_updates_per_hour] = max_updates_per_hour
@property_hash[:ssl_type] = ssl_type
@property_hash[:ssl_cipher] = ssl_cipher

exists? ? (return true) : (return false)
end
Expand Down Expand Up @@ -152,4 +169,25 @@ def max_updates_per_hour=(int)
max_updates_per_hour == int ? (return true) : (return false)
end

def ssl_type=(string)
merged_name = self.class.cmd_user(@resource[:name])
ssl_require = case string
when 'ANY' then 'SSL'
when 'SPECIFIED' then 'SSL'
when 'X509' then 'X509'
when '' then 'NONE'
else raise Puppet::Error, "Non-supported SSL-type #{string}"
end
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} REQUIRE #{ssl_require}"].compact).chomp

ssl_type == string ? (return true) : (return false)
end

def ssl_cipher=(string)
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} REQUIRE CIPHER '#{string}'"].compact).chomp

ssl_cipher == string ? (return true) : (return false)
end

end
24 changes: 24 additions & 0 deletions lib/puppet/type/mysql_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
autorequire(:file) { '/root/.my.cnf' }
autorequire(:class) { 'mysql::server' }

validate do
if !self[:ssl_cipher].empty? and self[:ssl_type] != 'SPECIFIED'
fail ArgumentError, "Specifying a SSL cipher requires SSL-type 'SPECIFIED'"
end
end

newparam(:name, :namevar => true) do
desc "The name of the user. This uses the 'username@hostname' or username@hostname."
validate do |value|
Expand Down Expand Up @@ -73,4 +79,22 @@
newvalue(/\d+/)
end

newproperty(:ssl_type) do
desc "SSL-type the user has to use to connect. Can be any of '', 'ANY', 'SPECIFIED', 'X509'."
newvalues('', 'ANY', 'SPECIFIED', 'X509')
defaultto ''
munge do |value|
String(value).upcase
end
end

newproperty(:ssl_cipher) do
desc "SSL-cipher the user has to use to connect. Requires ssl-type='SPECIFIED'."
newvalue(/\w*/)
defaultto ''
munge do |value|
String(value)
end
end

end