Class: Hiera::Backend::Http_backend

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/http_backend.rb

Instance Method Summary collapse

Constructor Details

#initializeHttp_backend

Returns a new instance of Http_backend.



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
# File 'lib/hiera/backend/http_backend.rb', line 5

def initialize
  require 'net/http'
  require 'net/https'
  @config = Config[:http]

  @http = Net::HTTP.new(@config[:host], @config[:port])
  @http.read_timeout = @config[:http_read_timeout] || 10
  @http.open_timeout = @config[:http_connect_timeout] || 10

  @cache = {}
  @cache_timeout = @config[:cache_timeout] || 10
  @cache_clean_interval = @config[:cache_clean_interval] || 3600

  @regex_key_match = nil

  if confine_keys = @config[:confine_to_keys]
    confine_keys.map! { |r| Regexp.new(r) }
    @regex_key_match = Regexp.union(confine_keys)
  end


  if @config[:use_ssl]
    @http.use_ssl = true

    if @config[:ssl_verify] == false
      @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end

    if @config[:ssl_cert]
      store = OpenSSL::X509::Store.new
      store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
      @http.cert_store = store

      @http.key = OpenSSL::PKey::RSA.new(File.read(@config[:ssl_cert]))
      @http.cert = OpenSSL::X509::Certificate.new(File.read(@config[:ssl_key]))
    end
  else
    @http.use_ssl = false
  end
end

Instance Method Details

#lookup(key, scope, order_override, resolution_type) ⇒ Object



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
# File 'lib/hiera/backend/http_backend.rb', line 48

def lookup(key, scope, order_override, resolution_type)

  # if confine_to_keys is configured, then only proceed if one of the
  # regexes matches the lookup key
  #
  if @regex_key_match
    return nil unless key[@regex_key_match] == key
  end


  answer = nil

  paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) }
  paths.insert(0, order_override) if order_override


  paths.each do |path|

    Hiera.debug("[hiera-http]: Lookup #{key} from #{@config[:host]}:#{@config[:port]}#{path}")

    result = http_get_and_parse_with_cache(path)
    result = result[key] if result.is_a?(Hash)
    next if result.nil?

    parsed_result = Backend.parse_answer(result, scope)

    case resolution_type
    when :array
      answer ||= []
      answer << parsed_result
    when :hash
      answer ||= {}
      answer = Backend.merge_answer(parsed_result, answer)
    else
      answer = parsed_result
      break
    end
  end
  answer
end