diff --git a/engines/data_export/lib/data_export/engine.rb b/engines/data_export/lib/data_export/engine.rb index 8a4601dc5..d1c4248dc 100644 --- a/engines/data_export/lib/data_export/engine.rb +++ b/engines/data_export/lib/data_export/engine.rb @@ -2,39 +2,17 @@ module DataExport class Engine < ::Rails::Engine isolate_namespace DataExport config.after_initialize do - # replaces RMT registration for SCC registration - Api::Connect::V3::Subscriptions::SystemsController.class_eval do - after_action :export_rmt_data, only: %i[announce_system], if: -> { DataExport.handler.presence && response.successful? && !@system.byos? } - - def export_rmt_data - # no need to check if system is nil - # as the response is successful - data_export_handler = DataExport.handler.new( - @system, - request, - params, - logger - ) - data_export_handler.export_rmt_data - logger.info "System #{@system.login} info updated by data export handler after announcing the system" - rescue StandardError => e - logger.error('Unexpected data export error has occurred:') - logger.error(e.message) - logger.error("System login: #{@system.login}, IP: #{request.remote_ip}") - logger.error('Backtrace:') - logger.error(e.backtrace) - end - end - Api::Connect::V3::Systems::SystemsController.class_eval do + # if the difference between registered_at and last_seen_at + # is shorter than 90 seconds, it is a registration after_action :export_rmt_data, only: %i[update], if: lambda { DataExport.handler.presence && response.successful? && !@system.byos? && - @system.products.present? + @system.products.present? && (@system.last_seen_at - @system.registered_at) > 90.seconds } def export_rmt_data - @system.products.pluck(:name).each do |prod_name| - params[:dw_product_name] = prod_name + @system.activations.each do |activation| + params[:dw_product_name] = activation.service.product.product_string send_data end rescue StandardError => e diff --git a/engines/data_export/spec/requests/api/connect/v3/subscriptions/systems_controller_spec.rb b/engines/data_export/spec/requests/api/connect/v3/subscriptions/systems_controller_spec.rb deleted file mode 100644 index a73dd41a5..000000000 --- a/engines/data_export/spec/requests/api/connect/v3/subscriptions/systems_controller_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -require 'rails_helper' - -# rubocop:disable RSpec/NestedGroups -describe Api::Connect::V3::Subscriptions::SystemsController, type: :request do - describe '#announce_system' do - let(:instance_data) { '' } - - context 'using SCC generated credentials (BYOS mode)' do - let(:scc_register_system_url) { 'https://scc.suse.com/connect/subscriptions/systems' } - let(:scc_register_response) do - { - id: 5684096, - login: 'SCC_foo', - password: '1234', - last_seen_at: '2021-10-24T09:48:52.658Z' - }.to_json - end - let(:params) do - { - hostname: 'test', - proxy_byos_mode: :payg, - instance_data: instance_data, - hwinfo: - { - hostname: 'test', - cpus: '1', - sockets: '1', - hypervisor: 'Xen', - arch: 'x86_64', - uuid: 'ec235f7d-b435-e27d-86c6-c8fef3180a01', - cloud_provider: 'super_cloud' - } - } - end - - context 'valid credentials' do - let(:plugin_double) { instance_double('DataExport::Handlers::Example') } - - before do - allow(DataExport::Handlers::Example).to receive(:new).and_return(plugin_double) - allow(plugin_double).to receive(:export_rmt_data) - stub_request(:post, scc_register_system_url) - .to_return( - status: 201, - body: scc_register_response.to_s, - headers: {} - ) - end - - it 'saves the data' do - expect(plugin_double).to receive(:export_rmt_data) - post '/connect/subscriptions/systems', params: params, headers: { HTTP_AUTHORIZATION: 'Token token=' } - end - - context 'export fails' do - let(:logger) { instance_double('RMT::Logger').as_null_object } - - before do - allow(DataExport::Handlers::Example).to receive(:new).and_return(plugin_double) - allow(plugin_double).to receive(:export_rmt_data).and_raise('foo') - allow(Rails.logger).to receive(:error) - stub_request(:post, scc_register_system_url) - .to_return( - status: 201, - body: scc_register_response.to_s, - headers: {} - ) - end - - it 'does not save the data and log error' do - expect(plugin_double).to receive(:export_rmt_data) - expect(Rails.logger).to receive(:error) - post '/connect/subscriptions/systems', params: params, headers: { HTTP_AUTHORIZATION: 'Token token=' } - end - end - end - end - end -end -# rubocop:enable RSpec/NestedGroups diff --git a/engines/data_export/spec/requests/api/connect/v3/systems/systems_controller_spec.rb b/engines/data_export/spec/requests/api/connect/v3/systems/systems_controller_spec.rb index 0dec5653d..ca1e940f7 100644 --- a/engines/data_export/spec/requests/api/connect/v3/systems/systems_controller_spec.rb +++ b/engines/data_export/spec/requests/api/connect/v3/systems/systems_controller_spec.rb @@ -27,7 +27,10 @@ subject(:update_action) { put url, params: payload, headers: headers } context 'when update success' do - before { allow(DataExport::Handlers::Example).to receive(:new).and_return(plugin_double) } + before do + system.update(last_seen_at: system.registered_at + 1.day) + allow(DataExport::Handlers::Example).to receive(:new).and_return(plugin_double) + end context 'when data export success' do before { allow(plugin_double).to receive(:export_rmt_data) } diff --git a/engines/zypper_auth/lib/zypper_auth/engine.rb b/engines/zypper_auth/lib/zypper_auth/engine.rb index af28d07d1..da53707d9 100644 --- a/engines/zypper_auth/lib/zypper_auth/engine.rb +++ b/engines/zypper_auth/lib/zypper_auth/engine.rb @@ -93,33 +93,7 @@ def path_allowed?(headers) return true if @system.byos? - instance_verified = InstanceVerification.verify_instance(request, logger, @system) - DataExport.handler.new(@system, request, params, logger).export_rmt_data if DataExport.handler.presence && instance_verified - instance_verified - rescue InstanceVerification::Exception => e - # if we are here that means the instance data in the database is not right - # but the instance data coming from the hypervisor in the client is OK - logger.error("Could not parse the instance data: #{e.message}") - decoded_instance_data = Base64.decode64(request.headers['X-Instance-Data'].to_s) - cache_params = {} - if @system.pubcloud_reg_code.present? - cache_params = { token: Base64.decode64(@system.pubcloud_reg_code.split(',')[0]), instance_data: decoded_instance_data } - end - cache_key = InstanceVerification.build_cache_entry( - request.remote_ip, - @system.login, - cache_params, - @system.proxy_byos_mode, - @system.products.find_by(product_type: 'base') - ) - logger.info("Removing entry #{cache_key} from the cache, if present") - InstanceVerification.remove_entry_from_cache(cache_key, @system.proxy_byos_mode) - instance_verified - rescue StandardError => e - logger.error("Unexpected data export error has occurred: #{e.message}") - logger.error('Data not exported') - logger.error("System login: #{@system.login}, IP: #{request.remote_ip}") - instance_verified + InstanceVerification.verify_instance(request, logger, @system) end end end diff --git a/engines/zypper_auth/spec/requests/strict_authentication/authentication_controller_spec.rb b/engines/zypper_auth/spec/requests/strict_authentication/authentication_controller_spec.rb index 08aa59e07..a6e48bb6d 100644 --- a/engines/zypper_auth/spec/requests/strict_authentication/authentication_controller_spec.rb +++ b/engines/zypper_auth/spec/requests/strict_authentication/authentication_controller_spec.rb @@ -89,71 +89,6 @@ end end - context 'system with instance_data headers instance data is invalid in the DB' do - context 'InstanceVerification error' do - let(:headers) { auth_header.merge({ 'X-Original-URI': requested_uri, 'X-Instance-Data': Base64.strict_encode64('test') }) } - let(:data_export_double) { instance_double('DataExport::Handlers::Example') } - let(:wrong_iid) { 'plugin:susecloud\n' } - - - before do - allow(InstanceVerification).to receive(:reg_code_in_cache?).and_return(nil) - Rails.cache.clear - expect_any_instance_of(InstanceVerification::Providers::Example).to receive(:instance_valid?).and_return(true) - allow(DataExport::Handlers::Example).to receive(:new).and_return(data_export_double) - allow(File).to receive(:directory?) - allow(Dir).to receive(:mkdir) - allow(FileUtils).to receive(:touch) - expect(data_export_double).to receive(:export_rmt_data).and_raise(InstanceVerification::Exception, "Malformed instance data #{wrong_iid}") - allow(Rails.logger).to receive(:error) - allow(Rails.logger).to receive(:info) - expect(Rails.logger).to receive(:error).with( - 'Could not parse the instance data: Malformed instance data plugin:susecloud\n' - ) - expect(Rails.logger).to receive(:info) - get '/api/auth/check', headers: headers - end - - it do - is_expected.to have_http_status(200) - end - end - - context 'StandardError' do - let(:system_byos) { FactoryBot.create(:system, :byos_reg_code, :with_activated_product) } - let(:headers) { auth_header.merge({ 'X-Original-URI': requested_uri, 'X-Instance-Data': Base64.strict_encode64('test') }) } - let(:data_export_double) { instance_double('DataExport::Handlers::Example') } - let(:wrong_iid) { 'plugin:susecloud\n' } - - - before do - allow(InstanceVerification).to receive(:reg_code_in_cache?).and_return(nil) - Rails.cache.clear - expect_any_instance_of(InstanceVerification::Providers::Example).to receive(:instance_valid?).and_return(true) - allow(DataExport::Handlers::Example).to receive(:new).and_return(data_export_double) - allow(File).to receive(:directory?) - allow(Dir).to receive(:mkdir) - allow(FileUtils).to receive(:touch) - expect(data_export_double).to receive(:export_rmt_data).and_raise( - StandardError, 'API BORKED' - ) - allow(Rails.logger).to receive(:error) - expect(Rails.logger).to receive(:error).with( - 'Unexpected data export error has occurred: API BORKED' - ) - expect(Rails.logger).to receive(:error).with('Data not exported') - expect(Rails.logger).to receive(:error).with( - "System login: #{system.login}, IP: 127.0.0.1" - ) - get '/api/auth/check', headers: headers - end - - it do - is_expected.to have_http_status(200) - end - end - end - context 'when system is BYOS proxy' do let(:local_path) { system_byos.activations.first.product.repositories.first.local_path } let(:paid_local_path) do @@ -399,7 +334,6 @@ allow(File).to receive(:directory?) allow(Dir).to receive(:mkdir) allow(FileUtils).to receive(:touch) - expect(data_export_double).to receive(:export_rmt_data) get '/api/auth/check', headers: headers end @@ -645,7 +579,6 @@ before do allow(InstanceVerification).to receive(:verify_instance).and_return(true) allow(DataExport::Handlers::Example).to receive(:new).and_return(data_export_double) - expect(data_export_double).to receive(:export_rmt_data) get '/api/auth/check', headers: headers end