Skip to content

Commit

Permalink
Refs #37487 - Refactor proxy selection
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka committed May 20, 2024
1 parent cb48879 commit c39f344
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 66 deletions.
44 changes: 0 additions & 44 deletions app/lib/proxy_api/available_proxy.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/lib/proxy_api/openscap.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 1 addition & 5 deletions app/models/concerns/foreman_openscap/data_stream_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/validators/foreman_openscap/data_stream_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions lib/foreman_openscap/data_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions test/unit/availble_proxy_test.rb

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/scap_content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down

0 comments on commit c39f344

Please sign in to comment.