PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/puphpet/puppet/modules/mysql/lib/puppet/type/mysql_grant.rb

https://gitlab.com/renatoruk/twentypress
Ruby | 101 lines | 89 code | 5 blank | 7 comment | 14 complexity | 2c04c9df671fc9c91d85cb2487d14ece MD5 | raw file
  1. # This has to be a separate type to enable collecting
  2. Puppet::Type.newtype(:mysql_grant) do
  3. @doc = "Manage a MySQL user's rights."
  4. ensurable
  5. autorequire(:file) { '/root/.my.cnf' }
  6. autorequire(:mysql_user) { self[:user] }
  7. def initialize(*args)
  8. super
  9. # Forcibly munge any privilege with 'ALL' in the array to exist of just
  10. # 'ALL'. This can't be done in the munge in the property as that iterates
  11. # over the array and there's no way to replace the entire array before it's
  12. # returned to the provider.
  13. if self[:ensure] == :present and Array(self[:privileges]).count > 1 and self[:privileges].to_s.include?('ALL')
  14. self[:privileges] = 'ALL'
  15. end
  16. # Sort the privileges array in order to ensure the comparision in the provider
  17. # self.instances method match. Otherwise this causes it to keep resetting the
  18. # privileges.
  19. self[:privileges] = Array(self[:privileges]).map{ |priv|
  20. # split and sort the column_privileges in the parentheses and rejoin
  21. if priv.include?('(')
  22. type, col=priv.strip.split(/\s+|\b/,2)
  23. type.upcase + " (" + col.slice(1...-1).strip.split(/\s*,\s*/).sort.join(', ') + ")"
  24. else
  25. priv.strip.upcase
  26. end
  27. }.uniq.reject{|k| k == 'GRANT' or k == 'GRANT OPTION'}.sort!
  28. end
  29. validate do
  30. fail('privileges parameter is required.') if self[:ensure] == :present and self[:privileges].nil?
  31. fail('table parameter is required.') if self[:ensure] == :present and self[:table].nil?
  32. fail('user parameter is required.') if self[:ensure] == :present and self[:user].nil?
  33. fail('name must match user and table parameters') if self[:name] != "#{self[:user]}/#{self[:table]}"
  34. end
  35. newparam(:name, :namevar => true) do
  36. desc 'Name to describe the grant.'
  37. munge do |value|
  38. value.delete("'")
  39. end
  40. end
  41. newproperty(:privileges, :array_matching => :all) do
  42. desc 'Privileges for user'
  43. end
  44. newproperty(:table) do
  45. desc 'Table to apply privileges to.'
  46. munge do |value|
  47. value.delete("`")
  48. end
  49. newvalues(/.*\..*/,/@/)
  50. end
  51. newproperty(:user) do
  52. desc 'User to operate on.'
  53. validate do |value|
  54. # http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
  55. # If at least one special char is used, string must be quoted
  56. # http://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions/8057827#8057827
  57. if matches = /^(['`"])((?!\1).)*\1@([\w%\.:\-\/]+)$/.match(value)
  58. user_part = matches[2]
  59. host_part = matches[3]
  60. elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-\/]+)$/.match(value)
  61. user_part = matches[1]
  62. host_part = matches[2]
  63. elsif matches = /^((?!['`"]).*[^0-9a-zA-Z$_].*)@(.+)$/.match(value)
  64. user_part = matches[1]
  65. host_part = matches[2]
  66. else
  67. raise(ArgumentError, "Invalid database user #{value}")
  68. end
  69. mysql_version = Facter.value(:mysql_version)
  70. unless mysql_version.nil?
  71. if Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') < 0 and user_part.size > 16
  72. raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters')
  73. elsif Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') > 0 and user_part.size > 80
  74. raise(ArgumentError, 'MySQL usernames are limited to a maximum of 80 characters')
  75. end
  76. end
  77. end
  78. munge do |value|
  79. matches = /^((['`"]?).*\2)@(.+)$/.match(value)
  80. "#{matches[1]}@#{matches[3].downcase}"
  81. end
  82. end
  83. newproperty(:options, :array_matching => :all) do
  84. desc 'Options to grant.'
  85. end
  86. end