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

fix: handle notice response during auth and terminate the creds retrieval process #514

Merged
merged 1 commit into from
Dec 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.67
1.1.68
26 changes: 17 additions & 9 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Process.flag(:trap_exit, true)
H.set_max_heap_size(150)

{:ok, sock} = :ranch.handshake(ref)

Check warning on line 44 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Dialyze

:ranch.handshake/1 is undefined (module :ranch is not available or is yet to be defined)

Check warning on line 44 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Run tests

:ranch.handshake/1 is undefined (module :ranch is not available or is yet to be defined)
:ok = trans.setopts(sock, active: true)
Logger.debug("ClientHandler is: #{inspect(self())}")

Expand Down Expand Up @@ -865,18 +865,26 @@
ssl_opts: ssl_opts || []
)

resp =
case H.get_user_secret(conn, tenant.auth_query, db_user) do
{:ok, secret} ->
try do
Logger.debug(
"ClientHandler: Connected to db #{tenant.db_host} #{tenant.db_port} #{tenant.db_database} #{user.db_user}"
)

resp =
with {:ok, secret} <- H.get_user_secret(conn, tenant.auth_query, db_user) do
t = if secret.digest == :md5, do: :auth_query_md5, else: :auth_query
{:ok, {t, fn -> Map.put(secret, :alias, user.db_user_alias) end}}
end

{:error, reason} ->
{:error, reason}
end

GenServer.stop(conn, :normal)
resp
Logger.info("ClientHandler: Get secrets finished")
resp
rescue
exception ->
Logger.error("ClientHandler: Couldn't fetch user secrets from #{tenant.db_host}")
reraise exception, __STACKTRACE__
after
GenServer.stop(conn, :normal, 5_000)
end
end

@spec exchange_first(:password | :auth_query, fun(), binary(), binary(), binary()) ::
Expand Down
7 changes: 5 additions & 2 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ defmodule Supavisor.DbHandler do
%{tag: :error_response, payload: error}, _ ->
{:error_response, error}

%{tag: :notice_response, payload: error}, _ ->
{:notice_response, error}

_e, acc ->
acc
end)
Expand Down Expand Up @@ -268,8 +271,8 @@ defmodule Supavisor.DbHandler do
Logger.error("DbHandler: Auth error #{inspect(reason)}")
{:stop, :invalid_password, data}

{:error_response, error} ->
Logger.error("DbHandler: Error auth response #{inspect(error)}")
{response, error} when response in [:notice_response, :error_response] ->
Logger.error("DbHandler: #{inspect(response)} #{inspect(error)}")
{:keep_state, data}

{:ready_for_query, ps, db_state} ->
Expand Down
5 changes: 2 additions & 3 deletions lib/supavisor/protocol/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ defmodule Supavisor.Protocol.Server do
end

# https://www.postgresql.org/docs/current/protocol-error-fields.html
def decode_payload(:error_response, payload) do
String.split(payload, <<0>>, trim: true)
end
def decode_payload(tag, payload) when tag in [:error_response, :notice_response],
do: String.split(payload, <<0>>, trim: true)

def decode_payload(
:password_message,
Expand Down
Loading