diff --git a/lib/smart_proxy_container_gateway/container_gateway_api.rb b/lib/smart_proxy_container_gateway/container_gateway_api.rb index 9d47371..af796d7 100644 --- a/lib/smart_proxy_container_gateway/container_gateway_api.rb +++ b/lib/smart_proxy_container_gateway/container_gateway_api.rb @@ -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 + # 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 diff --git a/lib/smart_proxy_container_gateway/container_gateway_main.rb b/lib/smart_proxy_container_gateway/container_gateway_main.rb index b722a72..beb8a1b 100644 --- a/lib/smart_proxy_container_gateway/container_gateway_main.rb +++ b/lib/smart_proxy_container_gateway/container_gateway_main.rb @@ -1,6 +1,7 @@ require 'net/http' require 'uri' require 'digest' +require 'erb' require 'smart_proxy_container_gateway/dependency_injection' require 'sequel' module Proxy @@ -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) diff --git a/test/container_gateway_api_test.rb b/test/container_gateway_api_test.rb index 63d0cc6..57a30d4 100644 --- a/test/container_gateway_api_test.rb +++ b/test/container_gateway_api_test.rb @@ -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