Skip to content

Commit fe0365e

Browse files
Support size 15 and 16 quoted usernames
As usernames containing special characters must be quoted, they may have two extra characters that are not counted against the size limit of 16 characters. This patch adds a regex to handle this case.
1 parent 31191b6 commit fe0365e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/puppet/type/mysql_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
raise(ArgumentError, "Database user #{value} must be quotted as it contains special characters") if value =~ /^[^'`"].*[^0-9a-zA-Z$_].*[^'`"]@[\w%\.:]+/
1616
raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^(?:['`"][^'`"]*['`"]|[0-9a-zA-Z$_]*)@[\w%\.:]+/
1717
username = value.split('@')[0]
18-
if username.size > 16
18+
if not ((username =~ /['"`]*['"`]$/ and username.size <= 18) or username.size <= 16)
1919
raise ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters'
2020
end
2121
end

spec/unit/puppet/type/mysql_user_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@
4949
end
5050
end
5151

52+
context 'using a quoted 16 char username' do
53+
before :each do
54+
@user = Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint"@localhost', :password_hash => 'pass')
55+
end
56+
57+
it 'should accept a user name' do
58+
expect(@user[:name]).to eq('"debian-sys-maint"@localhost')
59+
end
60+
end
61+
62+
context 'using a quoted username that is too long ' do
63+
it 'should fail with a size error' do
64+
expect {
65+
Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint2"@localhost', :password_hash => 'pass')
66+
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
67+
end
68+
end
69+
5270
context 'using `speci!al#`@localhost' do
5371
before :each do
5472
@user = Puppet::Type.type(:mysql_user).new(:name => '`speci!al#`@localhost', :password_hash => 'pass')

0 commit comments

Comments
 (0)