foreman_rh_cloud 3.0.26 → 3.0.32

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.
@@ -104,7 +104,9 @@ module ForemanInventoryUpload
104
104
  end
105
105
 
106
106
  def obfuscate_ip(ip, ips_dict)
107
- "10.230.230.#{ips_dict.count + 1}"
107
+ max_obfuscated = ips_dict.values.map { |v| IPAddr.new(v).to_i }.max || IPAddr.new('10.230.230.0').to_i
108
+
109
+ IPAddr.new(max_obfuscated + 1, Socket::AF_INET).to_s
108
110
  end
109
111
 
110
112
  def bios_uuid(host)
@@ -13,7 +13,7 @@ module ForemanInventoryUpload
13
13
  organizations +
14
14
  content_data +
15
15
  satellite_server_data
16
- ).reject { |key, value| value.empty? }
16
+ ).reject { |key, value| value.empty? }.map { |key, value| [key, truncated_value(value)] }
17
17
  end
18
18
 
19
19
  def generate_parameters
@@ -22,6 +22,7 @@ module ForemanInventoryUpload
22
22
  (@host.host_inherited_params_objects || [])
23
23
  .map { |item| [item.name, item.value] }
24
24
  .select { |_name, value| value.present? || value.is_a?(FalseClass) }
25
+ .map { |key, value| [key, truncated_value(value)] }
25
26
  end
26
27
 
27
28
  private
@@ -58,6 +59,12 @@ module ForemanInventoryUpload
58
59
  ['organization_id', @host.organization_id.to_s],
59
60
  ]
60
61
  end
62
+
63
+ def truncated_value(value)
64
+ return 'Original value exceeds 250 characters' if value.to_s.length > 250
65
+
66
+ value
67
+ end
61
68
  end
62
69
  end
63
70
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanRhCloud
2
- VERSION = '3.0.26'.freeze
2
+ VERSION = '3.0.32'.freeze
3
3
  end
@@ -98,6 +98,17 @@ module ForemanRhCloud
98
98
 
99
99
  # For testing purposes we can override the default hostname with an environment variable SATELLITE_RH_CLOUD_FOREMAN_HOST
100
100
  def self.foreman_host
101
- @foreman_host ||= ::Host.unscoped.friendly.find(ENV['SATELLITE_RH_CLOUD_FOREMAN_HOST'] || ::SmartProxy.default_capsule.name)
101
+ @foreman_host ||= begin
102
+ fullname = foreman_host_name
103
+ ::Host.unscoped.friendly.find(fullname)
104
+ rescue ActiveRecord::RecordNotFound
105
+ # fullname didn't work. Let's try shortname
106
+ shortname = /(?<shortname>[^\.]*)\.?.*/.match(fullname)[:shortname]
107
+ ::Host.unscoped.friendly.find(shortname)
108
+ end
109
+ end
110
+
111
+ def self.foreman_host_name
112
+ ENV['SATELLITE_RH_CLOUD_FOREMAN_HOST'] || ::SmartProxy.default_capsule.name
102
113
  end
103
114
  end
@@ -17,7 +17,6 @@ module InventorySync
17
17
  @sub_ids.map do |sub_id|
18
18
  host_id = host_id(sub_id)
19
19
  if host_id
20
- touched << host_id
21
20
  {
22
21
  host_id: host_id,
23
22
  status: InventorySync::InventoryStatus::SYNC,
@@ -28,10 +27,6 @@ module InventorySync
28
27
  end.compact
29
28
  end
30
29
 
31
- def touched
32
- @touched ||= []
33
- end
34
-
35
30
  def host_id(sub_id)
36
31
  hosts[sub_id]
37
32
  end
@@ -14,11 +14,7 @@ module InventorySync
14
14
  end
15
15
 
16
16
  def setup_statuses
17
- @subscribed_hosts_ids = Set.new(
18
- ForemanInventoryUpload::Generators::Queries.for_slice(
19
- Host.unscoped.where(organization: input[:organization_id])
20
- ).pluck(:id)
21
- )
17
+ @subscribed_hosts_ids = Set.new(affected_host_ids)
22
18
 
23
19
  InventorySync::InventoryStatus.transaction do
24
20
  InventorySync::InventoryStatus.where(host_id: @subscribed_hosts_ids).delete_all
@@ -35,15 +31,18 @@ module InventorySync
35
31
  def update_statuses_batch
36
32
  results = yield
37
33
 
38
- update_hosts_status(results.status_hashes, results.touched)
39
- host_statuses[:sync] += results.touched.size
34
+ existing_hosts = results.status_hashes.select { |hash| @subscribed_hosts_ids.include?(hash[:host_id]) }
35
+
36
+ update_hosts_status(existing_hosts)
37
+ host_statuses[:sync] += existing_hosts.size
40
38
  end
41
39
 
42
40
  private
43
41
 
44
- def update_hosts_status(status_hashes, touched)
42
+ def update_hosts_status(status_hashes)
45
43
  InventorySync::InventoryStatus.create(status_hashes)
46
- @subscribed_hosts_ids.subtract(touched)
44
+ updated_ids = status_hashes.map { |hash| hash[:host_id] }
45
+ @subscribed_hosts_ids.subtract(updated_ids)
47
46
  end
48
47
 
49
48
  def add_missing_hosts_statuses(hosts_ids)
@@ -64,6 +63,12 @@ module InventorySync
64
63
  disconnect: 0,
65
64
  }
66
65
  end
66
+
67
+ def affected_host_ids
68
+ ForemanInventoryUpload::Generators::Queries.for_slice(
69
+ Host.unscoped.where(organization: input[:organization_id])
70
+ ).pluck(:id)
71
+ end
67
72
  end
68
73
  end
69
74
  end
@@ -31,18 +31,14 @@ module InventorySync
31
31
  private
32
32
 
33
33
  def add_missing_insights_facets(uuids_hash)
34
- existing_facets = InsightsFacet.where(host_id: uuids_hash.keys).pluck(:host_id, :uuid)
35
- missing_facets = uuids_hash.except(*existing_facets.map(&:first)).map do |host_id, uuid|
34
+ all_facets = uuids_hash.map do |host_id, uuid|
36
35
  {
37
36
  host_id: host_id,
38
37
  uuid: uuid,
39
38
  }
40
39
  end
41
- InsightsFacet.create(missing_facets)
42
40
 
43
- existing_facets.select { |host_id, uuid| uuid.empty? }.each do |host_id, _uuid|
44
- InsightsFacet.where(host_id: host_id).update_all(uuid: uuids_hash[host_id])
45
- end
41
+ InsightsFacet.upsert_all(all_facets, unique_by: :host_id) unless all_facets.empty?
46
42
  end
47
43
 
48
44
  def plan_self_host_sync
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foreman_rh_cloud",
3
- "version": "3.0.26",
3
+ "version": "3.0.32",
4
4
  "description": "Inventory Upload =============",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,6 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@babel/core": "~7.7.0",
29
- "@redhat-cloud-services/frontend-components": "^2.5.0",
30
29
  "@theforeman/builder": "~4.14.0",
31
30
  "@theforeman/stories": "~4.14.0",
32
31
  "@theforeman/test": "~4.14.0",
@@ -43,6 +42,7 @@
43
42
  },
44
43
  "dependencies": {
45
44
  "jed": "~1.1.1",
46
- "react-intl": "~2.8.0"
45
+ "react-intl": "~2.8.0",
46
+ "@redhat-cloud-services/frontend-components": "^2.5.0"
47
47
  }
48
48
  }
@@ -37,6 +37,18 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
37
37
  @host2.subscription_facet.pools << pool
38
38
  @host2_inventory_id = '4536bf5c-ff03-4154-a8c9-32ff4b40e40c'
39
39
 
40
+ # this host would pass our plugin queries, so it could be uploaded to the cloud.
41
+ @host3 = FactoryBot.create(
42
+ :host,
43
+ :with_subscription,
44
+ :with_content,
45
+ content_view: cv.first,
46
+ lifecycle_environment: env,
47
+ organization: env.organization
48
+ )
49
+
50
+ @host3.subscription_facet.pools << pool
51
+
40
52
  ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
41
53
 
42
54
  inventory_json = <<-INVENTORY_JSON
@@ -151,7 +163,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
151
163
  {
152
164
  "insights_id": "b533848e-465f-4f1a-9b2b-b71cb2d5239d",
153
165
  "rhel_machine_id": null,
154
- "subscription_manager_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
166
+ "subscription_manager_id": "#{@host3.subscription_facet.uuid}",
155
167
  "satellite_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
156
168
  "bios_uuid": "3cd5d972-cfb5-451a-8314-fd2f56629d7c",
157
169
  "ip_addresses": [
@@ -159,7 +171,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
159
171
  "fd6e:2298:736e::857",
160
172
  "fd6e:2298:736e:0:2c66:6101:9cc6:2b23"
161
173
  ],
162
- "fqdn": "rhel8-demo.oss-lab.net",
174
+ "fqdn": "#{@host3.fqdn}",
163
175
  "mac_addresses": [
164
176
  "6e:66:a6:fe:fc:07",
165
177
  "00:00:00:00:00:00"
@@ -271,4 +283,18 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
271
283
 
272
284
  ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host1.organization)
273
285
  end
286
+
287
+ test 'Should skip hosts that are not returned in query' do
288
+ assert_nil InventorySync::InventoryStatus.where(host_id: @host3.id).first
289
+
290
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'TEST TOKEN')
291
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
292
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:affected_host_ids).returns([@host1.id, @host2.id])
293
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::uuid'], value: '1234', host: @host2)
294
+
295
+ ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host1.organization)
296
+ @host2.reload
297
+
298
+ assert_nil InventorySync::InventoryStatus.where(host_id: @host3.id).first
299
+ end
274
300
  end
@@ -265,4 +265,19 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
265
265
 
266
266
  assert_equal @host2_inventory_id, @host2.insights.uuid
267
267
  end
268
+
269
+ test 'Inventory should sync empty facets list' do
270
+ empty_inventory = @inventory.deep_clone
271
+ empty_inventory['results'] = []
272
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(empty_inventory)
273
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
274
+
275
+ assert_nil @host2.insights
276
+
277
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
278
+
279
+ @host2.reload
280
+
281
+ assert_nil @host2.insights
282
+ end
268
283
  end
@@ -30,13 +30,13 @@ class FactHelpersTest < ActiveSupport::TestCase
30
30
  test 'obfuscates ips with insights-client data' do
31
31
  host = mock('host')
32
32
  @instance.expects(:fact_value).with(host, 'insights_client::ips').returns(
33
- '[{"obfuscated": "10.230.230.1", "original": "224.0.0.1"}, {"obfuscated": "10.230.230.2", "original": "224.0.0.251"}]'
33
+ '[{"obfuscated": "10.230.230.1", "original": "224.0.0.1"}, {"obfuscated": "10.230.230.255", "original": "224.0.0.251"}]'
34
34
  )
35
35
 
36
36
  actual = @instance.obfuscated_ips(host)
37
37
 
38
38
  assert_equal '10.230.230.1', actual['224.0.0.1']
39
- assert_equal '10.230.230.3', actual['224.0.0.2']
39
+ assert_equal '10.230.231.0', actual['224.0.0.2']
40
40
  end
41
41
 
42
42
  test 'obfuscates ips without insights-client data' do
@@ -0,0 +1,28 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ForemanRhCloudSelfHostTest < ActiveSupport::TestCase
4
+ setup do
5
+ # reset cached value
6
+ ForemanRhCloud.instance_variable_set(:@foreman_host, nil)
7
+ end
8
+
9
+ test 'finds host by fullname' do
10
+ @domain
11
+ @host = FactoryBot.create(:host, :managed)
12
+ ForemanRhCloud.expects(:foreman_host_name).returns(@host.name)
13
+
14
+ actual = ForemanRhCloud.foreman_host
15
+
16
+ assert_not_nil actual
17
+ end
18
+
19
+ test 'finds host by shortname' do
20
+ @host = FactoryBot.create(:host, :managed)
21
+ Host.where(name: @host.name).update_all(name: @host.shortname)
22
+ ForemanRhCloud.expects(:foreman_host_name).returns(@host.name)
23
+
24
+ actual = ForemanRhCloud.foreman_host
25
+
26
+ assert_not_nil actual
27
+ end
28
+ end
@@ -139,4 +139,21 @@ class CloudRequestForwarderTest < ActiveSupport::TestCase
139
139
  )
140
140
  assert_equal params.merge(:branch_id => 74), @forwarder.prepare_forward_params(req, 74)
141
141
  end
142
+
143
+ test 'should forward content type correctly' do
144
+ user_agent = { :foo => :bar }
145
+ params = { :page => 5, :per_page => 42 }
146
+
147
+ req = ActionDispatch::Request.new(
148
+ 'REQUEST_URI' => '/foo/bar',
149
+ 'REQUEST_METHOD' => 'GET',
150
+ 'HTTP_USER_AGENT' => user_agent,
151
+ 'rack.input' => ::Puma::NullIO.new,
152
+ 'action_dispatch.request.query_parameters' => params
153
+ )
154
+
155
+ actual = @forwarder.prepare_request_opts(req, 'TEST PAYLOAD', params, { cert: @cert1, key: OpenSSL::PKey::RSA.new(1024).to_pem })
156
+
157
+ assert_match /text\/html/, actual[:headers][:content_type]
158
+ end
142
159
  end
@@ -103,6 +103,21 @@ class TagsGeneratorTest < ActiveSupport::TestCase
103
103
  assert_equal 0, actual.count
104
104
  end
105
105
 
106
+ test 'truncates parameter tags' do
107
+ FactoryBot.create(:setting, :name => 'include_parameter_tags', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => true)
108
+
109
+ @host.stubs(:host_inherited_params_objects).returns(
110
+ [
111
+ OpenStruct.new(name: 'str_param', value: 'a' * 251),
112
+ ]
113
+ )
114
+
115
+ generator = create_generator
116
+ actual = Hash[generator.generate_parameters]
117
+
118
+ assert_equal 'Original value exceeds 250 characters', actual['str_param']
119
+ end
120
+
106
121
  private
107
122
 
108
123
  def create_generator
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_rh_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.26
4
+ version: 3.0.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - Foreman Red Hat Cloud team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-30 00:00:00.000000000 Z
11
+ date: 2022-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: katello
@@ -188,6 +188,7 @@ files:
188
188
  - app/views/layouts/foreman_rh_cloud/application.html.erb
189
189
  - config/Gemfile.lock.gh_test
190
190
  - config/database.yml.example
191
+ - config/package-lock.json
191
192
  - config/package-lock.json.gh_test
192
193
  - config/package-lock.json.plugin
193
194
  - config/routes.rb
@@ -267,6 +268,7 @@ files:
267
268
  - test/test_plugin_helper.rb
268
269
  - test/unit/archived_report_generator_test.rb
269
270
  - test/unit/fact_helpers_test.rb
271
+ - test/unit/foreman_rh_cloud_self_host_test.rb
270
272
  - test/unit/insights_facet_test.rb
271
273
  - test/unit/metadata_generator_test.rb
272
274
  - test/unit/rh_cloud_http_proxy_test.rb
@@ -698,6 +700,7 @@ test_files:
698
700
  - test/test_plugin_helper.rb
699
701
  - test/unit/archived_report_generator_test.rb
700
702
  - test/unit/fact_helpers_test.rb
703
+ - test/unit/foreman_rh_cloud_self_host_test.rb
701
704
  - test/unit/insights_facet_test.rb
702
705
  - test/unit/metadata_generator_test.rb
703
706
  - test/unit/rh_cloud_http_proxy_test.rb