Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ else
endif

EXTENSION = supabase_vault
EXTVERSION = 0.3.2
EXTVERSION = 0.3.3

DATA = $(wildcard sql/*--*.sql)

Expand Down
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions nix/plpgsql-check.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

stdenv.mkDerivation rec {
pname = "plpgsql-check";
version = "2.10.15";
version = "6afcb44a5f9ec06de7b74d8f8ebbeb638ac7b945";

src = fetchFromGitHub {
owner = "okbob";
repo = "plpgsql_check";
rev = "v${version}";
hash = "sha256-war+GVHMDegfDXjxdIsUkrPPDntKeyb/bGxibHVbpDo=";
# rev = "v${version}";
rev = "6afcb44a5f9ec06de7b74d8f8ebbeb638ac7b945";

hash = "sha256-AQGJglOiuUC5djHNiJHpzDErfpmqglvs7djxfX8mHEc=";
};

buildInputs = [ postgresql ];
Expand Down
56 changes: 56 additions & 0 deletions sql/supabase_vault--0.3.2--0.3.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
CREATE OR REPLACE FUNCTION vault.create_secret(new_secret text
, new_name text = CAST(NULL AS text)
, new_description text = CAST('' AS text)
, new_key_id uuid = CAST(NULL AS uuid))
RETURNS uuid
LANGUAGE plpgsql SECURITY DEFINER SET search_path TO 'pg_catalog', 'pg_temp'
AS $$
DECLARE
rec record;
BEGIN
INSERT INTO vault.secrets (secret, name, description)
VALUES (
new_secret,
new_name,
new_description
)
RETURNING * INTO rec;
UPDATE vault.secrets s
SET secret = pg_catalog.encode(vault._crypto_aead_det_encrypt(
message := pg_catalog.convert_to(rec.secret, 'utf8'),
additional := pg_catalog.convert_to(s.id::pg_catalog.text, 'utf8'),
key_id := 0,
context := 'pgsodium'::pg_catalog.bytea,
nonce := rec.nonce
), 'base64')
WHERE s.id OPERATOR(pg_catalog.=) rec.id;
RETURN rec.id;
END
$$;


CREATE OR REPLACE FUNCTION vault.update_secret(secret_id uuid
, new_secret text = CAST(NULL AS text)
, new_name text = CAST(NULL AS text)
, new_description text = CAST(NULL AS text)
, new_key_id uuid = CAST(NULL AS uuid))
RETURNS void
LANGUAGE plpgsql SECURITY DEFINER SET search_path TO 'pg_catalog', 'pg_temp'
AS $$
BEGIN
UPDATE vault.secrets s
SET
secret = CASE WHEN new_secret IS NULL THEN s.secret
ELSE pg_catalog.encode(vault._crypto_aead_det_encrypt(
message := pg_catalog.convert_to(new_secret, 'utf8'),
additional := pg_catalog.convert_to(s.id::pg_catalog.text, 'utf8'),
key_id := 0,
context := 'pgsodium'::pg_catalog.bytea,
nonce := s.nonce
), 'base64') END,
name = coalesce(new_name, s.name),
description = coalesce(new_description, s.description),
updated_at = pg_catalog.now()
WHERE s.id OPERATOR(pg_catalog.=) secret_id;
END
$$;
22 changes: 11 additions & 11 deletions supabase_vault.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ BEGIN
)
RETURNING * INTO rec;
UPDATE vault.secrets s
SET secret = encode(vault._crypto_aead_det_encrypt(
message := convert_to(rec.secret, 'utf8'),
additional := convert_to(s.id::text, 'utf8'),
SET secret = pg_catalog.encode(vault._crypto_aead_det_encrypt(
message := pg_catalog.convert_to(rec.secret, 'utf8'),
additional := pg_catalog.convert_to(s.id::pg_catalog.text, 'utf8'),
key_id := 0,
context := 'pgsodium'::bytea,
context := 'pgsodium'::pg_catalog.bytea,
nonce := rec.nonce
), 'base64')
WHERE id = rec.id;
WHERE s.id OPERATOR(pg_catalog.=) rec.id;
RETURN rec.id;
END
$$;
Expand All @@ -75,17 +75,17 @@ BEGIN
UPDATE vault.secrets s
SET
secret = CASE WHEN new_secret IS NULL THEN s.secret
ELSE encode(vault._crypto_aead_det_encrypt(
message := convert_to(new_secret, 'utf8'),
additional := convert_to(s.id::text, 'utf8'),
ELSE pg_catalog.encode(vault._crypto_aead_det_encrypt(
message := pg_catalog.convert_to(new_secret, 'utf8'),
additional := pg_catalog.convert_to(s.id::pg_catalog.text, 'utf8'),
key_id := 0,
context := 'pgsodium'::bytea,
context := 'pgsodium'::pg_catalog.bytea,
nonce := s.nonce
), 'base64') END,
name = coalesce(new_name, s.name),
description = coalesce(new_description, s.description),
updated_at = now()
WHERE s.id = secret_id;
updated_at = pg_catalog.now()
WHERE s.id OPERATOR(pg_catalog.=) secret_id;
END
$$;

Expand Down
5 changes: 3 additions & 2 deletions test/expected/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where name = 'empty_secret';
SELECT extversion FROM pg_extension where extname = 'supabase_vault';
extversion
------------
0.3.2
0.3.3
(1 row)

-- Validate that each security definer function explicitly specifies pg_temp and
Expand Down Expand Up @@ -139,7 +139,8 @@ FROM
SELECT
plpgsql_check_function_tb(pg_proc.oid, COALESCE(pg_trigger.tgrelid, 0),
oldtable=>pg_trigger.tgoldtable,
newtable=>pg_trigger.tgnewtable) AS pcf
newtable=>pg_trigger.tgnewtable,
extra_security_warnings_level=>4) AS pcf
FROM pg_proc
LEFT JOIN pg_trigger
ON (pg_trigger.tgfoid = pg_proc.oid)
Expand Down
3 changes: 2 additions & 1 deletion test/sql/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ FROM
SELECT
plpgsql_check_function_tb(pg_proc.oid, COALESCE(pg_trigger.tgrelid, 0),
oldtable=>pg_trigger.tgoldtable,
newtable=>pg_trigger.tgnewtable) AS pcf
newtable=>pg_trigger.tgnewtable,
extra_security_warnings_level=>4) AS pcf
FROM pg_proc
LEFT JOIN pg_trigger
ON (pg_trigger.tgfoid = pg_proc.oid)
Expand Down
Loading