hiera-http 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63ab1df0ab23f968b79343ec215cffc367e0769d
4
- data.tar.gz: 7da4074c7417b661010bb7986b9788dbf7b3fd3a
3
+ metadata.gz: a17fac1e7bb38106d49186388941c002cf549eab
4
+ data.tar.gz: e53899e4ecc6f521e9d034154c4db3335efe2093
5
5
  SHA512:
6
- metadata.gz: 2a87f6180b5712df95d7d54a91c11ee50b90a5df488fc0840decd6a59de30d5886afb0ea2182335d6be53bbde17677c369eccdc13abb92ebae4f2d3d286f8081
7
- data.tar.gz: ef924a36d8c2b836b656b1be962ff5ec360b2c1543609f08f9560da090776ad3f8a4f7cd72fac1305a1d9da712e5112687e4b482450c003eb47e49f1e3608eb1
6
+ metadata.gz: 6d556293426068e0e2fb7fcc9f685927650e6cda109f419cd3c907d3c76f7ae21681546841548832e997eae50b51b1e19163dca0e5d07d8b1b467dbd214f9d08
7
+ data.tar.gz: 953bd0e2c5a9f5ea625166e628f01cef2e8375983fccff37b845baf625fe4c82cfceea89e844cb08c9670b507d3a118fb0b6877b852c99e673aab418d6187aa3
@@ -3,46 +3,37 @@ class Hiera
3
3
  class Http_backend
4
4
 
5
5
  def initialize
6
- require 'net/http'
7
- require 'net/https'
6
+ require 'lookup_http'
8
7
  @config = Config[:http]
9
-
10
- @http = Net::HTTP.new(@config[:host], @config[:port])
11
- @http.read_timeout = @config[:http_read_timeout] || 10
12
- @http.open_timeout = @config[:http_connect_timeout] || 10
8
+
9
+ lookup_supported_params = [
10
+ :host,
11
+ :port,
12
+ :output,
13
+ :failure,
14
+ :ignore_404,
15
+ :headers,
16
+ :http_connect_timeout,
17
+ :http_read_timeout,
18
+ :paths,
19
+ :use_ssl,
20
+ :ssl_ca_cert,
21
+ :ssl_cert,
22
+ :ssl_key,
23
+ :ssl_verify,
24
+ :use_auth,
25
+ :auth_user,
26
+ :auth_pass,
27
+ ]
28
+ lookup_params = @config.select { |p| lookup_supported_params.include?(p) }
29
+
30
+ @lookup = LookupHttp.new(lookup_params.merge( { :debug_log => "Hiera.debug" } ))
31
+
13
32
 
14
33
  @cache = {}
15
34
  @cache_timeout = @config[:cache_timeout] || 10
16
35
  @cache_clean_interval = @config[:cache_clean_interval] || 3600
17
36
 
18
- @regex_key_match = nil
19
-
20
- if confine_keys = @config[:confine_to_keys]
21
- confine_keys.map! { |r| Regexp.new(r) }
22
- @regex_key_match = Regexp.union(confine_keys)
23
- end
24
-
25
-
26
- if @config[:use_ssl]
27
- @http.use_ssl = true
28
-
29
- if @config[:ssl_verify] == false
30
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
31
- else
32
- @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
33
- end
34
-
35
- if @config[:ssl_cert]
36
- store = OpenSSL::X509::Store.new
37
- store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
38
- @http.cert_store = store
39
-
40
- @http.key = OpenSSL::PKey::RSA.new(File.read(@config[:ssl_cert]))
41
- @http.cert = OpenSSL::X509::Certificate.new(File.read(@config[:ssl_key]))
42
- end
43
- else
44
- @http.use_ssl = false
45
- end
46
37
  end
47
38
 
48
39
  def lookup(key, scope, order_override, resolution_type)
@@ -89,39 +80,9 @@ class Hiera
89
80
 
90
81
  private
91
82
 
92
- def parse_response(answer)
93
- return unless answer
94
-
95
- format = @config[:output] || 'plain'
96
- Hiera.debug("[hiera-http]: Query returned data, parsing response as #{format}")
97
-
98
- case format
99
- when 'json'
100
- parse_json answer
101
- when 'yaml'
102
- parse_yaml answer
103
- else
104
- answer
105
- end
106
- end
107
-
108
- # Handlers
109
- # Here we define specific handlers to parse the output of the http request
110
- # and return its structured representation. Currently we support YAML and JSON
111
- #
112
- def parse_json(answer)
113
- require 'rubygems'
114
- require 'json'
115
- JSON.parse(answer)
116
- end
117
-
118
- def parse_yaml(answer)
119
- require 'yaml'
120
- YAML.load(answer)
121
- end
122
83
 
123
84
  def http_get_and_parse_with_cache(path)
124
- return http_get_and_parse(path) if @cache_timeout <= 0
85
+ return @lookup.get_parsed(path) if @cache_timeout <= 0
125
86
 
126
87
  now = Time.now.to_i
127
88
  expired_at = now + @cache_timeout
@@ -133,39 +94,12 @@ class Hiera
133
94
  if !@cache[path] || @cache[path][:expired_at] < now
134
95
  @cache[path] = {
135
96
  :expired_at => expired_at,
136
- :result => http_get_and_parse(path)
97
+ :result => @lookup.get_parsed(path)
137
98
  }
138
99
  end
139
100
  @cache[path][:result]
140
101
  end
141
102
 
142
- def http_get_and_parse(path)
143
- httpreq = Net::HTTP::Get.new(path)
144
-
145
- if @config[:use_auth]
146
- httpreq.basic_auth @config[:auth_user], @config[:auth_pass]
147
- end
148
-
149
- begin
150
- httpres = @http.request(httpreq)
151
- rescue Exception => e
152
- Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}")
153
- raise Exception, e.message unless @config[:failure] == 'graceful'
154
- return
155
- end
156
-
157
- unless httpres.kind_of?(Net::HTTPSuccess)
158
- Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
159
- Hiera.debug("HTTP response code was #{httpres.code}")
160
- unless httpres.code == '404' && @config[:ignore_404]
161
- raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
162
- end
163
- return
164
- end
165
-
166
- parse_response httpres.body
167
- end
168
-
169
103
 
170
104
  def periodically_clean_cache(now)
171
105
  return if now < @clean_cache_at.to_i
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Dunn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: lookup_http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
27
41
  description: Hiera backend for looking up data over HTTP APIs
28
42
  email: craig@craigdunn.org
29
43
  executables: []