Skip to content
Open
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
22 changes: 20 additions & 2 deletions src/config/config_test.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
\set ON_ERROR_STOP on


-- Create tables for adding configuration
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
id bigint GENERATED ALWAYS AS IDENTITY,
name eql_v2_encrypted,
PRIMARY KEY(id)
);

DROP TABLE IF EXISTS blah;
CREATE TABLE blah
(
id bigint GENERATED ALWAYS AS IDENTITY,
vtha eql_v2_encrypted,
PRIMARY KEY(id)
);


--
-- Helper function for assertions
--
Expand Down Expand Up @@ -90,7 +108,7 @@ DO $$
PERFORM eql_v2.remove_search_config('blah', 'vtha', 'unique', migrating => true);
ASSERT NOT (SELECT _search_config_exists('users', 'vtha', 'unique'));

-- All indexes removed, but column config preserved
-- All indexes removed, but column config preserved
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'pending'));
ASSERT (SELECT data #> array['tables', 'blah', 'vtha', 'indexes'] = '{}' FROM eql_v2_configuration c WHERE c.state = 'pending');

Expand Down Expand Up @@ -222,7 +240,7 @@ DO $$
'Pending configuration exists but is empty',
'SELECT * FROM eql_v2_configuration c WHERE c.state = ''pending''',
1);

-- Verify the config is empty
ASSERT (SELECT data #> array['tables'] = '{}' FROM eql_v2_configuration c WHERE c.state = 'pending');

Expand Down
2 changes: 1 addition & 1 deletion src/config/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ AS $$
PERFORM eql_v2.activate_config();
END IF;

-- PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);
PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);

-- exeunt
RETURN _config;
Expand Down
60 changes: 60 additions & 0 deletions src/encrypted/constraints_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,66 @@ DO $$
$$ LANGUAGE plpgsql;


-- -----------------------------------------------
-- Adding search config adds the constraint
--
-- -----------------------------------------------
TRUNCATE TABLE eql_v2_configuration;

DO $$
BEGIN
-- reset the table
PERFORM create_table_with_encrypted();

PERFORM eql_v2.add_search_config('encrypted', 'e', 'match');

PERFORM assert_exception(
'Constraint catches invalid eql_v2_encrypted',
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted)');

-- add constraint without error
PERFORM eql_v2.add_encrypted_constraint('encrypted', 'e');

PERFORM eql_v2.remove_encrypted_constraint('encrypted', 'e');

PERFORM assert_result(
'Insert invalid data without constraint',
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted) RETURNING id');

END;
$$ LANGUAGE plpgsql;


-- -----------------------------------------------
-- Adding column adds the constraint
--
-- -----------------------------------------------
TRUNCATE TABLE eql_v2_configuration;

DO $$
BEGIN
-- reset the table
PERFORM create_table_with_encrypted();

PERFORM eql_v2.add_column('encrypted', 'e');

PERFORM assert_exception(
'Constraint catches invalid eql_v2_encrypted',
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted)');

-- add constraint without error
PERFORM eql_v2.add_encrypted_constraint('encrypted', 'e');

PERFORM eql_v2.remove_encrypted_constraint('encrypted', 'e');

PERFORM assert_result(
'Insert invalid data without constraint',
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted) RETURNING id');

END;
$$ LANGUAGE plpgsql;


-- EQL version is enforced
DO $$
DECLARE
Expand Down
10 changes: 7 additions & 3 deletions src/encrypted/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ CREATE FUNCTION eql_v2.add_encrypted_constraint(table_name TEXT, column_name TEX
RETURNS void
AS $$
BEGIN
EXECUTE format('ALTER TABLE %I ADD CONSTRAINT eql_v2_encrypted_check_%I CHECK (eql_v2.check_encrypted(%I))', table_name, column_name, column_name);
END;
EXECUTE format('ALTER TABLE %I ADD CONSTRAINT eql_v2_encrypted_constraint_%I_%I CHECK (eql_v2.check_encrypted(%I))', table_name, table_name, column_name, column_name);
EXCEPTION
WHEN duplicate_table THEN
WHEN duplicate_object THEN
RAISE NOTICE 'Constraint `eql_v2_encrypted_constraint_%_%` already exists, skipping', table_name, column_name;
END;
$$ LANGUAGE plpgsql;


Expand All @@ -66,7 +70,7 @@ CREATE FUNCTION eql_v2.remove_encrypted_constraint(table_name TEXT, column_name
RETURNS void
AS $$
BEGIN
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_check_%I', table_name, column_name);
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_constraint_%I_%I', table_name, table_name, column_name);
END;
$$ LANGUAGE plpgsql;

Expand Down
10 changes: 5 additions & 5 deletions src/encryptindex/functions_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ CREATE TABLE users
-- An encrypting config should exist
DO $$
BEGIN
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);
PERFORM eql_v2.migrate_config();
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'encrypting'));
Expand All @@ -167,7 +167,7 @@ $$ LANGUAGE plpgsql;
DO $$
BEGIN
TRUNCATE TABLE eql_v2_configuration;
PERFORM eql_v2.add_search_config('users', 'name', 'match');
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match');
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
END;
$$ LANGUAGE plpgsql;
Expand All @@ -177,7 +177,7 @@ $$ LANGUAGE plpgsql;
DO $$
BEGIN
TRUNCATE TABLE eql_v2_configuration;
PERFORM eql_v2.add_search_config('users', 'name', 'match');
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match');

PERFORM assert_exception(
'eql_v2.migrate_config() should raise an exception when no pending configuration exists',
Expand Down Expand Up @@ -226,7 +226,7 @@ CREATE TABLE users
-- An encrypting config should exist
DO $$
BEGIN
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);
PERFORM eql_v2.migrate_config();

ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
Expand Down Expand Up @@ -276,7 +276,7 @@ CREATE TABLE users
-- An encrypting config should exist
DO $$
BEGIN
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);

PERFORM eql_v2.migrate_config(); -- need to encrypt first
PERFORM eql_v2.activate_config();
Expand Down