Skip to content

Commit b2b9b13

Browse files
committed
Merge pull request puppetlabs#218 from fiddyspence/set_postgres_postgrespw
Alter escaping in postgresql::config::afterservice
2 parents 9d753b4 + eadfe3b commit b2b9b13

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

manifests/config/afterservice.pp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
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,
@@ -37,7 +38,7 @@
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
}

spec/system/install_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

0 commit comments

Comments
 (0)