Skip to content

Commit

Permalink
Support size 15 and 16 quoted usernames
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaeltchapman committed Nov 20, 2014
1 parent 31191b6 commit fe0365e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/puppet/type/mysql_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
raise(ArgumentError, "Database user #{value} must be quotted as it contains special characters") if value =~ /^[^'`"].*[^0-9a-zA-Z$_].*[^'`"]@[\w%\.:]+/
raise(ArgumentError, "Invalid database user #{value}") unless value =~ /^(?:['`"][^'`"]*['`"]|[0-9a-zA-Z$_]*)@[\w%\.:]+/
username = value.split('@')[0]
if username.size > 16
if not ((username =~ /['"`]*['"`]$/ and username.size <= 18) or username.size <= 16)
raise ArgumentError, 'MySQL usernames are limited to a maximum of 16 characters'
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/puppet/type/mysql_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@
end
end

context 'using a quoted 16 char username' do
before :each do
@user = Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint"@localhost', :password_hash => 'pass')
end

it 'should accept a user name' do
expect(@user[:name]).to eq('"debian-sys-maint"@localhost')
end
end

context 'using a quoted username that is too long ' do
it 'should fail with a size error' do
expect {
Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint2"@localhost', :password_hash => 'pass')
}.to raise_error /MySQL usernames are limited to a maximum of 16 characters/
end
end

context 'using `speci!al#`@localhost' do
before :each do
@user = Puppet::Type.type(:mysql_user).new(:name => '`speci!al#`@localhost', :password_hash => 'pass')
Expand Down

0 comments on commit fe0365e

Please sign in to comment.