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

Option to allow only SSL connection #130

Merged
merged 2 commits into from
Jul 14, 2023
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 @@
0.2.12
0.2.13
12 changes: 9 additions & 3 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ defmodule Supavisor.ClientHandler do

case Tenants.get_user(external_id, user) do
{:ok, user_info} ->
new_data = update_user_data(data, external_id, user_info)
if user_info.enforce_ssl and !data.ssl do
Logger.error("Tenant is not allowed to connect without SSL, user #{user}")
:ok = send_error(sock, "XX000", "SSL connection is required")
{:stop, :normal, data}
else
new_data = update_user_data(data, external_id, user_info)

{:keep_state, new_data,
{:next_event, :internal, {:handle, fn -> user_info.db_password end}}}
{:keep_state, new_data,
{:next_event, :internal, {:handle, fn -> user_info.db_password end}}}
end

{:error, reason} ->
Logger.error("User not found: #{inspect(reason)} #{inspect({user, external_id})}")
Expand Down
8 changes: 5 additions & 3 deletions lib/supavisor/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ defmodule Supavisor.Tenants do
nil ->
{:error, :not_found}

[[db_alias, pass, mode, timeout, ps]] ->
[[db_alias, pass, mode, timeout, ps, enforce_ssl]] ->
{:ok,
%{
db_password: pass,
db_user_alias: db_alias,
mode_type: mode,
pool_checkout_timeout: timeout,
default_parameter_status: ps
default_parameter_status: ps,
enforce_ssl: enforce_ssl
}}

_ ->
Expand Down Expand Up @@ -258,7 +259,8 @@ defmodule Supavisor.Tenants do
u.db_password,
u.mode_type,
u.pool_checkout_timeout,
t.default_parameter_status
t.default_parameter_status,
t.enforce_ssl
]
)

Expand Down
4 changes: 3 additions & 1 deletion lib/supavisor/tenants/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Supavisor.Tenants.Tenant do
field(:upstream_ssl, :boolean, default: false)
field(:upstream_verify, Ecto.Enum, values: [:none, :peer])
field(:upstream_tls_ca, :binary)
field(:enforce_ssl, :boolean, default: false)

has_many(:users, User,
foreign_key: :tenant_external_id,
Expand All @@ -43,7 +44,8 @@ defmodule Supavisor.Tenants.Tenant do
:ip_version,
:upstream_ssl,
:upstream_verify,
:upstream_tls_ca
:upstream_tls_ca,
:enforce_ssl
])
|> check_constraint(:upstream_ssl, name: :upstream_constraints, prefix: "_supavisor")
|> check_constraint(:upstream_verify, name: :upstream_constraints, prefix: "_supavisor")
Expand Down
1 change: 1 addition & 0 deletions lib/supavisor_web/views/tenant_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule SupavisorWeb.TenantView do
ip_version: tenant.ip_version,
upstream_ssl: tenant.upstream_ssl,
upstream_verify: tenant.upstream_verify,
enforce_ssl: tenant.enforce_ssl,
users: render_many(tenant.users, UserView, "user.json")
}
end
Expand Down
15 changes: 15 additions & 0 deletions priv/repo/migrations/20230711142028_add_enforce_ssl.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Supavisor.Repo.Migrations.AddEnforceSsl do
use Ecto.Migration

def up do
alter table("tenants", prefix: "_supavisor") do
add(:enforce_ssl, :boolean, null: false, default: false)
end
end

def down do
alter table("tenants", prefix: "_supavisor") do
remove(:enforce_ssl)
end
end
end
Loading