From fe0365e80d4f93cc0db978b448e4db083d36845f Mon Sep 17 00:00:00 2001 From: Michael Chapman Date: Thu, 20 Nov 2014 17:23:11 +1100 Subject: [PATCH] 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. --- lib/puppet/type/mysql_user.rb | 2 +- spec/unit/puppet/type/mysql_user_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/puppet/type/mysql_user.rb b/lib/puppet/type/mysql_user.rb index 72b672e48..5078649de 100644 --- a/lib/puppet/type/mysql_user.rb +++ b/lib/puppet/type/mysql_user.rb @@ -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 diff --git a/spec/unit/puppet/type/mysql_user_spec.rb b/spec/unit/puppet/type/mysql_user_spec.rb index 25ad03201..86d56d7cd 100644 --- a/spec/unit/puppet/type/mysql_user_spec.rb +++ b/spec/unit/puppet/type/mysql_user_spec.rb @@ -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')