handle array of certfiles
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / gen_tlsa_entry.rb
index 7b846b9..e2d0a60 100644 (file)
@@ -4,20 +4,38 @@ module Puppet::Parser::Functions
     hostname = args.shift()
     port = args.shift()
 
-    res = []
-    if File.exist?(certfile)
-      cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', certfile, '--port', port.to_s, hostname]
-      IO.popen(cmd, "r") {|i| res << i.read }
+    if port.kind_of?(Array)
+      ports = port
+    else
+      ports = [port]
+    end
+
+    if certfile.kind_of?(Array)
+      certs = certfile
     else
-      res << "; certfile #{certfile} did not exist to create TLSA record for #{hostname}:#{port}."
+      certs = [ certfile ]
     end
 
-    if certfile.gsub!(/\.crt$/, '-new.crt') and File.exist?(certfile)
-      cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', certfile, '--port', port.to_s, hostname]
-      new_entry = ''
-      IO.popen(cmd, "r") {|i| new_entry = i.read }
-      if not res.include?(new_entry)
-        res << new_entry
+    res = []
+    certs.each do |certfile|
+      res << "; cert #{certfile} for #{hostname}:#{ports}."
+      ports.each do |port|
+        if File.exist?(certfile)
+          cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', certfile, '--port', port.to_s, hostname]
+          IO.popen(cmd, "r") {|i| res << i.read }
+        else
+          res << "; certfile #{certfile} did not exist to create TLSA record for #{hostname}:#{port}."
+        end
+
+        cfnew = certfile.gsub(/\.crt$/, '-new.crt')
+        if cfnew != certfile and File.exist?(cfnew)
+          cmd = ['swede', 'create', '--usage=3', '--selector=1', '--mtype=1', '--certificate', cfnew, '--port', port.to_s, hostname]
+          new_entry = ''
+          IO.popen(cmd, "r") {|i| new_entry = i.read }
+          if not res.include?(new_entry)
+            res << new_entry
+          end
+        end
       end
     end