newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / lib / puppet / parser / functions / postgresql_escape.rb
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 !~ /\$\$/ and password[-1] != '$'
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