Skip to content

Commit 73522b6

Browse files
Simplify table marking utility methods.
1 parent 8bec2aa commit 73522b6

File tree

13 files changed

+76
-94
lines changed

13 files changed

+76
-94
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
--
22
-- accounts
33
--
4-
CALL diffix.mark_personal('public', 'accounts', 'account_id');
4+
CALL diffix.mark_personal('accounts', 'account_id');
55
ALTER TABLE accounts ADD CONSTRAINT accounts_pkey PRIMARY KEY (account_id);
66

77
--
88
-- accounts_receivables
99
--
10-
CALL diffix.mark_personal('public', 'accounts_receivables', 'customerid');
10+
CALL diffix.mark_personal('accounts_receivables', 'customerid');
1111

1212
--
1313
-- credit_cards
1414
--
15-
CALL diffix.mark_personal('public', 'credit_cards', 'disp_id');
15+
CALL diffix.mark_personal('credit_cards', 'disp_id');
1616

1717
--
1818
-- clients
1919
--
20-
CALL diffix.mark_personal('public', 'clients', 'client_id');
20+
CALL diffix.mark_personal('clients', 'client_id');
2121

2222
ALTER TABLE clients ADD CONSTRAINT clients_pkey PRIMARY KEY (client_id);
2323

2424
--
2525
-- dispositions
2626
--
27-
CALL diffix.mark_personal('public', 'dispositions', 'client_id', 'account_id');
27+
CALL diffix.mark_personal('dispositions', 'client_id', 'account_id');
2828

2929
ALTER TABLE dispositions ADD CONSTRAINT dispositions_pkey PRIMARY KEY (disp_id);
3030

3131
--
3232
-- loans
3333
--
34-
CALL diffix.mark_personal('public', 'loans', 'account_id');
34+
CALL diffix.mark_personal('loans', 'account_id');
3535

3636
ALTER TABLE loans ADD CONSTRAINT loans_pkey PRIMARY KEY (loan_id);
3737

3838
--
3939
-- loss_events
4040
--
41-
SECURITY LABEL FOR pg_diffix ON TABLE loss_events IS 'public';
41+
CALL diffix.mark_public('loss_events');
4242

4343
--
4444
-- orders
4545
--
46-
CALL diffix.mark_personal('public', 'orders', 'account_id', 'account_to');
46+
CALL diffix.mark_personal('orders', 'account_id', 'account_to');
4747

4848
ALTER TABLE orders ADD CONSTRAINT orders_pkey PRIMARY KEY (order_id);
4949

5050
--
5151
-- transactions
5252
--
53-
CALL diffix.mark_personal('public', 'transactions', 'account_id');
53+
CALL diffix.mark_personal('transactions', 'account_id');
5454

5555
ALTER TABLE transactions ADD CONSTRAINT transactions_pkey PRIMARY KEY (trans_id);

docs/user_guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Tables may have one of two security labels, `public` or `personal`.
5959
* Tables labeled as `personal` are anonymized by the extension (for `anonymized_trusted` and `anonymized_untrusted` users, not `direct` users).
6060
* Tables labeled as `public` are not anonymized: all users have full access to these tables.
6161

62-
The procedure `diffix.mark_public(namespace, table_name)` labels a table as `public`.
62+
The procedure `diffix.mark_public(table_name)` labels a table as `public`.
6363

6464
Note that unlabeled tables can not be queried by `anonymized_trusted` and `anonymized_untrusted` users (unless the setting variable `pg_diffix.treat_unmarked_tables_as_public` is set to `true`).
6565

@@ -71,18 +71,18 @@ Each protected entity must have at least one column that contains the identifier
7171

7272
__NOTE:__ if AID columns are not correctly labeled, the extension may fail to anonymize appropriately.
7373

74-
The procedure `diffix.mark_personal(namespace, table_name, aid_columns...)` is used to label a table as personal and to label its AID columns.
74+
The procedure `diffix.mark_personal(table_name, aid_columns...)` is used to label a table as personal and to label its AID columns.
7575

7676
For example,
7777

7878
```SQL
79-
CALL diffix.mark_personal('public', 'employee_info', 'employee_id');
79+
CALL diffix.mark_personal('employee_info', 'employee_id');
8080
```
8181

8282
labels the table `employee_info` as personal, and labels the `employee_id` column as `employee_id` an AID column.
8383

8484
```SQL
85-
CALL diffix.mark_personal('public', 'transactions', 'sender_acct', 'receiver_acct');
85+
CALL diffix.mark_personal('transactions', 'sender_acct', 'receiver_acct');
8686
```
8787

8888
labels the table `transactions` as personal, and labels the `sender_acct` and `receiver_acct` columns as AID columns.

pg_diffix--0.0.1.sql

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,29 @@ AS $$
3939
$$
4040
SECURITY INVOKER SET search_path = '';
4141

42-
CREATE PROCEDURE mark_personal(table_namespace text, table_name text, variadic aid_columns text[])
42+
CREATE PROCEDURE mark_personal(table_name text, variadic aid_columns text[])
4343
AS $$
4444
DECLARE
45-
table_oid integer := (SELECT pg_class.oid
46-
FROM pg_class, pg_namespace
47-
WHERE pg_class.relnamespace = pg_namespace.oid AND relname = table_name AND nspname = table_namespace);
48-
table_fullname text := quote_ident(table_namespace) || '.' || quote_ident(table_name);
4945
aid_column text;
5046
BEGIN
51-
IF (SELECT @[email protected]_level()) <> 'direct' THEN
52-
RAISE EXCEPTION '"mark_personal" requires direct access mode.';
53-
END IF;
47+
DELETE FROM pg_catalog.pg_seclabel WHERE provider = 'pg_diffix' AND objoid = table_name::regclass::oid AND label = 'aid';
5448

55-
DELETE FROM pg_catalog.pg_seclabel WHERE provider = 'pg_diffix' AND objoid = table_oid AND label = 'aid';
56-
57-
EXECUTE 'SECURITY LABEL FOR pg_diffix ON TABLE ' || table_fullname || ' IS ''personal''';
49+
EXECUTE 'SECURITY LABEL FOR pg_diffix ON TABLE ' || table_name || ' IS ''personal''';
5850

5951
FOREACH aid_column IN ARRAY aid_columns LOOP
60-
EXECUTE 'SECURITY LABEL FOR pg_diffix ON COLUMN ' || table_fullname || '.' || quote_ident(aid_column) || ' IS ''aid''';
52+
EXECUTE 'SECURITY LABEL FOR pg_diffix ON COLUMN ' || table_name || '.' || aid_column || ' IS ''aid''';
6153
END LOOP;
6254
END;
63-
$$ LANGUAGE plpgsql
64-
SECURITY INVOKER SET search_path = '';
55+
$$ LANGUAGE plpgsql;
6556

66-
CREATE PROCEDURE mark_public(table_namespace text, table_name text)
57+
CREATE PROCEDURE mark_public(table_name text)
6758
AS $$
68-
DECLARE
69-
table_oid integer := (SELECT pg_class.oid
70-
FROM pg_class, pg_namespace
71-
WHERE pg_class.relnamespace = pg_namespace.oid AND relname = table_name AND nspname = table_namespace);
7259
BEGIN
73-
DELETE FROM pg_catalog.pg_seclabel WHERE provider = 'pg_diffix' AND objoid = table_oid AND label = 'aid';
60+
DELETE FROM pg_catalog.pg_seclabel WHERE provider = 'pg_diffix' AND objoid = table_name::regclass::oid AND label = 'aid';
7461

75-
EXECUTE 'SECURITY LABEL FOR pg_diffix ON TABLE '
76-
|| quote_ident(table_namespace)
77-
|| '.'
78-
|| quote_ident(table_name)
79-
|| ' IS ''public''';
62+
EXECUTE 'SECURITY LABEL FOR pg_diffix ON TABLE ' || table_name || ' IS ''public''';
8063
END;
81-
$$ LANGUAGE plpgsql
82-
SECURITY INVOKER SET search_path = '';
64+
$$ LANGUAGE plpgsql;
8365

8466
/* ----------------------------------------------------------------
8567
* Common aggregation interface

test/expected/_setup.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ INSERT INTO test_patients VALUES
2424
CREATE TABLE empty_test_customers (id INTEGER PRIMARY KEY, name TEXT, city TEXT, discount REAL);
2525
-- Pre-filtered table to maintain LCF tests which relied on WHERE clause.
2626
CREATE TABLE london_customers AS (SELECT * FROM test_customers WHERE city = 'London');
27-
-- Config tables.
28-
CALL diffix.mark_personal('public', 'test_customers', 'id');
29-
CALL diffix.mark_personal('public', 'test_purchases', 'cid');
30-
CALL diffix.mark_personal('public', 'test_patients', 'id', 'name');
31-
CALL diffix.mark_personal('public', 'empty_test_customers', 'id');
32-
CALL diffix.mark_personal('public', 'london_customers', 'id');
33-
CALL diffix.mark_public('public', 'test_products');
27+
-- Config tables (and also check handling of namespaces).
28+
CALL diffix.mark_personal('public.test_customers', 'id');
29+
CALL diffix.mark_personal('public.test_purchases', 'cid');
30+
CALL diffix.mark_personal('public.test_patients', 'id', 'name');
31+
CALL diffix.mark_personal('public.empty_test_customers', 'id');
32+
CALL diffix.mark_personal('public.london_customers', 'id');
33+
CALL diffix.mark_public('public.test_products');
3434
-- There is no CREATE USER IF NOT EXISTS, we need to wrap and silence the output
3535
DO $$
3636
BEGIN

test/expected/led.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ INSERT INTO led_with_different_titles VALUES (22, 'cs', 'f', 'asst');
4747
CREATE TABLE led_with_star_bucket AS TABLE led_with_victim;
4848
INSERT INTO led_with_star_bucket VALUES
4949
(22, 'biol', 'f', 'asst'), (23, 'chem', 'm', 'asst'), (24, 'biol', 'f', 'prof');
50-
CALL diffix.mark_personal('public', 'led_base', 'id');
51-
CALL diffix.mark_personal('public', 'led_with_victim', 'id');
52-
CALL diffix.mark_personal('public', 'led_with_two_victims', 'id');
53-
CALL diffix.mark_personal('public', 'led_with_three_cs_women', 'id');
54-
CALL diffix.mark_personal('public', 'led_with_different_titles', 'id');
55-
CALL diffix.mark_personal('public', 'led_with_star_bucket', 'id');
50+
CALL diffix.mark_personal('led_base', 'id');
51+
CALL diffix.mark_personal('led_with_victim', 'id');
52+
CALL diffix.mark_personal('led_with_two_victims', 'id');
53+
CALL diffix.mark_personal('led_with_three_cs_women', 'id');
54+
CALL diffix.mark_personal('led_with_different_titles', 'id');
55+
CALL diffix.mark_personal('led_with_star_bucket', 'id');
5656
SET ROLE diffix_test;
5757
SET pg_diffix.session_access_level = 'anonymized_trusted';
5858
----------------------------------------------------------------

test/expected/star_bucket.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ INSERT INTO star_bucket_only VALUES
5353
(3, 'phys', 'm', 'asst'),
5454
(4, 'cs', 'f', 'prof'),
5555
(5, 'history', 'f', 'asst');
56-
CALL diffix.mark_personal('public', 'star_bucket_base', 'id');
57-
CALL diffix.mark_personal('public', 'star_bucket', 'id');
58-
CALL diffix.mark_personal('public', 'star_bucket_suppressed_1', 'id');
59-
CALL diffix.mark_personal('public', 'star_bucket_suppressed_2', 'id');
60-
CALL diffix.mark_personal('public', 'star_bucket_empty', 'id');
61-
CALL diffix.mark_personal('public', 'star_bucket_only', 'id');
56+
CALL diffix.mark_personal('star_bucket_base', 'id');
57+
CALL diffix.mark_personal('star_bucket', 'id');
58+
CALL diffix.mark_personal('star_bucket_suppressed_1', 'id');
59+
CALL diffix.mark_personal('star_bucket_suppressed_2', 'id');
60+
CALL diffix.mark_personal('star_bucket_empty', 'id');
61+
CALL diffix.mark_personal('star_bucket_only', 'id');
6262
SET ROLE diffix_test;
6363
SET pg_diffix.session_access_level = 'anonymized_trusted';
6464
----------------------------------------------------------------

test/expected/stress.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CREATE TABLE test_stress AS (
55
i AS id, left(md5(random()::text), 4) AS t, (random() * 10.0)::real AS r, round(random() * 1000)::integer AS i
66
FROM generate_series(1, 50000) series(i)
77
);
8-
CALL diffix.mark_personal('public', 'test_stress', 'id');
8+
CALL diffix.mark_personal('test_stress', 'id');
99
-- Prepare test session.
1010
SET pg_diffix.noise_layer_sd = 0;
1111
SET pg_diffix.low_count_layer_sd = 0;

test/expected/validation.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ CREATE TABLE test_validation (
88
lunchtime TIME,
99
last_seen TIMESTAMP
1010
);
11-
CALL diffix.mark_personal('public', 'test_validation', 'id');
11+
CALL diffix.mark_personal('test_validation', 'id');
1212
CREATE TABLE superclass (x INTEGER);
1313
CREATE TABLE subclass (x INTEGER, y INTEGER);
1414
INSERT INTO subclass VALUES (1, 2);
15-
CALL diffix.mark_personal('public', 'superclass', 'x');
16-
CALL diffix.mark_personal('public', 'subclass', 'y');
15+
CALL diffix.mark_personal('superclass', 'x');
16+
CALL diffix.mark_personal('subclass', 'y');
1717
ALTER TABLE subclass INHERIT superclass;
1818
-- No-op. Repeated to test the error on conflicting configuration
19-
CALL diffix.mark_personal('public', 'superclass', 'x');
19+
CALL diffix.mark_personal('superclass', 'x');
2020
ERROR: [PG_DIFFIX] Anonymization over tables using inheritance is not supported.
21-
CONTEXT: SQL statement "SECURITY LABEL FOR pg_diffix ON TABLE public.superclass IS 'personal'"
22-
PL/pgSQL function diffix.mark_personal(text,text,text[]) line 15 at EXECUTE
23-
CALL diffix.mark_personal('public', 'subclass', 'y');
21+
CONTEXT: SQL statement "SECURITY LABEL FOR pg_diffix ON TABLE superclass IS 'personal'"
22+
PL/pgSQL function diffix.mark_personal(text,text[]) line 7 at EXECUTE
23+
CALL diffix.mark_personal('subclass', 'y');
2424
ERROR: [PG_DIFFIX] Anonymization over tables using inheritance is not supported.
25-
CONTEXT: SQL statement "SECURITY LABEL FOR pg_diffix ON TABLE public.subclass IS 'personal'"
26-
PL/pgSQL function diffix.mark_personal(text,text,text[]) line 15 at EXECUTE
25+
CONTEXT: SQL statement "SECURITY LABEL FOR pg_diffix ON TABLE subclass IS 'personal'"
26+
PL/pgSQL function diffix.mark_personal(text,text[]) line 7 at EXECUTE
2727
SET ROLE diffix_test;
2828
----------------------------------------------------------------
2929
-- Trusted mode query restrictions

test/sql/_setup.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ CREATE TABLE empty_test_customers (id INTEGER PRIMARY KEY, name TEXT, city TEXT,
3131
-- Pre-filtered table to maintain LCF tests which relied on WHERE clause.
3232
CREATE TABLE london_customers AS (SELECT * FROM test_customers WHERE city = 'London');
3333

34-
-- Config tables.
35-
CALL diffix.mark_personal('public', 'test_customers', 'id');
36-
CALL diffix.mark_personal('public', 'test_purchases', 'cid');
37-
CALL diffix.mark_personal('public', 'test_patients', 'id', 'name');
38-
CALL diffix.mark_personal('public', 'empty_test_customers', 'id');
39-
CALL diffix.mark_personal('public', 'london_customers', 'id');
40-
CALL diffix.mark_public('public', 'test_products');
34+
-- Config tables (and also check handling of namespaces).
35+
CALL diffix.mark_personal('public.test_customers', 'id');
36+
CALL diffix.mark_personal('public.test_purchases', 'cid');
37+
CALL diffix.mark_personal('public.test_patients', 'id', 'name');
38+
CALL diffix.mark_personal('public.empty_test_customers', 'id');
39+
CALL diffix.mark_personal('public.london_customers', 'id');
40+
CALL diffix.mark_public('public.test_products');
4141

4242
-- There is no CREATE USER IF NOT EXISTS, we need to wrap and silence the output
4343
DO $$

test/sql/led.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ CREATE TABLE led_with_star_bucket AS TABLE led_with_victim;
5757
INSERT INTO led_with_star_bucket VALUES
5858
(22, 'biol', 'f', 'asst'), (23, 'chem', 'm', 'asst'), (24, 'biol', 'f', 'prof');
5959

60-
CALL diffix.mark_personal('public', 'led_base', 'id');
61-
CALL diffix.mark_personal('public', 'led_with_victim', 'id');
62-
CALL diffix.mark_personal('public', 'led_with_two_victims', 'id');
63-
CALL diffix.mark_personal('public', 'led_with_three_cs_women', 'id');
64-
CALL diffix.mark_personal('public', 'led_with_different_titles', 'id');
65-
CALL diffix.mark_personal('public', 'led_with_star_bucket', 'id');
60+
CALL diffix.mark_personal('led_base', 'id');
61+
CALL diffix.mark_personal('led_with_victim', 'id');
62+
CALL diffix.mark_personal('led_with_two_victims', 'id');
63+
CALL diffix.mark_personal('led_with_three_cs_women', 'id');
64+
CALL diffix.mark_personal('led_with_different_titles', 'id');
65+
CALL diffix.mark_personal('led_with_star_bucket', 'id');
6666

6767
SET ROLE diffix_test;
6868
SET pg_diffix.session_access_level = 'anonymized_trusted';

0 commit comments

Comments
 (0)