diff --git a/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb b/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb index e8350f45785..cc7c9aa19c0 100644 --- a/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb +++ b/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb @@ -211,7 +211,7 @@ def enabled_repos #api :POST, "/environments/:environment_id/consumers", N_("Register a consumer in environment") def consumer_create - host = Katello::RegistrationManager.process_registration(rhsm_params, find_content_view_environments) + host = Katello::RegistrationManager.process_registration(rhsm_params, content_view_environments) host.reload @@ -413,7 +413,8 @@ def find_activation_keys def update_environments_from_facts(param_environments) return if param_environments.blank? new_envs = param_environments.map do |env| - get_content_view_environment("cp_id", env['id']) + cve = get_content_view_environment("cp_id", env['id']) + cve unless cve&.content_view&.generated_for_repository_export? end new_envs.compact! Rails.logger.debug "Setting new content view environments for host #{@host.to_label}: #{new_envs.map(&:label)}" diff --git a/db/migrate/20240815080259_migrate_off_generated_content_views.rb b/db/migrate/20240815080259_migrate_off_generated_content_views.rb new file mode 100644 index 00000000000..6e542787fc4 --- /dev/null +++ b/db/migrate/20240815080259_migrate_off_generated_content_views.rb @@ -0,0 +1,47 @@ +class MigrateOffGeneratedContentViews < ActiveRecord::Migration[6.1] + class FakeCVECF < ApplicationRecord + self.table_name = 'katello_content_view_environment_content_facets' + end + + def up + say_with_time "Migrating hosts off generated content views" do + migrate_hosts + end + end + + def down + say "This migration cannot be reversed", true + end + + private + + def migrate_hosts + say_with_time "Migrating hosts..." do + generated_content_views = Katello::ContentView.generated_for_repository + + facets = Katello::Host::ContentFacet.joins(:content_view_environments). + where(content_view_environments: { content_view: generated_content_views }) + facets.all.each do |content_facet| + valid_cves = content_facet.content_view_environments.where.not(content_view: generated_content_views) + if valid_cves.empty? + organization = content_facet.host.organization + default_cve = organization.content_view_environments.find_by(lifecycle_environment: organization.library, + content_view: organization.default_content_view) + if default_cve + FakeCVECF.where(content_facet_id: content_facet).delete_all + FakeCVECF.create!(content_facet_id: content_facet.id, + content_view_environment_id: default_cve.id) + say "Replaced all content views with Default Organization View for host #{content_facet.host.name}", true + else + say "No Default Organization View found for host #{content_facet.host.name}. Skipping.", true + end + else + FakeCVECF.where(content_facet_id: content_facet). + where.not(content_view_environment_id: valid_cves). + delete_all + say "Removed offending content views for host #{content_facet.host.name}", true + end + end + end + end +end diff --git a/test/controllers/api/rhsm/candlepin_proxies_controller_test.rb b/test/controllers/api/rhsm/candlepin_proxies_controller_test.rb index 1f0223d5c2a..ea3485b2a81 100644 --- a/test/controllers/api/rhsm/candlepin_proxies_controller_test.rb +++ b/test/controllers/api/rhsm/candlepin_proxies_controller_test.rb @@ -85,7 +85,7 @@ module Katello end end - describe "register with a lifecycle environment" do + describe "register with a single lifecycle environment" do before do @facts = { 'network.hostname' => 'somehostname'} @content_view_environment = ContentViewEnvironment.find(katello_content_view_environments(:library_default_view_environment).id) @@ -122,7 +122,7 @@ module Katello assert_response 400 end - it "should not register" do + it "should not register with dead services" do ::Katello::RegistrationManager.expects(:check_registration_services).returns(false) ::Katello::RegistrationManager.expects(:process_registration).never @@ -131,6 +131,31 @@ module Katello assert_response 500 end + + it "should not register with a content view generated for repository export" do + organization = Organization.find(taxonomies(:empty_organization).id) + library = organization.library + + generated_cv = FactoryBot.create(:katello_content_view, :generated_for => :repository_export, :organization => organization) + generated_cve = FactoryBot.create(:katello_content_view_environment, + :content_view => generated_cv, + :environment => library) + + Resources::Candlepin::Consumer.stubs(:get) + ::Katello::RegistrationManager.expects(:check_registration_services).returns(true) + ::Katello::RegistrationManager.expects(:process_registration).never + + post( + :consumer_create, + :params => { + :organization_id => organization.label, + :environment_id => generated_cve.cp_id, + :facts => @facts + } + ) + + assert_response 400 + end end describe "update enabled_repos" do diff --git a/test/models/activation_key_test.rb b/test/models/activation_key_test.rb index 59605f18bfb..acced5359b9 100644 --- a/test/models/activation_key_test.rb +++ b/test/models/activation_key_test.rb @@ -107,6 +107,16 @@ def setup end end + test "should not allow generated content view to be associated with activation key" do + generated_cv = FactoryBot.create(:katello_content_view, :generated_for => :repository_export, :organization => @dev_view.organization) + + exception = assert_raises(ActiveRecord::RecordInvalid) do + @dev_key.update!(content_view: generated_cv) + end + + assert_match(/Generated content views cannot be assigned to hosts or activation keys/, exception.message) + end + def test_search_name activation_keys = ActivationKey.search_for("name = \"#{@dev_staging_view_key.name}\"") assert_includes activation_keys, @dev_staging_view_key diff --git a/test/models/host/content_facet_test.rb b/test/models/host/content_facet_test.rb index 9cf0ed54d27..41cf0d054fe 100644 --- a/test/models/host/content_facet_test.rb +++ b/test/models/host/content_facet_test.rb @@ -93,6 +93,17 @@ def test_audit_for_content_facet content_facet_rec = host1.associated_audits.where(auditable_id: content_facet1.id) assert content_facet_rec, "No associated audit record for content_facet" end + + def test_content_view_environments_not_generated_for_repository_export + generated_cv = FactoryBot.create(:katello_content_view, :generated_for => :repository_export, :organization => view.organization) + generated_cve = FactoryBot.create(:katello_content_view_environment, :content_view => generated_cv, :environment => library) + + exception = assert_raises(ActiveRecord::RecordInvalid) do + content_facet.content_view_environments = [generated_cve] + end + + assert_includes exception.message, "Content view '#{generated_cv.name}' is a generated content view, which cannot be assigned to hosts or activation keys." + end end class ContentFacetErrataTest < ContentFacetBase