5 module Puppet::Parser::Functions
6 newfunction(:fqdn_uuid, :type => :rvalue, :doc => <<-DOC) do |args|
7 Creates a UUID based on a given string, assumed to be the FQDN
9 For example, to generate a UUID based on the FQDN of a system:
13 $uuid = fqdn_uuid($::fqdn)
15 The generated UUID will be the same for the given hostname
17 The resulting UUID is returned on the form:
19 1d839dea-5e10-5243-88eb-e66815bd7d5c
21 (u.e. without any curly braces.)
23 The generated UUID is a version 5 UUID with the V5 DNS namespace:
25 6ba7b810-9dad-11d1-80b4-00c04fd430c8
27 This only supports a the V5 SHA-1 hash, using the DNS namespace.
29 Please consult http://www.ietf.org/rfc/rfc4122.txt for the details on
30 UUID generation and example implementation.
32 No verification is present at the moment as whether the domain name given
33 is in fact a correct fully-qualified domain name. Therefore any arbitrary
34 string and/or alpha-numeric value can subside for a domain name.
37 raise(ArgumentError, 'fqdn_uuid: No arguments given') if args.empty?
38 raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})") unless args.length == 1
41 # Code lovingly taken from
42 # https://github.com/puppetlabs/marionette-collective/blob/master/lib/mcollective/ssl.rb
44 # This is the UUID version 5 type DNS name space which is as follows:
46 # 6ba7b810-9dad-11d1-80b4-00c04fd430c8
48 uuid_name_space_dns = [0x6b,
63 0xc8].map { |b| b.chr }.join
65 sha1 = Digest::SHA1.new
66 sha1.update(uuid_name_space_dns)
70 bytes = sha1.digest[0, 16].bytes.to_a
72 # version 5 adjustments
80 bytes = [4, 2, 2, 2, 6].map do |i|
81 bytes.slice!(0, i).pack('C*').unpack('H*')