File tree Expand file tree Collapse file tree 4 files changed +77
-2
lines changed
lib/puppet/parser/functions Expand file tree Collapse file tree 4 files changed +77
-2
lines changed Original file line number Diff line number Diff line change 1+ require 'digest/md5'
2+
3+ module Puppet ::Parser ::Functions
4+ newfunction ( :postgresql_escape , :type => :rvalue , :doc => <<-EOS
5+ Safely escapes a string using $$ using a random tag which should be consistent
6+ EOS
7+ ) do |args |
8+
9+ raise ( Puppet ::ParseError , "postgresql_escape(): Wrong number of arguments " +
10+ "given (#{ args . size } for 1)" ) if args . size != 1
11+
12+ password = args [ 0 ]
13+
14+ if password !~ /\$ \$ /
15+ retval = "$$#{ password } $$"
16+ else
17+ escape = Digest ::MD5 . hexdigest ( password ) [ 0 ..5 ] . gsub ( /\d / , '' )
18+ until password !~ /#{ escape } /
19+ escape = Digest ::MD5 . hexdigest ( escape ) [ 0 ..5 ] . gsub ( /\d / , '' )
20+ end
21+ retval = "$#{ escape } $#{ password } $#{ escape } $"
22+ end
23+ retval
24+ end
25+ end
Original file line number Diff line number Diff line change 2626 # to allow the postgres system user to connect via psql without specifying
2727 # a password ('ident' or 'trust' security). This is the default
2828 # for pg_hba.conf.
29+ $escapedpassword = postgresql_escape($postgres_password )
2930 exec { 'set_postgres_postgrespw' :
3031 # This command works w/no password because we run it as postgres system user
31- command => " psql -c \" ALTER ROLE ${postgresql::params::user} PASSWORD ' ${postgres_password} ' \" " ,
32+ command => " psql -c ' ALTER ROLE \" ${postgresql::params::user} \" PASSWORD ${escapedpassword} ' " ,
3233 user => $postgresql::params::user ,
3334 group => $postgresql::params::group ,
3435 logoutput => true ,
3738 # a password. We specify the password via the PGPASSWORD environment variable. If
3839 # the password is correct (current), this command will exit with an exit code of 0,
3940 # which will prevent the main command from running.
40- unless => " env PGPASSWORD=\" ${postgres_password} \" psql -h localhost -c 'select 1' > /dev/null" ,
41+ unless => " env PGPASSWORD=' ${postgres_password} ' psql -h localhost -c 'select 1' > /dev/null" ,
4142 path => ' /usr/bin:/usr/local/bin:/bin' ,
4243 }
4344 }
Original file line number Diff line number Diff line change @@ -157,6 +157,45 @@ class { 'postgresql::server': }
157157 end
158158 end
159159
160+ describe 'custom postgres password' do
161+ it 'should install and successfully adjust the password' do
162+ pp = <<-EOS
163+ class { "postgresql::server":
164+ config_hash => {
165+ 'postgres_password' => 'TPSReports!',
166+ 'ip_mask_deny_postgres_user' => '0.0.0.0/32',
167+ },
168+ }
169+ EOS
170+
171+ puppet_apply ( pp ) do |r |
172+ [ 0 , 2 ] . should include ( r . exit_code )
173+ r . stdout . should =~ /\[ set_postgres_postgrespw\] \/ returns: executed successfully/
174+ end
175+ puppet_apply ( pp ) do |r |
176+ r . exit_code . should == 0
177+ end
178+
179+ pp = <<-EOS
180+ class { "postgresql::server":
181+ config_hash => {
182+ 'postgres_password' => 'TPSR$$eports!',
183+ 'ip_mask_deny_postgres_user' => '0.0.0.0/32',
184+ },
185+ }
186+ EOS
187+
188+ puppet_apply ( pp ) do |r |
189+ [ 0 , 2 ] . should include ( r . exit_code )
190+ r . stdout . should =~ /\[ set_postgres_postgrespw\] \/ returns: executed successfully/
191+ end
192+ puppet_apply ( pp ) do |r |
193+ r . exit_code . should == 0
194+ end
195+
196+ end
197+ end
198+
160199 describe 'postgresql::psql' do
161200 it 'should work but emit a deprecation warning' do
162201 pp = <<-EOS
Original file line number Diff line number Diff line change 1+ require 'spec_helper'
2+
3+ describe 'postgresql_escape' , :type => :puppet_function do
4+ it { should run . with_params ( 'foo' ) .
5+ and_return ( '$$foo$$' ) }
6+ end
7+ describe 'postgresql_escape' , :type => :puppet_function do
8+ it { should run . with_params ( 'fo$$o' ) .
9+ and_return ( '$ed$fo$$o$ed$' ) }
10+ end
You can’t perform that action at this time.
0 commit comments