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

Support http proxy configuration via env variables #2850

Merged
merged 4 commits into from
Nov 8, 2024
Merged

Conversation

jonatanklosko
Copy link
Member

Adds support for HTTP_PROXY/HTTPS_PROXY env vars.

@spec mint_connect_options_for_uri(URI.t()) :: keyword()
def mint_connect_options_for_uri(uri) do
http_proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
https_proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") || http_proxy
Copy link
Member Author

Choose a reason for hiding this comment

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

curl only uses HTTP_PROXY for http requests and HTTPS_PROXY for https requests, however :httpc uses :proxy as the default for :https_proxy, so I mirrored :httpc here.

Copy link

github-actions bot commented Nov 7, 2024

Uffizzi Preview deployment-58063 was deleted.

connect_options: fn request ->
uri = URI.parse(request.url)
connect_options = mint_connect_options_for_uri(uri)
Req.Request.merge_options(request, connect_options: connect_options)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine as is but if some other code previously set :connect_options, they'd be overridden. I don't have a solution for this in Req, there's just an issue as a reminder: wojtekmach/req#319.

I think ideally it'd be as easy as:

put_in(req.options[:connect_options][:proxy], proxy)

but it of course assumes the nested structure is already there. I guess what I'm trying to say is it would be nice if there's a language feature that makes above easy or at least if there's a Req feature. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually did check if Req.merge would handle it recursively, and then considered doing deep merge with req.options[:connect_options], but since we don't specify connect_options anywhere else, I ended up ignoring it.

Instead of making the replace/merge behaviour specified at option registration, we could shift the decision to the caller, as in Req.Request.deep_merge_options (or an option to merge_options).

Copy link
Contributor

Choose a reason for hiding this comment

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

having a separate function for deep merge is interesting, thanks!

cert_opts =
if uri.scheme == "https" do
if cacertfile = Livebook.Config.cacertfile() do
[transport_opts: [cacertfile: cacertfile]]
Copy link
Contributor

Choose a reason for hiding this comment

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

As an aside, in Req I have a few shortcuts like the following, these two calls are equivalent:

Req.new(inet6: true)
Req.new(connect_options: [transport_opts: [inet6: true]])

cacerts/cacertfile seems maybe common enough to warrant such shortcut but tbh I don't know where to draw the line, I'm kind of wary of adding a lot of top-level options and are looking into nesting (livebook-dev/req_athena#40 (comment)) but then this is already nested but like too nested? Curious if any of this resonate with you.

Copy link
Member Author

@jonatanklosko jonatanklosko Nov 7, 2024

Choose a reason for hiding this comment

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

No strong opinion, I don't think we need to rush into more top-level shortcuts. Though with #394 it may be a better argument to be able to specify proxy and certs on the same level.

@jonatanklosko jonatanklosko merged commit cb04745 into main Nov 8, 2024
7 checks passed
@jonatanklosko jonatanklosko deleted the jk-http-proxy branch November 8, 2024 08: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