Skip to content

Commit

Permalink
Merge pull request #154 from supabase/fix/decrypt_extensions_data
Browse files Browse the repository at this point in the history
fix Api.decrypt_extensions_data
  • Loading branch information
abc3 authored May 10, 2022
2 parents 04ec4f7 + 56cb065 commit 6ca5eef
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/extensions/postgres/postgres_subscriptions.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Extensions.Postgres.Subscriptions do
@moduledoc """
This module consolidates subscriptions handling
"""
require Logger
import Postgrex, only: [query: 3]

Expand Down Expand Up @@ -155,7 +158,6 @@ defmodule Extensions.Postgres.Subscriptions do
# %{"filter" => "room_id=eq.1", "schema" => "public", "table" => "messages"}

transform_to_oid_view(oids, params.config)
|> IO.inspect()
|> Enum.each(fn
{entity, filters} ->
query(conn, sql, [bin_uuid, entity, filters, params.claims])
Expand Down
1 change: 0 additions & 1 deletion lib/mix/tasks/seed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ defmodule Mix.Tasks.Seed do
[],
[]
)
|> IO.inspect()
end
end
10 changes: 4 additions & 6 deletions lib/realtime/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,13 @@ defmodule Realtime.Api do
|> Repo.preload(:extensions)
end

def decrypt_extensions_data(
%Realtime.Api.Tenant{extensions: %{settings: settings, type: type} = extensions} = tenant
)
when is_map(settings) and is_binary(type) do
def decrypt_extensions_data(%Realtime.Api.Tenant{} = tenant) do
secure_key = Application.get_env(:realtime, :db_enc_key)

decrypted_extensions =
for extension <- extensions do
%{required: required} = Realtime.Extensions.db_settings(type)
for extension <- tenant.extensions do
settings = extension.settings
%{required: required} = Realtime.Extensions.db_settings(extension.type)

decrypted_settings =
Enum.reduce(required, settings, fn
Expand Down
3 changes: 3 additions & 0 deletions lib/realtime/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Realtime.Helpers do
@moduledoc """
This module includes helper functions for different contexts that can't be union in one module.
"""
def encrypt(secret_key, text) do
:crypto.crypto_one_time(:aes_128_ecb, secret_key, pad(text), true)
|> Base.encode64()
Expand Down
13 changes: 12 additions & 1 deletion test/realtime/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.ApiTest do
alias Realtime.Api

describe "tenants" do
alias Realtime.Api.Tenant
alias Realtime.Api.{Tenant, Extensions}

@valid_attrs %{
external_id: "external_id",
Expand Down Expand Up @@ -67,6 +67,17 @@ defmodule Realtime.ApiTest do
assert {:error, %Ecto.Changeset{}} = Api.create_tenant(@invalid_attrs)
end

test "check get_dec_tenant_by_external_id/1" do
tenant = tenant_fixture()

%Tenant{extensions: [%Extensions{} = extension]} =
Api.get_dec_tenant_by_external_id("external_id")

assert Map.has_key?(extension.settings, "db_password")
password = extension.settings["db_password"]
assert password = "postgres"
end

test "update_tenant/2 with valid data updates the tenant" do
tenant = tenant_fixture()
assert {:ok, %Tenant{} = tenant} = Api.update_tenant(tenant, @update_attrs)
Expand Down

0 comments on commit 6ca5eef

Please sign in to comment.