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

Refactor repository and registry cache #1150

Open
wants to merge 1 commit into
base: refactor-verify-instance
Choose a base branch
from
Open
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
53 changes: 34 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already in an unless registry context, why do we need to test that again?

Also via the activations endpoint from the client side we do not send the product, because it is already known. Thus the question is whether we need the registry argument at all. If we have no product_id then we are on the registry path through the code, are we not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I was not 100% sure we could use the product identifier to differentiate between repositories/registry calls. I think we can

I will update this PR once #1149 is merged, so the changes can be applied to this one

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
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