Skip to content

length check for usernames should take mysql version into consideration #722

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
Jun 25, 2015
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
9 changes: 8 additions & 1 deletion lib/puppet/type/mysql_grant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ def initialize(*args)
raise(ArgumentError, "Invalid database user #{value}")
end

raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters') unless user_part.size <= 16
mysql_version = Facter.value(:mysql_version)
unless mysql_version.nil?
if Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') < 0 and user_part.size > 16
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters')
elsif Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') > 0 and user_part.size > 80
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 80 characters')
end
end
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/puppet/type/mysql_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
raise(ArgumentError, "Invalid database user #{value}")
end

raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters') if user_part.size > 16
mysql_version = Facter.value(:mysql_version)
unless mysql_version.nil?
if Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') < 0 and user_part.size > 16
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters')
elsif Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') > 0 and user_part.size > 80
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 80 characters')
end
end
end

munge do |value|
Expand Down
1 change: 1 addition & 0 deletions spec/unit/puppet/provider/mysql_user/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
before :each do
# Set up the stubs for an instances call.
Facter.stubs(:value).with(:root_home).returns('/root')
Facter.stubs(:value).with(:mysql_version).returns('5.6.24')
Puppet::Util.stubs(:which).with('mysql').returns('/usr/bin/mysql')
File.stubs(:file?).with('/root/.my.cnf').returns(true)
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT CONCAT(User, '@',Host) AS User FROM mysql.user"]).returns('joe@localhost')
Expand Down
22 changes: 18 additions & 4 deletions spec/unit/puppet/type/mysql_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
require 'puppet/type/mysql_user'
describe Puppet::Type.type(:mysql_user) do

it 'should fail with a long user name' do
expect {
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
context "On MySQL 5.x" do
let(:facts) {{ :mysql_version => '5.6.24' }}
it 'should fail with a long user name' do
expect {
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
end
end

context "On MariaDB 10.0.0+" do
let(:facts) {{ :mysql_version => '10.0.19' }}
it 'should succeed with a long user name on MariaDB' do
expect {
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
end
end

it 'should require a name' do
Expand Down Expand Up @@ -60,6 +72,7 @@
end

context 'using a quoted 16 char username' do
let(:facts) {{ :mysql_version => '5.6.24' }}
before :each do
@user = Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint"@localhost', :password_hash => 'pass')
end
Expand All @@ -70,6 +83,7 @@
end

context 'using a quoted username that is too long ' do
let(:facts) {{ :mysql_version => '5.6.24' }}
it 'should fail with a size error' do
expect {
Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint2"@localhost', :password_hash => 'pass')
Expand Down