Skip to content

Commit

Permalink
Fixes #37657 - Prevent assigning multiple environments after registra…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jeremylenz committed Jul 18, 2024
1 parent 287adc6 commit 874e6f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
21 changes: 13 additions & 8 deletions app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ def server_status
def facts
User.current = User.anonymous_admin
@host.update_candlepin_associations(rhsm_params)
if params[:environments]
new_envs = params[:environments].map do |env|
get_content_view_environment("cp_id", env['id'])
end
new_envs.compact!
Rails.logger.debug "Setting new content view environments for host #{@host.to_label}: #{new_envs.map(&:label)}"
@host.content_facet.content_view_environments = new_envs
end
update_environments_from_facts(params[:environments]) if params[:environments]
update_host_registered_through(@host, request.headers)
@host.refresh_statuses([::Katello::RhelLifecycleStatus])
render :json => {:content => _("Facts successfully updated.")}, :status => :ok
Expand Down Expand Up @@ -412,6 +405,18 @@ def find_activation_keys
activation_keys
end

def update_environments_from_facts(param_environments)
return unless param_environments.present?
new_envs = param_environments.map do |env|
get_content_view_environment("cp_id", env['id'])
end
new_envs.compact!
Rails.logger.debug "Setting new content view environments for host #{@host.to_label}: #{new_envs.map(&:label)}"
@host.content_facet.content_view_environments = new_envs
rescue ::Katello::Errors::MultiEnvironmentNotSupportedError => e
fail HttpErrors::BadRequest, e.message
end

def get_content_view_environment(key, value)
cve = nil
if value
Expand Down
4 changes: 4 additions & 0 deletions app/models/katello/host/content_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def single_lifecycle_environment
end

def content_view_environments=(new_cves)
if new_cves.length > 1 && !Setting['allow_multiple_content_views']
fail ::Katello::Errors::MultiEnvironmentNotSupportedError,
_("Assigning a host to multiple content view environments is not enabled.")
end
super(new_cves)
Katello::ContentViewEnvironmentContentFacet.reprioritize_for_content_facet(self, new_cves)
self.content_view_environments.reload unless self.new_record?
Expand Down
2 changes: 1 addition & 1 deletion lib/katello/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def katello_template_setting_values(name)
type: :boolean,
default: false,
full_name: N_('Allow multiple content views'),
description: N_("Allow a host to be registered to multiple content view environments with 'subscription-manager register --environments'.") # TODO: update this description when AKs support this setting as well
description: N_("Allow a host to be assigned to multiple content view environments with 'subscription-manager register --environments' or 'subscription-manager environments --set'.") # TODO: update this description when AKs support this setting as well

setting 'content_default_http_proxy',
type: :string,
Expand Down
13 changes: 13 additions & 0 deletions test/models/host/content_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ def test_in_content_view_version_environments
end

def test_content_view_environments=
Setting['allow_multiple_content_views'] = true
Katello::ContentViewEnvironmentContentFacet.expects(:reprioritize_for_content_facet).twice
content_facet.content_view_environments.reload
content_facet.content_view_environments = [katello_content_view_environments(:library_dev_view_dev), katello_content_view_environments(:library_dev_staging_view_dev)]
assert_equal 2, content_facet.content_view_environments.length
end

def test_multi_cv_not_enabled
Setting['allow_multiple_content_views'] = false
assert_equal 1, content_facet.content_view_environments.length
assert_raises(::Katello::Errors::MultiEnvironmentNotSupportedError) do
content_facet.content_view_environments = [katello_content_view_environments(:library_dev_view_dev), katello_content_view_environments(:library_dev_staging_view_dev)]
end
end

def test_audit_for_content_facet
org = taxonomies(:empty_organization)
host1 = ::Host::Managed.create!(:name => 'foohost.example.com', :managed => false, :organization_id => org.id)
Expand Down Expand Up @@ -441,6 +450,10 @@ def setup
assert host_one
end

def teardown
Setting['allow_multiple_content_views'] = false
end

def test_content_view_search
assert_includes ::Host::Managed.search_for("content_view = \"#{view.name}\""), host
end
Expand Down

0 comments on commit 874e6f4

Please sign in to comment.