Skip to content

Commit 01e9f3d

Browse files
authored
Patch Katello controllers to enable access by smart proxy (#1003)
* Patch Katello controllers to enable access by smart proxy * Make the tests more predictable
1 parent 4dc5e0e commit 01e9f3d

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

db/seeds.d/200_features.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ForemanRhCloud.on_prem_smart_proxy_features.each do |feature_name|
2+
f = Feature.where(:name => feature_name).first_or_create
3+
raise "Unable to create proxy feature: #{SeedHelper.format_errors f}" if f.nil? || f.errors.any?
4+
end

lib/foreman_rh_cloud/engine.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,30 @@ def self.register_scheduled_task(task_class, cronline)
210210
)
211211
end
212212

213+
# Ideally this code belongs to an initializer. The problem is that Katello controllers are not initialized completely until after the end of the to_prepare blocks
214+
# This means I can patch the controller only in the after_initialize block that is promised to run after the to_prepare
215+
# initializer 'foreman_rh_cloud.allow_smart_proxy_actions', :before => :finisher_hook, :after => 'katello.register_plugin' do |_app|
216+
# end
217+
config.after_initialize do
218+
# skip overrides in migrations, since the controller initialization depends on tables existense
219+
if defined?(Katello) && !Foreman.in_setup_db_rake?
220+
Katello::Api::V2::OrganizationsController.include Foreman::Controller::SmartProxyAuth
221+
# patch the callbacks order for :download_debug_certificate, since local_find_taxonomy has to run after the user is already initialized
222+
Katello::Api::V2::OrganizationsController.skip_before_action(:local_find_taxonomy, only: :download_debug_certificate)
223+
Katello::Api::V2::OrganizationsController.add_smart_proxy_filters(
224+
[:index, :download_debug_certificate],
225+
features: ForemanRhCloud.on_prem_smart_proxy_features
226+
)
227+
Katello::Api::V2::OrganizationsController.before_action(:local_find_taxonomy, only: :download_debug_certificate)
228+
229+
Katello::Api::V2::RepositoriesController.include Foreman::Controller::SmartProxyAuth
230+
Katello::Api::V2::RepositoriesController.add_smart_proxy_filters(
231+
:index,
232+
features: ForemanRhCloud.on_prem_smart_proxy_features
233+
)
234+
end
235+
end
236+
213237
rake_tasks do
214238
Rake::Task['db:seed'].enhance do
215239
ForemanRhCloud::Engine.load_seed
@@ -231,4 +255,8 @@ def self.ca_cert
231255
::SETTINGS[:ssl_ca_file]
232256
end
233257
end
258+
259+
def self.on_prem_smart_proxy_features
260+
['Insights']
261+
end
234262
end

test/controllers/insights_cloud/api/cloud_request_controller_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class CloudRequestControllerTest < ActionController::TestCase
5151
mock_composer = mock('composer')
5252
::JobInvocationComposer.expects(:for_feature).with do |feature, host_ids, params|
5353
feature == :rh_cloud_connector_run_playbook &&
54-
host_ids.first == host1.id &&
55-
host_ids.last == host2.id
54+
host_ids.sort == [host1.id, host2.id].sort
5655
end.returns(mock_composer)
5756
mock_composer.expects(:trigger!)
5857
mock_composer.expects(:job_invocation)

test/unit/rh_cloud_http_proxy_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class RhCloudHttpProxyTest < ActiveSupport::TestCase
44
setup do
55
@global_content_proxy_mock = 'http://global:content@localhost:80'
66
@global_foreman_proxy_mock = 'http://global:foreman@localhost:80'
7+
ForemanRhCloud.stubs(:with_local_advisor_engine?).returns(false)
78
end
89

910
test 'selects global content proxy' do
@@ -18,6 +19,13 @@ class RhCloudHttpProxyTest < ActiveSupport::TestCase
1819
assert_equal @global_foreman_proxy_mock, ForemanRhCloud.proxy_setting
1920
end
2021

22+
test 'returns empty string in on-prem setup' do
23+
ForemanRhCloud.unstub(:with_local_advisor_engine?)
24+
ForemanRhCloud.stubs(:with_local_advisor_engine?).returns(true)
25+
26+
assert_empty ForemanRhCloud.proxy_setting
27+
end
28+
2129
def setup_global_content_proxy
2230
http_proxy = FactoryBot.create(:http_proxy, url: @global_content_proxy_mock)
2331
HttpProxy.stubs(:default_global_content_proxy).returns(http_proxy)

0 commit comments

Comments
 (0)