|
| 1 | +-- migrate:up |
| 2 | +DO $$ |
| 3 | +BEGIN |
| 4 | + IF EXISTS (SELECT FROM pg_extension WHERE extname = 'pg_net') |
| 5 | + THEN |
| 6 | + CREATE OR REPLACE FUNCTION extensions.grant_pg_net_access() |
| 7 | + RETURNS event_trigger |
| 8 | + LANGUAGE plpgsql |
| 9 | + AS $func$ |
| 10 | + BEGIN |
| 11 | + IF EXISTS ( |
| 12 | + SELECT 1 |
| 13 | + FROM pg_event_trigger_ddl_commands() AS ev |
| 14 | + JOIN pg_extension AS ext |
| 15 | + ON ev.objid = ext.oid |
| 16 | + WHERE ext.extname = 'pg_net' |
| 17 | + ) |
| 18 | + THEN |
| 19 | + IF NOT EXISTS ( |
| 20 | + SELECT 1 |
| 21 | + FROM pg_roles |
| 22 | + WHERE rolname = 'supabase_functions_admin' |
| 23 | + ) |
| 24 | + THEN |
| 25 | + CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION; |
| 26 | + END IF; |
| 27 | + |
| 28 | + GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role; |
| 29 | + |
| 30 | + IF EXISTS ( |
| 31 | + SELECT FROM pg_extension |
| 32 | + WHERE extname = 'pg_net' |
| 33 | + -- all versions in use on existing projects as of 2025-02-20 |
| 34 | + -- version 0.12.0 onwards don't need these applied |
| 35 | + AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8', '0.10.0', '0.11.0') |
| 36 | + ) THEN |
| 37 | + ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; |
| 38 | + ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; |
| 39 | + |
| 40 | + ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; |
| 41 | + ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; |
| 42 | + |
| 43 | + REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; |
| 44 | + REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; |
| 45 | + |
| 46 | + GRANT EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; |
| 47 | + GRANT EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; |
| 48 | + END IF; |
| 49 | + END IF; |
| 50 | + END; |
| 51 | + $func$; |
| 52 | + |
| 53 | + ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY INVOKER; |
| 54 | + ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY INVOKER; |
| 55 | + |
| 56 | + REVOKE EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM supabase_functions_admin, postgres, anon, authenticated, service_role; |
| 57 | + REVOKE EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM supabase_functions_admin, postgres, anon, authenticated, service_role; |
| 58 | + |
| 59 | + GRANT ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO PUBLIC; |
| 60 | + GRANT ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO PUBLIC; |
| 61 | + END IF; |
| 62 | +END $$; |
| 63 | + |
| 64 | +-- migrate:down |
0 commit comments