Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 120 lines (98 sloc) 2.892 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
require 'rexml/document'
require 'net/http'

module Puppet::Parser::Functions
  newfunction(:ssh_public_keys, :type => :rvalue) do |args|
    hashindex = args[0]

    servermgmt_host = lookupvar('servermgmt_host')
    servermgmt_secret = lookupvar('servermgmt_secret')
    fqdn = lookupvar('fqdn')
    sshkeys = Hash.new
    cache_path = lookupvar("servermgmt_cache_path")

    if !cache_path or !File.directory?(cache_path)
      raise Puppet::ParseError, "Please set `servermgmt_cache_path`"
    end

    def cache_save(cache_path, fqdn, content)
      begin
        File.open(File.join(cache_path, fqdn), 'w') { |f|
          f.write(content)
        }
      rescue Exception => e
        raise Puppet::ParseError, "Could not write cache data to cache at #{File.join(cache_path, fqdn)}"
      end
    end

    def cache_read(cache_path, fqdn)
      begin
        File.open(File.join(cache_path, fqdn), 'r') { |f|
          return f.read
        }
      rescue

      end
      false
    end

    def parse_xml(content)

      sshkeys = Hash.new
      xml = REXML::Document.new(content)
      xml.elements.each("//username") { |element|
        username = element.attributes['name']
        username_keys = Array.new
        element.elements.each("key") { |key|
          username_keys << key.text
        }

        sshkeys[username] = username_keys.join("\n")
      }
      sshkeys
    end


    begin

      tries = 0
      begin
        uri = URI.parse(servermgmt_host + fqdn)
        http = Net::HTTP.new(uri.host, uri.port)
        request = Net::HTTP::Post.new(uri.request_uri)
        request.set_form_data({"secret" => servermgmt_secret});
      rescue
        if tries < 2 then
          tries += 1
          retry
        else
          raise "Could not build URI and Net::HTTP"
        end
      end

      tries = 0
      begin
        response = http.request(request)
      rescue
        if tries < 2 then
          tries += 1
          retry
        else
          cache = cache_read(cache_path, fqdn)

          if cache
            Puppet.notice "SSH public key webservice not available, used cached version"
            sshkeys = parse_xml(cache)

          else
            raise "Could not connect to host"
          end
        end
      end

      if response and response.code == '200'
        sshkeys = parse_xml(response.body)
        cache_save(cache_path, fqdn, response.body)
      else
        raise "Response from ServerMGMT was: #{response.code}"
      end

    rescue Exception => e
      cache = cache_read(cache_path, fqdn)

      if cache
        Puppet.notice "SSH public key webservice not available, used cached version"
        sshkeys = parse_xml(cache)

      else
        raise Puppet::ParseError, "Could not connect to Server Manager: #{e.message}"
      end
    end

    if hashindex
      sshkeys.keys
    else
      sshkeys
    end

  end
end
Something went wrong with that request. Please try again.