|
| 1 | +module Puppet::Parser::Functions |
| 2 | + newfunction(:get_certificate, :type => :rvalue, :doc => <<-EOS |
| 3 | +Returns the public certificate of the given CN from the local or remote Puppet |
| 4 | +CA. |
| 5 | +
|
| 6 | +Usage is: |
| 7 | +
|
| 8 | + get_certificate($cn, $options) |
| 9 | +
|
| 10 | +The first argument $cn is a valid CN for the certificate you wish to |
| 11 | +return. A CN is usually the hostname of a machine in Puppet. You can view all available |
| 12 | +certificates using the facility: |
| 13 | +
|
| 14 | + puppet cert --list --all |
| 15 | +
|
| 16 | +On the main CA or puppetmaster. |
| 17 | +
|
| 18 | +The second argument $options allows the user to define a hash of options to |
| 19 | +pass to the function. |
| 20 | +
|
| 21 | +The options and descriptions are: |
| 22 | +
|
| 23 | +* *conn_timeout*: Adjust timeout for remote CA connectivity (in seconds). Default is 7. |
| 24 | + EOS |
| 25 | + ) do |arguments| |
| 26 | + |
| 27 | + # Make sure we have enough arguments |
| 28 | + if not (1..2).include?(arguments.size) then |
| 29 | + raise(Puppet::ParseError, "get_certificate(): Wrong number of arguments " + |
| 30 | + "given (#{arguments.size} for 1 or 2)") |
| 31 | + end |
| 32 | + |
| 33 | + # Obtain arguments and set defaults |
| 34 | + cn = arguments[0] |
| 35 | + options = arguments[1] ||= {} |
| 36 | + options[:conn_timeout] = 7 if !options.has_key?(:conn_timeout) |
| 37 | + |
| 38 | + # Validation of arguments |
| 39 | + if not (cn.is_a?(String) and cn.match(/^[a-zA-Z0-9@_\-\.]+$/)) then |
| 40 | + raise(Puppet::ParseError, 'get_certificate(): CN name must be a valid string. Hashes and Arrays are not valid') |
| 41 | + end |
| 42 | + if not (1..600).include?(options[:conn_timeout]) then |
| 43 | + raise(Puppet::ParseError, "get_certificate(): The option 'conn_timeout' must be an integer between 1 and 600") |
| 44 | + end |
| 45 | + |
| 46 | + # Get and return certificate using file or rest |
| 47 | + if Puppet[:ca] == true then |
| 48 | + # Get the certificate locally if we are acting as a CA |
| 49 | + # TODO: wrap: puppet certificate --render-as s --ca-location remote find [email protected] |
| 50 | + ssl_cert_path = Puppet[:signeddir] + "/" + cn + ".pem" |
| 51 | + if FileTest.exists?(ssl_cert_path) then |
| 52 | + cert = File.open(ssl_cert_path,"r") |
| 53 | + return cert.read |
| 54 | + end |
| 55 | + else |
| 56 | + # Obtain the certificate from the CA if its remote |
| 57 | + # TODO: wrap: puppet certificate --render-as s --ca-location local find [email protected] |
| 58 | + require 'net/http' |
| 59 | + require 'net/https' |
| 60 | + |
| 61 | + http = Net::HTTP.new(Puppet[:ca_server], Puppet[:ca_port]) |
| 62 | + http.use_ssl = true |
| 63 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 64 | + |
| 65 | + begin |
| 66 | + res = timeout(options[:conn_timeout]) do |
| 67 | + http.start {|h| |
| 68 | + h.get("/production/certificate/#{cn}", { "Accept" => "s" }) |
| 69 | + } |
| 70 | + end |
| 71 | + rescue Timeout::Error |
| 72 | + raise(Puppet::Error, "Transaction timed out when connecting to #{Puppet[:ca_server]}:#{Puppet[:ca_port]}. Check your CA is running and that your ca_server and ca_port settings are correct on the machine this function ran on.") |
| 73 | + rescue Errno::ECONNREFUSED |
| 74 | + raise(Puppet::Error, "Connection refused when connecting to #{Puppet[:ca_server]}:#{Puppet[:ca_port]}. Check your CA is running and that your ca_server and ca_port settings are correct on the machine this function ran on.") |
| 75 | + end |
| 76 | + |
| 77 | + case res.code |
| 78 | + when "200" |
| 79 | + return res.body if res.body |
| 80 | + when "404" |
| 81 | + return :undef |
| 82 | + else |
| 83 | + raise(Puppet::Error, "Error with REST call: #{res.code}") |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + :undef |
| 88 | + end |
| 89 | +end |
0 commit comments