Bug 2153273

Summary: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations
Product: Red Hat Satellite Reporter: Jan Jansky <jjansky>
Component: Subscription ManagementAssignee: Jeremy Lenz <jlenz>
Status: CLOSED ERRATA QA Contact: Cole Higgins <chiggins>
Severity: high Docs Contact:
Priority: unspecified    
Version: 6.12.0CC: ahumbe, chiggins, hyu, jbhatia, jpasqual, pcreech, saydas, wpinheir, zhunting
Target Milestone: 6.13.0Keywords: Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: tfm-rubygem-katello-4.7.0.16-1 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 2174998 (view as bug list) Environment:
Last Closed: 2023-05-03 13:23:38 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Jan Jansky 2022-12-14 11:49:43 UTC
Description of problem:
During manifest refresh or foreman-rake katello:reimport error as below appear, full traceback in additional info

Importing Katello::Subscription
Importing Katello::Pool
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations


Version-Release number of selected component (if applicable):
satellite-6.12.0-4.el8sat.noarch

How reproducible:
Only with customer data

Steps to Reproduce:
1. foreman-rake katello:reimport

Actual results:
Error appear

Expected results:
reimport successful

Additional info:

Importing Katello::Subscription
Importing Katello::Pool
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:80:in `raise_validation_error'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:53:in `save!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `block in save!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:212:in `transaction'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `save!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/suppressor.rb:48:in `save!'
/usr/share/gems/gems/katello-4.5.0.20/app/lib/katello/lazy_accessor.rb:67:in `save!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/persistence.rb:55:in `create!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `block in create!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `block in scoping'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:787:in `_scoping'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `scoping'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `create!'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:124:in `first_or_create!'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:31:in `block in import_candlepin_record'
/usr/share/gems/gems/katello-4.5.0.20/app/lib/katello/util/support.rb:97:in `active_record_retry'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:30:in `import_candlepin_record'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/pool.rb:66:in `import_candlepin_record'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:17:in `block in import_candlepin_records'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `each'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `import_candlepin_records'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:53:in `block in import_all'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/usr/share/gems/gems/katello-4.5.0.20/app/models/katello/glue/candlepin/candlepin_object.rb:51:in `import_all'
/usr/share/gems/gems/katello-4.5.0.20/lib/katello/tasks/reimport.rake:27:in `block (3 levels) in <top (required)>'
/usr/share/gems/gems/katello-4.5.0.20/lib/katello/tasks/reimport.rake:25:in `each'
/usr/share/gems/gems/katello-4.5.0.20/lib/katello/tasks/reimport.rake:25:in `block (2 levels) in <top (required)>'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `block in execute'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `each'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `execute'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:199:in `synchronize'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:83:in `block in run'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
/usr/share/gems/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/usr/bin/rake:23:in `load'
/usr/bin/rake:23:in `<main>'
Tasks: TOP => katello:reimport

Same error found in upstream community: https://community.theforeman.org/t/activerecord-validation-failed-subscription-cant-be-blank-a-pool-and-its-subscription-cannot-belong-to-different-organizations/30701

Comment 3 Hao Chang Yu 2022-12-15 06:29:40 UTC
This issue can happen when one or more custom products have been deleted from the Katello but not yet deleted from Candlepin. When user performs an import/refresh manifest, Katello tries to import the deleted custom products back to Katello. The bug is, Katello thought that all the importing products are Red Hat products so it assigns "Red Hat" providers to all of them.

# app/models/katello/glue/provider.rb
"engineering_product_id?" method will match all CP product id with digits only(regex \d+) which includes custom products because they are 11 digit numbers, such as '856945816336'
-----------------------------------
      def import_products_from_cp
        cp_products = ::Katello::Resources::Candlepin::Product.all(organization.label, [:id, :name, :multiplier, :productContent])
        cp_products = cp_products.select { |prod| Glue::Candlepin::Product.engineering_product_id?(prod['id']) }  <======================

        prod_content_importer = Katello::ProductContentImporter.new

        Katello::Logging.time("Imported #{cp_products.size} products") do
          cp_products.each do |product_json|
            product = import_product(product_json)
            prod_content_importer.add_product_content(product, product_json['productContent']) if product.redhat?
          end
        end
-----------------------------------

# app/models/katello/glue/candlepin/product.rb
----------------------------------
    def self.import_from_cp(attrs, organization)  <==== call by import_product()
      import_logger = attrs[:import_logger]

      product_attrs = {'name' => attrs['name'],
                       'cp_id' => attrs['id'],
                       'label' => Util::Model.labelize(attrs['name']),
                       'multiplier' => attrs['multiplier'],
                       'organization_id' => organization.id,
                       'provider_id' => organization.redhat_provider.id}  <================== assigns "Red Hat" provider to the importing product 

      Product.create!(product_attrs)
----------------------------------



Steps to reproduce:
1) Create a custom product on Satellite.

2) Run the following command to delete the custom product from Katello database only
-----------------------------------
foreman-rake console
product = Katello::Product.find_by_name("<PRODUCT NAME>")
product.provider    <====================== This should be an "Anonymous" provider
product.destroy
exit
-----------------------------------

3) Trigger manifest refresh on Satellite and wait for it to finish successfully

4) Now we will see that the deleted product got imported from Candlepin with wrong provider.
For example:
-----------------------
irb(main):021:0> product = Katello::Product.find_by_name("test_custom_product")
=> #<Katello::Product id: 314, name: "test_custom_product", description: nil, cp_id: "450776116221", multiplier: nil, provider_id: 2, created_at: "2022-12-15 02:46:41", updated_at: "2022-12-15 02:46:41", gp...
irb(main):022:0> product.provider
=> #<Katello::Provider id: 2, name: "Red Hat", description: nil, provider_type: "Red Hat", organization_id: 1, created_at: "2022-02-21 09:41:28", updated_at: "2022-02-21 09:41:28", task_status_id: nil>   <============ Provider becomes "Red Hat"!!!
------------------------

5) Run reimport script.

# foreman-rake katello:reimport
------------------------
API controllers newer than Apipie cache! Run apipie:cache rake task to regenerate cache.
Importing Katello::Subscription
Importing Katello::Pool
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:80:in `raise_validation_error'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:53:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `block in save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:212:in `transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/suppressor.rb:48:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/lib/katello/lazy_accessor.rb:67:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/persistence.rb:55:in `create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `block in create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `block in scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:787:in `_scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:124:in `first_or_create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:31:in `block in import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/lib/katello/util/support.rb:97:in `active_record_retry'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:30:in `import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/pool.rb:66:in `import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:17:in `block in import_candlepin_records'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `import_candlepin_records'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:53:in `block in import_all'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/app/models/katello/glue/candlepin/candlepin_object.rb:51:in `import_all'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/lib/katello/tasks/reimport.rake:27:in `block (3 levels) in <top (required)>'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/lib/katello/tasks/reimport.rake:25:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.3.0.50/lib/katello/tasks/reimport.rake:25:in `block (2 levels) in <top (required)>'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
------------------------

Comment 5 Jeremy Lenz 2023-01-31 22:36:33 UTC
Created redmine issue https://projects.theforeman.org/issues/36025 from this bug

Comment 6 Bryan Kearney 2023-02-01 00:03:14 UTC
Upstream bug assigned to jlenz

Comment 7 Bryan Kearney 2023-02-01 00:03:17 UTC
Upstream bug assigned to jlenz

Comment 15 errata-xmlrpc 2023-05-03 13:23:38 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Important: Satellite 6.13 Release), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2023:2097