Skip to content

Commit f92a24e

Browse files
author
Morgan Haskel
committed
MODULES-1520 - update username validation
Don't fail on validation where the user isn't quoted with special characters. The providers quote these strings by default.
1 parent 4203867 commit f92a24e

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

lib/puppet/type/mysql_grant.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def initialize(*args)
7070
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-]+)/.match(value)
7171
user_part = matches[1]
7272
host_part = matches[2]
73-
elsif matches = /^(?:(?!['`"]).*)([^0-9a-zA-Z$_]).*@.+$/.match(value)
74-
# does not start with a quote, but contains a special character
75-
raise(ArgumentError, "Database user #{value} must be properly quoted, invalid character: '#{matches[1]}'")
73+
elsif matches = /^((?!['`"]).*[^0-9a-zA-Z$_].*)@(.+)$/.match(value)
74+
user_part = matches[1]
75+
host_part = matches[2]
7676
else
7777
raise(ArgumentError, "Invalid database user #{value}")
7878
end

lib/puppet/type/mysql_user.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
elsif matches = /^([0-9a-zA-Z$_]*)@([\w%\.:\-]+)/.match(value)
2020
user_part = matches[1]
2121
host_part = matches[2]
22-
elsif matches = /^(?:(?!['`"]).*)([^0-9a-zA-Z$_]).*@.+$/.match(value)
23-
# does not start with a quote, but contains a special character
24-
raise(ArgumentError, "Database user #{value} must be properly quoted, invalid character: '#{matches[1]}'")
22+
elsif matches = /^((?!['`"]).*[^0-9a-zA-Z$_].*)@(.+)$/.match(value)
23+
user_part = matches[1]
24+
host_part = matches[2]
2525
else
2626
raise(ArgumentError, "Invalid database user #{value}")
2727
end

spec/acceptance/types/mysql_grant_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ class { 'mysql::server': }
7070
end
7171
end
7272

73+
describe 'adding privileges with special character in name' do
74+
it 'should work without errors' do
75+
pp = <<-EOS
76+
mysql_grant { 'test-2@tester/test.*':
77+
ensure => 'present',
78+
table => 'test.*',
79+
user => 'test-2@tester',
80+
privileges => ['SELECT', 'UPDATE'],
81+
}
82+
EOS
83+
84+
apply_manifest(pp, :catch_failures => true)
85+
end
86+
87+
it 'should find the user' do
88+
shell("mysql -NBe \"SHOW GRANTS FOR 'test-2'@tester\"") do |r|
89+
expect(r.stdout).to match(/GRANT SELECT, UPDATE.*TO 'test-2'@'tester'/)
90+
expect(r.stderr).to be_empty
91+
end
92+
end
93+
end
94+
7395
describe 'adding privileges with invalid name' do
7496
it 'should fail' do
7597
pp = <<-EOS

spec/acceptance/types/mysql_user_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ class { 'mysql::server': }
3232
end
3333
end
3434

35+
context 'using ashp-dash@localhost' do
36+
describe 'adding user' do
37+
it 'should work without errors' do
38+
pp = <<-EOS
39+
mysql_user { 'ashp-dash@localhost':
40+
password_hash => '6f8c114b58f2ce9e',
41+
}
42+
EOS
43+
44+
apply_manifest(pp, :catch_failures => true)
45+
end
46+
47+
it 'should find the user' do
48+
shell("mysql -NBe \"select '1' from mysql.user where CONCAT(user, '@', host) = 'ashp-dash@localhost'\"") do |r|
49+
expect(r.stdout).to match(/^1$/)
50+
expect(r.stderr).to be_empty
51+
end
52+
end
53+
end
54+
end
55+
3556
context 'using ashp@LocalHost' do
3657
describe 'adding user' do
3758
it 'should work without errors' do

spec/unit/puppet/type/mysql_user_spec.rb

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

52+
context 'ensure the default \'debian-sys-main\'@localhost user can be parsed' 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+
5262
context 'using a quoted 16 char username' do
5363
before :each do
5464
@user = Puppet::Type.type(:mysql_user).new(:name => '"debian-sys-maint"@localhost', :password_hash => 'pass')
@@ -78,10 +88,12 @@
7888
end
7989

8090
context 'using in-valid@localhost' do
81-
it 'should fail with an unquotted username with special char' do
82-
expect {
83-
Puppet::Type.type(:mysql_user).new(:name => 'in-valid@localhost', :password_hash => 'pass')
84-
}.to raise_error /Database user in-valid@localhost must be properly quoted, invalid character: '-'/
91+
before :each do
92+
@user = Puppet::Type.type(:mysql_user).new(:name => 'in-valid@localhost', :password_hash => 'pass')
93+
end
94+
95+
it 'should accept a user name with special chatracters' do
96+
expect(@user[:name]).to eq('in-valid@localhost')
8597
end
8698
end
8799

0 commit comments

Comments
 (0)