aboutsummaryrefslogtreecommitdiff
path: root/modules/base_installation/lib/puppet/parser/functions/generate_password.rb
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2017-08-30 22:16:39 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2017-08-31 22:59:53 +0200
commit548061112d2e2627317f9379d2f501fcf3f6ea54 (patch)
tree19fb949ad1fb965b4705897650809b4ffb6e9f29 /modules/base_installation/lib/puppet/parser/functions/generate_password.rb
parent1508e956adaa97b7a82c27537f4b124266dacdf0 (diff)
downloadPuppet-548061112d2e2627317f9379d2f501fcf3f6ea54.tar.gz
Puppet-548061112d2e2627317f9379d2f501fcf3f6ea54.tar.zst
Puppet-548061112d2e2627317f9379d2f501fcf3f6ea54.zip
Add LDAP support
Diffstat (limited to 'modules/base_installation/lib/puppet/parser/functions/generate_password.rb')
-rw-r--r--modules/base_installation/lib/puppet/parser/functions/generate_password.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/modules/base_installation/lib/puppet/parser/functions/generate_password.rb b/modules/base_installation/lib/puppet/parser/functions/generate_password.rb
new file mode 100644
index 0000000..384d81b
--- /dev/null
+++ b/modules/base_installation/lib/puppet/parser/functions/generate_password.rb
@@ -0,0 +1,31 @@
1module Puppet::Parser::Functions
2 newfunction(:generate_password, :type => :rvalue, :doc => <<-EOS
3Returns a semi-random string based on a seed and a value. Will always generate the same value with the same entry.
4Prototype:
5 generate_password(length, seed_file, password_key)
6EOS
7) do |*arguments|
8 arguments = arguments.shift if arguments.first.is_a?(Array)
9
10 raise Puppet::ParseError, "generate_password(): Wrong number of arguments " +
11 "given (#{arguments.size} for 3)" if arguments.size != 3
12
13 size = arguments.shift
14 seed_file = arguments.shift
15 password_key = arguments.shift
16
17 unless size.class.ancestors.include?(Numeric) or size.is_a?(String)
18 raise Puppet::ParseError, 'generate_password(): Requires a numeric first argument'
19 end
20
21 size = size.to_i
22
23 set = ('a' .. 'z').to_a + ('A' .. 'Z').to_a + ('0' .. '9').to_a
24
25 key = "#{File.open(seed_file).read}:#{password_key}"
26
27 size.times.collect do |i|
28 set[OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, i.to_s).to_i(16) % set.size]
29 end.join
30end
31end