hiera-http 0.0.2 → 1.3.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hiera/backend/http_backend.rb +83 -45
  3. metadata +37 -53
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e885355dcd8030bc8ec2147a964dac179edc9fd
4
+ data.tar.gz: 7465b53a13d54122afe45511c07a53eda4dcde4e
5
+ SHA512:
6
+ metadata.gz: 5864399b5f14e23bd5873d6f57413c2fba8d6e60053872f3ad4b96299a59c8236a81fd3a018d886dd11978c3ed1c1941dfe81337fb9e8817066ca8f8018f7261
7
+ data.tar.gz: a5fd96d518809b4f962c78f9fd778c23b9c93231a61f1575af945250005d408b47774ebaeb8ea8e7617c96ac9c19642cc72b2a07e610968e52901ed5af45b5e0
@@ -11,10 +11,20 @@ class Hiera
11
11
  @http.read_timeout = @config[:http_read_timeout] || 10
12
12
  @http.open_timeout = @config[:http_connect_timeout] || 10
13
13
 
14
+ @cache = {}
15
+ @cache_timeout = @config[:cache_timeout] || 10
16
+ @cache_clean_interval = @config[:cache_clean_interval] || 3600
17
+
14
18
  if @config[:use_ssl]
15
19
  @http.use_ssl = true
16
- if @config[:ssl_cert]
20
+
21
+ if @config[:ssl_verify] == false
22
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
+ else
17
24
  @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
25
+ end
26
+
27
+ if @config[:ssl_cert]
18
28
  store = OpenSSL::X509::Store.new
19
29
  store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
20
30
  @http.cert_store = store
@@ -28,7 +38,6 @@ class Hiera
28
38
  end
29
39
 
30
40
  def lookup(key, scope, order_override, resolution_type)
31
-
32
41
  answer = nil
33
42
 
34
43
  paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) }
@@ -38,25 +47,10 @@ class Hiera
38
47
  paths.each do |path|
39
48
 
40
49
  Hiera.debug("[hiera-http]: Lookup #{key} from #{@config[:host]}:#{@config[:port]}#{path}")
41
- httpreq = Net::HTTP::Get.new(path)
42
-
43
- begin
44
- httpres = @http.request(httpreq)
45
- rescue Exception => e
46
- Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}")
47
- raise Exception, e.message unless @config[:failure] == 'graceful'
48
- next
49
- end
50
50
 
51
- unless httpres.kind_of?(Net::HTTPSuccess)
52
- Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
53
- Hiera.debug("HTTP response code was #{httpres.code}")
54
- raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
55
- next
56
- end
57
-
58
- result = self.parse_response(key, httpres.body)
59
- next unless result
51
+ result = http_get_and_parse_with_cache(path)
52
+ result = result[key] if result.is_a?(Hash)
53
+ next if result.nil?
60
54
 
61
55
  parsed_result = Backend.parse_answer(result, scope)
62
56
 
@@ -66,7 +60,7 @@ class Hiera
66
60
  answer << parsed_result
67
61
  when :hash
68
62
  answer ||= {}
69
- answer = parsed_result.merge answer
63
+ answer = Backend.merge_answer(parsed_result, answer)
70
64
  else
71
65
  answer = parsed_result
72
66
  break
@@ -76,30 +70,19 @@ class Hiera
76
70
  end
77
71
 
78
72
 
79
- def parse_response(key,answer)
73
+ private
80
74
 
81
- return nil unless answer
75
+ def parse_response(answer)
76
+ return unless answer
82
77
 
83
- Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}")
78
+ format = @config[:output] || 'plain'
79
+ Hiera.debug("[hiera-http]: Query returned data, parsing response as #{format}")
84
80
 
85
- case @config[:output]
86
-
87
- when 'plain'
88
- # When the output format is configured as plain we assume that if the
89
- # endpoint URL returns an HTTP success then the contents of the response
90
- # body is the value itself, or nil.
91
- #
92
- answer
81
+ case format
93
82
  when 'json'
94
- # If JSON is specified as the output format, assume the output of the
95
- # endpoint URL is a JSON document and return keypart that matched our
96
- # lookup key
97
- self.json_handler(key,answer)
83
+ parse_json answer
98
84
  when 'yaml'
99
- # If YAML is specified as the output format, assume the output of the
100
- # endpoint URL is a YAML document and return keypart that matched our
101
- # lookup key
102
- self.yaml_handler(key,answer)
85
+ parse_yaml answer
103
86
  else
104
87
  answer
105
88
  end
@@ -107,19 +90,74 @@ class Hiera
107
90
 
108
91
  # Handlers
109
92
  # Here we define specific handlers to parse the output of the http request
110
- # and return a value. Currently we support YAML and JSON
93
+ # and return its structured representation. Currently we support YAML and JSON
111
94
  #
112
- def json_handler(key,answer)
95
+ def parse_json(answer)
113
96
  require 'rubygems'
114
97
  require 'json'
115
- JSON.parse(answer)[key]
98
+ JSON.parse(answer)
116
99
  end
117
100
 
118
- def yaml_handler(answer)
101
+ def parse_yaml(answer)
119
102
  require 'yaml'
120
- YAML.parse(answer)[key]
103
+ YAML.parse(answer)
121
104
  end
122
105
 
106
+ def http_get_and_parse_with_cache(path)
107
+ return http_get(path) if @cache_timeout <= 0
108
+
109
+ now = Time.now.to_i
110
+ expired_at = now + @cache_timeout
111
+
112
+ # Deleting all stale cache entries can be expensive. Do not do it every time
113
+ periodically_clean_cache(now, expired_at) unless @cache_clean_interval == 0
114
+
115
+ # Just refresh the entry being requested for performance
116
+ unless @cache[path] && @cache[path][:created_at] < expired_at
117
+ @cache[path] = {
118
+ :created_at => now,
119
+ :result => http_get_and_parse(path)
120
+ }
121
+ end
122
+ @cache[path][:result]
123
+ end
124
+
125
+ def http_get_and_parse(path)
126
+ httpreq = Net::HTTP::Get.new(path)
127
+
128
+ if @config[:use_auth]
129
+ httpreq.basic_auth @config[:auth_user], @config[:auth_pass]
130
+ end
131
+
132
+ begin
133
+ httpres = @http.request(httpreq)
134
+ rescue Exception => e
135
+ Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}")
136
+ raise Exception, e.message unless @config[:failure] == 'graceful'
137
+ return
138
+ end
139
+
140
+ unless httpres.kind_of?(Net::HTTPSuccess)
141
+ Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
142
+ Hiera.debug("HTTP response code was #{httpres.code}")
143
+ unless httpres.code == '404' && @config[:ignore_404]
144
+ raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
145
+ end
146
+ return
147
+ end
148
+
149
+ parse_response httpres.body
150
+ end
151
+
152
+
153
+ def periodically_clean_cache(now, expired_at)
154
+ return if now < @clean_cache_at.to_i
155
+
156
+ @clean_cache_at = now + @cache_clean_interval
157
+ @cache.select! do |_, entry|
158
+ entry[:created_at] < expired_at
159
+ end
160
+ end
123
161
  end
124
162
  end
125
163
  end
metadata CHANGED
@@ -1,77 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hiera-http
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Craig Dunn
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-06 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: hiera
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 0.3.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 0.3.0
30
- - !ruby/object:Gem::Dependency
31
- name: json
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
11
+
12
+ date: 2015-03-18 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
37
19
  version: 1.1.1
38
- type: :runtime
20
+ version_requirements: *id001
39
21
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: 1.1.1
22
+ type: :runtime
23
+ name: json
46
24
  description: Hiera backend for looking up data over HTTP APIs
47
25
  email: craig@craigdunn.org
48
26
  executables: []
27
+
49
28
  extensions: []
29
+
50
30
  extra_rdoc_files: []
51
- files:
31
+
32
+ files:
52
33
  - lib/hiera/backend/http_backend.rb
53
34
  homepage: http://github.com/crayfishx/hiera-http
54
35
  licenses: []
36
+
37
+ metadata: {}
38
+
55
39
  post_install_message:
56
40
  rdoc_options: []
57
- require_paths:
41
+
42
+ require_paths:
58
43
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
60
- none: false
61
- requirements:
62
- - - ! '>='
63
- - !ruby/object:Gem::Version
64
- version: '0'
65
- required_rubygems_version: !ruby/object:Gem::Requirement
66
- none: false
67
- requirements:
68
- - - ! '>='
69
- - !ruby/object:Gem::Version
70
- version: '0'
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - &id002
47
+ - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - *id002
71
53
  requirements: []
54
+
72
55
  rubyforge_project:
73
- rubygems_version: 1.8.24
56
+ rubygems_version: 2.0.14
74
57
  signing_key:
75
- specification_version: 3
58
+ specification_version: 4
76
59
  summary: HTTP backend for Hiera
77
60
  test_files: []
61
+