Skip to content
Merged
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
22 changes: 22 additions & 0 deletions lib/smart_proxy_container_gateway/container_gateway_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ class Api < ::Sinatra::Base
inject_attr :database_impl, :database
inject_attr :container_gateway_main_impl, :container_gateway_main

get '/index/static/?' do
# TODO: filter out repositories that are not tied to the (optional) authenticated host
Copy link
Member

Choose a reason for hiding this comment

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

What's the plan with this TODO?

Copy link
Member Author

Choose a reason for hiding this comment

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

After this merges we're going to implement filtering the index by host. Originally I was thinking of doing that at the same time but it depends on another PR, so I'm just saving my code for the next developer who comes by.

# host = <lookup host>
# catalog = host_catalog(host)

# pulp_response = container_gateway_main.flatpak_static_index(translated_headers_for_proxy, params)
# if pulp_response.code.to_i >= 400
# status pulp_response.code.to_i
# body pulp_response.body
# end

# pulp_index = JSON.parse(pulp_response.body)
# pulp_index["Results"].select! { |result| catalog.include?(result["Name"]) }

# status 200
# body pulp_index.to_json

pulp_response = container_gateway_main.flatpak_static_index(translated_headers_for_proxy, params)
status pulp_response.code.to_i
body pulp_response.body
end

get '/v1/_ping/?' do
pulp_response = container_gateway_main.ping(translated_headers_for_proxy)
status pulp_response.code.to_i
Expand Down
9 changes: 9 additions & 0 deletions lib/smart_proxy_container_gateway/container_gateway_main.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'net/http'
require 'uri'
require 'digest'
require 'erb'
require 'smart_proxy_container_gateway/dependency_injection'
require 'sequel'
module Proxy
Expand Down Expand Up @@ -40,6 +41,14 @@ def pulp_registry_request(uri, headers)
end
end

def flatpak_static_index(headers, params = {})
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/index/static")
unless params.empty?
uri.query = params.map { |k, v| "#{ERB::Util.url_encode(k.to_s)}=#{ERB::Util.url_encode(v.to_s)}" }.join('&')
end
pulp_registry_request(uri, headers)
end

def ping(headers)
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/v2/")
pulp_registry_request(uri, headers)
Expand Down
52 changes: 52 additions & 0 deletions test/container_gateway_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,56 @@ def test_handle_client_cert_auth_no_cert
assert_equal 404, last_response.status
assert_includes last_response.body, 'Repository name unknown'
end

def test_flatpak_static_index_success
static_index_response = {
"Results" => [
{ "Name" => "org.test.app", "Images" => [] },
{ "Name" => "org.another.app", "Images" => [] }
]
}

stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/index/static").
to_return(:status => 200, :body => static_index_response.to_json)

get '/index/static'
assert last_response.ok?, "Last response was not ok: #{last_response.body}"
assert_equal static_index_response.to_json, last_response.body
assert_equal 200, last_response.status
end

def test_flatpak_static_index_with_params
static_index_response = {
"Results" => [
{ "Name" => "org.test.app", "Images" => [] }
]
}

stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}" \
"/pulpcore_registry/index/static?branch=stable&arch=x86_64").
to_return(:status => 200, :body => static_index_response.to_json)

get '/index/static?branch=stable&arch=x86_64'
assert last_response.ok?, "Last response was not ok: #{last_response.body}"
assert_equal static_index_response.to_json, last_response.body
assert_equal 200, last_response.status
end

def test_flatpak_static_index_error_response
stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/index/static").
to_return(:status => 404, :body => '{"error": "not found"}')

get '/index/static'
assert_equal 404, last_response.status
assert_equal '{"error": "not found"}', last_response.body
end

def test_flatpak_static_index_server_error
stub_request(:get, "#{::Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/index/static").
to_return(:status => 500, :body => '{"error": "internal server error"}')

get '/index/static'
assert_equal 500, last_response.status
assert_equal '{"error": "internal server error"}', last_response.body
end
end