diff --git a/app/lib/proxy_api/available_proxy.rb b/app/lib/proxy_api/available_proxy.rb deleted file mode 100644 index ce74c6283..000000000 --- a/app/lib/proxy_api/available_proxy.rb +++ /dev/null @@ -1,44 +0,0 @@ -module ::ProxyAPI - class AvailableProxy - HTTP_ERRORS = [ - EOFError, - Errno::ECONNRESET, - Errno::EINVAL, - Net::HTTPBadResponse, - Net::HTTPHeaderSyntaxError, - Net::ProtocolError, - Timeout::Error, - ProxyAPI::ProxyException - ].freeze - - def initialize(args) - @args = args - end - - def available? - begin - return true if has_scap_feature? && minimum_version - rescue *HTTP_ERRORS - return false - end - false - end - - private - - def has_scap_feature? - @features ||= ::ProxyAPI::Features.new(@args).features - @features.include?('openscap') - end - - def openscap_proxy_version - @versions ||= ::ProxyAPI::Version.new(@args).proxy_versions['modules'] - @versions['openscap'] if @versions && @versions['openscap'] - end - - def minimum_version - return false unless openscap_proxy_version - Gem::Version.new(openscap_proxy_version) >= Gem::Version.new('0.5') - end - end -end diff --git a/app/lib/proxy_api/openscap.rb b/app/lib/proxy_api/openscap.rb index 876b21ad5..cf3004ab0 100644 --- a/app/lib/proxy_api/openscap.rb +++ b/app/lib/proxy_api/openscap.rb @@ -1,5 +1,16 @@ module ::ProxyAPI class Openscap < ::ProxyAPI::Resource + HTTP_ERRORS = [ + EOFError, + Errno::ECONNRESET, + Errno::EINVAL, + Net::HTTPBadResponse, + Net::HTTPHeaderSyntaxError, + Net::ProtocolError, + Timeout::Error, + ProxyAPI::ProxyException + ].freeze + def initialize(args) @url = args[:url] + '/compliance/' super args diff --git a/app/models/concerns/foreman_openscap/data_stream_content.rb b/app/models/concerns/foreman_openscap/data_stream_content.rb index c6dbf9b2d..cdaf34687 100644 --- a/app/models/concerns/foreman_openscap/data_stream_content.rb +++ b/app/models/concerns/foreman_openscap/data_stream_content.rb @@ -10,11 +10,7 @@ module DataStreamContent end def proxy_url - @proxy_url ||= SmartProxy.with_features('Openscap').find do |proxy| - available = ProxyAPI::AvailableProxy.new(:url => proxy.url) - available.available? - end.try(:url) - @proxy_url + @proxy_url ||= SmartProxy.with_features('Openscap').first&.url end def create_profiles diff --git a/app/validators/foreman_openscap/data_stream_validator.rb b/app/validators/foreman_openscap/data_stream_validator.rb index d0b16b75c..719fea162 100644 --- a/app/validators/foreman_openscap/data_stream_validator.rb +++ b/app/validators/foreman_openscap/data_stream_validator.rb @@ -22,7 +22,7 @@ def validate(data_stream_content) errors['errors'].each { |error| data_stream_content.errors.add(:scap_file, _(error)) } return false end - rescue *ProxyAPI::AvailableProxy::HTTP_ERRORS => e + rescue *ProxyAPI::Openscap::HTTP_ERRORS => e data_stream_content.errors.add(:base, _('No available proxy to validate. Returned with error: %s') % e) return false end diff --git a/lib/foreman_openscap/data_migration.rb b/lib/foreman_openscap/data_migration.rb index 37601142d..929423a59 100644 --- a/lib/foreman_openscap/data_migration.rb +++ b/lib/foreman_openscap/data_migration.rb @@ -6,14 +6,15 @@ module ForemanOpenscap class DataMigration def initialize(proxy_id) - @proxy = ::SmartProxy.find(proxy_id) - puts "Found proxy #{@proxy.to_label}" - @url = @proxy.url + @proxy = ::SmartProxy.with_features('Openscap').where(id: proxy_id).first + if @proxy + puts "Found proxy #{@proxy.to_label}" + @url = @proxy.url + end end def available? - return false unless @proxy && @url - ::ProxyAPI::AvailableProxy.new(:url => @url).available? && foreman_available? + @proxy && @url && foreman_available? end def migrate @@ -47,7 +48,7 @@ def foreman_available? foreman_status_url = Setting[:foreman_url] + '/status' response = JSON.parse(RestClient.get(foreman_status_url)) return true if response["status"] == "ok" - rescue *::ProxyAPI::AvailableProxy::HTTP_ERRORS + rescue *::ProxyAPI::Openscap::HTTP_ERRORS return false end diff --git a/test/unit/availble_proxy_test.rb b/test/unit/availble_proxy_test.rb deleted file mode 100644 index 6afa33bb5..000000000 --- a/test/unit/availble_proxy_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'test_plugin_helper' - -class AvailableProxyTest < ActiveSupport::TestCase - test '#minimum_version should correctly compare versions' do - api = ProxyAPI::AvailableProxy.new({ url: 'https://something.somewhere.com' }) - ::ProxyAPI::Version.any_instance.expects(:proxy_versions).returns({ 'modules' => { 'openscap' => '0.10.0' }}) - assert api.send(:minimum_version), '0.10.0 is expected to be a higher version that 0.5' - end -end diff --git a/test/unit/scap_content_test.rb b/test/unit/scap_content_test.rb index 2146fe501..ab9b5cf73 100644 --- a/test/unit/scap_content_test.rb +++ b/test/unit/scap_content_test.rb @@ -17,7 +17,6 @@ class ScapContentTest < ActiveSupport::TestCase test 'scap content should fail if no openscap proxy' do SmartProxy.stubs(:with_features).returns([]) - ProxyAPI::AvailableProxy.any_instance.stubs(:available?).returns(false) scap_content = ForemanOpenscap::ScapContent.new(:title => 'Fedora', :scap_file => @scap_file) refute(scap_content.save) assert_includes(scap_content.errors.messages[:base], 'No proxy with OpenSCAP feature was found.')