@@ -7,13 +7,13 @@ def self.update_cache(remote_ip, system_login, product_id, is_byos: false, regis
7
7
unless registry
8
8
InstanceVerification . write_cache_file (
9
9
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 )
11
11
)
12
12
end
13
13
14
14
InstanceVerification . write_cache_file (
15
15
Rails . application . config . registry_cache_dir ,
16
- [ remote_ip , system_login ] . join ( '-' )
16
+ InstanceVerification . build_cache_key ( remote_ip , system_login )
17
17
)
18
18
end
19
19
@@ -22,6 +22,73 @@ def self.write_cache_file(cache_dir, cache_key)
22
22
FileUtils . touch ( File . join ( cache_dir , cache_key ) )
23
23
end
24
24
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
+
25
92
class Engine < ::Rails ::Engine
26
93
isolate_namespace InstanceVerification
27
94
config . generators . api_only = true
0 commit comments