Skip to content

Commit

Permalink
Refactor repository and registry cache
Browse files Browse the repository at this point in the history
- Remove registry cache in the same engine that it is created
- Get repository and registry cache paths in one place to avoid duplication
  • Loading branch information
jesusbv committed May 10, 2024
1 parent a62e989 commit f0825de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
54 changes: 35 additions & 19 deletions engines/instance_verification/lib/instance_verification/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,46 @@ def self.update_cache(remote_ip, system_login, product_id, is_byos: false, regis
# TODO: BYOS scenario
# to be addressed on a different PR
unless registry
InstanceVerification.write_cache_file(
Rails.application.config.repo_cache_dir,
InstanceVerification.build_cache_key(remote_ip, system_login, base_product_id: product_id)
)
# write repository cache
repository_cache_path = InstanceVerification.repository_cache_path(remote_ip, system_login, product_id)
InstanceVerification.write_cache_file(repository_cache_path) unless registry
end

InstanceVerification.write_cache_file(
# write registry cache
registry_cache_path = InstanceVerification.registry_cache_path(remote_ip, system_login)
InstanceVerification.write_cache_file(registry_cache_path)
end

def self.write_cache_file(cache_path)
Dir.mkdir(File.dirname(cache_path)) unless File.directory?(File.dirname(cache_path))

FileUtils.touch(cache_path)
end

def self.repository_cache_path(remote_ip, system_login, product_id)
File.join(
Rails.application.config.repo_cache_dir,
InstanceVerification.build_cache_key(remote_ip, system_login, product_id: product_id)
)
end

def self.registry_cache_path(remote_ip, system_login)
File.join(
Rails.application.config.registry_cache_dir,
InstanceVerification.build_cache_key(remote_ip, system_login)
)
end

def self.write_cache_file(cache_dir, cache_key)
FileUtils.mkdir_p(cache_dir)
FileUtils.touch(File.join(cache_dir, cache_key))
def self.remove_registry_cache(remote_ip, system_login)
registry_cache_path = InstanceVerification.registry_cache_path(remote_ip, system_login)
File.unlink(registry_cache_path) if File.exist?(registry_cache_path)
end

def self.build_cache_key(remote_ip, system_login, product_id: nil)
cache_key = [remote_ip, system_login]
cache_key.append(product_id) unless product_id.nil?

cache_key.join('-')
end

def self.verify_instance(request, logger, system)
Expand All @@ -30,10 +55,7 @@ def self.verify_instance(request, logger, system)
return false unless base_product

# check the cache for the system (20 min)
cache_path = File.join(
Rails.application.config.repo_cache_dir,
InstanceVerification.build_cache_key(request.remote_ip, system.login, base_product_id: base_product.id)
)
cache_path = InstanceVerification.repository_cache_path(request.remote_ip, system.login, base_product.id)
if File.exist?(cache_path)
# only update registry cache key
InstanceVerification.update_cache(request.remote_ip, system.login, nil, is_byos: system.proxy_byos, registry: true)
Expand Down Expand Up @@ -82,13 +104,6 @@ def self.verify_instance(request, logger, system)
false
end

def self.build_cache_key(remote_ip, login, base_product_id: nil)
cache_key = [remote_ip, login]
cache_key.append(base_product_id) unless base_product_id.nil?

cache_key.join('-')
end

class Engine < ::Rails::Engine
isolate_namespace InstanceVerification
config.generators.api_only = true
Expand Down Expand Up @@ -178,6 +193,7 @@ def verify_base_product_activation(product)
)

raise 'Unspecified error' unless verification_provider.instance_valid?
# puts "AAAA"
InstanceVerification.update_cache(request.remote_ip, @system.login, product.id, is_byos: @system.proxy_byos)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
allow(File).to receive(:directory?)
allow(Dir).to receive(:mkdir)
allow(FileUtils).to receive(:touch)
expect(InstanceVerification).to receive(:write_cache_file).once.with('repo/cache', "127.0.0.1-#{system.login}-#{product_sap.id}")
expect(InstanceVerification).to receive(:write_cache_file).once.with('registry/cache', "127.0.0.1-#{system.login}")
expect(InstanceVerification).to receive(:write_cache_file).once.with("repo/cache/127.0.0.1-#{system.login}-#{product_sap.id}")
expect(InstanceVerification).to receive(:write_cache_file).once.with("registry/cache/127.0.0.1-#{system.login}")
post url, params: payload_sap, headers: headers
end

Expand Down
10 changes: 1 addition & 9 deletions engines/registry/lib/registry/engine.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
module Registry
class << self
def remove_auth_cache(registry_cache_key)
cache_path = File.join(Rails.application.config.registry_cache_dir, registry_cache_key)
File.unlink(registry_cache_path) if File.exist?(cache_path)
end
end

class Engine < ::Rails::Engine
isolate_namespace Registry
config.generators.api_only = true
Expand All @@ -25,8 +18,7 @@ def handle_auth_cache
before_action :remove_auth_cache, only: %w[deregister]

def remove_auth_cache
registry_cache_key = [request.remote_ip, @system.login].join('-')
Registry.remove_auth_cache(registry_cache_key)
InstanceVerification.remove_registry_cache(request.remote_ip, @system.login)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@

before do
allow(File).to receive(:join).and_call_original
# allow(File).to receive(:exist?).with('repo/cache/127.0.0.1-login45-1052122')
# allow(File).to receive(:exist?).with("repo/cache/127.0.0.1-#{system.login}-#{system.products.first.id}").and_return(true)
# allow(File).to receive(:exist?).with("repo/cache/127.0.0.1-#{system.login}-#{system.products.first.id}").and_return(true)
# allow(File).to receive(:exist?)
allow(File).to receive(:directory?)
allow(Dir).to receive(:mkdir)
allow(FileUtils).to receive(:touch)
allow(InstanceVerification).to receive(:repository_cache_path).and_return('.')
allow(InstanceVerification).to receive(:registry_cache_path)
allow(InstanceVerification).to receive(:write_cache_file)
allow(InstanceVerification).to receive(:update_cache)
allow(InstanceVerification).to receive(:verify_instance).and_call_original
headers['X-Instance-Data'] = 'IMDS'
end

it 'refreshes registry cache key only' do
FileUtils.mkdir_p('repo/cache')
FileUtils.touch(cache_name)
allow(File).to receive(:directory?)
allow(Dir).to receive(:mkdir)
expect(InstanceVerification).to receive(:update_cache).with('127.0.0.1', system.login, nil, is_byos: system.proxy_byos, registry: true)
get '/connect/systems/activations', headers: headers
FileUtils.rm_rf('repo/cache')
data = JSON.parse(response.body)
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
allow(FileUtils).to receive(:mkdir_p)
allow(FileUtils).to receive(:touch)

allow(InstanceVerification).to receive(:write_cache_file).twice.with('repo/cache', "127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with('registry/cache', "127.0.0.1-#{system.login}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("repo/cache/127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("registry/cache/127.0.0.1-#{system.login}")
end

it 'renders service JSON' do
Expand All @@ -132,8 +132,8 @@
body: '{"error": "No product found on SCC for: foo bar x86_64 json api"}',
headers: {}
)
allow(InstanceVerification).to receive(:write_cache_file).twice.with('repo/cache', "127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with('registry/cache', "127.0.0.1-#{system.login}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("repo/cache/127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("registry/cache/127.0.0.1-#{system.login}")
allow(FileUtils).to receive(:mkdir_p)
allow(FileUtils).to receive(:touch)

Expand Down Expand Up @@ -165,8 +165,8 @@
body: '{"id": "bar"}',
headers: {}
)
allow(InstanceVerification).to receive(:write_cache_file).twice.with('repo/cache', "127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with('registry/cache', "127.0.0.1-#{system.login}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("repo/cache/127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("registry/cache/127.0.0.1-#{system.login}")
allow(File).to receive(:directory?)
allow(FileUtils).to receive(:mkdir_p)
allow(FileUtils).to receive(:touch)
Expand Down Expand Up @@ -201,8 +201,8 @@
headers: {}
)

allow(InstanceVerification).to receive(:write_cache_file).twice.with('repo/cache', "127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with('registry/cache', "127.0.0.1-#{system.login}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("repo/cache/127.0.0.1-#{system.login}-#{product.id}")
allow(InstanceVerification).to receive(:write_cache_file).twice.with("registry/cache/127.0.0.1-#{system.login}")
allow(File).to receive(:directory?)
allow(FileUtils).to receive(:mkdir_p)
allow(FileUtils).to receive(:touch)
Expand Down

0 comments on commit f0825de

Please sign in to comment.