From 2fd7b5d46a2abc6a8fa694397c746862a99fc069 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 8 Nov 2024 15:05:25 -0500 Subject: [PATCH] Add Global params for host obfuscation and migrate off settings --- .../uploads_settings_controller.rb | 2 -- ..._convert_obfuscation_settings_to_params.rb | 22 +++++++++++++++++++ .../generators/fact_helpers.rb | 6 ++--- lib/foreman_rh_cloud/engine.rb | 2 -- test/unit/metadata_generator_test.rb | 4 ++-- test/unit/slice_generator_test.rb | 4 ++-- .../AdvancedSettingsConstants.js | 10 --------- .../InventorySettings.test.js.snap | 8 ------- 8 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 db/migrate/20241108165827_convert_obfuscation_settings_to_params.rb diff --git a/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb b/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb index 86c800c7..02f799d6 100644 --- a/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb +++ b/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb @@ -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, diff --git a/db/migrate/20241108165827_convert_obfuscation_settings_to_params.rb b/db/migrate/20241108165827_convert_obfuscation_settings_to_params.rb new file mode 100644 index 00000000..9fef878d --- /dev/null +++ b/db/migrate/20241108165827_convert_obfuscation_settings_to_params.rb @@ -0,0 +1,22 @@ +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 + # rubocop:disable Style/GuardClause + 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 + end + # rubocop:enable Style/GuardClause + end +end diff --git a/lib/foreman_inventory_upload/generators/fact_helpers.rb b/lib/foreman_inventory_upload/generators/fact_helpers.rb index 8f3db409..118e2172 100644 --- a/lib/foreman_inventory_upload/generators/fact_helpers.rb +++ b/lib/foreman_inventory_upload/generators/fact_helpers.rb @@ -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) @@ -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) @@ -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 diff --git a/lib/foreman_rh_cloud/engine.rb b/lib/foreman_rh_cloud/engine.rb index 69d3a57a..d858d49e 100644 --- a/lib/foreman_rh_cloud/engine.rb +++ b/lib/foreman_rh_cloud/engine.rb @@ -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')) diff --git a/test/unit/metadata_generator_test.rb b/test/unit/metadata_generator_test.rb index a91c5b32..25f62f2d 100644 --- a/test/unit/metadata_generator_test.rb +++ b/test/unit/metadata_generator_test.rb @@ -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_or_create_by!(name: 'obfuscate_inventory_ips', value: true) + CommonParameter.find_or_create_by!(name: 'obfuscate_inventory_hostnames', value: true) @host = FactoryBot.create(:host, :managed) ForemanRhCloud.expects(:foreman_host_name).returns(@host.name) generator = ForemanInventoryUpload::Generators::Metadata.new diff --git a/test/unit/slice_generator_test.rb b/test/unit/slice_generator_test.rb index 661ee337..f6a36d65 100644 --- a/test/unit/slice_generator_test.rb +++ b/test/unit/slice_generator_test.rb @@ -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_or_create_by!(name: 'obfuscate_inventory_ips', value: true) @host.interfaces << FactoryBot.build(:nic_managed) batch = Host.where(id: @host.id).in_batches.first @@ -261,7 +261,7 @@ def fact_names end test 'obfuscates fqdn when setting set' do - Setting[:obfuscate_inventory_hostnames] = true + CommonParameter.find_or_create_by!(name: 'obfuscate_inventory_hostnames', value: true) batch = Host.where(id: @host.id).in_batches.first generator = create_generator(batch) diff --git a/webpack/ForemanInventoryUpload/Components/InventorySettings/AdvancedSetting/AdvancedSettingsConstants.js b/webpack/ForemanInventoryUpload/Components/InventorySettings/AdvancedSetting/AdvancedSettingsConstants.js index c8898624..21c55d09 100644 --- a/webpack/ForemanInventoryUpload/Components/InventorySettings/AdvancedSetting/AdvancedSettingsConstants.js +++ b/webpack/ForemanInventoryUpload/Components/InventorySettings/AdvancedSetting/AdvancedSettingsConstants.js @@ -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'), diff --git a/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/__snapshots__/InventorySettings.test.js.snap b/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/__snapshots__/InventorySettings.test.js.snap index 6f5229a2..d4f7d835 100644 --- a/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/__snapshots__/InventorySettings.test.js.snap +++ b/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/__snapshots__/InventorySettings.test.js.snap @@ -11,14 +11,6 @@ exports[`InventorySettings rendering render without Props 1`] = ` key="autoUploadEnabled" setting="autoUploadEnabled" /> - -