@specific.dev/spectest 0.83.0 → 0.85.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/postgres-tls.d.ts +10 -0
- package/dist/components/postgres-tls.js +9 -0
- package/dist/components/postgres.d.ts +59 -1
- package/dist/components/postgres.js +5 -2
- package/dist/components/supabase-config.d.ts +5 -0
- package/dist/components/supabase-config.js +12 -4
- package/dist/components/supabase-webhooks.d.ts +1 -0
- package/dist/components/supabase-webhooks.js +262 -0
- package/dist/components/supabase.d.ts +36 -11
- package/dist/components/supabase.js +75 -18
- package/dist/components/temporal.d.ts +89 -0
- package/dist/components/temporal.js +185 -0
- package/dist/coverage.d.ts +72 -0
- package/dist/coverage.js +232 -8
- package/dist/daemon.js +71 -5
- package/dist/environment-ca.d.ts +8 -0
- package/dist/environment-ca.js +16 -0
- package/dist/harness/container-run.js +3 -0
- package/dist/harness/coverage.d.ts +15 -4
- package/dist/harness/coverage.js +48 -6
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/src/components/index.ts +3 -0
- package/src/components/postgres-tls.test.ts +23 -0
- package/src/components/postgres-tls.ts +11 -0
- package/src/components/postgres.ts +8 -2
- package/src/components/supabase-config.ts +11 -2
- package/src/components/supabase-db.test.ts +64 -0
- package/src/components/supabase-dir.test.ts +71 -0
- package/src/components/supabase-webhooks.ts +263 -0
- package/src/components/supabase.ts +108 -28
- package/src/components/temporal.test.ts +86 -0
- package/src/components/temporal.ts +188 -0
- package/src/coverage.test.ts +78 -2
- package/src/coverage.ts +276 -8
- package/src/daemon.ts +68 -3
- package/src/environment-ca.test.ts +44 -0
- package/src/environment-ca.ts +20 -0
- package/src/harness/container-run.test.ts +14 -2
- package/src/harness/container-run.ts +3 -0
- package/src/harness/coverage.test.ts +32 -1
- package/src/harness/coverage.ts +43 -6
- package/src/index.ts +1 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CertificateMount } from "../index.js";
|
|
2
|
+
|
|
3
|
+
/** Postgres terminates its own TLS; HTTP ingress cannot proxy its wire protocol. */
|
|
4
|
+
export function postgresTls(hostnames = ["{{SPECTEST_SERVICE}}", "{{SPECTEST_SERVICE}}.internal"]) {
|
|
5
|
+
return {
|
|
6
|
+
args: ["-c", "ssl=on", "-c", "ssl_cert_file=/tls/postgres.crt", "-c", "ssl_key_file=/tls/postgres.key"],
|
|
7
|
+
certificates: [{
|
|
8
|
+
hostnames, certPath: "/tls/postgres.crt", keyPath: "/tls/postgres.key", user: "postgres",
|
|
9
|
+
}] satisfies CertificateMount[],
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ServiceDefinition, ServiceSetupContext } from "../index.js";
|
|
2
2
|
import { SQL, type SqlClient } from "../sql.js";
|
|
3
|
+
import { postgresTls } from "./postgres-tls.js";
|
|
3
4
|
|
|
4
5
|
export interface PostgresOptions {
|
|
5
6
|
/** Image tag for the official `postgres` image. Default `"18-alpine"`.
|
|
@@ -26,6 +27,9 @@ export interface PostgresOptions {
|
|
|
26
27
|
persistent?: boolean;
|
|
27
28
|
/** TCP port the container listens on. Default `5432`. */
|
|
28
29
|
port?: number;
|
|
30
|
+
/** Native TLS is available by default using the environment CA; plaintext
|
|
31
|
+
* connections remain accepted. Set false to disable TLS for a custom image. */
|
|
32
|
+
tls?: boolean;
|
|
29
33
|
/**
|
|
30
34
|
* CMD args appended after the image entrypoint (keeps the entrypoint, unlike
|
|
31
35
|
* `command`). For extension tuning, e.g.
|
|
@@ -97,13 +101,15 @@ export function postgres(opts: PostgresOptions) {
|
|
|
97
101
|
const reference = opts.image ?? `postgres:${version}`;
|
|
98
102
|
const port = opts.port ?? 5432;
|
|
99
103
|
const persistent = opts.persistent ?? true;
|
|
104
|
+
const tls = opts.tls !== false ? postgresTls() : undefined;
|
|
100
105
|
// `satisfies` (instead of a return-type annotation) preserves the
|
|
101
106
|
// literal type — in particular, the `helpers` factory's return shape.
|
|
102
107
|
// The mapped type in `ServiceHandlesFor<S>` reads that to type
|
|
103
108
|
// `ctx.svc.<name>` as `{ client: SqlClient }`.
|
|
104
109
|
return {
|
|
105
110
|
image: { type: "registry", reference },
|
|
106
|
-
...(
|
|
111
|
+
...(tls ? { args: [...(opts.args ?? ["postgres"]), ...tls.args], certificates: tls.certificates }
|
|
112
|
+
: opts.args ? { args: opts.args } : {}),
|
|
107
113
|
env: {
|
|
108
114
|
POSTGRES_DB: opts.database,
|
|
109
115
|
POSTGRES_USER: opts.user,
|
|
@@ -120,7 +126,7 @@ export function postgres(opts: PostgresOptions) {
|
|
|
120
126
|
const url =
|
|
121
127
|
`postgres://${encodeURIComponent(opts.user)}` +
|
|
122
128
|
`:${encodeURIComponent(opts.password)}` +
|
|
123
|
-
`@${name}:${port}/${encodeURIComponent(opts.database)}
|
|
129
|
+
`@${name}:${port}/${encodeURIComponent(opts.database)}` + (tls ? "?sslmode=verify-full" : "");
|
|
124
130
|
return { client: new SQL(url, { label: name }) };
|
|
125
131
|
},
|
|
126
132
|
// The user's hook, forwarded verbatim — postgres() declares none of
|
|
@@ -63,6 +63,8 @@ export interface MailTemplateSpec {
|
|
|
63
63
|
export interface MappedSupabaseConfig {
|
|
64
64
|
/** GoTrue (`<name>-auth`) environment. */
|
|
65
65
|
auth: Record<string, string>;
|
|
66
|
+
/** GoTrue variables whose values come from the component's env option. */
|
|
67
|
+
deferredAuth: Record<string, { from: string; key: string }>;
|
|
66
68
|
/** PostgREST (`<name>-rest`) environment. */
|
|
67
69
|
rest: Record<string, string>;
|
|
68
70
|
/** Per-function JWT verification, by function name (`functions.<name>.verify_jwt`). */
|
|
@@ -299,8 +301,14 @@ const RULES: Rule[] = [
|
|
|
299
301
|
const prefix = path[2]!.toUpperCase();
|
|
300
302
|
const field = path[3];
|
|
301
303
|
if (field === "enabled") out.auth[`GOTRUE_HOOK_${prefix}_ENABLED`] = bool(v);
|
|
302
|
-
else if (field === "uri"
|
|
303
|
-
|
|
304
|
+
else if (field === "uri" || field === "secrets") {
|
|
305
|
+
const target = `GOTRUE_HOOK_${prefix}_${field.toUpperCase()}`;
|
|
306
|
+
const value = configValue(v);
|
|
307
|
+
if (value.literal !== undefined) out.auth[target] = value.literal;
|
|
308
|
+
else if (value.from !== undefined) {
|
|
309
|
+
out.deferredAuth[target] = { from: value.from, key: path.join(".") };
|
|
310
|
+
} else if (value.encrypted) out.encryptedValues.push(path.join("."));
|
|
311
|
+
}
|
|
304
312
|
},
|
|
305
313
|
},
|
|
306
314
|
|
|
@@ -480,6 +488,7 @@ function isOff(v: unknown): boolean {
|
|
|
480
488
|
export function mapSupabaseConfig(doc: unknown): MappedSupabaseConfig {
|
|
481
489
|
const out: MappedSupabaseConfig = {
|
|
482
490
|
auth: {},
|
|
491
|
+
deferredAuth: {},
|
|
483
492
|
rest: {},
|
|
484
493
|
functionVerifyJwt: {},
|
|
485
494
|
functionSecrets: {},
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Opt-in against a disposable supabase/postgres container. The setup changes
|
|
2
|
+
// its postgres database; never point SPECTEST_TEST_SUPABASE_CONTAINER at app data.
|
|
3
|
+
import { describe, expect, test } from "bun:test";
|
|
4
|
+
import { supabase, type SupabaseOptions } from "./supabase.js";
|
|
5
|
+
import type { ExecOpts, ServiceDefinition, ServiceSetupContext } from "../index.js";
|
|
6
|
+
|
|
7
|
+
const container = process.env.SPECTEST_TEST_SUPABASE_CONTAINER;
|
|
8
|
+
|
|
9
|
+
async function exec(_service: string, command: string[], opts?: ExecOpts) {
|
|
10
|
+
const child = Bun.spawn(["sudo", "docker", "exec", "-i", container!, ...command], {
|
|
11
|
+
stdin: new Blob([opts?.stdin ?? ""]), stdout: "pipe", stderr: "pipe",
|
|
12
|
+
});
|
|
13
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
14
|
+
new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited,
|
|
15
|
+
]);
|
|
16
|
+
return { stdout, stderr, exitCode };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function setup(options: SupabaseOptions = {}) {
|
|
20
|
+
const stack = supabase(options);
|
|
21
|
+
const parts = stack.group.services({ name: "supabase", key: (part) => `supabase-${part}` });
|
|
22
|
+
const db = parts.db as ServiceDefinition;
|
|
23
|
+
await db.setup!({ name: "supabase-db", exec } as unknown as ServiceSetupContext);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function sql(text: string) {
|
|
27
|
+
const result = await exec("supabase-db", ["psql", "-U", "postgres", "-d", "postgres", "-v", "ON_ERROR_STOP=1", "-At", "-f", "-"], { stdin: text });
|
|
28
|
+
if (result.exitCode !== 0) throw new Error(result.stderr);
|
|
29
|
+
return result.stdout;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe.skipIf(!container)("Supabase database compatibility", () => {
|
|
33
|
+
test("bootstrap is repeatable and creates real webhook objects with working grants", async () => {
|
|
34
|
+
await setup();
|
|
35
|
+
await setup();
|
|
36
|
+
const output = await sql(`
|
|
37
|
+
BEGIN;
|
|
38
|
+
CREATE TABLE public.sdk_webhook_probe (value text);
|
|
39
|
+
CREATE TRIGGER sdk_probe AFTER INSERT ON public.sdk_webhook_probe
|
|
40
|
+
FOR EACH ROW EXECUTE FUNCTION supabase_functions.http_request(
|
|
41
|
+
'http://127.0.0.1:1/webhook', 'POST', '{"Content-Type":"application/json"}', '{}', '1000');
|
|
42
|
+
INSERT INTO public.sdk_webhook_probe VALUES ('delivered');
|
|
43
|
+
SELECT convert_from(body, 'UTF8')::jsonb->'record'->>'value' FROM net.http_request_queue
|
|
44
|
+
WHERE id = (SELECT request_id FROM supabase_functions.hooks WHERE hook_name = 'sdk_probe');
|
|
45
|
+
SELECT setval('supabase_functions.hooks_id_seq', 42, false);
|
|
46
|
+
ROLLBACK;`);
|
|
47
|
+
expect(output).toContain("delivered");
|
|
48
|
+
expect(output).toContain("42");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("explicit image-default vector selection supports a historical IF NOT EXISTS pin", async () => {
|
|
52
|
+
await setup({ db: { extensions: { vector: { schema: "public" } } } });
|
|
53
|
+
const output = await sql(`
|
|
54
|
+
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public VERSION '0.7.0';
|
|
55
|
+
SELECT '[1,2,3]'::vector <-> '[1,2,3]'::vector;`);
|
|
56
|
+
expect(output.trim().split("\n").at(-1)).toBe("0");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("an existing extension cannot silently ignore an explicitly requested version", async () => {
|
|
60
|
+
await setup({ db: { extensions: { vector: { schema: "public" } } } });
|
|
61
|
+
await expect(setup({ db: { extensions: { vector: { version: "0.0.0-missing" } } } }))
|
|
62
|
+
.rejects.toThrow(/does not match the requested schema\/version/);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -93,6 +93,77 @@ function expand(stack: ReturnType<typeof supabase>, name = "supabase"): Services
|
|
|
93
93
|
const svc = (parts: ServicesMap, key: string): ServiceConfig => parts[key] as ServiceConfig;
|
|
94
94
|
|
|
95
95
|
describe("supabase({ dir })", () => {
|
|
96
|
+
test("overrides hook addresses without moving migrations or leaking env references", () => {
|
|
97
|
+
const stack = supabase({
|
|
98
|
+
dir: "supabase",
|
|
99
|
+
configOverrides: {
|
|
100
|
+
auth: { hook: { before_user_created: {
|
|
101
|
+
uri: "https://app.test/api/auth/hooks/before-user-created",
|
|
102
|
+
secrets: "env(AUTH_HOOK_SECRET)",
|
|
103
|
+
} } },
|
|
104
|
+
},
|
|
105
|
+
env: { AUTH_HOOK_SECRET: "v1,whsec_test" },
|
|
106
|
+
});
|
|
107
|
+
const parts = expand(stack);
|
|
108
|
+
expect(svc(parts, "auth").env).toMatchObject({
|
|
109
|
+
GOTRUE_HOOK_BEFORE_USER_CREATED_ENABLED: "true",
|
|
110
|
+
GOTRUE_HOOK_BEFORE_USER_CREATED_URI: "https://app.test/api/auth/hooks/before-user-created",
|
|
111
|
+
GOTRUE_HOOK_BEFORE_USER_CREATED_SECRETS: "v1,whsec_test",
|
|
112
|
+
GOTRUE_MAILER_AUTOCONFIRM: "false",
|
|
113
|
+
});
|
|
114
|
+
expect(parts.functions).toBeDefined();
|
|
115
|
+
expect(parts.templates).toBeDefined();
|
|
116
|
+
expect(stack.group.setup).toBeDefined();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("an unresolved HTTP hook secret fails with its config key and env name", () => {
|
|
120
|
+
expect(() => supabase({
|
|
121
|
+
configOverrides: { auth: { hook: { custom_access_token: {
|
|
122
|
+
enabled: true, uri: "https://app.test/hook", secrets: "env(HOOK_SECRET)",
|
|
123
|
+
} } } },
|
|
124
|
+
})).toThrow(/auth.hook.custom_access_token.secrets.*HOOK_SECRET/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("disabled hooks need no secret and never forward a reference", () => {
|
|
128
|
+
const parts = expand(supabase({
|
|
129
|
+
configOverrides: { auth: { hook: { custom_access_token: {
|
|
130
|
+
enabled: false, secrets: "env(HOOK_SECRET)",
|
|
131
|
+
} } } },
|
|
132
|
+
}));
|
|
133
|
+
expect(svc(parts, "auth").env!.GOTRUE_HOOK_CUSTOM_ACCESS_TOKEN_SECRETS).toBeUndefined();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("encrypted hook values fail before GoTrue boots", () => {
|
|
137
|
+
expect(() => supabase({
|
|
138
|
+
configOverrides: { auth: { hook: { custom_access_token: {
|
|
139
|
+
enabled: true, secrets: "encrypted:not-a-test-secret",
|
|
140
|
+
} } } },
|
|
141
|
+
})).toThrow(/auth.hook.custom_access_token.secrets is encrypted/);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("auth disabled does not require an unused hook secret", () => {
|
|
145
|
+
const parts = expand(supabase({
|
|
146
|
+
auth: false,
|
|
147
|
+
configOverrides: { auth: { hook: { custom_access_token: {
|
|
148
|
+
enabled: true, secrets: "env(HOOK_SECRET)",
|
|
149
|
+
} } } },
|
|
150
|
+
}));
|
|
151
|
+
expect(parts.auth).toBeUndefined();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("overrides replace arrays while preserving sibling settings", () => {
|
|
155
|
+
const parts = expand(supabase({
|
|
156
|
+
dir: "supabase", configOverrides: { api: { schemas: ["public"] } },
|
|
157
|
+
}));
|
|
158
|
+
expect(svc(parts, "rest").env!.PGRST_DB_SCHEMAS).toBe("public");
|
|
159
|
+
expect(svc(parts, "auth").env!.GOTRUE_MAILER_AUTOCONFIRM).toBe("false");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("can select a database image with the project's extension versions", () => {
|
|
163
|
+
const parts = expand(supabase({ db: { image: "supabase/postgres:custom" } }));
|
|
164
|
+
expect(svc(parts, "db").image).toEqual({ type: "registry", reference: "supabase/postgres:custom" });
|
|
165
|
+
});
|
|
166
|
+
|
|
96
167
|
test("sends each config setting to the service that owns it", () => {
|
|
97
168
|
const parts = expand(supabase({ dir: "supabase" }));
|
|
98
169
|
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// Supabase CLI database-webhook bootstrap, pinned at 9266dce406db8266d3c6f82839619330aab773c2.
|
|
2
|
+
// https://github.com/supabase/cli/blob/9266dce406db8266d3c6f82839619330aab773c2/apps/cli-go/internal/db/start/templates/webhook.sql
|
|
3
|
+
/*
|
|
4
|
+
MIT License
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2021 Supabase, Inc. and contributors
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export const SUPABASE_WEBHOOKS_SQL = String.raw`
|
|
28
|
+
SELECT to_regprocedure('supabase_functions.http_request()') IS NULL AS install_webhooks \gset
|
|
29
|
+
\if :install_webhooks
|
|
30
|
+
BEGIN;
|
|
31
|
+
|
|
32
|
+
-- Create pg_net extension
|
|
33
|
+
CREATE EXTENSION IF NOT EXISTS pg_net SCHEMA extensions;
|
|
34
|
+
|
|
35
|
+
-- Create supabase_functions schema
|
|
36
|
+
CREATE SCHEMA supabase_functions AUTHORIZATION supabase_admin;
|
|
37
|
+
|
|
38
|
+
GRANT USAGE ON SCHEMA supabase_functions TO postgres, anon, authenticated, service_role;
|
|
39
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT ALL ON TABLES TO postgres, anon, authenticated, service_role;
|
|
40
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT ALL ON FUNCTIONS TO postgres, anon, authenticated, service_role;
|
|
41
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT ALL ON SEQUENCES TO postgres, anon, authenticated, service_role;
|
|
42
|
+
|
|
43
|
+
-- supabase_functions.migrations definition
|
|
44
|
+
CREATE TABLE supabase_functions.migrations (
|
|
45
|
+
version text PRIMARY KEY,
|
|
46
|
+
inserted_at timestamptz NOT NULL DEFAULT NOW()
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
-- Initial supabase_functions migration
|
|
50
|
+
INSERT INTO supabase_functions.migrations (version) VALUES ('initial');
|
|
51
|
+
|
|
52
|
+
-- supabase_functions.hooks definition
|
|
53
|
+
CREATE TABLE supabase_functions.hooks (
|
|
54
|
+
id bigserial PRIMARY KEY,
|
|
55
|
+
hook_table_id integer NOT NULL,
|
|
56
|
+
hook_name text NOT NULL,
|
|
57
|
+
created_at timestamptz NOT NULL DEFAULT NOW(),
|
|
58
|
+
request_id bigint
|
|
59
|
+
);
|
|
60
|
+
CREATE INDEX supabase_functions_hooks_request_id_idx ON supabase_functions.hooks USING btree (request_id);
|
|
61
|
+
CREATE INDEX supabase_functions_hooks_h_table_id_h_name_idx ON supabase_functions.hooks USING btree (hook_table_id, hook_name);
|
|
62
|
+
COMMENT ON TABLE supabase_functions.hooks IS 'Supabase Functions Hooks: Audit trail for triggered hooks.';
|
|
63
|
+
|
|
64
|
+
CREATE FUNCTION supabase_functions.http_request()
|
|
65
|
+
RETURNS trigger
|
|
66
|
+
LANGUAGE plpgsql
|
|
67
|
+
AS $function$
|
|
68
|
+
DECLARE
|
|
69
|
+
request_id bigint;
|
|
70
|
+
payload jsonb;
|
|
71
|
+
url text := TG_ARGV[0]::text;
|
|
72
|
+
method text := TG_ARGV[1]::text;
|
|
73
|
+
headers jsonb DEFAULT '{}'::jsonb;
|
|
74
|
+
params jsonb DEFAULT '{}'::jsonb;
|
|
75
|
+
timeout_ms integer DEFAULT 1000;
|
|
76
|
+
BEGIN
|
|
77
|
+
IF url IS NULL OR url = 'null' THEN
|
|
78
|
+
RAISE EXCEPTION 'url argument is missing';
|
|
79
|
+
END IF;
|
|
80
|
+
|
|
81
|
+
IF method IS NULL OR method = 'null' THEN
|
|
82
|
+
RAISE EXCEPTION 'method argument is missing';
|
|
83
|
+
END IF;
|
|
84
|
+
|
|
85
|
+
IF TG_ARGV[2] IS NULL OR TG_ARGV[2] = 'null' THEN
|
|
86
|
+
headers = '{"Content-Type": "application/json"}'::jsonb;
|
|
87
|
+
ELSE
|
|
88
|
+
headers = TG_ARGV[2]::jsonb;
|
|
89
|
+
END IF;
|
|
90
|
+
|
|
91
|
+
IF TG_ARGV[3] IS NULL OR TG_ARGV[3] = 'null' THEN
|
|
92
|
+
params = '{}'::jsonb;
|
|
93
|
+
ELSE
|
|
94
|
+
params = TG_ARGV[3]::jsonb;
|
|
95
|
+
END IF;
|
|
96
|
+
|
|
97
|
+
IF TG_ARGV[4] IS NULL OR TG_ARGV[4] = 'null' THEN
|
|
98
|
+
timeout_ms = 1000;
|
|
99
|
+
ELSE
|
|
100
|
+
timeout_ms = TG_ARGV[4]::integer;
|
|
101
|
+
END IF;
|
|
102
|
+
|
|
103
|
+
CASE
|
|
104
|
+
WHEN method = 'GET' THEN
|
|
105
|
+
SELECT http_get INTO request_id FROM net.http_get(
|
|
106
|
+
url,
|
|
107
|
+
params,
|
|
108
|
+
headers,
|
|
109
|
+
timeout_ms
|
|
110
|
+
);
|
|
111
|
+
WHEN method = 'POST' THEN
|
|
112
|
+
payload = jsonb_build_object(
|
|
113
|
+
'old_record', OLD,
|
|
114
|
+
'record', NEW,
|
|
115
|
+
'type', TG_OP,
|
|
116
|
+
'table', TG_TABLE_NAME,
|
|
117
|
+
'schema', TG_TABLE_SCHEMA
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
SELECT http_post INTO request_id FROM net.http_post(
|
|
121
|
+
url,
|
|
122
|
+
payload,
|
|
123
|
+
params,
|
|
124
|
+
headers,
|
|
125
|
+
timeout_ms
|
|
126
|
+
);
|
|
127
|
+
ELSE
|
|
128
|
+
RAISE EXCEPTION 'method argument % is invalid', method;
|
|
129
|
+
END CASE;
|
|
130
|
+
|
|
131
|
+
INSERT INTO supabase_functions.hooks
|
|
132
|
+
(hook_table_id, hook_name, request_id)
|
|
133
|
+
VALUES
|
|
134
|
+
(TG_RELID, TG_NAME, request_id);
|
|
135
|
+
|
|
136
|
+
RETURN NEW;
|
|
137
|
+
END
|
|
138
|
+
$function$;
|
|
139
|
+
|
|
140
|
+
-- Supabase super admin
|
|
141
|
+
DO
|
|
142
|
+
$$
|
|
143
|
+
BEGIN
|
|
144
|
+
IF NOT EXISTS (
|
|
145
|
+
SELECT 1
|
|
146
|
+
FROM pg_roles
|
|
147
|
+
WHERE rolname = 'supabase_functions_admin'
|
|
148
|
+
)
|
|
149
|
+
THEN
|
|
150
|
+
CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
|
|
151
|
+
END IF;
|
|
152
|
+
END
|
|
153
|
+
$$;
|
|
154
|
+
|
|
155
|
+
GRANT ALL PRIVILEGES ON SCHEMA supabase_functions TO supabase_functions_admin;
|
|
156
|
+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA supabase_functions TO supabase_functions_admin;
|
|
157
|
+
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA supabase_functions TO supabase_functions_admin;
|
|
158
|
+
ALTER USER supabase_functions_admin SET search_path = "supabase_functions";
|
|
159
|
+
ALTER table "supabase_functions".migrations OWNER TO supabase_functions_admin;
|
|
160
|
+
ALTER table "supabase_functions".hooks OWNER TO supabase_functions_admin;
|
|
161
|
+
ALTER function "supabase_functions".http_request() OWNER TO supabase_functions_admin;
|
|
162
|
+
GRANT supabase_functions_admin TO postgres;
|
|
163
|
+
|
|
164
|
+
-- Remove unused supabase_pg_net_admin role
|
|
165
|
+
DO
|
|
166
|
+
$$
|
|
167
|
+
BEGIN
|
|
168
|
+
IF EXISTS (
|
|
169
|
+
SELECT 1
|
|
170
|
+
FROM pg_roles
|
|
171
|
+
WHERE rolname = 'supabase_pg_net_admin'
|
|
172
|
+
)
|
|
173
|
+
THEN
|
|
174
|
+
REASSIGN OWNED BY supabase_pg_net_admin TO supabase_admin;
|
|
175
|
+
DROP OWNED BY supabase_pg_net_admin;
|
|
176
|
+
DROP ROLE supabase_pg_net_admin;
|
|
177
|
+
END IF;
|
|
178
|
+
END
|
|
179
|
+
$$;
|
|
180
|
+
|
|
181
|
+
-- pg_net grants when extension is already enabled
|
|
182
|
+
DO
|
|
183
|
+
$$
|
|
184
|
+
BEGIN
|
|
185
|
+
IF EXISTS (
|
|
186
|
+
SELECT 1
|
|
187
|
+
FROM pg_extension
|
|
188
|
+
WHERE extname = 'pg_net'
|
|
189
|
+
)
|
|
190
|
+
THEN
|
|
191
|
+
GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;
|
|
192
|
+
|
|
193
|
+
ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
|
|
194
|
+
ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
|
|
195
|
+
|
|
196
|
+
ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
|
|
197
|
+
ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
|
|
198
|
+
|
|
199
|
+
REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
|
|
200
|
+
REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
|
|
201
|
+
|
|
202
|
+
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;
|
|
203
|
+
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;
|
|
204
|
+
END IF;
|
|
205
|
+
END
|
|
206
|
+
$$;
|
|
207
|
+
|
|
208
|
+
-- Event trigger for pg_net
|
|
209
|
+
CREATE OR REPLACE FUNCTION extensions.grant_pg_net_access()
|
|
210
|
+
RETURNS event_trigger
|
|
211
|
+
LANGUAGE plpgsql
|
|
212
|
+
AS $$
|
|
213
|
+
BEGIN
|
|
214
|
+
IF EXISTS (
|
|
215
|
+
SELECT 1
|
|
216
|
+
FROM pg_event_trigger_ddl_commands() AS ev
|
|
217
|
+
JOIN pg_extension AS ext
|
|
218
|
+
ON ev.objid = ext.oid
|
|
219
|
+
WHERE ext.extname = 'pg_net'
|
|
220
|
+
)
|
|
221
|
+
THEN
|
|
222
|
+
GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;
|
|
223
|
+
|
|
224
|
+
ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
|
|
225
|
+
ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
|
|
226
|
+
|
|
227
|
+
ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
|
|
228
|
+
ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
|
|
229
|
+
|
|
230
|
+
REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
|
|
231
|
+
REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
|
|
232
|
+
|
|
233
|
+
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;
|
|
234
|
+
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;
|
|
235
|
+
END IF;
|
|
236
|
+
END;
|
|
237
|
+
$$;
|
|
238
|
+
COMMENT ON FUNCTION extensions.grant_pg_net_access IS 'Grants access to pg_net';
|
|
239
|
+
|
|
240
|
+
DO
|
|
241
|
+
$$
|
|
242
|
+
BEGIN
|
|
243
|
+
IF NOT EXISTS (
|
|
244
|
+
SELECT 1
|
|
245
|
+
FROM pg_event_trigger
|
|
246
|
+
WHERE evtname = 'issue_pg_net_access'
|
|
247
|
+
) THEN
|
|
248
|
+
CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end WHEN TAG IN ('CREATE EXTENSION')
|
|
249
|
+
EXECUTE PROCEDURE extensions.grant_pg_net_access();
|
|
250
|
+
END IF;
|
|
251
|
+
END
|
|
252
|
+
$$;
|
|
253
|
+
|
|
254
|
+
INSERT INTO supabase_functions.migrations (version) VALUES ('20210809183423_update_grants');
|
|
255
|
+
|
|
256
|
+
ALTER function supabase_functions.http_request() SECURITY DEFINER;
|
|
257
|
+
ALTER function supabase_functions.http_request() SET search_path = supabase_functions;
|
|
258
|
+
REVOKE ALL ON FUNCTION supabase_functions.http_request() FROM PUBLIC;
|
|
259
|
+
GRANT EXECUTE ON FUNCTION supabase_functions.http_request() TO postgres, anon, authenticated, service_role;
|
|
260
|
+
|
|
261
|
+
COMMIT;
|
|
262
|
+
\endif
|
|
263
|
+
`;
|