Skip to content

Commit 7467c32

Browse files
committed
Patch Katello controllers to enable access by smart proxy
1 parent a7dbbb8 commit 7467c32

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,31 @@ 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+
rescue ActiveRecord::NoDatabaseError
236+
end
237+
213238
rake_tasks do
214239
Rake::Task['db:seed'].enhance do
215240
ForemanRhCloud::Engine.load_seed
@@ -231,4 +256,8 @@ def self.ca_cert
231256
::SETTINGS[:ssl_ca_file]
232257
end
233258
end
259+
260+
def self.on_prem_smart_proxy_features
261+
['insights']
262+
end
234263
end

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)