Skip to content

Conversation

@ianballou
Copy link
Member

@ianballou ianballou commented Jun 24, 2025

Serves the Flatpak index at /index/static.

Also prepares the code to filter the index based on the authenticated client. Includes sample code.

This will require a change to the Apache config: theforeman/puppet-foreman_proxy_content#522

Comment on lines 44 to 49
query = ""
unless params.empty?
query_parts = params.map { |key, value| "#{key}=#{value}" }
query = "?#{query_parts.join('&')}"
end
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/index/static#{query}")
Copy link
Member

Choose a reason for hiding this comment

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

I'd like the parameters to be escaped. I'd suggest this:

Suggested change
query = ""
unless params.empty?
query_parts = params.map { |key, value| "#{key}=#{value}" }
query = "?#{query_parts.join('&')}"
end
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/index/static#{query}")
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/index/static")
uri.query = params.map { |k, v| "#{URI.encode_uri_component(k)}=#{URI.encode_uri_component(v)}" }.join('&') if params.any?

I really wish there was a method similar to URI.encode_www_form that uses URI.encode_uri_component and accepts a hash, but I can't find it.

And the tags method probably needs the same fixes.

Copy link
Member Author

@ianballou ianballou Jun 26, 2025

Choose a reason for hiding this comment

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

I was surprised too that there wasn't a cleaner way to build the URL with the query from a hash with escaping.

Copy link
Member

Choose a reason for hiding this comment

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

I swear I saw it used somewhere because I too thought there must be an easy way, but I couldn't find it. Feels so stupid because it's so extremely common.

Copy link
Member Author

Choose a reason for hiding this comment

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

Time to enhance encode_www_form with an escape arg...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh - encode_uri_component doesn't seem to exist before Ruby 3.1

Copy link
Member Author

@ianballou ianballou Jun 26, 2025

Choose a reason for hiding this comment

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

I wonder if the + that encode_www_form uses for spaces is okay.

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.

# end

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

Choose a reason for hiding this comment

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

I wonder if it's cheaper to inline the change

Suggested change
# pulp_index["Results"] = pulp_index["Results"].select { |result| catalog.include?(result["Name"]) }
# pulp_index["Results"].select! { |result| catalog.include?(result["Name"]) }

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that should be a bit better.

@ianballou ianballou force-pushed the 38530-capsule-static-index branch from e69ca68 to 244ca24 Compare June 26, 2025 21:57
def flatpak_static_index(headers, params = {})
uri = URI.parse("#{@pulp_endpoint}/pulpcore_registry/index/static")
unless params.empty?
uri.query = URI.encode_www_form(params)
Copy link
Member Author

Choose a reason for hiding this comment

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

For now, I'm going with encode_www_form since encode_uri_component isn't available in the versions of Ruby we use (3.0).

Copy link
Member

Choose a reason for hiding this comment

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

I found https://bibwild.wordpress.com/2023/02/14/escaping-encoding-uri-components-in-ruby-3-2/ as a very good write up of he mess. encode_www_form is invalid because it encodes a space as + instead of %20. It points to the very unlikely ERB::Util.url_encode as a cross version compatible API.

Copy link
Member Author

@ianballou ianballou Jun 27, 2025

Choose a reason for hiding this comment

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

I did some research before and + subbed for seems to be regarded as safe in the query string, at least according to forums. This misguidance is probably what blog refers to in the "Why is this so confusing and weird?" section. Even then it wasn't super clear - I might've just stumbled upon some forums that were more misguided than others :)

Anyway, it's clear that %20 is most correct. I'd much prefer supporting getting away from using + chars in a strange way.
Including ERB for this does feel a little odd, but it sounds safe enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to use ERB::Util.url_encode

@sjha4
Copy link
Member

sjha4 commented Jun 27, 2025

Functionally this is working well..Tested adding proxy as the flatpak remote and able to install apps with basic auth.

@ianballou ianballou force-pushed the 38530-capsule-static-index branch from 244ca24 to 3e6a1a4 Compare June 27, 2025 18:59
@ianballou ianballou force-pushed the 38530-capsule-static-index branch from 3e6a1a4 to e3a167f Compare June 30, 2025 15:03
@ianballou ianballou requested a review from ekohl June 30, 2025 15:04
Copy link
Member

@sjha4 sjha4 left a comment

Choose a reason for hiding this comment

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

Ack..This has been working well in my testing..

@ianballou
Copy link
Member Author

Since there is an ACK and the previous comments have generally been addressed: merging.

@ianballou ianballou merged commit 783c450 into Katello:main Jul 14, 2025
12 checks passed
@ianballou ianballou deleted the 38530-capsule-static-index branch July 14, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants