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

feat: auth_query support #132

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@
0.2.13
0.2.14
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ if config_env() != :test do
jwt_claim_validators: System.get_env("JWT_CLAIM_VALIDATORS", "{}") |> Jason.decode!(),
api_jwt_secret: System.get_env("API_JWT_SECRET"),
metrics_jwt_secret: System.get_env("METRICS_JWT_SECRET"),
proxy_port: System.get_env("PROXY_PORT", "7654") |> String.to_integer(),
proxy_port: System.get_env("PROXY_PORT", "6543") |> String.to_integer(),
proxy_port_session: System.get_env("PROXY_PORT_SESSION", "5432") |> String.to_integer(),
prom_poll_rate: System.get_env("PROM_POLL_RATE", "15000") |> String.to_integer(),
global_upstream_ca: upstream_ca,
global_downstream_cert: downstream_cert,
Expand Down
22 changes: 16 additions & 6 deletions lib/supavisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ defmodule Supavisor do
@registry Supavisor.Registry.Tenants
@type workers :: %{manager: pid, pool: pid}

@spec start(String.t(), String.t()) :: {:ok, pid} | {:error, any()}
def start(tenant, user_alias) do
@spec start(String.t(), String.t(), fun(), atom() | nil) :: {:ok, pid} | {:error, any()}
def start(tenant, user_alias, client_key, def_mode_type \\ nil) do
case get_global_sup(tenant, user_alias) do
nil ->
start_local_pool(tenant, user_alias)
start_local_pool(tenant, user_alias, client_key, def_mode_type)

pid ->
{:ok, pid}
Expand Down Expand Up @@ -97,8 +97,9 @@ defmodule Supavisor do

## Internal functions

@spec start_local_pool(String.t(), String.t()) :: {:ok, pid} | {:error, any()}
defp start_local_pool(tenant, user_alias) do
@spec start_local_pool(String.t(), String.t(), term(), atom() | nil) ::
{:ok, pid} | {:error, any()}
defp start_local_pool(tenant, user_alias, auth_secrets, def_mode_type) do
Logger.debug("Starting pool for #{inspect({tenant, user_alias})}")

case Tenants.get_pool_config(tenant, user_alias) do
Expand Down Expand Up @@ -129,9 +130,18 @@ defmodule Supavisor do
ip_version: H.ip_version(ip_ver, db_host),
upstream_ssl: tenant_record.upstream_ssl,
upstream_verify: tenant_record.upstream_verify,
upstream_tls_ca: H.upstream_cert(tenant_record.upstream_tls_ca)
upstream_tls_ca: H.upstream_cert(tenant_record.upstream_tls_ca),
require_user: tenant_record.require_user,
secrets: auth_secrets
}

mode =
if is_nil(def_mode_type) do
mode
else
def_mode_type
end
abc3 marked this conversation as resolved.
Show resolved Hide resolved

args = %{
tenant: tenant,
user_alias: user_alias,
Expand Down
36 changes: 21 additions & 15 deletions lib/supavisor/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ defmodule Supavisor.Application do
{Supavisor.SignalHandler, []}
)

proxy_port = Application.get_env(:supavisor, :proxy_port)
proxy_ports = [
{Application.get_env(:supavisor, :proxy_port), :transaction},
abc3 marked this conversation as resolved.
Show resolved Hide resolved
{Application.get_env(:supavisor, :proxy_port_session), :session}
]

:ranch.start_listener(
:pg_proxy,
:ranch_tcp,
%{
max_connections: String.to_integer(System.get_env("MAX_CONNECTIONS") || "25000"),
num_acceptors: String.to_integer(System.get_env("NUM_ACCEPTORS") || "100"),
socket_opts: [port: proxy_port]
},
Supavisor.ClientHandler,
[]
)
|> then(&"Proxy started on port #{proxy_port}, result: #{inspect(&1)}")
|> Logger.warning()
for {port, mode} <- proxy_ports do
:ranch.start_listener(
:pg_proxy,
:ranch_tcp,
%{
max_connections: String.to_integer(System.get_env("MAX_CONNECTIONS") || "25000"),
num_acceptors: String.to_integer(System.get_env("NUM_ACCEPTORS") || "100"),
socket_opts: [port: port]
},
Supavisor.ClientHandler,
%{def_mode_type: mode}
)
|> then(&"Proxy started #{mode} on port #{port}, result: #{inspect(&1)}")
|> Logger.warning()
end

:syn.set_event_handler(Supavisor.SynHandler)
:syn.add_node_to_scopes([:tenants])
Expand Down Expand Up @@ -63,7 +68,8 @@ defmodule Supavisor.Application do
PartitionSupervisor,
child_spec: DynamicSupervisor, strategy: :one_for_one, name: Supavisor.DynamicSupervisor
},
Supavisor.Vault
Supavisor.Vault,
{Cachex, name: Supavisor.Cache}
abc3 marked this conversation as resolved.
Show resolved Hide resolved
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
Loading
Loading