Skip to content

Commit 444baf2

Browse files
authored
fix missing semicolons (supabase#445)
* fix missing semicolons * update migration
1 parent ded929f commit 444baf2

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

ebssurrogate/files/unit-tests/unit-test-01.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ SELECT has_schema('information_schema');
2424
SELECT has_schema('public');
2525

2626
-- Check that service_role can execute certain pgsodium functions
27-
SELECT function_privs_are('pgsodium', 'crypto_aead_det_decrypt', array['bytea', 'bytea', 'uuid', 'bytea'], 'service_role', array['EXECUTE'])
28-
SELECT function_privs_are('pgsodium', 'crypto_aead_det_encrypt', array['bytea', 'bytea', 'uuid', 'bytea'], 'service_role', array['EXECUTE'])
29-
SELECT function_privs_are('pgsodium', 'crypto_aead_det_keygen', array[], 'service_role', array['EXECUTE'])
30-
SELECT function_privs_are('pgsodium', 'crypto_aead_det_noncegen', array[], 'service_role', array['EXECUTE'])
27+
SELECT function_privs_are('pgsodium', 'crypto_aead_det_decrypt', array['bytea', 'bytea', 'uuid', 'bytea'], 'service_role', array['EXECUTE']);
28+
SELECT function_privs_are('pgsodium', 'crypto_aead_det_encrypt', array['bytea', 'bytea', 'uuid', 'bytea'], 'service_role', array['EXECUTE']);
29+
SELECT function_privs_are('pgsodium', 'crypto_aead_det_keygen', array[], 'service_role', array['EXECUTE']);
30+
SELECT function_privs_are('pgsodium', 'crypto_aead_det_noncegen', array[], 'service_role', array['EXECUTE']);
3131

3232
SELECT * from finish();
3333
ROLLBACK;

migrations/db/migrations/20221207154255_create_pgsodium_and_vault.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ grant pgsodium_keyiduser to postgres with admin option;
66
grant pgsodium_keyholder to postgres with admin option;
77
grant pgsodium_keymaker to postgres with admin option;
88

9-
create extension if not exists supabase_vault;
9+
do $$
10+
begin
11+
if not exists (select from pg_extension where extname = 'supabase_vault') then
12+
create extension supabase_vault;
13+
-- Creating the extension creates a table and creates a security label on the table.
14+
-- Creating the security label triggers a function that recreates these objects.
15+
-- Since the recreation happens in an extension script, these objects become owned by the `supabase_vault` extension.
16+
-- This is an issue because then we can't recreate these objects without also dropping the extension.
17+
-- Thus we drop the dependency on the `supabase_vault` extension for these objects.
18+
alter extension supabase_vault drop view pgsodium.decrypted_key;
19+
alter extension supabase_vault drop function pgsodium.key_encrypt_secret;
20+
end if;
21+
end;
22+
$$;
1023

1124
-- migrate:down

migrations/schema.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,25 @@ END;
510510
$$;
511511

512512

513+
--
514+
-- Name: key_encrypt_secret(); Type: FUNCTION; Schema: pgsodium; Owner: -
515+
--
516+
517+
CREATE FUNCTION pgsodium.key_encrypt_secret() RETURNS trigger
518+
LANGUAGE plpgsql
519+
AS $$
520+
BEGIN
521+
new.raw_key = CASE WHEN new.raw_key IS NULL THEN NULL ELSE
522+
CASE WHEN new.parent_key IS NULL THEN NULL ELSE
523+
pgsodium.crypto_aead_det_encrypt(new.raw_key::bytea, pg_catalog.convert_to((new.id::text || new.associated_data::text)::text, 'utf8'),
524+
new.parent_key::uuid,
525+
new.raw_key_nonce
526+
) END END;
527+
RETURN new;
528+
END;
529+
$$;
530+
531+
513532
--
514533
-- Name: extension(text); Type: FUNCTION; Schema: storage; Owner: -
515534
--
@@ -735,6 +754,35 @@ CREATE TABLE auth.users (
735754
COMMENT ON TABLE auth.users IS 'Auth: Stores user login data within a secure schema.';
736755

737756

757+
--
758+
-- Name: decrypted_key; Type: VIEW; Schema: pgsodium; Owner: -
759+
--
760+
761+
CREATE VIEW pgsodium.decrypted_key AS
762+
SELECT key.id,
763+
key.status,
764+
key.created,
765+
key.expires,
766+
key.key_type,
767+
key.key_id,
768+
key.key_context,
769+
key.name,
770+
key.associated_data,
771+
key.raw_key,
772+
CASE
773+
WHEN (key.raw_key IS NULL) THEN NULL::bytea
774+
ELSE
775+
CASE
776+
WHEN (key.parent_key IS NULL) THEN NULL::bytea
777+
ELSE pgsodium.crypto_aead_det_decrypt(key.raw_key, convert_to(((key.id)::text || key.associated_data), 'utf8'::name), key.parent_key, key.raw_key_nonce)
778+
END
779+
END AS decrypted_raw_key,
780+
key.raw_key_nonce,
781+
key.parent_key,
782+
key.comment
783+
FROM pgsodium.key;
784+
785+
738786
--
739787
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
740788
--

0 commit comments

Comments
 (0)