Skip to content

Add support for REQUIRE SSL|X509 option #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2016
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ Maximum updates per hour for the user. Must be an integer value. A value of '0'
```
mysql_grant { 'root@localhost/*.*':
ensure => 'present',
options => ['GRANT'],
options => ['REQUIRE SSL', 'GRANT'],
privileges => ['ALL'],
table => '*.*',
user => 'root@localhost',
Expand Down Expand Up @@ -939,7 +939,8 @@ User to whom privileges are granted.

##### `options`

MySQL options to grant. Optional.
Array of MySQL options to grant. Optional.
Supported options are 'REQUIRE SSL', 'REQUIRE X509', 'GRANT'.

#### mysql_plugin

Expand Down
6 changes: 4 additions & 2 deletions lib/puppet/provider/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def self.cmd_privs(privileges)
# Take in potential options and build up a query string with them.
def self.cmd_options(options)
option_string = ''
options.each do |opt|
if opt == 'GRANT'
options.sort.reverse_each do |opt|
if op = opt.match(/^REQUIRE\s(SSL|X509)$/)
option_string << " #{op[0]}"
elsif opt == 'GRANT'
option_string << ' WITH GRANT OPTION'
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/puppet/provider/mysql_grant/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def self.instances
end
end
# Same here, but to remove OPTION leaving just GRANT.
if rest.match(/WITH\sGRANT\sOPTION/)
options = ['GRANT']
else
options = ['NONE']
end
options = []
req_opt = rest.match(/REQUIRE\s(SSL|X509)/)
options << req_opt[0] if req_opt
Copy link
Contributor

Choose a reason for hiding this comment

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

If the above recommendation is taken, this also needs to be changed,

options << 'GRANT' if rest.match(/WITH\sGRANT\sOPTION/)
options << 'NONE' if options.empty?
# fix double backslash that MySQL prints, so resources match
table.gsub!("\\\\", "\\")
# We need to return an array of instances so capture these
Expand Down
88 changes: 86 additions & 2 deletions spec/acceptance/types/mysql_grant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class { 'mysql::server':
describe 'missing privileges for user' do
it 'should fail' do
pp = <<-EOS
mysql_user { 'test1@tester':
mysql_user { 'test1@tester':
ensure => present,
}
mysql_grant { 'test1@tester/test.*':
Expand Down Expand Up @@ -129,7 +129,35 @@ class { 'mysql::server':
end
end

describe 'adding option' do
describe 'adding REQUIRE SSL option' do
it 'should work without errors' do
pp = <<-EOS
mysql_user { 'test3@tester':
ensure => present,
}
mysql_grant { 'test3@tester/test.*':
ensure => 'present',
table => 'test.*',
user => 'test3@tester',
options => ['REQUIRE SSL'],
privileges => ['SELECT', 'UPDATE'],
require => Mysql_user['test3@tester'],
}
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'should find the user' do
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE SSL$/)
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester'$/)
expect(r.stderr).to be_empty
end
end
end

describe 'adding GRANT option' do
it 'should work without errors' do
pp = <<-EOS
mysql_user { 'test3@tester':
Expand All @@ -156,6 +184,62 @@ class { 'mysql::server':
end
end

describe 'adding REQUIRE X509 and GRANT option' do
it 'should work without errors' do
pp = <<-EOS
mysql_user { 'test3@tester':
ensure => present,
}
mysql_grant { 'test3@tester/test.*':
ensure => 'present',
table => 'test.*',
user => 'test3@tester',
options => ['REQUIRE X509', 'GRANT'],
privileges => ['SELECT', 'UPDATE'],
require => Mysql_user['test3@tester'],
}
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'should find the user' do
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE X509$/)
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester' WITH GRANT OPTION$/)
expect(r.stderr).to be_empty
end
end
end

describe 'adding GRANT and REQUIRE X509 option' do
it 'should work without errors' do
pp = <<-EOS
mysql_user { 'test3@tester':
ensure => present,
}
mysql_grant { 'test3@tester/test.*':
ensure => 'present',
table => 'test.*',
user => 'test3@tester',
options => ['GRANT', 'REQUIRE X509'],
privileges => ['SELECT', 'UPDATE'],
require => Mysql_user['test3@tester'],
}
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'should find the user' do
shell("mysql -NBe \"SHOW GRANTS FOR test3@tester\"") do |r|
expect(r.stdout).to match(/GRANT USAGE ON *.* TO 'test3'@'tester' REQUIRE X509$/)
expect(r.stdout).to match(/GRANT SELECT, UPDATE ON `test`.* TO 'test3'@'tester' WITH GRANT OPTION$/)
expect(r.stderr).to be_empty
end
end
end

describe 'adding all privileges without table' do
it 'should fail' do
pp = <<-EOS
Expand Down