puppet 4 foo
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / gen_tlsa_entry.rb
1 module Puppet::Parser::Functions
2   newfunction(:gen_tlsa_entry, :type => :rvalue) do |args|
3     certfile = args.shift()
4     hostname = args.shift()
5     port = args.shift()
6
7     if port.kind_of?(Array)
8       ports = port
9     else
10       ports = [port]
11     end
12
13     if certfile.kind_of?(Array)
14       certs = certfile
15     else
16       certs = [ certfile ]
17     end
18
19     res = []
20     certs.each do |certfile|
21       res << "; cert #{certfile} for #{hostname}:#{ports}."
22       ports.each do |port|
23         if File.exist?(certfile)
24           cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', certfile, '--port', port.to_s, hostname]
25           IO.popen(cmd, "r") {|i| res << i.read }
26         else
27           res << "; certfile #{certfile} did not exist to create TLSA record for #{hostname}:#{port}."
28         end
29
30         cfnew = certfile.gsub(/\.crt$/, '-new.crt')
31         if cfnew != certfile and File.exist?(cfnew)
32           cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', cfnew, '--port', port.to_s, hostname]
33           new_entry = ''
34           IO.popen(cmd, "r") {|i| new_entry = i.read }
35           if not res.include?(new_entry)
36             res << new_entry
37           end
38         end
39       end
40     end
41
42     return res.join("\n")
43   end
44 end