From 07b661dcea926981cf5cd1c703a1c982d6eb6ef1 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Tue, 3 Jun 2014 12:49:10 -0700 Subject: [PATCH] Require title of mysql_grant resource to match form user/table This addresses https://tickets.puppetlabs.com/browse/MODULES-1040. The user parameter is required to have the form username@host. A grant is identified in the instances method by a name of the form username@host/table. The resource will fail to be identified as already existing if the name given to the resource does not match this form. --- lib/puppet/type/mysql_grant.rb | 1 + spec/acceptance/types/mysql_grant_spec.rb | 15 +++++++++++++++ spec/unit/puppet/type/mysql_grant_spec.rb | 8 +++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/puppet/type/mysql_grant.rb b/lib/puppet/type/mysql_grant.rb index be8dfbc34..a268e4cb5 100644 --- a/lib/puppet/type/mysql_grant.rb +++ b/lib/puppet/type/mysql_grant.rb @@ -24,6 +24,7 @@ def initialize(*args) fail('privileges parameter is required.') if self[:ensure] == :present and self[:privileges].nil? fail('table parameter is required.') if self[:ensure] == :present and self[:table].nil? fail('user parameter is required.') if self[:ensure] == :present and self[:user].nil? + fail('name must match user and table parameters') if self[:name] != "#{self[:user]}/#{self[:table]}" end newparam(:name, :namevar => true) do diff --git a/spec/acceptance/types/mysql_grant_spec.rb b/spec/acceptance/types/mysql_grant_spec.rb index df8bea4f3..3a4f453b3 100644 --- a/spec/acceptance/types/mysql_grant_spec.rb +++ b/spec/acceptance/types/mysql_grant_spec.rb @@ -70,6 +70,21 @@ class { 'mysql::server': } end end + describe 'adding privileges with invalid name' do + it 'should fail' do + pp = <<-EOS + mysql_grant { 'test': + ensure => 'present', + table => 'test.*', + user => 'test2@tester', + privileges => ['SELECT', 'UPDATE'], + } + EOS + + expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/name must match user and table parameters/) + end + end + describe 'adding option' do it 'should work without errors' do pp = <<-EOS diff --git a/spec/unit/puppet/type/mysql_grant_spec.rb b/spec/unit/puppet/type/mysql_grant_spec.rb index d56ba241a..4171ab28e 100644 --- a/spec/unit/puppet/type/mysql_grant_spec.rb +++ b/spec/unit/puppet/type/mysql_grant_spec.rb @@ -41,4 +41,10 @@ }.to raise_error(Puppet::Error, 'Title or name must be provided') end -end \ No newline at end of file + it 'should require the name to match the user and table' do + expect { + Puppet::Type.type(:mysql_grant).new(:name => 'foo', :privileges => ['ALL', 'PROXY'], :table => ['*.*','@'], :user => 'foo@localhost') + }.to raise_error /name must match user and table parameters/ + end + +end