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

Add loadbalancer support #46

Open
wants to merge 1 commit into
base: main
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
3 changes: 2 additions & 1 deletion lib/smart_proxy_container_gateway/container_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Plugin < ::Proxy::Plugin
container_instance.singleton_dependency :container_gateway_main_impl, (lambda do
Proxy::ContainerGateway::ContainerGatewayMain.new(
database: container_instance.get_dependency(:database_impl),
**settings.slice(:pulp_endpoint, :pulp_client_ssl_ca, :pulp_client_ssl_cert, :pulp_client_ssl_key)
**settings.slice(:pulp_endpoint, :pulp_client_ssl_ca, :pulp_client_ssl_cert,
:pulp_client_ssl_key, :client_endpoint)
)
end)
end
Expand Down
10 changes: 6 additions & 4 deletions lib/smart_proxy_container_gateway/container_gateway_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Api < ::Sinatra::Base
status pulp_response.code.to_i
body pulp_response.body
else
redirection_location = pulp_response['location']
redirect to(redirection_location)
redirection_uri = URI(pulp_response['location'])
redirection_uri.host = URI(container_gateway_main.client_endpoint).host
redirect(redirection_uri.to_s)
end
end

Expand Down Expand Up @@ -236,8 +237,9 @@ def head_or_get_blobs
status pulp_response.code.to_i
body pulp_response.body
else
redirection_location = pulp_response['location']
redirect to(redirection_location)
redirection_uri = URI(pulp_response['location'])
redirection_uri.host = URI(container_gateway_main.client_endpoint).host
redirect(redirection_uri.to_s)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/smart_proxy_container_gateway/container_gateway_main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ module ContainerGateway
extend ::Proxy::Log

class ContainerGatewayMain
attr_reader :database
attr_reader :database, :client_endpoint

def initialize(database:, pulp_endpoint:, pulp_client_ssl_ca:, pulp_client_ssl_cert:, pulp_client_ssl_key:)
# rubocop:disable Metrics/ParameterLists, Layout/LineLength
def initialize(database:, pulp_endpoint:, pulp_client_ssl_ca:, pulp_client_ssl_cert:, pulp_client_ssl_key:, client_endpoint: nil)
@database = database
@pulp_endpoint = pulp_endpoint
@client_endpoint = client_endpoint || pulp_endpoint
@pulp_client_ssl_ca = pulp_client_ssl_ca
@pulp_client_ssl_cert = OpenSSL::X509::Certificate.new(File.read(pulp_client_ssl_cert))
@pulp_client_ssl_key = OpenSSL::PKey::RSA.new(
File.read(pulp_client_ssl_key)
)
end
# rubocop:enable Metrics/ParameterLists, Layout/LineLength

def pulp_registry_request(uri, headers)
http_client = Net::HTTP.new(uri.host, uri.port)
Expand Down
34 changes: 34 additions & 0 deletions test/container_gateway_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ def test_redirects_manifest_request
assert_equal('', last_response.body)
end

def test_redirects_manifest_request_with_client_endpoint
::Proxy::ContainerGateway::Api.any_instance.expects(:handle_repo_auth).returns({})
@container_gateway_main.stubs(:client_endpoint).returns("https://loadbalancer.example.com")

redirect_headers = {
'location' => "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
"/pulp/container/library/test_repo/manifests/test_tag?validate_token=test_token"
}
stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
"/pulpcore_registry/v2/library/test_repo/manifests/test_tag").
to_return(:status => 302, :body => '', :headers => redirect_headers)

get '/v2/library/test_repo/manifests/test_tag'
assert last_response.redirect?, "Last response was not a redirect: #{last_response.body}"
assert_equal('', last_response.body)
assert last_response.headers['Location'].include?("https://loadbalancer.example.com")
end

def test_redirects_blob_request
::Proxy::ContainerGateway::Api.any_instance.expects(:handle_repo_auth).returns({})
redirect_headers = { 'location' => "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
Expand All @@ -133,6 +151,22 @@ def test_redirects_blob_request
assert_equal('', last_response.body)
end

def test_redirects_blob_request_with_client_endpoint
::Proxy::ContainerGateway::Api.any_instance.expects(:handle_repo_auth).returns({})
@container_gateway_main.stubs(:client_endpoint).returns("https://loadbalancer.example.com")

redirect_headers = { 'location' => "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
"/pulp/container/library/test_repo/blobs/test_digest?validate_token=test_token" }
stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
"/pulpcore_registry/v2/library/test_repo/blobs/test_digest").
to_return(:status => 302, :body => '', :headers => redirect_headers)

get '/v2/library/test_repo/blobs/test_digest'
assert last_response.redirect?, "Last response was not a redirect: #{last_response.body}"
assert_equal('', last_response.body)
assert last_response.headers['Location'].include?("https://loadbalancer.example.com")
end

def test_redirects_blob_head_request
::Proxy::ContainerGateway::Api.any_instance.expects(:handle_repo_auth).returns({})
redirect_headers = { 'location' => "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
Expand Down