aboutsummaryrefslogtreecommitdiff
path: root/modules/base_installation/lib/puppet/parser/functions/generate_password.rb
blob: 384d81b7c15fbcbfae70c964b52a023f18fbe36e (plain) (blame)
1
2
3
4
5
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
module Puppet::Parser::Functions
  newfunction(:generate_password, :type => :rvalue, :doc => <<-EOS
Returns a semi-random string based on a seed and a value. Will always generate the same value with the same entry.
Prototype:
  generate_password(length, seed_file, password_key)
EOS
) do |*arguments|
  arguments = arguments.shift if arguments.first.is_a?(Array)

  raise Puppet::ParseError, "generate_password(): Wrong number of arguments " +
    "given (#{arguments.size} for 3)" if arguments.size != 3

  size = arguments.shift
  seed_file = arguments.shift
  password_key = arguments.shift

  unless size.class.ancestors.include?(Numeric) or size.is_a?(String)
    raise Puppet::ParseError, 'generate_password(): Requires a numeric first argument'
  end

  size = size.to_i

  set = ('a' .. 'z').to_a + ('A' .. 'Z').to_a + ('0' .. '9').to_a

  key = "#{File.open(seed_file).read}:#{password_key}"

  size.times.collect do |i|
    set[OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, i.to_s).to_i(16) % set.size]
  end.join
end
end