e325fe73233e14a4741201f583858624f68f85b8
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / fqdn_uuid.rb
1 require 'digest/sha1'
2 #
3 # fqdn_uuid.rb
4 #
5 module Puppet::Parser::Functions
6   newfunction(:fqdn_uuid, :type => :rvalue, :doc => <<-DOC) do |args|
7     @summary
8       Returns a [RFC 4122](https://tools.ietf.org/html/rfc4122) valid version 5 UUID based
9       on an FQDN string under the DNS namespace
10
11     @return
12       Returns a [RFC 4122](https://tools.ietf.org/html/rfc4122) valid version 5 UUID
13
14     @example Example Usage:
15       fqdn_uuid('puppetlabs.com') # Returns '9c70320f-6815-5fc5-ab0f-debe68bf764c'
16       fqdn_uuid('google.com') # Returns '64ee70a4-8cc1-5d25-abf2-dea6c79a09c8'
17     DOC
18
19     raise(ArgumentError, 'fqdn_uuid: No arguments given') if args.empty?
20     raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})") unless args.length == 1
21     fqdn = args[0]
22
23     # Code lovingly taken from
24     # https://github.com/puppetlabs/marionette-collective/blob/master/lib/mcollective/ssl.rb
25
26     # This is the UUID version 5 type DNS name space which is as follows:
27     #
28     #  6ba7b810-9dad-11d1-80b4-00c04fd430c8
29     #
30     uuid_name_space_dns = [0x6b,
31                            0xa7,
32                            0xb8,
33                            0x10,
34                            0x9d,
35                            0xad,
36                            0x11,
37                            0xd1,
38                            0x80,
39                            0xb4,
40                            0x00,
41                            0xc0,
42                            0x4f,
43                            0xd4,
44                            0x30,
45                            0xc8].map { |b| b.chr }.join
46
47     sha1 = Digest::SHA1.new
48     sha1.update(uuid_name_space_dns)
49     sha1.update(fqdn)
50
51     # first 16 bytes..
52     bytes = sha1.digest[0, 16].bytes.to_a
53
54     # version 5 adjustments
55     bytes[6] &= 0x0f
56     bytes[6] |= 0x50
57
58     # variant is DCE 1.1
59     bytes[8] &= 0x3f
60     bytes[8] |= 0x80
61
62     bytes = [4, 2, 2, 2, 6].map do |i|
63       bytes.slice!(0, i).pack('C*').unpack('H*')
64     end
65
66     bytes.join('-')
67   end
68 end