lookup_http 1.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 +7 -0
- data/lib/lookup_http.rb +110 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c04f52622862bcbdc77f95eb81149c1c74f1892
|
4
|
+
data.tar.gz: 52a734dfb486e3ba47607a0fedc66dd78ca4ad25
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e170cbb6d0b52406cfd6baae3c3c603a59281e08f256ef95ede9c442f78685bbd425f15b4b081ec399b40666649138276e73f531efd0bc787ee7c5675c05ccd5
|
7
|
+
data.tar.gz: 577778de1f77e2f4dea599c6058364c6d662ede95af0554ebc58269e44216a777454fc083623210ac0b778c45cb4bcdcd824aebd4c5406333824095d489ed07f
|
data/lib/lookup_http.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
class LookupHttp
|
2
|
+
|
3
|
+
def initialize(opts={})
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
@config = opts
|
7
|
+
|
8
|
+
@debug_log = @config[:debug_log]
|
9
|
+
@http = Net::HTTP.new(@config[:host], @config[:port])
|
10
|
+
@http.read_timeout = @config[:http_read_timeout] || 10
|
11
|
+
@http.open_timeout = @config[:http_connect_timeout] || 10
|
12
|
+
|
13
|
+
if @config[:use_ssl]
|
14
|
+
@http.use_ssl = true
|
15
|
+
|
16
|
+
if @config[:ssl_verify] == false
|
17
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
18
|
+
else
|
19
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
20
|
+
end
|
21
|
+
|
22
|
+
if @config[:ssl_cert]
|
23
|
+
store = OpenSSL::X509::Store.new
|
24
|
+
store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
|
25
|
+
@http.cert_store = store
|
26
|
+
|
27
|
+
@http.key = OpenSSL::PKey::RSA.new(File.read(@config[:ssl_cert]))
|
28
|
+
@http.cert = OpenSSL::X509::Certificate.new(File.read(@config[:ssl_key]))
|
29
|
+
end
|
30
|
+
else
|
31
|
+
@http.use_ssl = false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_response(answer)
|
36
|
+
return unless answer
|
37
|
+
|
38
|
+
format = @config[:output] || 'plain'
|
39
|
+
log_debug("[lookup_http]: Query returned data, parsing response as #{format}")
|
40
|
+
|
41
|
+
case format
|
42
|
+
when 'json'
|
43
|
+
parse_json answer
|
44
|
+
when 'yaml'
|
45
|
+
parse_yaml answer
|
46
|
+
else
|
47
|
+
answer
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Handlers
|
52
|
+
# Here we define specific handlers to parse the output of the http request
|
53
|
+
# and return its structured representation. Currently we support YAML and JSON
|
54
|
+
#
|
55
|
+
def parse_json(answer)
|
56
|
+
require 'rubygems'
|
57
|
+
require 'json'
|
58
|
+
JSON.parse(answer)
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_yaml(answer)
|
62
|
+
require 'yaml'
|
63
|
+
YAML.load(answer)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def get_parsed(path)
|
68
|
+
httpreq = Net::HTTP::Get.new(path)
|
69
|
+
|
70
|
+
if @config[:use_auth]
|
71
|
+
httpreq.basic_auth @config[:auth_user], @config[:auth_pass]
|
72
|
+
end
|
73
|
+
|
74
|
+
if @config[:headers]
|
75
|
+
@config[:headers].each do |name,content|
|
76
|
+
httpreq.add_field name.to_s, content
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
begin
|
81
|
+
httpres = @http.request(httpreq)
|
82
|
+
rescue Exception => e
|
83
|
+
Hiera.warn("[lookup_http]: Net::HTTP threw exception #{e.message}")
|
84
|
+
raise Exception, e.message unless @config[:failure] == 'graceful'
|
85
|
+
return
|
86
|
+
end
|
87
|
+
|
88
|
+
unless httpres.kind_of?(Net::HTTPSuccess)
|
89
|
+
log_debug("[lookup_http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
|
90
|
+
log_debug("HTTP response code was #{httpres.code}")
|
91
|
+
unless httpres.code == '404' && @config[:ignore_404]
|
92
|
+
raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
|
93
|
+
end
|
94
|
+
return
|
95
|
+
end
|
96
|
+
|
97
|
+
parse_response httpres.body
|
98
|
+
end
|
99
|
+
|
100
|
+
## This allows us to pass Hiera.debug or Jerakia.log.debug to the lookup_http class
|
101
|
+
## we should find a better way to handle logging withing LookupHttp
|
102
|
+
def log_debug(msg)
|
103
|
+
if @debug_log
|
104
|
+
eval "#{@debug_log} '#{msg}'"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
end
|
110
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lookup_http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Craig Dunn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.1
|
27
|
+
description: 'Library for data lookup tools (eg: hiera/jerakia) for looking up data
|
28
|
+
over HTTP APIs'
|
29
|
+
email: craig@craigdunn.org
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/lookup_http.rb
|
35
|
+
homepage: http://github.com/crayfishx/lookup_http
|
36
|
+
licenses: []
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.0.14
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: HTTP library for data lookup tools
|
58
|
+
test_files: []
|