Skip to content

Commit 5b1e603

Browse files
committed
chore: add safeguards when enabling Vaault
1 parent 7475b39 commit 5b1e603

File tree

2 files changed

+112
-49
lines changed

2 files changed

+112
-49
lines changed

migrations/db/migrations/20221207154255_create_pgsodium_and_vault.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ grant execute on function pgsodium.crypto_aead_det_decrypt(bytea, bytea, uuid, b
1010
grant execute on function pgsodium.crypto_aead_det_encrypt(bytea, bytea, uuid, bytea) to service_role;
1111
grant execute on function pgsodium.crypto_aead_det_keygen to service_role;
1212

13-
create extension if not exists supabase_vault;
13+
-- Only install as well if the extension is actually installed
14+
DO $$
15+
DECLARE
16+
vault_exists boolean;
17+
BEGIN
18+
vault_exists = (
19+
select count(*) = 1
20+
from pg_available_extensions
21+
where name = 'supabase_vault'
22+
);
23+
24+
IF vault_exists
25+
THEN
26+
create extension if not exists supabase_vault;
27+
END IF;
28+
END $$;
29+
30+
1431

1532
-- migrate:down

migrations/schema.sql

Lines changed: 94 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ CREATE SCHEMA realtime;
7878

7979
CREATE SCHEMA storage;
8080

81-
82-
--
83-
-- Name: vault; Type: SCHEMA; Schema: -; Owner: -
84-
--
85-
86-
CREATE SCHEMA vault;
87-
88-
8981
--
9082
-- Name: pg_graphql; Type: EXTENSION; Schema: -; Owner: -
9183
--
@@ -141,19 +133,43 @@ CREATE EXTENSION IF NOT EXISTS pgjwt WITH SCHEMA extensions;
141133

142134
COMMENT ON EXTENSION pgjwt IS 'JSON Web Token API for Postgresql';
143135

144-
145136
--
146-
-- Name: supabase_vault; Type: EXTENSION; Schema: -; Owner: -
147137
--
138+
--
139+
140+
DO $$
141+
DECLARE
142+
vault_exists boolean;
143+
BEGIN
144+
vault_exists = (
145+
select count(*) = 1
146+
from pg_available_extensions
147+
where name = 'supabase_vault'
148+
);
148149

149-
CREATE EXTENSION IF NOT EXISTS supabase_vault WITH SCHEMA vault;
150+
IF vault_exists
151+
THEN
152+
153+
--
154+
-- Name: vault; Type: SCHEMA; Schema: -; Owner: -
155+
--
150156

157+
CREATE SCHEMA vault;
158+
--
159+
-- Name: supabase_vault; Type: EXTENSION; Schema: -; Owner: -
160+
--
151161

152-
--
153-
-- Name: EXTENSION supabase_vault; Type: COMMENT; Schema: -; Owner: -
154-
--
162+
CREATE EXTENSION IF NOT EXISTS supabase_vault WITH SCHEMA vault;
163+
164+
165+
--
166+
-- Name: EXTENSION supabase_vault; Type: COMMENT; Schema: -; Owner: -
167+
--
168+
169+
COMMENT ON EXTENSION supabase_vault IS 'Supabase Vault Extension';
155170

156-
COMMENT ON EXTENSION supabase_vault IS 'Supabase Vault Extension';
171+
END IF;
172+
END $$;
157173

158174

159175
--
@@ -577,23 +593,38 @@ $$;
577593
-- Name: secrets_encrypt_secret_secret(); Type: FUNCTION; Schema: vault; Owner: -
578594
--
579595

580-
CREATE FUNCTION vault.secrets_encrypt_secret_secret() RETURNS trigger
581-
LANGUAGE plpgsql
582-
AS $$
583-
BEGIN
584-
new.secret = CASE WHEN new.secret IS NULL THEN NULL ELSE
585-
CASE WHEN new.key_id IS NULL THEN NULL ELSE pg_catalog.encode(
586-
pgsodium.crypto_aead_det_encrypt(
587-
pg_catalog.convert_to(new.secret, 'utf8'),
588-
pg_catalog.convert_to((new.id::text || new.description::text || new.created_at::text || new.updated_at::text)::text, 'utf8'),
589-
new.key_id::uuid,
590-
new.nonce
591-
),
592-
'base64') END END;
593-
RETURN new;
594-
END;
595-
$$;
596+
DO $$
597+
DECLARE
598+
vault_exists boolean;
599+
BEGIN
600+
vault_exists = (
601+
select count(*) = 1
602+
from pg_available_extensions
603+
where name = 'supabase_vault'
604+
);
605+
606+
IF vault_exists
607+
THEN
608+
609+
CREATE FUNCTION vault.secrets_encrypt_secret_secret() RETURNS trigger
610+
LANGUAGE plpgsql
611+
AS $$
612+
BEGIN
613+
new.secret = CASE WHEN new.secret IS NULL THEN NULL ELSE
614+
CASE WHEN new.key_id IS NULL THEN NULL ELSE pg_catalog.encode(
615+
pgsodium.crypto_aead_det_encrypt(
616+
pg_catalog.convert_to(new.secret, 'utf8'),
617+
pg_catalog.convert_to((new.id::text || new.description::text || new.created_at::text || new.updated_at::text)::text, 'utf8'),
618+
new.key_id::uuid,
619+
new.nonce
620+
),
621+
'base64') END END;
622+
RETURN new;
623+
END;
624+
$$;
596625

626+
END IF;
627+
END $$;
597628

598629
SET default_tablespace = '';
599630

@@ -785,25 +816,40 @@ CREATE TABLE storage.objects (
785816
-- Name: decrypted_secrets; Type: VIEW; Schema: vault; Owner: -
786817
--
787818

788-
CREATE VIEW vault.decrypted_secrets AS
789-
SELECT secrets.id,
790-
secrets.name,
791-
secrets.description,
792-
secrets.secret,
793-
CASE
794-
WHEN (secrets.secret IS NULL) THEN NULL::text
795-
ELSE
819+
DO $$
820+
DECLARE
821+
vault_exists boolean;
822+
BEGIN
823+
vault_exists = (
824+
select count(*) = 1
825+
from pg_available_extensions
826+
where name = 'supabase_vault'
827+
);
828+
829+
IF vault_exists
830+
THEN
831+
832+
CREATE VIEW vault.decrypted_secrets AS
833+
SELECT secrets.id,
834+
secrets.name,
835+
secrets.description,
836+
secrets.secret,
796837
CASE
797-
WHEN (secrets.key_id IS NULL) THEN NULL::text
798-
ELSE convert_from(pgsodium.crypto_aead_det_decrypt(decode(secrets.secret, 'base64'::text), convert_to(((((secrets.id)::text || secrets.description) || (secrets.created_at)::text) || (secrets.updated_at)::text), 'utf8'::name), secrets.key_id, secrets.nonce), 'utf8'::name)
799-
END
800-
END AS decrypted_secret,
801-
secrets.key_id,
802-
secrets.nonce,
803-
secrets.created_at,
804-
secrets.updated_at
805-
FROM vault.secrets;
838+
WHEN (secrets.secret IS NULL) THEN NULL::text
839+
ELSE
840+
CASE
841+
WHEN (secrets.key_id IS NULL) THEN NULL::text
842+
ELSE convert_from(pgsodium.crypto_aead_det_decrypt(decode(secrets.secret, 'base64'::text), convert_to(((((secrets.id)::text || secrets.description) || (secrets.created_at)::text) || (secrets.updated_at)::text), 'utf8'::name), secrets.key_id, secrets.nonce), 'utf8'::name)
843+
END
844+
END AS decrypted_secret,
845+
secrets.key_id,
846+
secrets.nonce,
847+
secrets.created_at,
848+
secrets.updated_at
849+
FROM vault.secrets;
806850

851+
END IF;
852+
END $$;
807853

808854
--
809855
-- Name: refresh_tokens id; Type: DEFAULT; Schema: auth; Owner: -

0 commit comments

Comments
 (0)