Skip to content

Commit 50efe34

Browse files
authored
perf: optimize password authentication path with caching (#894)
1 parent f832062 commit 50efe34

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/supavisor/client_handler.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ defmodule Supavisor.ClientHandler do
743743
## Internal functions
744744
defp handle_auth_success(sock, final_secrets, data) do
745745
Logger.info("ClientHandler: Connection authenticated")
746+
cache_validated_password(data, final_secrets)
746747

747748
if data.mode != :proxy do
748749
Supavisor.UpstreamAuthentication.put_upstream_auth_secrets(data.id, final_secrets)
@@ -766,6 +767,19 @@ defmodule Supavisor.ClientHandler do
766767
}
767768
end
768769

770+
defp cache_validated_password(%{tenant: tenant}, %Supavisor.Secrets.PasswordSecrets{} = secrets) do
771+
case Supavisor.ClientAuthentication.get_validation_secrets(tenant, secrets.user) do
772+
{:ok, %{password_secrets: nil} = validation} ->
773+
updated = %{validation | password_secrets: secrets}
774+
Supavisor.ClientAuthentication.put_validation_secrets(tenant, secrets.user, updated)
775+
776+
_ ->
777+
:ok
778+
end
779+
end
780+
781+
defp cache_validated_password(_data, _secrets), do: :ok
782+
769783
defp handle_auth_failure(exception, data) do
770784
AuthMethods.handle_auth_failure(data.auth_context, exception)
771785
Supavisor.CircuitBreaker.record_failure({data.tenant, data.peer_ip}, :auth_error)

lib/supavisor/client_handler/auth_methods/password.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ defmodule Supavisor.ClientHandler.AuthMethods.Password do
8383
defp validate_password(password, ctx) do
8484
with {:ok, %{password_secrets: password_secrets, sasl_secrets: sasl_secrets}} <-
8585
ClientAuthentication.fetch_validation_secrets(ctx.id, ctx.tenant, ctx.user) do
86-
if password_secrets && password == password_secrets.password do
86+
if password_secrets && Plug.Crypto.secure_compare(password, password_secrets.password) do
8787
:ok
8888
else
8989
salted_password =

0 commit comments

Comments
 (0)