Skip to content

Commit

Permalink
Avoid overwriting ssl opts with url if already set in config (#4498)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Aug 23, 2024
1 parent 7e60edc commit 07cdcc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/ecto/repo/supervisor.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Ecto.Repo.Supervisor do
@moduledoc false
use Supervisor
require Logger

@defaults [timeout: 15000, pool_size: 10]
@integer_url_query_params ["timeout", "pool_size", "idle_interval"]
Expand All @@ -25,7 +26,20 @@ defmodule Ecto.Repo.Supervisor do
case repo_init(type, repo, config) do
{:ok, config} ->
{url, config} = Keyword.pop(config, :url)
{:ok, Keyword.merge(config, parse_url(url || ""))}
url_config = parse_url(url || "")

url_config =
if is_list(config[:ssl]) and url_config[:ssl] == true do
Logger.warning(
"ignoring `ssl=true` parameter in URL because `ssl` is already set in the configuration: #{inspect(config[:ssl])}"
)

Keyword.delete(url_config, :ssl)
else
url_config
end

{:ok, Keyword.merge(config, url_config)}

:ignore ->
:ignore
Expand Down
14 changes: 14 additions & 0 deletions test/ecto/repo/supervisor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ defmodule Ecto.Repo.SupervisorTest do
assert normalize(config) == [database: "mydb", extra: "extra", otp_app: :ecto, scheme: "ecto"]
end

@tag :capture_log
test "URL options do not overwrite SSL cacertfile from config" do
put_env(database: "hello", url: "ecto:///mydb?ssl=true", ssl: [cacertfile: "/path/to/file"])
{:ok, config} = init_config(:runtime, __MODULE__, :ecto, extra: "extra")

assert normalize(config) == [
database: "mydb",
extra: "extra",
otp_app: :ecto,
scheme: "ecto",
ssl: [cacertfile: "/path/to/file"]
]
end

test "is no-op for nil or empty URL" do
put_env(database: "hello", url: nil)
{:ok, config} = init_config(:runtime, __MODULE__, :ecto, [])
Expand Down

0 comments on commit 07cdcc2

Please sign in to comment.