Skip to content

Commit

Permalink
Add Global params for host obfuscation and migrate off settings
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 committed Nov 8, 2024
1 parent 48f9d74 commit 95ceb7a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class UploadsSettingsController < ::ApplicationController
def index
render json: {
autoUploadEnabled: Setting[:allow_auto_inventory_upload],
hostObfuscationEnabled: Setting[:obfuscate_inventory_hostnames],
ipsObfuscationEnabled: Setting[:obfuscate_inventory_ips],
excludePackagesEnabled: Setting[:exclude_installed_packages],
allowAutoInsightsMismatchDelete: Setting[:allow_auto_insights_mismatch_delete],
CloudConnectorStatus: ForemanInventoryUpload::UploadsSettingsController.cloud_connector_status,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ConvertObfuscationSettingsToParams < ActiveRecord::Migration[6.1]
def change
# Create the common parameters if they don't exist and set the default values to false
CommonParameter.find_or_create_by!(name: 'obfuscate_inventory_hostnames', value: false)
CommonParameter.find_or_create_by!(name: 'obfuscate_inventory_ips', value: false)

# Copy the settings to the common parameters and remove the settings from the database
if Setting.find_by(name: 'obfuscate_inventory_hostnames')&.value
hostname_setting = CommonParameter.find_by(name: 'obfuscate_inventory_hostnames')
hostname_setting.update!(value: Setting.find_by(name: 'obfuscate_inventory_hostnames')&.value)
Setting.find_by(name: 'obfuscate_inventory_hostnames').destroy_all
end

if Setting.find_by(name: 'obfuscate_inventory_ips')&.value
ip_setting = CommonParameter.find_by(name: 'obfuscate_inventory_ips')
ip_setting.update!(value: Setting.find_by(name: 'obfuscate_inventory_ips')&.value)
Setting.find_by(name: 'obfuscate_inventory_ips')destroy_all

Check failure on line 17 in db/migrate/20241108165827_convert_obfuscation_settings_to_params.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Lint/Syntax: unexpected token tIDENTIFIER (Using Ruby 2.7 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
end
end
end
6 changes: 3 additions & 3 deletions lib/foreman_inventory_upload/generators/fact_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def obfuscate_hostname?(host)
insights_client_setting = ActiveModel::Type::Boolean.new.cast(insights_client_setting)
return insights_client_setting unless insights_client_setting.nil?

Setting[:obfuscate_inventory_hostnames]
CommonParameter.find_by(name: 'obfuscate_inventory_hostnames')&.value
end

def fqdn(host)
Expand All @@ -79,7 +79,7 @@ def obfuscate_ips?(host)
insights_client_setting = ActiveModel::Type::Boolean.new.cast(insights_client_setting)
return insights_client_setting unless insights_client_setting.nil?

Setting[:obfuscate_inventory_ips]
CommonParameter.find_by(name: 'obfuscate_inventory_ips')&.value
end

def host_ips(host)
Expand Down Expand Up @@ -114,7 +114,7 @@ def hostname_match
foreman_hostname = ForemanRhCloud.foreman_host&.name
if bash_hostname == foreman_hostname
fqdn(ForemanRhCloud.foreman_host)
elsif Setting[:obfuscate_inventory_hostnames]
elsif CommonParameter.find_by(name: 'obfuscate_inventory_hostnames')&.value
obfuscate_fqdn(bash_hostname)
else
bash_hostname
Expand Down
2 changes: 0 additions & 2 deletions lib/foreman_rh_cloud/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def self.register_scheduled_task(task_class, cronline)
setting('allow_auto_inventory_upload', type: :boolean, description: N_('Enable automatic upload of your host inventory to the Red Hat cloud'), default: true, full_name: N_('Automatic inventory upload'))
setting('allow_auto_insights_sync', type: :boolean, description: N_('Enable automatic synchronization of Insights recommendations from the Red Hat cloud'), default: false, full_name: N_('Synchronize recommendations Automatically'))
setting('allow_auto_insights_mismatch_delete', type: :boolean, description: N_('Enable automatic deletion of mismatched host records from the Red Hat cloud'), default: false, full_name: N_('Automatic mismatch deletion'))
setting('obfuscate_inventory_hostnames', type: :boolean, description: N_('Obfuscate host names sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host names'))
setting('obfuscate_inventory_ips', type: :boolean, description: N_('Obfuscate ipv4 addresses sent to the Red Hat cloud'), default: false, full_name: N_('Obfuscate host ipv4 addresses'))
setting('exclude_installed_packages', type: :boolean, description: N_('Exclude installed packages from being uploaded to the Red Hat cloud'), default: false, full_name: N_("Exclude installed Packages"))
setting('include_parameter_tags', type: :boolean, description: N_('Should import include parameter tags from Foreman?'), default: false, full_name: N_('Include parameters in insights-client reports'))
setting('rhc_instance_id', type: :string, description: N_('RHC daemon id'), default: nil, full_name: N_('ID of the RHC(Yggdrasil) daemon'))
Expand Down
4 changes: 2 additions & 2 deletions test/unit/metadata_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class MetadataGeneratorTest < ActiveSupport::TestCase
end

test 'generates an empty report with hidden hostname and ip' do
Setting[:obfuscate_inventory_hostnames] = true
Setting[:obfuscate_inventory_ips] = true
CommonParameter.find_by(name: 'obfuscate_inventory_hostnames')&.value = true
CommonParameter.find_by(name: 'obfuscate_inventory_ips')&.value = true
@host = FactoryBot.create(:host, :managed)
ForemanRhCloud.expects(:foreman_host_name).returns(@host.name)
generator = ForemanInventoryUpload::Generators::Metadata.new
Expand Down
4 changes: 2 additions & 2 deletions test/unit/slice_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def fact_names
end

test 'generates obfuscated ip_address fields without inisghts-client' do
Setting[:obfuscate_inventory_ips] = true
CommonParameter.find_by(name: 'obfuscate_inventory_ips')&.value = true

@host.interfaces << FactoryBot.build(:nic_managed)
batch = Host.where(id: @host.id).in_batches.first
Expand Down Expand Up @@ -261,7 +261,7 @@ def fact_names
end

test 'obfuscates fqdn when setting set' do
Setting[:obfuscate_inventory_hostnames] = true
CommonParameter.find_by(name: 'obfuscate_inventory_hostnames')&.value = true

batch = Host.where(id: @host.id).in_batches.first
generator = create_generator(batch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ export const settingsDict = {
'Enable automatic upload of your hosts inventory to the Red Hat cloud'
),
},
hostObfuscationEnabled: {
name: 'obfuscate_inventory_hostnames',
label: __('Obfuscate host names'),
tooltip: __('Obfuscate host names sent to the Red Hat cloud'),
},
ipsObfuscationEnabled: {
name: 'obfuscate_inventory_ips',
label: __('Obfuscate host ipv4 addresses'),
tooltip: __('Obfuscate ipv4 addresses sent to the Red Hat cloud'),
},
excludePackagesEnabled: {
name: 'exclude_installed_packages',
label: __('Exclude installed Packages'),
Expand Down

0 comments on commit 95ceb7a

Please sign in to comment.