Skip to content
Open
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
58 changes: 7 additions & 51 deletions test/integration/client_idle_timeout_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Supavisor.Integration.ClientIdleTimeoutTest do
use Supavisor.DataCase, async: false

alias Supavisor.Support.ProtocolClient

@moduletag :integration

@idle_timeout_ms 1000
Expand All @@ -10,7 +12,7 @@ defmodule Supavisor.Integration.ClientIdleTimeoutTest do
end

test "server disconnects a client that sits idle past client_idle_timeout", %{db_conf: db_conf} do
{:gen_tcp, sock} = db_conf |> idle_tenant() |> scram_connect()
sock = db_conf |> idle_tenant() |> connect()

assert {:error, :closed} = :gen_tcp.recv(sock, 0, @idle_timeout_ms * 5)
end
Expand Down Expand Up @@ -48,59 +50,13 @@ defmodule Supavisor.Integration.ClientIdleTimeoutTest do
tenant: tenant_id,
port: Application.get_env(:supavisor, :proxy_port_transaction),
user: db_conf[:username],
password: db_conf[:password],
database: to_string(db_conf[:database])
password: db_conf[:password]
}
end

defp scram_connect(%{tenant: tenant, port: port, user: user, password: password, database: db}) do
defp connect(%{tenant: tenant, port: port, user: user, password: password}) do
{:ok, sock} = :gen_tcp.connect(~c"127.0.0.1", port, [:binary, active: false])

startup =
:pgo_protocol.encode_startup_message([{"user", "#{user}.#{tenant}"}, {"database", db}])

:ok = :gen_tcp.send(sock, startup)

# SASL auth request
{:ok, <<?R, _::32, 10::32, _methods::binary>>} = :gen_tcp.recv(sock, 0, 5000)

# SCRAM client-first
nonce = :pgo_scram.get_nonce(16)
client_first = :pgo_scram.get_client_first(user, nonce)
client_first_size = :erlang.iolist_size(client_first)
sasl_initial = ["SCRAM-SHA-256", 0, <<client_first_size::32>>, client_first]
:ok = :gen_tcp.send(sock, :pgo_protocol.encode_scram_response_message(sasl_initial))

# SCRAM server-first
{:ok, <<?R, _::32, 11::32, server_first::binary>>} = :gen_tcp.recv(sock, 0, 5000)
server_first_parts = :pgo_scram.parse_server_first(server_first, nonce)

# SCRAM client-final
{client_final, server_proof} =
:pgo_scram.get_client_final(server_first_parts, nonce, user, password)

:ok = :gen_tcp.send(sock, :pgo_protocol.encode_scram_response_message(client_final))

# SCRAM server-final + auth ok + params + ReadyForQuery
{:ok, auth_data} = :gen_tcp.recv(sock, 0, 5000)

{[<<?R, _::32, 12::32, server_final::binary>> | _], ""} =
Supavisor.Protocol.split_pkts(auth_data)

{:ok, ^server_proof} = :pgo_scram.parse_server_final(server_final)
recv_until_ready_for_query(sock, auth_data)

{:gen_tcp, sock}
end

defp recv_until_ready_for_query(sock, buf) do
{pkts, ""} = Supavisor.Protocol.split_pkts(buf)

if Enum.any?(pkts, &match?(<<?Z, _::binary>>, &1)) do
:ok
else
{:ok, more} = :gen_tcp.recv(sock, 0, 5000)
recv_until_ready_for_query(sock, more)
end
ProtocolClient.authenticate(sock, "#{user}.#{tenant}", password)
sock
end
end
34 changes: 2 additions & 32 deletions test/integration/protocol_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,8 @@ defmodule Supavisor.Integration.ProtocolIntegrationTest do

{:ok, sock} = :gen_tcp.connect(~c"127.0.0.1", port, [:binary, active: false])

startup = :pgo_protocol.encode_startup_message([{"user", "#{user}.#{tenant}"}])
:ok = :gen_tcp.send(sock, startup)

# SASL auth request
{:ok, <<?R, _::32, 10::32, methods_bin::binary>>} = :gen_tcp.recv(sock, 0, 5000)
assert "SCRAM-SHA-256" in :pgo_protocol.decode_strings(methods_bin)

# SCRAM client-first
nonce = :pgo_scram.get_nonce(16)
client_first = :pgo_scram.get_client_first(user, nonce)
client_first_size = :erlang.iolist_size(client_first)
sasl_initial = ["SCRAM-SHA-256", 0, <<client_first_size::32>>, client_first]
:ok = :gen_tcp.send(sock, :pgo_protocol.encode_scram_response_message(sasl_initial))

# SCRAM server-first
{:ok, <<?R, _::32, 11::32, server_first::binary>>} = :gen_tcp.recv(sock, 0, 5000)
server_first_parts = :pgo_scram.parse_server_first(server_first, nonce)

# SCRAM client-final
{client_final, server_proof} =
:pgo_scram.get_client_final(server_first_parts, nonce, user, password)

:ok = :gen_tcp.send(sock, :pgo_protocol.encode_scram_response_message(client_final))

# SCRAM server-final + auth ok + params + ReadyForQuery
{:ok, auth_data} = :gen_tcp.recv(sock, 0, 5000)

{[<<?R, _::32, 12::32, server_final::binary>> | _], ""} =
Supavisor.Protocol.split_pkts(auth_data)

{:ok, ^server_proof} = :pgo_scram.parse_server_final(server_final)
ProtocolClient.recv_until_ready_for_query(sock, auth_data)
# authenticate/3 sends a user-only startup without database parameter
ProtocolClient.authenticate(sock, "#{user}.#{tenant}", password)

# Verify the connection defaults to the correct database
:ok = :gen_tcp.send(sock, :pgo_protocol.encode_query_message("SELECT current_database()"))
Expand Down