Skip to content

Commit c6afa11

Browse files
author
Morgan Haskel
committed
Merge pull request #722 from igalic/lenght_check
length check for usernames should take mysql version into consideration
2 parents 545f0a5 + 837c21b commit c6afa11

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

lib/puppet/type/mysql_grant.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ def initialize(*args)
7878
raise(ArgumentError, "Invalid database user #{value}")
7979
end
8080

81-
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters') unless user_part.size <= 16
81+
mysql_version = Facter.value(:mysql_version)
82+
unless mysql_version.nil?
83+
if Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') < 0 and user_part.size > 16
84+
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters')
85+
elsif Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') > 0 and user_part.size > 80
86+
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 80 characters')
87+
end
88+
end
8289
end
8390
end
8491

lib/puppet/type/mysql_user.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@
2626
raise(ArgumentError, "Invalid database user #{value}")
2727
end
2828

29-
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters') if user_part.size > 16
29+
mysql_version = Facter.value(:mysql_version)
30+
unless mysql_version.nil?
31+
if Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') < 0 and user_part.size > 16
32+
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters')
33+
elsif Puppet::Util::Package.versioncmp(mysql_version, '10.0.0') > 0 and user_part.size > 80
34+
raise(ArgumentError, 'MySQL usernames are limited to a maximum of 80 characters')
35+
end
36+
end
3037
end
3138

3239
munge do |value|

spec/unit/puppet/provider/mysql_user/mysql_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
before :each do
3535
# Set up the stubs for an instances call.
3636
Facter.stubs(:value).with(:root_home).returns('/root')
37+
Facter.stubs(:value).with(:mysql_version).returns('5.6.24')
3738
Puppet::Util.stubs(:which).with('mysql').returns('/usr/bin/mysql')
3839
File.stubs(:file?).with('/root/.my.cnf').returns(true)
3940
provider.class.stubs(:mysql).with([defaults_file, '-NBe', "SELECT CONCAT(User, '@',Host) AS User FROM mysql.user"]).returns('joe@localhost')

spec/unit/puppet/type/mysql_user_spec.rb

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
require 'puppet/type/mysql_user'
33
describe Puppet::Type.type(:mysql_user) do
44

5-
it 'should fail with a long user name' do
6-
expect {
7-
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
8-
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
5+
context "On MySQL 5.x" do
6+
let(:facts) {{ :mysql_version => '5.6.24' }}
7+
it 'should fail with a long user name' do
8+
expect {
9+
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
10+
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
11+
end
12+
end
13+
14+
context "On MariaDB 10.0.0+" do
15+
let(:facts) {{ :mysql_version => '10.0.19' }}
16+
it 'should succeed with a long user name on MariaDB' do
17+
expect {
18+
Puppet::Type.type(:mysql_user).new({:name => '12345678901234567@localhost', :password_hash => 'pass'})
19+
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
20+
end
921
end
1022

1123
it 'should require a name' do
@@ -60,6 +72,7 @@
6072
end
6173

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

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

0 commit comments

Comments
 (0)