From c1c2c2f663be40872e5b1427fa552e61acc0e1c1 Mon Sep 17 00:00:00 2001
From: Ewoud Kohl van Wijngaarden
Date: Tue, 9 Jul 2024 16:25:54 +0200
Subject: [PATCH] Fixes #37642 - Drop compatibility with smart_proxy_openscap <
0.6.1
If this was still needed, we could use capabilities and easily check
this statically but version 0.6.1 was released in 2017 so by now it's
safe to assume it's new enough.
Dropping the old code makes it more reliable and we have less code to
maintain.
---
.../compliance/tailoring_files_controller.rb | 8 ---
app/helpers/tailoring_files_helper.rb | 5 --
.../foreman_openscap/host_extensions.rb | 4 +-
app/models/foreman_openscap/policy.rb | 4 --
.../openscap_proxy_assigned_version_check.rb | 14 -----
.../openscap_proxy_version_check.rb | 62 -------------------
app/views/tailoring_files/welcome.html.erb | 9 +--
.../tailoring_files_controller_test.rb | 11 ----
test/unit/concerns/host_extensions_test.rb | 9 ---
.../tailoring_files_proxy_check_test.rb | 27 --------
10 files changed, 2 insertions(+), 151 deletions(-)
delete mode 100644 app/helpers/tailoring_files_helper.rb
delete mode 100644 app/services/foreman_openscap/openscap_proxy_assigned_version_check.rb
delete mode 100644 app/services/foreman_openscap/openscap_proxy_version_check.rb
delete mode 100644 test/unit/services/tailoring_files_proxy_check_test.rb
diff --git a/app/controllers/api/v2/compliance/tailoring_files_controller.rb b/app/controllers/api/v2/compliance/tailoring_files_controller.rb
index 786d29eef..361bc337e 100644
--- a/app/controllers/api/v2/compliance/tailoring_files_controller.rb
+++ b/app/controllers/api/v2/compliance/tailoring_files_controller.rb
@@ -6,7 +6,6 @@ class TailoringFilesController < ::Api::V2::BaseController
include ForemanOpenscap::Api::V2::ScapApiControllerExtensions
before_action :find_resource, :except => %w[index create]
- before_action :openscap_proxy_check, :only => %w[create]
api :GET, '/compliance/tailoring_files', N_('List Tailoring files')
param_group :search_and_pagination, ::Api::V2::BaseController
@@ -77,13 +76,6 @@ def action_permission
super
end
end
-
- def openscap_proxy_check
- unless ForemanOpenscap::TailoringFile.any?
- check = ForemanOpenscap::OpenscapProxyVersionCheck.new.run
- render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => check.message } unless check.pass?
- end
- end
end
end
end
diff --git a/app/helpers/tailoring_files_helper.rb b/app/helpers/tailoring_files_helper.rb
deleted file mode 100644
index 01fda263a..000000000
--- a/app/helpers/tailoring_files_helper.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-module TailoringFilesHelper
- def run_tailoring_proxy_check
- ForemanOpenscap::OpenscapProxyVersionCheck.new.run
- end
-end
diff --git a/app/models/concerns/foreman_openscap/host_extensions.rb b/app/models/concerns/foreman_openscap/host_extensions.rb
index 69388bd39..ade326135 100644
--- a/app/models/concerns/foreman_openscap/host_extensions.rb
+++ b/app/models/concerns/foreman_openscap/host_extensions.rb
@@ -102,9 +102,7 @@ def policies_enc
end
def policies_enc_raw
- check = ForemanOpenscap::OpenscapProxyAssignedVersionCheck.new(self).run
- method = check.pass? ? :to_enc : :to_enc_legacy
- combined_policies.map(&method)
+ combined_policies.map(&:to_enc)
end
def combined_policies
diff --git a/app/models/foreman_openscap/policy.rb b/app/models/foreman_openscap/policy.rb
index 96caa019e..cf72db2d2 100644
--- a/app/models/foreman_openscap/policy.rb
+++ b/app/models/foreman_openscap/policy.rb
@@ -196,10 +196,6 @@ def to_enc
}.merge(period_enc)
end
- def to_enc_legacy
- to_enc.tap { |hash| hash['download_path'] = "/compliance/policies/#{self.id}/content" }
- end
-
def should_validate?(step_name)
if new_record? && wizard_initiated?
step_index > step_to_i(step_name)
diff --git a/app/services/foreman_openscap/openscap_proxy_assigned_version_check.rb b/app/services/foreman_openscap/openscap_proxy_assigned_version_check.rb
deleted file mode 100644
index d1d8e69a4..000000000
--- a/app/services/foreman_openscap/openscap_proxy_assigned_version_check.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-module ForemanOpenscap
- class OpenscapProxyAssignedVersionCheck < OpenscapProxyVersionCheck
- def initialize(host)
- @host = host
- super()
- end
-
- private
-
- def get_openscap_proxies
- @host.openscap_proxy ? [@host.openscap_proxy] : []
- end
- end
-end
diff --git a/app/services/foreman_openscap/openscap_proxy_version_check.rb b/app/services/foreman_openscap/openscap_proxy_version_check.rb
deleted file mode 100644
index e812fba1b..000000000
--- a/app/services/foreman_openscap/openscap_proxy_version_check.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-module ForemanOpenscap
- class OpenscapProxyVersionCheck
- def initialize
- @versions = {}
- @message = ''
- @down = []
- end
-
- def run
- @versions = openscap_proxy_versions.select do |key, value|
- Gem::Version.new(value) < Gem::Version.new("0.6.1")
- end
- self
- end
-
- def pass?
- !any_outdated? && !any_unreachable?
- end
-
- def any_outdated?
- !@versions.empty?
- end
-
- def any_unreachable?
- !@down.empty?
- end
-
- def message
- if pass?
- @message
- else
- build_message
- end
- end
-
- private
-
- def build_message
- @message = _('This feature is temporarily disabled. ')
- @message << _('The following Smart Proxies need to be updated to unlock the feature: %s. ') % @versions.keys.to_sentence if any_outdated?
- @message << _('The following proxies could not be reached: %s. Please make sure they are available so Foreman can check their versions.') % @down.to_sentence if any_unreachable?
- @message
- end
-
- def get_openscap_proxies
- SmartProxy.with_features "Openscap"
- end
-
- def openscap_proxy_versions
- get_openscap_proxies.inject({}) do |memo, proxy|
- begin
- status = ProxyStatus::Version.new(proxy).version
- openscap_version = status["modules"]["openscap"]
- memo[proxy.name] = openscap_version
- rescue Foreman::WrappedException
- @down << proxy.name
- end
- memo
- end
- end
- end
-end
diff --git a/app/views/tailoring_files/welcome.html.erb b/app/views/tailoring_files/welcome.html.erb
index bf5c00dbf..b4717f4b5 100644
--- a/app/views/tailoring_files/welcome.html.erb
+++ b/app/views/tailoring_files/welcome.html.erb
@@ -8,14 +8,7 @@
<%= (_('In Foreman, tailoring_files represent the custom modifications to default XCCDF profiles and they can be applied to hosts
via %s') % link_to('compliance policies', policies_path)).html_safe %>
- <% proxy_check = run_tailoring_proxy_check %>
- <%= new_link(_('New Tailoring File'), {}, { :class => "btn-lg", :disabled => !proxy_check.pass? }) %>
+ <%= new_link(_('New Tailoring File'), {}, { :class => "btn-lg" }) %>
-
-
- <% unless proxy_check.pass? %>
- <%= alert :class => 'alert-warning', :header => '', :text => proxy_check.message.html_safe %>
- <% end %>
-
diff --git a/test/functional/api/v2/compliance/tailoring_files_controller_test.rb b/test/functional/api/v2/compliance/tailoring_files_controller_test.rb
index f053fc432..375c18c53 100644
--- a/test/functional/api/v2/compliance/tailoring_files_controller_test.rb
+++ b/test/functional/api/v2/compliance/tailoring_files_controller_test.rb
@@ -24,8 +24,6 @@ class Api::V2::Compliance::TailoringFilesControllerTest < ActionController::Test
test "should create tailoring_file" do
tf = FactoryBot.build(:tailoring_file)
tf_params = { :name => tf.name, :original_filename => tf.original_filename, :scap_file => tf.scap_file }
- ForemanOpenscap::OpenscapProxyVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns({})
post :create, :params => tf_params, :session => set_session_user
assert_response :success
end
@@ -50,13 +48,4 @@ class Api::V2::Compliance::TailoringFilesControllerTest < ActionController::Test
assert_response :ok
refute ForemanOpenscap::ScapContent.exists?(tailoring_file.id)
end
-
- test "should not create tailoring file when there is outdated proxy version" do
- tf = FactoryBot.build(:tailoring_file)
- tf_params = { :name => tf.name, :original_filename => tf.original_filename, :scap_file => tf.scap_file }
- ForemanOpenscap::OpenscapProxyVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns('test-proxy' => '0.5.4')
- post :create, :params => tf_params, :session => set_session_user
- assert_response :unprocessable_entity
- end
end
diff --git a/test/unit/concerns/host_extensions_test.rb b/test/unit/concerns/host_extensions_test.rb
index af7d40df8..25140821f 100644
--- a/test/unit/concerns/host_extensions_test.rb
+++ b/test/unit/concerns/host_extensions_test.rb
@@ -9,16 +9,7 @@ class HostExtensionsTest < ActiveSupport::TestCase
@host = FactoryBot.create(:compliance_host, :policies => [@policy])
end
- test "should have download_path in enc without digest" do
- ForemanOpenscap::OpenscapProxyAssignedVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns('test-proxy' => '0.5.4')
- enc_out = JSON.parse @host.policies_enc
- assert_equal 5, enc_out.first['download_path'].split('/').length
- end
-
test "should have download_path in enc with digest" do
- ForemanOpenscap::OpenscapProxyAssignedVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns({})
enc_out = JSON.parse @host.policies_enc
assert_equal 6, enc_out.first['download_path'].split('/').length
end
diff --git a/test/unit/services/tailoring_files_proxy_check_test.rb b/test/unit/services/tailoring_files_proxy_check_test.rb
deleted file mode 100644
index 5d2cc2abc..000000000
--- a/test/unit/services/tailoring_files_proxy_check_test.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-require 'test_plugin_helper'
-
-class TailoringFilesProxyCheckTest < ActiveSupport::TestCase
- test 'should find proxies with old versions' do
- ForemanOpenscap::OpenscapProxyVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns('old-proxy.test.com' => "0.5.4", "outdate-proxy.test.com" => "0.6.0")
- check = ForemanOpenscap::OpenscapProxyVersionCheck.new.run
- refute check.pass?
- refute check.message.empty?
- end
-
- test 'should not find any outdated proxies' do
- ForemanOpenscap::OpenscapProxyVersionCheck.any_instance.stubs(:openscap_proxy_versions)
- .returns({})
- check = ForemanOpenscap::OpenscapProxyVersionCheck.new.run
- assert check.pass?
- assert check.message.empty?
- end
-
- test 'should fail when proxy cannot be reached' do
- ProxyStatus::Version.any_instance.stubs(:version).raises(Foreman::WrappedException.new(nil, 'test message'))
- ForemanOpenscap::OpenscapProxyVersionCheck.any_instance.stubs(:get_openscap_proxies).returns([FactoryBot.create(:openscap_proxy)])
- check = ForemanOpenscap::OpenscapProxyVersionCheck.new.run
- refute check.pass?
- refute check.message.empty?
- end
-end