Skip to content

Commit f4961e9

Browse files
committed
Add support for REQUIRE SSL|X509 option
1 parent 53044aa commit f4961e9

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

lib/puppet/provider/mysql.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ def self.cmd_privs(privileges)
104104
# Take in potential options and build up a query string with them.
105105
def self.cmd_options(options)
106106
option_string = ''
107-
options.each do |opt|
108-
if opt == 'GRANT'
107+
options.sort.reverse_each do |opt|
108+
if op = opt.match(/^REQUIRE\s(SSL|X509)$/)
109+
option_string << " #{op[0]}"
110+
elsif opt == 'GRANT'
109111
option_string << ' WITH GRANT OPTION'
110112
end
111113
end

lib/puppet/provider/mysql_grant/mysql.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def self.instances
4646
end
4747
end
4848
# Same here, but to remove OPTION leaving just GRANT.
49-
if rest.match(/WITH\sGRANT\sOPTION/)
50-
options = ['GRANT']
51-
else
52-
options = ['NONE']
53-
end
49+
options = []
50+
req_opt = rest.match(/REQUIRE\s(SSL|X509)/)
51+
options << req_opt[0] if req_opt
52+
options << 'GRANT' if rest.match(/WITH\sGRANT\sOPTION/)
53+
options << 'NONE' if options.empty?
5454
# fix double backslash that MySQL prints, so resources match
5555
table.gsub!("\\\\", "\\")
5656
# We need to return an array of instances so capture these

spec/acceptance/types/mysql_grant_spec.rb

+86-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class { 'mysql::server':
1717
describe 'missing privileges for user' do
1818
it 'should fail' do
1919
pp = <<-EOS
20-
mysql_user { 'test1@tester':
20+
mysql_user { 'test1@tester':
2121
ensure => present,
2222
}
2323
mysql_grant { 'test1@tester/test.*':
@@ -129,7 +129,35 @@ class { 'mysql::server':
129129
end
130130
end
131131

132-
describe 'adding option' do
132+
describe 'adding REQUIRE SSL option' do
133+
it 'should work without errors' do
134+
pp = <<-EOS
135+
mysql_user { 'test3@tester':
136+
ensure => present,
137+
}
138+
mysql_grant { 'test3@tester/test.*':
139+
ensure => 'present',
140+
table => 'test.*',
141+
user => 'test3@tester',
142+
options => ['REQUIRE SSL'],
143+
privileges => ['SELECT', 'UPDATE'],
144+
require => Mysql_user['test3@tester'],
145+
}
146+
EOS
147+
148+
apply_manifest(pp, :catch_failures => true)
149+
end
150+
151+
it 'should find the user' do
152+
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
153+
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE SSL$/)
154+
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester'$/)
155+
expect(r.stderr).to be_empty
156+
end
157+
end
158+
end
159+
160+
describe 'adding GRANT option' do
133161
it 'should work without errors' do
134162
pp = <<-EOS
135163
mysql_user { 'test3@tester':
@@ -156,6 +184,62 @@ class { 'mysql::server':
156184
end
157185
end
158186

187+
describe 'adding REQUIRE X509 and GRANT option' do
188+
it 'should work without errors' do
189+
pp = <<-EOS
190+
mysql_user { 'test3@tester':
191+
ensure => present,
192+
}
193+
mysql_grant { 'test3@tester/test.*':
194+
ensure => 'present',
195+
table => 'test.*',
196+
user => 'test3@tester',
197+
options => ['REQUIRE X509', 'GRANT'],
198+
privileges => ['SELECT', 'UPDATE'],
199+
require => Mysql_user['test3@tester'],
200+
}
201+
EOS
202+
203+
apply_manifest(pp, :catch_failures => true)
204+
end
205+
206+
it 'should find the user' do
207+
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
208+
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE X509$/)
209+
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester' WITH GRANT OPTION$/)
210+
expect(r.stderr).to be_empty
211+
end
212+
end
213+
end
214+
215+
describe 'adding GRANT and REQUIRE X509 option' do
216+
it 'should work without errors' do
217+
pp = <<-EOS
218+
mysql_user { 'test3@tester':
219+
ensure => present,
220+
}
221+
mysql_grant { 'test3@tester/test.*':
222+
ensure => 'present',
223+
table => 'test.*',
224+
user => 'test3@tester',
225+
options => ['GRANT', 'REQUIRE X509'],
226+
privileges => ['SELECT', 'UPDATE'],
227+
require => Mysql_user['test3@tester'],
228+
}
229+
EOS
230+
231+
apply_manifest(pp, :catch_failures => true)
232+
end
233+
234+
it 'should find the user' do
235+
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
236+
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE X509$/)
237+
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester' WITH GRANT OPTION$/)
238+
expect(r.stderr).to be_empty
239+
end
240+
end
241+
end
242+
159243
describe 'adding all privileges without table' do
160244
it 'should fail' do
161245
pp = <<-EOS

0 commit comments

Comments
 (0)