Puppet Function: postgresql_escape

Defined in:
lib/puppet/parser/functions/postgresql_escape.rb
Function type:
Ruby 3.x API

Overview

postgresql_escape()Any

This function safely escapes a string using a consistent random tag

Returns:

  • (Any)

    Safely escapes a string using $$ using a random tag which should be consistent



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/parser/functions/postgresql_escape.rb', line 5

newfunction(:postgresql_escape, type: :rvalue, doc: <<-EOS
  This function safely escapes a string using a consistent random tag
  @return Safely escapes a string using $$ using a random tag which should be consistent
  EOS
           ) do |args|

  if args.size != 1
    raise(Puppet::ParseError, 'postgresql_escape(): Wrong number of arguments ' \
      "given (#{args.size} for 1)")
  end

  password = args[0]

  if password !~ %r{\$\$} && password[-1] != '$'
    retval = "$$#{password}$$"
  else
    escape = Digest::MD5.hexdigest(password)[0..5].gsub(%r{\d}, '')
    until password !~ %r{#{escape}}
      escape = Digest::MD5.hexdigest(escape)[0..5].gsub(%r{\d}, '')
    end
    retval = "$#{escape}$#{password}$#{escape}$"
  end
  retval
end