2023-02-08 05:58:45 -05:00
|
|
|
/* This SQL will run after all other queries. */
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
---- Grant CRUD access to the group ----
|
2023-02-08 05:58:45 -05:00
|
|
|
grant select, insert, update, delete
|
|
|
|
on all tables
|
|
|
|
in schema public
|
|
|
|
to logto_tenant_${database};
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
---- Security policies for tenants table ----
|
2023-02-09 05:31:14 -05:00
|
|
|
|
2023-02-08 05:58:45 -05:00
|
|
|
revoke all privileges
|
|
|
|
on table tenants
|
|
|
|
from logto_tenant_${database};
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
-- Allow limited select to perform the RLS policy query in `after_each` (using select ... from tenants ...)
|
2023-02-09 05:31:14 -05:00
|
|
|
grant select (id, db_user)
|
|
|
|
on table tenants
|
|
|
|
to logto_tenant_${database};
|
|
|
|
|
|
|
|
alter table tenants enable row level security;
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
-- Create RLS policy to minimize the privilege
|
2023-02-09 05:31:14 -05:00
|
|
|
create policy tenants_tenant_id on tenants
|
|
|
|
using (db_user = current_user);
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
---- Revoke all privileges on systems table for tenant roles ----
|
2023-02-08 05:58:45 -05:00
|
|
|
revoke all privileges
|
|
|
|
on table systems
|
|
|
|
from logto_tenant_${database};
|
2023-03-08 11:07:33 -05:00
|
|
|
|
2023-03-14 02:15:45 -05:00
|
|
|
---- Revoke all privileges on service_logs table for tenant roles ----
|
|
|
|
revoke all privileges
|
|
|
|
on table service_logs
|
|
|
|
from logto_tenant_${database};
|
|
|
|
|
2023-03-08 11:07:33 -05:00
|
|
|
---- Create policies to make internal roles read-only ----
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note:
|
|
|
|
*
|
|
|
|
* Internal roles have scope preset and they are read-only, but we do not
|
|
|
|
* limit user or application assignment since it's business logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
-- Restrict direct role modification
|
|
|
|
create policy roles_select on roles
|
|
|
|
for select using (true);
|
|
|
|
|
|
|
|
drop policy roles_modification on roles;
|
|
|
|
create policy roles_modification on roles
|
|
|
|
using (not starts_with(name, '#internal:'));
|
|
|
|
|
|
|
|
-- Restrict role - scope modification
|
|
|
|
create policy roles_scopes_select on roles_scopes
|
|
|
|
for select using (true);
|
|
|
|
|
|
|
|
drop policy roles_scopes_modification on roles_scopes;
|
|
|
|
create policy roles_scopes_modification on roles_scopes
|
|
|
|
using (not starts_with((select roles.name from roles where roles.id = role_id), '#internal:'));
|
|
|
|
|
|
|
|
---- TODO: Make internal API Resources read-only ----
|