Ignore missing swede
[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           begin
26             IO.popen(cmd, "r") {|i| res << i.read }
27           rescue Errno::ENOENT
28             res << "; Failed to find swede"
29           end
30         else
31           res << "; certfile #{certfile} did not exist to create TLSA record for #{hostname}:#{port}."
32         end
33
34         cfnew = certfile.gsub(/\.crt$/, '-new.crt')
35         if cfnew != certfile and File.exist?(cfnew)
36           cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', cfnew, '--port', port.to_s, hostname]
37           new_entry = ''
38           IO.popen(cmd, "r") {|i| new_entry = i.read }
39           if not res.include?(new_entry)
40             res << new_entry
41           end
42         end
43       end
44     end
45
46     return res.join("\n")
47   end
48 end