Puppet Function: postgresql::postgresql_escape

Defined in:
lib/puppet/functions/postgresql/postgresql_escape.rb
Function type:
Ruby 4.x API

Summary

This function safely escapes a string using a consistent random tag

Overview

postgresql::postgresql_escape(Any *$args)Data type

postgresql_escape.rb —- original file header —-

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

Parameters:

  • *args (Any)

    The original array of arguments. Port this to individually managed params to get the full benefit of the modern function API.

Returns:

  • (Data type)

    Describe what the function returns here



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/functions/postgresql/postgresql_escape.rb', line 19

Puppet::Functions.create_function(:'postgresql::postgresql_escape') do
  # @param args
  #   The original array of arguments. Port this to individually managed params
  #   to get the full benefit of the modern function API.
  #
  # @return [Data type]
  #   Describe what the function returns here
  #
  dispatch :default_impl do
    # Call the method named 'default_impl' when this is matched
    # Port this to match individual params for better type safety
    repeated_param 'Any', :args
  end

  def default_impl(*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
end