diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c05ac68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2014 Craig Dunn + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 0a494d1..fa027b7 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ The following is an example hiera.yaml configuration for use with hiera-http :host: 127.0.0.1 :port: 5984 :output: json + :cache_timeout: 10 :failure: graceful :paths: - /configuration/%{fqdn} @@ -31,12 +32,22 @@ The following are optional configuration parameters `:http_read_timeout: ` : Timeout in seconds for waiting for a HTTP response (default 10) +`:cache_timeout: ` : Timeout in seconds for HTTP requests to a same path (default 10) + +`:cache_clean_interval: ` : Interval (in secs) to clean the cache (default 3600), set to 0 to disable cache cleaning + `: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 +`: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. + The `:paths:` parameter can also parse the lookup key, eg: :paths: - /configuraiton.php?lookup=%{key} + /configuration.php?lookup=%{key} + +`:path_base:`: This string will be prepended to every path. + +`:path_suffix:`: This string will be appended to every path. `:use_ssl:`: When set to true, enable SSL (default: false) @@ -46,6 +57,14 @@ The `:paths:` parameter can also parse the lookup key, eg: `:ssl_key`: Specify location of SSL key +`:ssl_verify`: Specify whether to verify SSL certificates (default: true) + +`:use_auth:`: When set to true, enable basic auth (default: false) + +`:auth_user:`: The user for basic auth + +`:auth_pass:`: The password for basic auth + ### TODO Theres a few things still on my list that I'm going to be adding, including @@ -62,13 +81,33 @@ Theres a few things still on my list that I'm going to be adding, including * IRC (freenode) crayfishx * http://www.craigdunn.org -### Credits +### Contributors * SSL components contributed from Ben Ford +* Louis Jencka +* Jun Wu ### Change Log +#### 1.3.1 + +* Bugfix release for ruby 1.8.7 support + +#### 1.3.0 + +* Added lookup caching features + +#### 1.2.0 + +* Support for SSL verify options +* Support for HTTP auth + +#### 1.0.1 + +* 1.0 release +* Support for ignoring 404's when failure is not set to graceful + #### 0.1.0 * Stable * Puppet Forge release diff --git a/Rakefile b/Rakefile index 3bc84c1..8b65489 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,9 @@ require 'rubygems' -require 'rake/gempackagetask' +require 'rubygems/package_task' spec = Gem::Specification.new do |gem| gem.name = "hiera-http" - gem.version = "0.1.0" + gem.version = "1.3.0" gem.summary = "HTTP backend for Hiera" gem.email = "craig@craigdunn.org" gem.author = "Craig Dunn" @@ -14,7 +14,7 @@ spec = Gem::Specification.new do |gem| gem.add_dependency('json', '>=1.1.1') end -Rake::GemPackageTask.new(spec) do |pkg| +Gem::PackageTask.new(spec) do |pkg| pkg.need_tar = true + pkg.gem_spec = spec end - diff --git a/lib/hiera/backend/http_backend.rb b/lib/hiera/backend/http_backend.rb index 4c79473..a1c2455 100644 --- a/lib/hiera/backend/http_backend.rb +++ b/lib/hiera/backend/http_backend.rb @@ -10,11 +10,23 @@ def initialize @http = Net::HTTP.new(@config[:host], @config[:port]) @http.read_timeout = @config[:http_read_timeout] || 10 @http.open_timeout = @config[:http_connect_timeout] || 10 + @path_base = @config[:path_base] || '' + @path_suffix = @config[:path_suffix] || '' + + @cache = {} + @cache_timeout = @config[:cache_timeout] || 10 + @cache_clean_interval = @config[:cache_clean_interval] || 3600 if @config[:use_ssl] @http.use_ssl = true - if @config[:ssl_cert] + + 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 @@ -28,35 +40,19 @@ def initialize end def lookup(key, scope, order_override, resolution_type) - answer = nil - paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) } + paths = @config[:paths].clone paths.insert(0, order_override) if order_override - + paths.map! { |p| Backend.parse_string(@path_base + p + @path_suffix, scope, { 'key' => key }) } paths.each do |path| Hiera.debug("[hiera-http]: Lookup #{key} from #{@config[:host]}:#{@config[:port]}#{path}") - httpreq = Net::HTTP::Get.new(path) - - begin - httpres = @http.request(httpreq) - rescue Exception => e - Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}") - raise Exception, e.message unless @config[:failure] == 'graceful' - next - end - unless httpres.kind_of?(Net::HTTPSuccess) - Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}") - Hiera.debug("HTTP response code was #{httpres.code}") - raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful' - next - end - - result = self.parse_response(key, httpres.body) - next unless result + 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) @@ -66,7 +62,7 @@ def lookup(key, scope, order_override, resolution_type) answer << parsed_result when :hash answer ||= {} - answer = parsed_result.merge answer + answer = Backend.merge_answer(parsed_result, answer) else answer = parsed_result break @@ -76,30 +72,19 @@ def lookup(key, scope, order_override, resolution_type) end - def parse_response(key,answer) + private - return nil unless answer + def parse_response(answer) + return unless answer - Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}") + format = @config[:output] || 'plain' + Hiera.debug("[hiera-http]: Query returned data, parsing response as #{format}") - case @config[:output] - - when 'plain' - # When the output format is configured as plain we assume that if the - # endpoint URL returns an HTTP success then the contents of the response - # body is the value itself, or nil. - # - answer + case format when 'json' - # If JSON is specified as the output format, assume the output of the - # endpoint URL is a JSON document and return keypart that matched our - # lookup key - self.json_handler(key,answer) + parse_json answer when 'yaml' - # If YAML is specified as the output format, assume the output of the - # endpoint URL is a YAML document and return keypart that matched our - # lookup key - self.yaml_handler(key,answer) + parse_yaml answer else answer end @@ -107,20 +92,74 @@ def parse_response(key,answer) # Handlers # Here we define specific handlers to parse the output of the http request - # and return a value. Currently we support YAML and JSON + # and return its structured representation. Currently we support YAML and JSON # - def json_handler(key,answer) + def parse_json(answer) require 'rubygems' require 'json' - JSON.parse(answer)[key] + JSON.parse(answer) end - def yaml_handler(answer) + def parse_yaml(answer) require 'yaml' - YAML.parse(answer)[key] + YAML.parse(answer) end + def http_get_and_parse_with_cache(path) + return http_get_and_parse(path) if @cache_timeout <= 0 + + now = Time.now.to_i + expired_at = now + @cache_timeout + + # Deleting all stale cache entries can be expensive. Do not do it every time + periodically_clean_cache(now, expired_at) unless @cache_clean_interval == 0 + + # Just refresh the entry being requested for performance + unless @cache[path] && @cache[path][:created_at] < expired_at + @cache[path] = { + :created_at => now, + :result => http_get_and_parse(path) + } + end + @cache[path][:result] + end + + def http_get_and_parse(path) + httpreq = Net::HTTP::Get.new(path) + + if @config[:use_auth] + httpreq.basic_auth @config[:auth_user], @config[:auth_pass] + end + + begin + httpres = @http.request(httpreq) + rescue Exception => e + Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}") + raise Exception, e.message unless @config[:failure] == 'graceful' + return + end + + unless httpres.kind_of?(Net::HTTPSuccess) + Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}") + Hiera.debug("HTTP response code was #{httpres.code}") + unless httpres.code == '404' && @config[:ignore_404] + raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful' + end + return + end + + parse_response httpres.body + end + + + def periodically_clean_cache(now, expired_at) + return if now < @clean_cache_at.to_i + + @clean_cache_at = now + @cache_clean_interval + @cache.delete_if do |_, entry| + entry[:created_at] > expired_at + end + end end end end - diff --git a/metadata.json b/metadata.json new file mode 100644 index 0000000..9667efc --- /dev/null +++ b/metadata.json @@ -0,0 +1,37 @@ +{ + "name": "crayfishx-hiera_http", + "version": "1.3.1", + "author": "Craig Dunn", + "summary": "Back end plugin for Hiera that allows lookup to be sourced from HTTP queries.", + "license": "Apache 2.0", + "source": "http://github.com/crayfishx/hiera-http", + "project_page": "https://github.com/crayfishx/hiera-http", + "issues_url": "https://github.com/crayfishx/hiera-http/issues", + "operatingsystem_support": [ + { + "operatingsystem": "Debian", + "operatingsystemrelease": [ + "6", + "7" + ] + }, + { + "operatingsystem": "Ubuntu", + "operatingsystemrelease": [ + "12.04", + "14.04" + ] + }, + { + "operatingsystem": "RedHat", + "operatingsystemrelease": [ + "5", + "6", + "7" + ] + } + ], + "dependencies": [ + ] +} + diff --git a/pkg/crayfishx-hiera_http-1.0.0.tar.gz b/pkg/crayfishx-hiera_http-1.0.0.tar.gz new file mode 100644 index 0000000..7b9535f Binary files /dev/null and b/pkg/crayfishx-hiera_http-1.0.0.tar.gz differ diff --git a/pkg/crayfishx-hiera_http-1.0.0/Modulefile b/pkg/crayfishx-hiera_http-1.0.0/Modulefile new file mode 100644 index 0000000..4206d82 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.0.0/Modulefile @@ -0,0 +1,10 @@ +name 'crayfishx-hiera_http' +version '1.0.0' +source 'http://github.com/crayfishx/hiera-http' +author 'Craig Dunn' +license 'Apache License, Version 2.0' +summary 'HTTP/REST API back end for Hiera' +description 'Back end plugin for Hiera that allows lookup to be sourced from HTTP queries.' +project_page 'http://github.com/crayfishx/hiera-http' + + diff --git a/pkg/crayfishx-hiera_http-1.0.0/README.md b/pkg/crayfishx-hiera_http-1.0.0/README.md new file mode 100644 index 0000000..0a494d1 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.0.0/README.md @@ -0,0 +1,82 @@ +## hiera_http : a HTTP back end for Hiera + + +### Description + +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 + +### Configuration + +The following is an example hiera.yaml configuration for use with hiera-http + + :backends: + - http + + :http: + :host: 127.0.0.1 + :port: 5984 + :output: json + :failure: graceful + :paths: + - /configuration/%{fqdn} + - /configuration/%{env} + - /configuration/common + + +The following are optional configuration parameters + +`: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 + +`:http_connect_timeout: ` : Timeout in seconds for the HTTP connect (default 10) + +`:http_read_timeout: ` : Timeout in seconds for waiting for a HTTP response (default 10) + +`: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 + +The `:paths:` parameter can also parse the lookup key, eg: + + :paths: + /configuraiton.php?lookup=%{key} + +`:use_ssl:`: When set to true, enable SSL (default: false) + +`:ssl_ca_cert`: Specify a CA cert for use with SSL + +`:ssl_cert`: Specify location of SSL certificate + +`:ssl_key`: Specify location of SSL key + +### TODO + +Theres a few things still on my list that I'm going to be adding, including + +* Add HTTP basic auth support +* Add proxy support +* Add further handlers (eg: XML) + + +### Author + +* Craig Dunn +* @crayfishX +* IRC (freenode) crayfishx +* http://www.craigdunn.org + +### Credits + +* SSL components contributed from Ben Ford + + +### Change Log + +#### 0.1.0 +* Stable +* Puppet Forge release + +#### 0.0.2 +* Added SSL support + +#### 0.0.1 +* Initial release + + diff --git a/pkg/crayfishx-hiera_http-1.0.0/Rakefile b/pkg/crayfishx-hiera_http-1.0.0/Rakefile new file mode 100644 index 0000000..12033a9 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.0.0/Rakefile @@ -0,0 +1,20 @@ +require 'rubygems' +require 'rubygems/package_task' + +spec = Gem::Specification.new do |gem| + gem.name = "hiera-http" + gem.version = "1.0.0" + gem.summary = "HTTP backend for Hiera" + gem.email = "craig@craigdunn.org" + gem.author = "Craig Dunn" + gem.homepage = "http://github.com/crayfishx/hiera-http" + gem.description = "Hiera backend for looking up data over HTTP APIs" + gem.require_path = "lib" + gem.files = FileList["lib/**/*"].to_a + gem.add_dependency('json', '>=1.1.1') +end + +Gem::PackageTask.new(spec) do |pkg| + pkg.need_tar = true + pkg.gem_spec = spec +end diff --git a/pkg/crayfishx-hiera_http-1.0.0/lib/hiera/backend/http_backend.rb b/pkg/crayfishx-hiera_http-1.0.0/lib/hiera/backend/http_backend.rb new file mode 100644 index 0000000..4c79473 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.0.0/lib/hiera/backend/http_backend.rb @@ -0,0 +1,126 @@ +class Hiera + module Backend + class Http_backend + + 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 + + if @config[:use_ssl] + @http.use_ssl = true + if @config[:ssl_cert] + @http.verify_mode = OpenSSL::SSL::VERIFY_PEER + 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 + + def lookup(key, scope, order_override, resolution_type) + + 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}") + httpreq = Net::HTTP::Get.new(path) + + begin + httpres = @http.request(httpreq) + rescue Exception => e + Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}") + raise Exception, e.message unless @config[:failure] == 'graceful' + next + end + + unless httpres.kind_of?(Net::HTTPSuccess) + Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}") + Hiera.debug("HTTP response code was #{httpres.code}") + raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful' + next + end + + result = self.parse_response(key, httpres.body) + next unless result + + parsed_result = Backend.parse_answer(result, scope) + + case resolution_type + when :array + answer ||= [] + answer << parsed_result + when :hash + answer ||= {} + answer = parsed_result.merge answer + else + answer = parsed_result + break + end + end + answer + end + + + def parse_response(key,answer) + + return nil unless answer + + Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}") + + case @config[:output] + + when 'plain' + # When the output format is configured as plain we assume that if the + # endpoint URL returns an HTTP success then the contents of the response + # body is the value itself, or nil. + # + answer + when 'json' + # If JSON is specified as the output format, assume the output of the + # endpoint URL is a JSON document and return keypart that matched our + # lookup key + self.json_handler(key,answer) + when 'yaml' + # If YAML is specified as the output format, assume the output of the + # endpoint URL is a YAML document and return keypart that matched our + # lookup key + self.yaml_handler(key,answer) + else + answer + end + end + + # Handlers + # Here we define specific handlers to parse the output of the http request + # and return a value. Currently we support YAML and JSON + # + def json_handler(key,answer) + require 'rubygems' + require 'json' + JSON.parse(answer)[key] + end + + def yaml_handler(answer) + require 'yaml' + YAML.parse(answer)[key] + end + + end + end +end + diff --git a/pkg/crayfishx-hiera_http-1.0.0/metadata.json b/pkg/crayfishx-hiera_http-1.0.0/metadata.json new file mode 100644 index 0000000..80e8d33 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.0.0/metadata.json @@ -0,0 +1,22 @@ +{ + "name": "crayfishx-hiera_http", + "version": "1.0.0", + "source": "http://github.com/crayfishx/hiera-http", + "author": "Craig Dunn", + "license": "Apache License, Version 2.0", + "summary": "HTTP/REST API back end for Hiera", + "description": "Back end plugin for Hiera that allows lookup to be sourced from HTTP queries.", + "project_page": "http://github.com/crayfishx/hiera-http", + "dependencies": [ + + ], + "types": [ + + ], + "checksums": { + "Modulefile": "5cf306983a57ea99f9c54a9564a82c39", + "README.md": "013ba51adbb24bccb770f6ade54f6072", + "Rakefile": "da6f9edc81d86a8f3955612016244aa0", + "lib/hiera/backend/http_backend.rb": "101eb5cabd578a440cc36be5bbf8e949" + } +} \ No newline at end of file diff --git a/pkg/crayfishx-hiera_http-1.2.0.tar.gz b/pkg/crayfishx-hiera_http-1.2.0.tar.gz new file mode 100644 index 0000000..5adc2c1 Binary files /dev/null and b/pkg/crayfishx-hiera_http-1.2.0.tar.gz differ diff --git a/pkg/crayfishx-hiera_http-1.2.0/README.md b/pkg/crayfishx-hiera_http-1.2.0/README.md new file mode 100644 index 0000000..d2a7ae3 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.2.0/README.md @@ -0,0 +1,102 @@ +## hiera_http : a HTTP back end for Hiera + + +### Description + +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 + +### Configuration + +The following is an example hiera.yaml configuration for use with hiera-http + + :backends: + - http + + :http: + :host: 127.0.0.1 + :port: 5984 + :output: json + :failure: graceful + :paths: + - /configuration/%{fqdn} + - /configuration/%{env} + - /configuration/common + + +The following are optional configuration parameters + +`: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 + +`:http_connect_timeout: ` : Timeout in seconds for the HTTP connect (default 10) + +`:http_read_timeout: ` : Timeout in seconds for waiting for a HTTP response (default 10) + +`: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 + +`: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. + +The `:paths:` parameter can also parse the lookup key, eg: + + :paths: + /configuration.php?lookup=%{key} + +`:use_ssl:`: When set to true, enable SSL (default: false) + +`:ssl_ca_cert`: Specify a CA cert for use with SSL + +`:ssl_cert`: Specify location of SSL certificate + +`:ssl_key`: Specify location of SSL key + +`:ssl_verify`: Specify whether to verify SSL certificates (default: true) + +`:use_auth:`: When set to true, enable basic auth (default: false) + +`:auth_user:`: The user for basic auth + +`:auth_pass:`: The password for basic auth + +### TODO + +Theres a few things still on my list that I'm going to be adding, including + +* Add HTTP basic auth support +* Add proxy support +* Add further handlers (eg: XML) + + +### Author + +* Craig Dunn +* @crayfishX +* IRC (freenode) crayfishx +* http://www.craigdunn.org + +### Contributors + +* SSL components contributed from Ben Ford +* Louis Jencka + +### Change Log + +#### 1.2.0 + +* Support for SSL verify options +* Support for HTTP auth + +#### 1.0.1 + +* 1.0 release +* Support for ignoring 404's when failure is not set to graceful + +#### 0.1.0 +* Stable +* Puppet Forge release + +#### 0.0.2 +* Added SSL support + +#### 0.0.1 +* Initial release + + diff --git a/pkg/crayfishx-hiera_http-1.2.0/Rakefile b/pkg/crayfishx-hiera_http-1.2.0/Rakefile new file mode 100644 index 0000000..b55991e --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.2.0/Rakefile @@ -0,0 +1,20 @@ +require 'rubygems' +require 'rubygems/package_task' + +spec = Gem::Specification.new do |gem| + gem.name = "hiera-http" + gem.version = "1.2.0" + gem.summary = "HTTP backend for Hiera" + gem.email = "craig@craigdunn.org" + gem.author = "Craig Dunn" + gem.homepage = "http://github.com/crayfishx/hiera-http" + gem.description = "Hiera backend for looking up data over HTTP APIs" + gem.require_path = "lib" + gem.files = FileList["lib/**/*"].to_a + gem.add_dependency('json', '>=1.1.1') +end + +Gem::PackageTask.new(spec) do |pkg| + pkg.need_tar = true + pkg.gem_spec = spec +end diff --git a/pkg/crayfishx-hiera_http-1.2.0/checksums.json b/pkg/crayfishx-hiera_http-1.2.0/checksums.json new file mode 100644 index 0000000..fc37c0d --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.2.0/checksums.json @@ -0,0 +1,6 @@ +{ + "README.md": "f056722b33745154fd6e5d26f7a72c62", + "Rakefile": "f81b83a0476379b591205ee364994805", + "lib/hiera/backend/http_backend.rb": "fcb2bbba21f94db829969aa1ffb6c152", + "metadata.json": "f36cdae2700eefedd2282d3f9776a25a" +} \ No newline at end of file diff --git a/pkg/crayfishx-hiera_http-1.2.0/lib/hiera/backend/http_backend.rb b/pkg/crayfishx-hiera_http-1.2.0/lib/hiera/backend/http_backend.rb new file mode 100644 index 0000000..0e61ec6 --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.2.0/lib/hiera/backend/http_backend.rb @@ -0,0 +1,138 @@ +class Hiera + module Backend + class Http_backend + + 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 + + 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 + + def lookup(key, scope, order_override, resolution_type) + + 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}") + httpreq = Net::HTTP::Get.new(path) + + if @config[:use_auth] + httpreq.basic_auth @config[:auth_user], @config[:auth_pass] + end + + begin + httpres = @http.request(httpreq) + rescue Exception => e + Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}") + raise Exception, e.message unless @config[:failure] == 'graceful' + next + end + + unless httpres.kind_of?(Net::HTTPSuccess) + Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}") + Hiera.debug("HTTP response code was #{httpres.code}") + unless ( httpres.code == '404' && @config[:ignore_404] == true ) + raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful' + end + next + end + + result = self.parse_response(key, httpres.body) + next unless result + + parsed_result = Backend.parse_answer(result, scope) + + case resolution_type + when :array + answer ||= [] + answer << parsed_result + when :hash + answer ||= {} + answer = parsed_result.merge answer + else + answer = parsed_result + break + end + end + answer + end + + + def parse_response(key,answer) + + return nil unless answer + + Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}") + + case @config[:output] + + when 'plain' + # When the output format is configured as plain we assume that if the + # endpoint URL returns an HTTP success then the contents of the response + # body is the value itself, or nil. + # + answer + when 'json' + # If JSON is specified as the output format, assume the output of the + # endpoint URL is a JSON document and return keypart that matched our + # lookup key + self.json_handler(key,answer) + when 'yaml' + # If YAML is specified as the output format, assume the output of the + # endpoint URL is a YAML document and return keypart that matched our + # lookup key + self.yaml_handler(key,answer) + else + answer + end + end + + # Handlers + # Here we define specific handlers to parse the output of the http request + # and return a value. Currently we support YAML and JSON + # + def json_handler(key,answer) + require 'rubygems' + require 'json' + JSON.parse(answer)[key] + end + + def yaml_handler(answer) + require 'yaml' + YAML.parse(answer)[key] + end + + end + end +end + diff --git a/pkg/crayfishx-hiera_http-1.2.0/metadata.json b/pkg/crayfishx-hiera_http-1.2.0/metadata.json new file mode 100644 index 0000000..917bc5f --- /dev/null +++ b/pkg/crayfishx-hiera_http-1.2.0/metadata.json @@ -0,0 +1,13 @@ +{ + "name": "crayfishx-hiera_http", + "version": "1.2.0", + "author": "Craig Dunn", + "summary": "Back end plugin for Hiera that allows lookup to be sourced from HTTP queries.", + "license": "Apache 2.0", + "source": "http://github.com/crayfishx/hiera-http", + "project_page": "https://github.com/crayfishx/hiera-http", + "issues_url": "https://github.com/crayfishx/hiera-http/issues", + "dependencies": [ + + ] +} diff --git a/pkg/hiera-http-1.0.0.gem b/pkg/hiera-http-1.0.0.gem new file mode 100644 index 0000000..c82ddc9 Binary files /dev/null and b/pkg/hiera-http-1.0.0.gem differ diff --git a/pkg/hiera-http-1.2.0.gem b/pkg/hiera-http-1.2.0.gem new file mode 100644 index 0000000..25cc5d2 Binary files /dev/null and b/pkg/hiera-http-1.2.0.gem differ