newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / lib / puppet / parser / functions / postgresql_escape.rb
diff --git a/3rdparty/modules/postgresql/lib/puppet/parser/functions/postgresql_escape.rb b/3rdparty/modules/postgresql/lib/puppet/parser/functions/postgresql_escape.rb
new file mode 100644 (file)
index 0000000..1ec11b8
--- /dev/null
@@ -0,0 +1,25 @@
+require 'digest/md5'
+
+module Puppet::Parser::Functions
+  newfunction(:postgresql_escape, :type => :rvalue, :doc => <<-EOS
+    Safely escapes a string using $$ using a random tag which should be consistent
+    EOS
+  ) do |args|
+
+    raise(Puppet::ParseError, "postgresql_escape(): Wrong number of arguments " +
+      "given (#{args.size} for 1)") if args.size != 1
+
+    password = args[0]
+
+    if password !~ /\$\$/ and password[-1] != '$'
+      retval = "$$#{password}$$"
+    else
+      escape = Digest::MD5.hexdigest(password)[0..5].gsub(/\d/,'')
+      until password !~ /#{escape}/
+        escape = Digest::MD5.hexdigest(escape)[0..5].gsub(/\d/,'')
+      end
+      retval = "$#{escape}$#{password}$#{escape}$"
+    end
+    retval
+  end
+end