Skip to content

Commit a62e989

Browse files
committed
Move ZypperAuth.verify_instance to InstanceVerification engine
1 parent 045d6e7 commit a62e989

File tree

4 files changed

+74
-67
lines changed

4 files changed

+74
-67
lines changed

engines/instance_verification/lib/instance_verification/engine.rb

+69-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ def self.update_cache(remote_ip, system_login, product_id, is_byos: false, regis
77
unless registry
88
InstanceVerification.write_cache_file(
99
Rails.application.config.repo_cache_dir,
10-
[remote_ip, system_login, product_id].join('-')
10+
InstanceVerification.build_cache_key(remote_ip, system_login, base_product_id: product_id)
1111
)
1212
end
1313

1414
InstanceVerification.write_cache_file(
1515
Rails.application.config.registry_cache_dir,
16-
[remote_ip, system_login].join('-')
16+
InstanceVerification.build_cache_key(remote_ip, system_login)
1717
)
1818
end
1919

@@ -22,6 +22,73 @@ def self.write_cache_file(cache_dir, cache_key)
2222
FileUtils.touch(File.join(cache_dir, cache_key))
2323
end
2424

25+
def self.verify_instance(request, logger, system)
26+
return false unless request.headers['X-Instance-Data']
27+
28+
instance_data = Base64.decode64(request.headers['X-Instance-Data'].to_s)
29+
base_product = system.products.find_by(product_type: 'base')
30+
return false unless base_product
31+
32+
# check the cache for the system (20 min)
33+
cache_path = File.join(
34+
Rails.application.config.repo_cache_dir,
35+
InstanceVerification.build_cache_key(request.remote_ip, system.login, base_product_id: base_product.id)
36+
)
37+
if File.exist?(cache_path)
38+
# only update registry cache key
39+
InstanceVerification.update_cache(request.remote_ip, system.login, nil, is_byos: system.proxy_byos, registry: true)
40+
return true
41+
end
42+
43+
verification_provider = InstanceVerification.provider.new(
44+
logger,
45+
request,
46+
base_product.attributes.symbolize_keys.slice(:identifier, :version, :arch, :release_type),
47+
instance_data
48+
)
49+
50+
is_valid = verification_provider.instance_valid?
51+
# update repository and registry cache
52+
InstanceVerification.update_cache(request.remote_ip, system.login, base_product.id, is_byos: system.proxy_byos)
53+
is_valid
54+
rescue InstanceVerification::Exception => e
55+
message = ''
56+
if system.proxy_byos
57+
result = SccProxy.scc_check_subscription_expiration(request.headers, system.login, system.system_token, logger)
58+
if result[:is_active]
59+
InstanceVerification.update_cache(request.remote_ip, system.login, base_product.id, is_byos: system.proxy_byos)
60+
return true
61+
end
62+
63+
message = result[:message]
64+
else
65+
message = e.message
66+
end
67+
details = [ "System login: #{system.login}", "IP: #{request.remote_ip}" ]
68+
details << "Instance ID: #{verification_provider.instance_id}" if verification_provider.instance_id
69+
details << "Billing info: #{verification_provider.instance_billing_info}" if verification_provider.instance_billing_info
70+
71+
ZypperAuth.auth_logger.info <<~LOGMSG
72+
Access to the repos denied: #{message}
73+
#{details.join(', ')}
74+
LOGMSG
75+
false
76+
rescue StandardError => e
77+
logger.error('Unexpected instance verification error has occurred:')
78+
logger.error(e.message)
79+
logger.error("System login: #{system.login}, IP: #{request.remote_ip}")
80+
logger.error('Backtrace:')
81+
logger.error(e.backtrace)
82+
false
83+
end
84+
85+
def self.build_cache_key(remote_ip, login, base_product_id: nil)
86+
cache_key = [remote_ip, login]
87+
cache_key.append(base_product_id) unless base_product_id.nil?
88+
89+
cache_key.join('-')
90+
end
91+
2592
class Engine < ::Rails::Engine
2693
isolate_namespace InstanceVerification
2794
config.generators.api_only = true

engines/registry/lib/registry/engine.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Engine < ::Rails::Engine
1515
before_action :handle_auth_cache, only: %w[index]
1616

1717
def handle_auth_cache
18-
unless ZypperAuth.verify_instance(request, logger, @system)
18+
unless InstanceVerification.verify_instance(request, logger, @system)
1919
render(xml: { error: 'Instance verification failed' }, status: :forbidden)
2020
end
2121
end

engines/registry/spec/requests/api/connect/v3/systems/activations_controller_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
context 'without valid repository cache' do
1010
before do
1111
headers['X-Instance-Data'] = 'IMDS'
12-
allow(ZypperAuth).to receive(:verify_instance).and_return(true)
12+
allow(InstanceVerification).to receive(:verify_instance).and_return(true)
1313
end
1414

1515
context 'without X-Instance-Data headers or hw_info' do
@@ -31,7 +31,7 @@
3131
# allow(File).to receive(:exist?).with("repo/cache/127.0.0.1-#{system.login}-#{system.products.first.id}").and_return(true)
3232
# allow(File).to receive(:exist?)
3333
allow(InstanceVerification).to receive(:update_cache)
34-
allow(ZypperAuth).to receive(:verify_instance).and_call_original
34+
allow(InstanceVerification).to receive(:verify_instance).and_call_original
3535
headers['X-Instance-Data'] = 'IMDS'
3636
end
3737

engines/zypper_auth/lib/zypper_auth/engine.rb

+2-62
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,6 @@ def auth_logger
55
Thread.current[:logger].reopen
66
Thread.current[:logger]
77
end
8-
9-
def verify_instance(request, logger, system)
10-
return false unless request.headers['X-Instance-Data']
11-
12-
instance_data = Base64.decode64(request.headers['X-Instance-Data'].to_s)
13-
14-
base_product = system.products.find_by(product_type: 'base')
15-
return false unless base_product
16-
17-
# check the cache for the system (20 min)
18-
cache_key = [request.remote_ip, system.login, base_product.id].join('-')
19-
cache_path = File.join(Rails.application.config.repo_cache_dir, cache_key)
20-
if File.exist?(cache_path)
21-
# only update registry cache key
22-
InstanceVerification.update_cache(request.remote_ip, system.login, nil, is_byos: system.proxy_byos, registry: true)
23-
return true
24-
end
25-
26-
verification_provider = InstanceVerification.provider.new(
27-
logger,
28-
request,
29-
base_product.attributes.symbolize_keys.slice(:identifier, :version, :arch, :release_type),
30-
instance_data
31-
)
32-
33-
is_valid = verification_provider.instance_valid?
34-
# update repository and registry cache
35-
InstanceVerification.update_cache(request.remote_ip, system.login, base_product.id, is_byos: system.proxy_byos)
36-
is_valid
37-
rescue InstanceVerification::Exception => e
38-
message = ''
39-
if system.proxy_byos
40-
result = SccProxy.scc_check_subscription_expiration(request.headers, system.login, system.system_token, logger)
41-
if result[:is_active]
42-
InstanceVerification.update_cache(request.remote_ip, system.login, base_product.id, is_byos: system.proxy_byos)
43-
return true
44-
end
45-
46-
message = result[:message]
47-
else
48-
message = e.message
49-
end
50-
details = [ "System login: #{system.login}", "IP: #{request.remote_ip}" ]
51-
details << "Instance ID: #{verification_provider.instance_id}" if verification_provider.instance_id
52-
details << "Billing info: #{verification_provider.instance_billing_info}" if verification_provider.instance_billing_info
53-
54-
ZypperAuth.auth_logger.info <<~LOGMSG
55-
Access to the repos denied: #{message}
56-
#{details.join(', ')}
57-
LOGMSG
58-
59-
false
60-
rescue StandardError => e
61-
logger.error('Unexpected instance verification error has occurred:')
62-
logger.error(e.message)
63-
logger.error("System login: #{system.login}, IP: #{request.remote_ip}")
64-
logger.error('Backtrace:')
65-
logger.error(e.backtrace)
66-
false
67-
end
688
end
699

7010
class Engine < ::Rails::Engine
@@ -126,7 +66,7 @@ def make_repo_url(base_url, repo_local_path, service_name = nil)
12666
# additional validation for zypper service XML controller
12767
before_action :verify_instance
12868
def verify_instance
129-
unless ZypperAuth.verify_instance(request, logger, @system)
69+
unless InstanceVerification.verify_instance(request, logger, @system)
13070
render(xml: { error: 'Instance verification failed' }, status: 403)
13171
end
13272
end
@@ -138,7 +78,7 @@ def verify_instance
13878
# additional validation for strict_authentication auth subrequest
13979
def path_allowed?(path)
14080
return false unless original_path_allowed?(path)
141-
ZypperAuth.verify_instance(request, logger, @system)
81+
InstanceVerification.verify_instance(request, logger, @system)
14282
end
14383
end
14484
end

0 commit comments

Comments
 (0)