chef_handler_foreman 0.1.2 → 0.1.3

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: 21fab85d6ff9110b08cc8e2ee0282c3a3115bee1
4
- data.tar.gz: c0e49b5679ef653d329936ac1e5d5084221af978
3
+ metadata.gz: 718d58d3a2f128912963d25ec640369276fbd9a8
4
+ data.tar.gz: 155ec66bf4d85295070a69496949ea289e749b40
5
5
  SHA512:
6
- metadata.gz: 9cdb697d5e51963f999af809a768d3552b2efabd92ce39b4527ac05ffdb174541cc09693fbf64e3ae24b095826413024f0f1296df5038d9449a0b67618ddeba1
7
- data.tar.gz: 999b4f8cd62099bf400e4274e34f7dc2a6ea79324ded6d072ce1dcc17f22bdd9b9c1fecbd45d53daa94ce7541e27961deeee2e28fb7f79b987c66b8dcda3a963
6
+ metadata.gz: ade834538128c1ad4870625081303065a799797f1a89f4edab89ac228b7f17f4209cf294673ff85cbae0130551b94e4cd2d4d7561060cf2b64c56e19928e841a
7
+ data.tar.gz: cf43b3658e2fcb037f2dcaa519e63660133328f9d52c1bbb3bd462d0ed77d039d893b46465c5ccd97a6097f3dd9dd3810b2a0d32d9b6da23312a4b1c4a5924dc
@@ -10,6 +10,7 @@ require "#{File.dirname(__FILE__)}/foreman_uploader"
10
10
 
11
11
  module ChefHandlerForeman
12
12
  module ForemanHooks
13
+ attr_reader :foreman_uploader, :foreman_facts_handler, :foreman_report_handler, :foreman_reporter
13
14
 
14
15
  # Provide a chef-client cookbook friendly option
15
16
  def foreman_server_url(url)
@@ -109,16 +109,7 @@ module ChefHandlerForeman
109
109
  end
110
110
 
111
111
  def resources_per_status
112
- if @why_run
113
- {
114
- "applied" => @total_updated,
115
- "restarted" => @total_restarted,
116
- "failed" => @total_failed,
117
- "failed_restarts" => @total_failed_restart,
118
- "skipped" => @total_skipped,
119
- "pending" => 0
120
- }
121
- else
112
+ if Chef::Config.why_run
122
113
  {
123
114
  "applied" => 0,
124
115
  "restarted" => 0,
@@ -127,6 +118,15 @@ module ChefHandlerForeman
127
118
  "skipped" => @total_skipped,
128
119
  "pending" => @total_updated + @total_restarted + @total_failed + @total_failed_restart
129
120
  }
121
+ else
122
+ {
123
+ "applied" => @total_updated,
124
+ "restarted" => @total_restarted,
125
+ "failed" => @total_failed,
126
+ "failed_restarts" => @total_failed_restart,
127
+ "skipped" => @total_skipped,
128
+ "pending" => 0
129
+ }
130
130
  end
131
131
  end
132
132
 
@@ -26,7 +26,7 @@ module ChefHandlerForeman
26
26
  @options = opts
27
27
  end
28
28
 
29
- def foreman_request(path, body, client_name)
29
+ def foreman_request(path, body, client_name, method = 'post')
30
30
  uri = URI.parse(options[:url])
31
31
  http = Net::HTTP.new(uri.host, uri.port)
32
32
  http.use_ssl = uri.scheme == 'https'
@@ -44,21 +44,39 @@ module ChefHandlerForeman
44
44
  end
45
45
  end
46
46
 
47
- req = Net::HTTP::Post.new("#{uri.path}/#{path}")
47
+ req = build_request(method, uri, path)
48
48
  req.add_field('Accept', 'application/json,version=2')
49
- req.content_type = 'application/json'
50
- body_json = body.to_json
51
- req.body = body_json
52
- req.add_field('X-Foreman-Signature', sign_request(body_json, options[:client_key]))
53
49
  req.add_field('X-Foreman-Client', client_name)
54
- response = http.request(req)
50
+ req.body = body.to_json
51
+ req.content_type = 'application/json'
52
+ # signature can be computed once we set body and X-Foreman-Client
53
+ req.add_field('X-Foreman-Signature', signature(req))
54
+ response = http.request(req)
55
+ end
56
+
57
+ def build_request(method, uri, path)
58
+ Net::HTTP.const_get(method.capitalize).new("#{uri.path}/#{path}")
59
+ rescue NameError => e
60
+ raise "unsupported method #{method}, try one of get, post, delete, put"
55
61
  end
56
62
 
57
- def sign_request(body_json, key_path)
58
- hash_body = Digest::SHA256.hexdigest(body_json)
59
- key = OpenSSL::PKey::RSA.new(File.read(key_path))
63
+ def signature(request)
64
+ case request
65
+ when Net::HTTP::Post, Net::HTTP::Patch, Net::HTTP::Put
66
+ sign_data(request.body)
67
+ when Net::HTTP::Get, Net::HTTP::Delete
68
+ sign_data(request['X-Foreman-Client'])
69
+ else
70
+ raise "Don't know how to sign #{req.class} requests"
71
+ end
72
+ end
73
+
74
+ def sign_data(data)
75
+ hash_to_sign = Digest::SHA256.hexdigest(data)
76
+ key = OpenSSL::PKey::RSA.new(File.read(options[:client_key]))
60
77
  # Base64.encode64 is adding \n in the string
61
- signature = Base64.encode64(key.sign(OpenSSL::Digest::SHA256.new, hash_body)).gsub("\n",'')
78
+ signature = Base64.encode64(key.sign(OpenSSL::Digest::SHA256.new, hash_to_sign)).gsub("\n",'')
62
79
  end
63
80
  end
64
81
  end
82
+
@@ -1,3 +1,3 @@
1
1
  module ChefHandlerForeman
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef_handler_foreman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Hulan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-01 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler