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 specifying CA_BUNDLE via env variables #933

Merged
merged 1 commit into from
Oct 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export Connection, newconnection, releaseconnection, getrawstream, inactivesecon

using Sockets, LoggingExtras, NetworkOptions
using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake!
using OpenSSL
using MbedTLS, OpenSSL
using ..IOExtras, ..Conditions, ..Exceptions

const default_connection_limit = 8
Expand Down Expand Up @@ -439,6 +439,11 @@ function global_sslconfig(require_ssl_verification::Bool)::SSLConfig
default_sslconfig = SSLConfig(true)
noverify_sslconfig = SSLConfig(false)
end
if haskey(ENV, "HTTP_CA_BUNDLE")
MbedTLS.ca_chain!(default_sslconfig, MbedTLS.crt_parse(read(ENV["HTTP_CA_BUNDLE"], String)))
elseif haskey(ENV, "CURL_CA_BUNDLE")
MbedTLS.ca_chain!(default_sslconfig, MbedTLS.crt_parse(read(ENV["CURL_CA_BUNDLE"], String)))
end
return require_ssl_verification ? default_sslconfig : noverify_sslconfig
end

Expand All @@ -463,6 +468,13 @@ function getconnection(::Type{SSLStream},
tcp = getconnection(TCPSocket, host, port; kw...)
# Create SSL stream.
ssl_stream = SSLStream(tcp)
if isdefined(OpenSSL, :ca_chain!)
if haskey(ENV, "HTTP_CA_BUNDLE")
OpenSSL.ca_chain!(ssl_stream.ssl_context, ENV["HTTP_CA_BUNDLE"])
elseif haskey(ENV, "CURL_CA_BUNDLE")
OpenSSL.ca_chain!(ssl_stream.ssl_context, ENV["CURL_CA_BUNDLE"])
end
end
OpenSSL.hostname!(ssl_stream, host)
OpenSSL.connect(ssl_stream)
return ssl_stream
Expand Down
11 changes: 10 additions & 1 deletion test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,6 @@ end
findnewline(bytes) = something(findfirst(==(UInt8('\n')), bytes), 0)

@testset "readuntil on Stream" begin

HTTP.open(:GET, "http://httpbin.org/stream/5") do io
while !eof(io)
bytes = readuntil(io, findnewline)
Expand All @@ -567,7 +566,17 @@ findnewline(bytes) = something(findfirst(==(UInt8('\n')), bytes), 0)
@show x
end
end
end

@testset "CA_BUNDEL env" begin
resp = withenv("HTTP_CA_BUNDLE" => HTTP.MbedTLS.MozillaCACerts_jll.cacert) do
HTTP.get("https://httpbin.org/ip"; socket_type_tls=SSLStream)
end
@test resp.status == 200
resp = withenv("HTTP_CA_BUNDLE" => HTTP.MbedTLS.MozillaCACerts_jll.cacert) do
HTTP.get("https://httpbin.org/ip")
end
@test resp.status == 200
end

end # module