Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SCC activations check when no product info is available #1226

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include_context 'version header', 3

describe '#activations' do
let(:system) { FactoryBot.create(:system, :with_activated_product) }
let(:system) { FactoryBot.create(:system, :payg, :with_activated_product) }
let(:headers) { auth_header.merge(version_header) }

context 'without valid repository cache' do
Expand Down Expand Up @@ -57,5 +57,68 @@
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end

context 'system is hybrid' do
let(:system) { FactoryBot.create(:system, :hybrid, :with_activated_product) }
let(:plugin_double) { instance_double('InstanceVerification::Providers::Example') }
let(:cache_name) { "repo/cache/127.0.0.1-#{system.login}-#{system.products.first.id}" }
let(:scc_systems_activations_url) { 'https://scc.suse.com/connect/systems/activations' }
let(:body_active) do
{
id: 1,
regcode: '631dc51f',
name: 'Subscription 1',
type: 'FULL',
status: 'ACTIVE',
starts_at: 'null',
expires_at: '2014-03-14T13:10:21.164Z',
system_limit: 6,
systems_count: 1,
service: {
product: {
id: system.activations.first.product.id
}
}
}
end

before do
allow(InstanceVerification::Providers::Example).to receive(:new).and_return(plugin_double)

allow(plugin_double).to(
receive(:instance_valid?).and_return(true)
)
allow(File).to receive(:join).and_call_original
allow(InstanceVerification).to receive(:update_cache)
allow(ZypperAuth).to receive(:verify_instance).and_call_original
stub_request(:get, scc_systems_activations_url).to_return(status: 200, body: [body_active].to_json, headers: {})
headers['X-Instance-Data'] = 'IMDS'
end

context 'no registry' do
it 'refreshes registry cache key only' do
FileUtils.mkdir_p('repo/cache')
expect(InstanceVerification).to receive(:update_cache).with('127.0.0.1', system.login, system.activations.first.product.id)
get '/connect/systems/activations', headers: headers
FileUtils.rm_rf('repo/cache')
data = JSON.parse(response.body)
expect(SccProxy).not_to receive(:product_path_access)
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end

context 'registry' do
it 'refreshes registry cache key only' do
FileUtils.mkdir_p('repo/cache')
FileUtils.touch(cache_name)
expect(InstanceVerification).to receive(:update_cache).with('127.0.0.1', system.login, nil, registry: true)
get '/connect/systems/activations', headers: headers
FileUtils.rm_rf('repo/cache')
data = JSON.parse(response.body)
expect(SccProxy).not_to receive(:product_path_access)
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end
end
end
end
7 changes: 7 additions & 0 deletions engines/scc_proxy/lib/scc_proxy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,17 @@ def product_class_access(scc_systems_activations, product)
end
end

# rubocop:disable Metrics/PerceivedComplexity
def activations_fail_state(scc_systems_activations, headers, product = nil)
return SccProxy.product_class_access(scc_systems_activations, product) unless product.nil?

active_products_ids = scc_systems_activations.map { |act| act['service']['product']['id'] if act['status'].casecmp('active').zero? }.flatten
x_original_uri = headers.fetch('X-Original-URI', '')
# if there is no product info to compare the activations with
# probably means the query is to refresh credentials
# in any case, verification is true if ALL activations are ACTIVE
return { is_active: (scc_systems_activations.length == active_products_ids.length) } if x_original_uri.empty?

if SccProxy.product_path_access(x_original_uri, active_products_ids)
{ is_active: true }
else
Expand All @@ -265,6 +271,7 @@ def activations_fail_state(scc_systems_activations, headers, product = nil)
end
end
end
# rubocop:enable Metrics/PerceivedComplexity

def scc_check_subscription_expiration(headers, login, system_token, logger, mode, product = nil) # rubocop:disable Metrics/ParameterLists
response = SccProxy.get_scc_activations(headers, system_token, mode)
Expand Down