Skip to content

Commit b71632c

Browse files
committed
Forge release 1.2.0
1 parent 92e5ef4 commit b71632c

File tree

6 files changed

+279
-0
lines changed

6 files changed

+279
-0
lines changed

pkg/crayfishx-hiera_http-1.2.0.tar.gz

3.38 KB
Binary file not shown.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## hiera_http : a HTTP back end for Hiera
2+
3+
4+
### Description
5+
6+
This is a back end plugin for Hiera that allows lookup to be sourced from HTTP queries. The intent is to make this backend adaptable to allow you to query any data stored in systems with a RESTful API such as CouchDB or even a custom store with a web front-end
7+
8+
### Configuration
9+
10+
The following is an example hiera.yaml configuration for use with hiera-http
11+
12+
:backends:
13+
- http
14+
15+
:http:
16+
:host: 127.0.0.1
17+
:port: 5984
18+
:output: json
19+
:failure: graceful
20+
:paths:
21+
- /configuration/%{fqdn}
22+
- /configuration/%{env}
23+
- /configuration/common
24+
25+
26+
The following are optional configuration parameters
27+
28+
`:output: ` : Specify what handler to use for the output of the request. Currently supported outputs are plain, which will just return the whole document, or YAML and JSON which parse the data and try to look up the key
29+
30+
`:http_connect_timeout: ` : Timeout in seconds for the HTTP connect (default 10)
31+
32+
`:http_read_timeout: ` : Timeout in seconds for waiting for a HTTP response (default 10)
33+
34+
`:failure: ` : When set to `graceful` will stop hiera-http from throwing an exception in the event of a connection error, timeout or invalid HTTP response and move on. Without this option set hiera-http will throw an exception in such circumstances
35+
36+
`:ignore_404: ` : If `failure` is _not_ set to `graceful` then any error code received from the HTTP response will throw an exception. This option makes 404 responses exempt from exceptions. This is useful if you expect to get 404's for data items not in a certain part of the hierarchy and need to fall back to the next level in the hierarchy, but you still want to bomb out on other errors.
37+
38+
The `:paths:` parameter can also parse the lookup key, eg:
39+
40+
:paths:
41+
/configuration.php?lookup=%{key}
42+
43+
`:use_ssl:`: When set to true, enable SSL (default: false)
44+
45+
`:ssl_ca_cert`: Specify a CA cert for use with SSL
46+
47+
`:ssl_cert`: Specify location of SSL certificate
48+
49+
`:ssl_key`: Specify location of SSL key
50+
51+
`:ssl_verify`: Specify whether to verify SSL certificates (default: true)
52+
53+
`:use_auth:`: When set to true, enable basic auth (default: false)
54+
55+
`:auth_user:`: The user for basic auth
56+
57+
`:auth_pass:`: The password for basic auth
58+
59+
### TODO
60+
61+
Theres a few things still on my list that I'm going to be adding, including
62+
63+
* Add HTTP basic auth support
64+
* Add proxy support
65+
* Add further handlers (eg: XML)
66+
67+
68+
### Author
69+
70+
* Craig Dunn <[email protected]>
71+
* @crayfishX
72+
* IRC (freenode) crayfishx
73+
* http://www.craigdunn.org
74+
75+
### Contributors
76+
77+
* SSL components contributed from Ben Ford <[email protected]>
78+
* Louis Jencka <jencka>
79+
80+
### Change Log
81+
82+
#### 1.2.0
83+
84+
* Support for SSL verify options <jencka>
85+
* Support for HTTP auth <jencka>
86+
87+
#### 1.0.1
88+
89+
* 1.0 release
90+
* Support for ignoring 404's when failure is not set to graceful
91+
92+
#### 0.1.0
93+
* Stable
94+
* Puppet Forge release
95+
96+
#### 0.0.2
97+
* Added SSL support
98+
99+
#### 0.0.1
100+
* Initial release
101+
102+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'rubygems'
2+
require 'rubygems/package_task'
3+
4+
spec = Gem::Specification.new do |gem|
5+
gem.name = "hiera-http"
6+
gem.version = "1.2.0"
7+
gem.summary = "HTTP backend for Hiera"
8+
gem.email = "[email protected]"
9+
gem.author = "Craig Dunn"
10+
gem.homepage = "http://github.com/crayfishx/hiera-http"
11+
gem.description = "Hiera backend for looking up data over HTTP APIs"
12+
gem.require_path = "lib"
13+
gem.files = FileList["lib/**/*"].to_a
14+
gem.add_dependency('json', '>=1.1.1')
15+
end
16+
17+
Gem::PackageTask.new(spec) do |pkg|
18+
pkg.need_tar = true
19+
pkg.gem_spec = spec
20+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"README.md": "f056722b33745154fd6e5d26f7a72c62",
3+
"Rakefile": "f81b83a0476379b591205ee364994805",
4+
"lib/hiera/backend/http_backend.rb": "fcb2bbba21f94db829969aa1ffb6c152",
5+
"metadata.json": "f36cdae2700eefedd2282d3f9776a25a"
6+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
class Hiera
2+
module Backend
3+
class Http_backend
4+
5+
def initialize
6+
require 'net/http'
7+
require 'net/https'
8+
@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
13+
14+
if @config[:use_ssl]
15+
@http.use_ssl = true
16+
17+
if @config[:ssl_verify] == false
18+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19+
else
20+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
21+
end
22+
23+
if @config[:ssl_cert]
24+
store = OpenSSL::X509::Store.new
25+
store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
26+
@http.cert_store = store
27+
28+
@http.key = OpenSSL::PKey::RSA.new(File.read(@config[:ssl_cert]))
29+
@http.cert = OpenSSL::X509::Certificate.new(File.read(@config[:ssl_key]))
30+
end
31+
else
32+
@http.use_ssl = false
33+
end
34+
end
35+
36+
def lookup(key, scope, order_override, resolution_type)
37+
38+
answer = nil
39+
40+
paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) }
41+
paths.insert(0, order_override) if order_override
42+
43+
44+
paths.each do |path|
45+
46+
Hiera.debug("[hiera-http]: Lookup #{key} from #{@config[:host]}:#{@config[:port]}#{path}")
47+
httpreq = Net::HTTP::Get.new(path)
48+
49+
if @config[:use_auth]
50+
httpreq.basic_auth @config[:auth_user], @config[:auth_pass]
51+
end
52+
53+
begin
54+
httpres = @http.request(httpreq)
55+
rescue Exception => e
56+
Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}")
57+
raise Exception, e.message unless @config[:failure] == 'graceful'
58+
next
59+
end
60+
61+
unless httpres.kind_of?(Net::HTTPSuccess)
62+
Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
63+
Hiera.debug("HTTP response code was #{httpres.code}")
64+
unless ( httpres.code == '404' && @config[:ignore_404] == true )
65+
raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
66+
end
67+
next
68+
end
69+
70+
result = self.parse_response(key, httpres.body)
71+
next unless result
72+
73+
parsed_result = Backend.parse_answer(result, scope)
74+
75+
case resolution_type
76+
when :array
77+
answer ||= []
78+
answer << parsed_result
79+
when :hash
80+
answer ||= {}
81+
answer = parsed_result.merge answer
82+
else
83+
answer = parsed_result
84+
break
85+
end
86+
end
87+
answer
88+
end
89+
90+
91+
def parse_response(key,answer)
92+
93+
return nil unless answer
94+
95+
Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}")
96+
97+
case @config[:output]
98+
99+
when 'plain'
100+
# When the output format is configured as plain we assume that if the
101+
# endpoint URL returns an HTTP success then the contents of the response
102+
# body is the value itself, or nil.
103+
#
104+
answer
105+
when 'json'
106+
# If JSON is specified as the output format, assume the output of the
107+
# endpoint URL is a JSON document and return keypart that matched our
108+
# lookup key
109+
self.json_handler(key,answer)
110+
when 'yaml'
111+
# If YAML is specified as the output format, assume the output of the
112+
# endpoint URL is a YAML document and return keypart that matched our
113+
# lookup key
114+
self.yaml_handler(key,answer)
115+
else
116+
answer
117+
end
118+
end
119+
120+
# Handlers
121+
# Here we define specific handlers to parse the output of the http request
122+
# and return a value. Currently we support YAML and JSON
123+
#
124+
def json_handler(key,answer)
125+
require 'rubygems'
126+
require 'json'
127+
JSON.parse(answer)[key]
128+
end
129+
130+
def yaml_handler(answer)
131+
require 'yaml'
132+
YAML.parse(answer)[key]
133+
end
134+
135+
end
136+
end
137+
end
138+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "crayfishx-hiera_http",
3+
"version": "1.2.0",
4+
"author": "Craig Dunn",
5+
"summary": "Back end plugin for Hiera that allows lookup to be sourced from HTTP queries.",
6+
"license": "Apache 2.0",
7+
"source": "http://github.com/crayfishx/hiera-http",
8+
"project_page": "https://github.com/crayfishx/hiera-http",
9+
"issues_url": "https://github.com/crayfishx/hiera-http/issues",
10+
"dependencies": [
11+
12+
]
13+
}

0 commit comments

Comments
 (0)