PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/puphpet/puppet/modules/mysql/spec/acceptance/types/mysql_grant_spec.rb

https://gitlab.com/billyprice1/phpservermon
Ruby | 308 lines | 271 code | 36 blank | 1 comment | 2 complexity | f937987498c01590c666a4e12b49541f MD5 | raw file
  1. require 'spec_helper_acceptance'
  2. describe 'mysql_grant', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
  3. describe 'setup' do
  4. it 'setup mysql::server' do
  5. pp = <<-EOS
  6. class { 'mysql::server': }
  7. EOS
  8. apply_manifest(pp, :catch_failures => true)
  9. end
  10. end
  11. describe 'missing privileges for user' do
  12. it 'should fail' do
  13. pp = <<-EOS
  14. mysql_grant { 'test1@tester/test.*':
  15. ensure => 'present',
  16. table => 'test.*',
  17. user => 'test1@tester',
  18. }
  19. EOS
  20. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/privileges parameter is required/)
  21. end
  22. it 'should not find the user' do
  23. expect(shell("mysql -NBe \"SHOW GRANTS FOR test1@tester\"", { :acceptable_exit_codes => 1}).stderr).to match(/There is no such grant defined for user 'test1' on host 'tester'/)
  24. end
  25. end
  26. describe 'missing table for user' do
  27. it 'should fail' do
  28. pp = <<-EOS
  29. mysql_grant { 'atest@tester/test.*':
  30. ensure => 'present',
  31. user => 'atest@tester',
  32. privileges => ['ALL'],
  33. }
  34. EOS
  35. apply_manifest(pp, :expect_failures => true)
  36. end
  37. it 'should not find the user' do
  38. expect(shell("mysql -NBe \"SHOW GRANTS FOR atest@tester\"", {:acceptable_exit_codes => 1}).stderr).to match(/There is no such grant defined for user 'atest' on host 'tester'/)
  39. end
  40. end
  41. describe 'adding privileges' do
  42. it 'should work without errors' do
  43. pp = <<-EOS
  44. mysql_grant { 'test2@tester/test.*':
  45. ensure => 'present',
  46. table => 'test.*',
  47. user => 'test2@tester',
  48. privileges => ['SELECT', 'UPDATE'],
  49. }
  50. EOS
  51. apply_manifest(pp, :catch_failures => true)
  52. end
  53. it 'should find the user' do
  54. shell("mysql -NBe \"SHOW GRANTS FOR test2@tester\"") do |r|
  55. expect(r.stdout).to match(/GRANT SELECT, UPDATE.*TO 'test2'@'tester'/)
  56. expect(r.stderr).to be_empty
  57. end
  58. end
  59. end
  60. describe 'adding option' do
  61. it 'should work without errors' do
  62. pp = <<-EOS
  63. mysql_grant { 'test3@tester/test.*':
  64. ensure => 'present',
  65. table => 'test.*',
  66. user => 'test3@tester',
  67. options => ['GRANT'],
  68. privileges => ['SELECT', 'UPDATE'],
  69. }
  70. EOS
  71. apply_manifest(pp, :catch_failures => true)
  72. end
  73. it 'should find the user' do
  74. shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
  75. expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester' WITH GRANT OPTION$/)
  76. expect(r.stderr).to be_empty
  77. end
  78. end
  79. end
  80. describe 'adding all privileges without table' do
  81. it 'should fail' do
  82. pp = <<-EOS
  83. mysql_grant { 'test4@tester/test.*':
  84. ensure => 'present',
  85. user => 'test4@tester',
  86. options => ['GRANT'],
  87. privileges => ['SELECT', 'UPDATE', 'ALL'],
  88. }
  89. EOS
  90. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/table parameter is required./)
  91. end
  92. end
  93. describe 'adding all privileges' do
  94. it 'should only try to apply ALL' do
  95. pp = <<-EOS
  96. mysql_grant { 'test4@tester/test.*':
  97. ensure => 'present',
  98. table => 'test.*',
  99. user => 'test4@tester',
  100. options => ['GRANT'],
  101. privileges => ['SELECT', 'UPDATE', 'ALL'],
  102. }
  103. EOS
  104. apply_manifest(pp, :catch_failures => true)
  105. end
  106. it 'should find the user' do
  107. shell("mysql -NBe \"SHOW GRANTS FOR test4@tester\"") do |r|
  108. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test4'@'tester' WITH GRANT OPTION/)
  109. expect(r.stderr).to be_empty
  110. end
  111. end
  112. end
  113. # Test combinations of user@host to ensure all cases work.
  114. describe 'short hostname' do
  115. it 'should apply' do
  116. pp = <<-EOS
  117. mysql_grant { 'test@short/test.*':
  118. ensure => 'present',
  119. table => 'test.*',
  120. user => 'test@short',
  121. privileges => 'ALL',
  122. }
  123. mysql_grant { 'test@long.hostname.com/test.*':
  124. ensure => 'present',
  125. table => 'test.*',
  126. user => 'test@long.hostname.com',
  127. privileges => 'ALL',
  128. }
  129. mysql_grant { 'test@192.168.5.6/test.*':
  130. ensure => 'present',
  131. table => 'test.*',
  132. user => 'test@192.168.5.6',
  133. privileges => 'ALL',
  134. }
  135. mysql_grant { 'test@2607:f0d0:1002:0051:0000:0000:0000:0004/test.*':
  136. ensure => 'present',
  137. table => 'test.*',
  138. user => 'test@2607:f0d0:1002:0051:0000:0000:0000:0004',
  139. privileges => 'ALL',
  140. }
  141. mysql_grant { 'test@::1/128/test.*':
  142. ensure => 'present',
  143. table => 'test.*',
  144. user => 'test@::1/128',
  145. privileges => 'ALL',
  146. }
  147. EOS
  148. apply_manifest(pp, :catch_failures => true)
  149. end
  150. it 'finds short hostname' do
  151. shell("mysql -NBe \"SHOW GRANTS FOR test@short\"") do |r|
  152. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'short'/)
  153. expect(r.stderr).to be_empty
  154. end
  155. end
  156. it 'finds long hostname' do
  157. shell("mysql -NBe \"SHOW GRANTS FOR 'test'@'long.hostname.com'\"") do |r|
  158. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'long.hostname.com'/)
  159. expect(r.stderr).to be_empty
  160. end
  161. end
  162. it 'finds ipv4' do
  163. shell("mysql -NBe \"SHOW GRANTS FOR 'test'@'192.168.5.6'\"") do |r|
  164. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'192.168.5.6'/)
  165. expect(r.stderr).to be_empty
  166. end
  167. end
  168. it 'finds ipv6' do
  169. shell("mysql -NBe \"SHOW GRANTS FOR 'test'@'2607:f0d0:1002:0051:0000:0000:0000:0004'\"") do |r|
  170. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'2607:f0d0:1002:0051:0000:0000:0000:0004'/)
  171. expect(r.stderr).to be_empty
  172. end
  173. end
  174. it 'finds short ipv6' do
  175. shell("mysql -NBe \"SHOW GRANTS FOR 'test'@'::1/128'\"") do |r|
  176. expect(r.stdout).to match(/GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'::1\/128'/)
  177. expect(r.stderr).to be_empty
  178. end
  179. end
  180. end
  181. describe 'complex test' do
  182. it 'setup mysql::server' do
  183. pp = <<-EOS
  184. $dbSubnet = '10.10.10.%'
  185. mysql_database { 'foo':
  186. ensure => present,
  187. }
  188. exec { 'mysql-create-table':
  189. command => '/usr/bin/mysql -NBe "CREATE TABLE foo.bar (name VARCHAR(20))"',
  190. environment => "HOME=${::root_home}",
  191. unless => '/usr/bin/mysql -NBe "SELECT 1 FROM foo.bar LIMIT 1;"',
  192. require => Mysql_database['foo'],
  193. }
  194. Mysql_grant {
  195. ensure => present,
  196. options => ['GRANT'],
  197. privileges => ['ALL'],
  198. table => '*.*',
  199. require => [ Mysql_database['foo'], Exec['mysql-create-table'] ],
  200. }
  201. mysql_grant { "user1@${dbSubnet}/*.*":
  202. user => "user1@${dbSubnet}",
  203. }
  204. mysql_grant { "user2@${dbSubnet}/foo.bar":
  205. privileges => ['SELECT', 'INSERT', 'UPDATE'],
  206. user => "user2@${dbSubnet}",
  207. table => 'foo.bar',
  208. }
  209. mysql_grant { "user3@${dbSubnet}/foo.*":
  210. privileges => ['SELECT', 'INSERT', 'UPDATE'],
  211. user => "user3@${dbSubnet}",
  212. table => 'foo.*',
  213. }
  214. mysql_grant { 'web@%/*.*':
  215. user => 'web@%',
  216. }
  217. mysql_grant { "web@${dbSubnet}/*.*":
  218. user => "web@${dbSubnet}",
  219. }
  220. mysql_grant { "web@${fqdn}/*.*":
  221. user => "web@${fqdn}",
  222. }
  223. mysql_grant { 'web@localhost/*.*':
  224. user => 'web@localhost',
  225. }
  226. EOS
  227. apply_manifest(pp, :catch_failures => true)
  228. apply_manifest(pp, :catch_changes => true)
  229. end
  230. end
  231. describe 'lower case privileges' do
  232. it 'create ALL privs' do
  233. pp = <<-EOS
  234. mysql_grant { 'lowercase@localhost/*.*':
  235. user => 'lowercase@localhost',
  236. privileges => 'ALL',
  237. table => '*.*',
  238. }
  239. EOS
  240. apply_manifest(pp, :catch_failures => true)
  241. end
  242. it 'create lowercase all privs' do
  243. pp = <<-EOS
  244. mysql_grant { 'lowercase@localhost/*.*':
  245. user => 'lowercase@localhost',
  246. privileges => 'all',
  247. table => '*.*',
  248. }
  249. EOS
  250. expect(apply_manifest(pp, :catch_failures => true).exit_code).to eq(0)
  251. end
  252. end
  253. describe 'adding procedure privileges' do
  254. it 'should work without errors' do
  255. pp = <<-EOS
  256. mysql_grant { 'test2@tester/PROCEDURE test.simpleproc':
  257. ensure => 'present',
  258. table => 'PROCEDURE test.simpleproc',
  259. user => 'test2@tester',
  260. privileges => ['EXECUTE'],
  261. }
  262. EOS
  263. apply_manifest(pp, :catch_failures => true)
  264. end
  265. it 'should find the user' do
  266. shell("mysql -NBe \"SHOW GRANTS FOR test2@tester\"") do |r|
  267. expect(r.stdout).to match(/GRANT EXECUTE ON PROCEDURE `test`.`simpleproc` TO 'test2'@'tester'/)
  268. expect(r.stderr).to be_empty
  269. end
  270. end
  271. end
  272. end