Skip to content

Commit

Permalink
chore(electric): Support sslmode query option in DATABASE_URL (#848)
Browse files Browse the repository at this point in the history
Fixes VAX-1543.
  • Loading branch information
alco committed Jan 18, 2024
1 parent a011768 commit dd27d6a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-timers-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/electric": patch
---

[VAX-1543] Add support for the sslmode query option in DATABASE_URL.
20 changes: 18 additions & 2 deletions components/electric/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,26 @@ config :electric, Electric.Features,

connector_config =
if conn_params do
require_ssl? = env!("DATABASE_REQUIRE_SSL", :boolean, default_database_require_ssl)
require_ssl_config = env!("DATABASE_REQUIRE_SSL", :boolean, nil)

# Always try connecting with SSL first.
# In Electric, we only support two ways of using SSL when connecting to the database:
#
# 1. It is either required, in which case a failure to establish a secure connection to the
# database will be treated as a fatal error.
#
# 2. Or it is not required, in which case Electric will still try connecting with SSL first
# and will only fallback to using unencrypted connection if that fails.
#
# When DATABASE_REQUIRE_SSL is set by the user, the sslmode query option in DATABASE_URL is ignored.
require_ssl? =
case {require_ssl_config, conn_params[:sslmode]} do
{nil, :require} -> true
{nil, _} -> false
{nil, nil} -> default_database_require_ssl
{true, _} -> true
{false, _} -> false
end

# When require_ssl?=true, :epgsql will try to connect using SSL and fail if the server does not accept encrypted
# connections.
#
Expand Down
78 changes: 53 additions & 25 deletions components/electric/lib/electric/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Electric.Config do
end
end

@doc """
@doc ~S"""
Parse a PostgreSQL URI into a keyword list.
## Examples
Expand Down Expand Up @@ -174,8 +174,29 @@ defmodule Electric.Config do
password: "weird/password"
]}
iex> parse_postgresql_uri("postgres://super_user@localhost:7801/postgres?sslmode=disable")
{:ok, [
host: "localhost",
port: 7801,
database: "postgres",
username: "super_user",
sslmode: :disable
]}
iex> parse_postgresql_uri("postgres://super_user@localhost:7801/postgres?sslmode=require")
{:ok, [
host: "localhost",
port: 7801,
database: "postgres",
username: "super_user",
sslmode: :require
]}
iex> parse_postgresql_uri("postgres://super_user@localhost:7801/postgres?sslmode=yesplease")
{:error, "has invalid \"sslmode\" value: \"yesplease\""}
iex> parse_postgresql_uri("postgrex://localhost")
{:error, "has invalid URL scheme: \\"postgrex\\""}
{:error, "has invalid URL scheme: \"postgrex\""}
iex> parse_postgresql_uri("postgresql://localhost")
{:error, "has invalid or missing username"}
Expand All @@ -192,17 +213,14 @@ defmodule Electric.Config do
iex> parse_postgresql_uri("postgresql://user:password@")
{:error, "missing host"}
iex> parse_postgresql_uri("postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff")
{:error, "has unsupported query string. Please remove all URL query options"}
iex> parse_postgresql_uri("postgresql://user@localhost:5433/mydb?opts=-c%20synchronous_commit%3Doff&foo=bar")
{:error, "has unsupported query options: \"foo\", \"opts\""}
iex> parse_postgresql_uri("postgresql://electric@localhost/db?replication=database")
{:error, "has unsupported \\"replication\\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
{:error, "has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
iex> parse_postgresql_uri("postgresql://electric@localhost/db?replication=off")
{:error, "has unsupported \\"replication\\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
iex> parse_postgresql_uri("postgres://super_user@localhost:7801/postgres?sslmode=yesplease")
{:error, "has unsupported \\"sslmode\\" query option. Use the DATABASE_REQUIRE_SSL configuration option instead"}
{:error, "has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
"""
@spec parse_postgresql_uri(binary) :: {:ok, keyword} | {:error, binary}
def parse_postgresql_uri(uri_str) do
Expand All @@ -212,16 +230,18 @@ defmodule Electric.Config do
with :ok <- validate_url_scheme(scheme),
:ok <- validate_url_host(host),
{:ok, {username, password}} <- parse_url_userinfo(userinfo),
:ok <- validate_url_query(query) do
{:ok, options} <- parse_url_query(query) do
conn_params =
[
host: host,
port: port || 5432,
database: parse_database(path, username) |> URI.decode(),
username: URI.decode(username),
password: if(password, do: URI.decode(password))
]
|> Enum.reject(fn {_key, val} -> is_nil(val) end)
Enum.reject(
[
host: host,
port: port || 5432,
database: parse_database(path, username) |> URI.decode(),
username: URI.decode(username),
password: if(password, do: URI.decode(password))
] ++ options,
fn {_key, val} -> is_nil(val) end
)

{:ok, conn_params}
end
Expand Down Expand Up @@ -256,23 +276,31 @@ defmodule Electric.Config do
end
end

defp validate_url_query(nil), do: :ok
defp parse_url_query(nil), do: {:ok, []}

defp validate_url_query(query_str) do
defp parse_url_query(query_str) do
case URI.decode_query(query_str) do
empty when map_size(empty) == 0 ->
:ok
{:ok, []}

%{"sslmode" => _} ->
%{"sslmode" => sslmode} when sslmode in ~w[disable allow prefer require] ->
{:ok, sslmode: String.to_existing_atom(sslmode)}

%{"sslmode" => sslmode} when sslmode in ~w[verify-ca verify-full] ->
{:error,
"has unsupported \"sslmode\" query option. Use the DATABASE_REQUIRE_SSL configuration option instead"}
"has unsupported \"sslmode\" value #{inspect(sslmode)}. Consider using the DATABASE_REQUIRE_SSL configuration option"}

%{"sslmode" => sslmode} ->
{:error, "has invalid \"sslmode\" value: #{inspect(sslmode)}"}

%{"replication" => _} ->
{:error,
"has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}

_ ->
{:error, "has unsupported query string. Please remove all URL query options"}
map ->
{:error,
"has unsupported query options: " <>
(map |> Map.keys() |> Enum.sort() |> Enum.map_join(", ", &inspect/1))}
end
end

Expand Down

0 comments on commit dd27d6a

Please sign in to comment.