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

fix: Reduce postgres user grants #1159

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions lib/realtime/tenants/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ defmodule Realtime.Tenants.Migrations do
FixWalrusRoleHandling,
UnloggedMessagesTable,
LoggedMessagesTable,
FilterDeletePostgresChanges
FilterDeletePostgresChanges,
ReduceGrantsPostgresUser
}

@migrations [
Expand Down Expand Up @@ -109,7 +110,8 @@ defmodule Realtime.Tenants.Migrations do
{20_240_618_124_746, FixWalrusRoleHandling},
{20_240_801_235_015, UnloggedMessagesTable},
{20_240_805_133_720, LoggedMessagesTable},
{20_240_827_160_934, FilterDeletePostgresChanges}
{20_240_827_160_934, FilterDeletePostgresChanges},
{20_240_919_140_541, ReduceGrantsPostgresUser}
]
defstruct [:tenant_external_id, :settings]
@spec run_migrations(map()) :: :ok | {:error, any()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Realtime.Tenants.Migrations.ReduceGrantsPostgresUser do
@moduledoc false
use Ecto.Migration

def change do
execute("revoke supabase_realtime_admin from postgres")
execute("alter default privileges for role supabase_admin in schema realtime revoke all on tables from postgres")
execute("alter default privileges for role supabase_admin in schema realtime revoke all on functions from postgres")
execute("alter default privileges for role supabase_admin in schema realtime revoke all on sequences from postgres")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually breaks our tests and implementations as supabase_admin is the user we use to connect to tenant database and we still need them 😭

so I will need to remove this 3 execute @soedirgo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm how does it break? It shouldn't affect existing tables/functions

Copy link
Contributor Author

@filipecabaco filipecabaco Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running required migrations fails:

     18:14:41.532 project=dev_tenant external_id=dev_tenant [error] MigrationsFailedToRun: %Postgrex.Error{
       message: nil,
       postgres: %{
         code: :undefined_object,
         line: "5101",
         message: "role \"supabase_admin\" does not exist",
         file: "acl.c",
         unknown: "ERROR",
         severity: "ERROR",
         pg_code: "42704",
         routine: "get_role_oid"
       },
       connection_id: 156,
       query: nil
     }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh we granted the default privileges here 😭 then yeah we need to move these out of Realtime tenant migrations


filipecabaco marked this conversation as resolved.
Show resolved Hide resolved
execute("revoke all on table realtime.schema_migrations from postgres, anon, authenticated, service_role")
execute("grant select on table realtime.schema_migrations to postgres with grant option")

execute("revoke all on table realtime.messages from postgres")
execute("grant select on table realtime.messages to postgres with grant option")
filipecabaco marked this conversation as resolved.
Show resolved Hide resolved
filipecabaco marked this conversation as resolved.
Show resolved Hide resolved

execute("revoke all on table realtime.subscription from postgres")
execute("grant select on table realtime.subscription to postgres with grant option")
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.32.11",
version: "2.32.12",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
Loading