Puppet Function: simplib::gen_random_password

Defined in:
lib/puppet/functions/simplib/gen_random_password.rb
Function type:
Ruby 4.x API

Overview

simplib::gen_random_password(Integer[8] $length, Optional[Integer[0,2]] $complexity, Optional[Boolean] $complex_only, Optional[Variant[Integer[0],Float[0]]] $timeout_seconds)String

Generates a random password string.

Terminates catalog compilation if the password cannot be created in the allotted time.

Parameters:

  • length (Integer[8])

    Length of the new password.

  • complexity (Optional[Integer[0,2]])

    Specifies the types of characters to be used in the password

    • ‘0` => Use only Alphanumeric characters (safest)

    • ‘1` => Use Alphanumeric characters and reasonably safe symbols

    • ‘2` => Use any printable ASCII characters

  • complex_only (Optional[Boolean])

    Use only the characters explicitly added by the complexity rules

  • timeout_seconds (Optional[Variant[Integer[0],Float[0]]])

    Maximum time allotted to generate the password; a value of 0 disables the timeout

Returns:

  • (String)

    Generated password

Raises:

  • (RuntimeError)

    if password cannot be created within allotted time



6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/functions/simplib/gen_random_password.rb', line 6

Puppet::Functions.create_function(:'simplib::gen_random_password') do

  # @param length Length of the new password.
  #
  # @param complexity Specifies the types of characters to be used in the password
  #   * `0` => Use only Alphanumeric characters (safest)
  #   * `1` => Use Alphanumeric characters and reasonably safe symbols
  #   * `2` => Use any printable ASCII characters
  #
  # @param complex_only Use only the characters explicitly added by the complexity rules
  #
  # @param timeout_seconds Maximum time allotted to generate
  #    the password; a value of 0 disables the timeout
  #
  # @return [String] Generated password
  #
  # @raise [RuntimeError] if password cannot be created within allotted time
  dispatch :gen_random_password do
    required_param 'Integer[8]',                   :length
    optional_param 'Integer[0,2]',                 :complexity
    optional_param 'Boolean',                      :complex_only
    optional_param 'Variant[Integer[0],Float[0]]', :timeout_seconds
  end

  def gen_random_password(length, complexity=nil, complex_only=false, timeout_seconds = 30)
    require 'timeout'
    passwd = ''
    Timeout::timeout(timeout_seconds) do
      lower_charlist = ('a'..'z').to_a
      upper_charlist = ('A'..'Z').to_a
      digit_charlist = ('0'..'9').to_a
      symbol_charlist = nil
      case complexity
        when 1
          symbol_charlist = ['@','%','-','_','+','=','~']
        when 2
          symbol_charlist = (' '..'/').to_a + ('['..'`').to_a + ('{'..'~').to_a
        else
      end

      unless symbol_charlist.nil?
        if complex_only == true
          charlists = [
            symbol_charlist
          ]
        else
          charlists = [
            lower_charlist,
            upper_charlist,
            digit_charlist,
            symbol_charlist
          ]
        end

      else
        charlists = [
          lower_charlist,
          upper_charlist,
          digit_charlist
        ]
      end

      last_list_rand = nil
      last_char_rand = nil
      Integer(length).times do |i|
        rand_list_index = rand(charlists.length).floor

        if rand_list_index == last_list_rand
          rand_list_index = rand_list_index-1
        end

        last_list_rand = rand_list_index

        rand_index = rand(charlists[rand_list_index].length).floor

        if rand_index == last_char_rand
          rand_index = rand_index-1
        end

        passwd += charlists[rand_list_index][rand_index]

        last_char_rand = rand_index
      end
    end

    return passwd
  end
end