Skip to content

Commit 89d518a

Browse files
committed
Cleanup
1 parent 3670cb0 commit 89d518a

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

sql/010-core.sql

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ BEGIN
1414
END
1515
$$;
1616

17+
18+
-- Should include a kind field
19+
DROP FUNCTION IF EXISTS _cs_encrypted_check_k(jsonb);
20+
CREATE FUNCTION _cs_encrypted_check_k(val jsonb)
21+
RETURNS boolean
22+
AS $$
23+
BEGIN
24+
IF (val->>'k' = ANY('{ct, sv}')) THEN
25+
RETURN true;
26+
END IF;
27+
RAISE 'Invalid kind (%) in Encrypted column. Kind should be one of {ct, sv}', val;
28+
END;
29+
$$ LANGUAGE plpgsql;
30+
31+
1732
--
1833
-- CT payload should include a c field
1934
--
@@ -26,12 +41,13 @@ AS $$
2641
IF (val ? 'c') THEN
2742
RETURN true;
2843
END IF;
29-
RAISE 'Encrypted kind (k) of "ct" missing data field (c): %', val;
44+
RAISE 'Encrypted column kind (k) of "ct" missing data field (c): %', val;
3045
END IF;
3146
RETURN true;
3247
END;
3348
$$ LANGUAGE plpgsql;
3449

50+
3551
--
3652
-- SV payload should include an sv field
3753
--
@@ -44,7 +60,7 @@ AS $$
4460
IF (val ? 'sv') THEN
4561
RETURN true;
4662
END IF;
47-
RAISE 'Encrypted kind (k) of "sv" missing data field (sv): %', val;
63+
RAISE 'Encrypted column kind (k) of "sv" missing data field (sv): %', val;
4864
END IF;
4965
RETURN true;
5066
END;
@@ -60,7 +76,7 @@ AS $$
6076
IF NOT val ? 'p' THEN
6177
RETURN true;
6278
END IF;
63-
RAISE 'Encrypted includes plaintext (p) field: %', val;
79+
RAISE 'Encrypted column includes plaintext (p) field: %', val;
6480
END;
6581
$$ LANGUAGE plpgsql;
6682

@@ -73,7 +89,7 @@ AS $$
7389
IF val ? 'i' THEN
7490
RETURN true;
7591
END IF;
76-
RAISE 'Encrypted missing ident (i) field: %', val;
92+
RAISE 'Encrypted column missing ident (i) field: %', val;
7793
END;
7894
$$ LANGUAGE plpgsql;
7995

@@ -86,7 +102,7 @@ AS $$
86102
IF (val->'i' ?& array['t', 'c']) THEN
87103
RETURN true;
88104
END IF;
89-
RAISE 'Encrypted ident (i) missing table (t) or column (c) fields: %', val;
105+
RAISE 'Encrypted column ident (i) missing table (t) or column (c) fields: %', val;
90106
END;
91107
$$ LANGUAGE plpgsql;
92108

@@ -99,7 +115,7 @@ AS $$
99115
IF (val ? 'v') THEN
100116
RETURN true;
101117
END IF;
102-
RAISE 'Encrypted missing version (v) field: %', val;
118+
RAISE 'Encrypted column missing version (v) field: %', val;
103119
END;
104120
$$ LANGUAGE plpgsql;
105121

@@ -113,6 +129,7 @@ BEGIN ATOMIC
113129
RETURN (
114130
_cs_encrypted_check_v(val) AND
115131
_cs_encrypted_check_i(val) AND
132+
_cs_encrypted_check_k(val) AND
116133
_cs_encrypted_check_k_ct(val) AND
117134
_cs_encrypted_check_k_sv(val) AND
118135
_cs_encrypted_check_p(val)

sql/020-config-schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ CREATE FUNCTION _cs_config_check_tables(val jsonb)
7777
RETURNS boolean
7878
AS $$
7979
BEGIN
80-
IF (val ? 'tables') AND (val->'tables' <> '{"A":"a"}'::jsonb) THEN
80+
IF (val ? 'tables') AND (val->'tables' <> '{}'::jsonb) THEN
8181
RETURN true;
8282
END IF;
8383
RAISE 'Configuration missing tables (tables) field: %', val;

tests/core.sql

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ INSERT INTO users (name_encrypted) VALUES (
8484
}'::jsonb
8585
);
8686

87-
-- no source detauils
87+
-- no ident details
8888
INSERT INTO users (name_encrypted) VALUES (
8989
'{
9090
"v": 1,
@@ -93,6 +93,35 @@ INSERT INTO users (name_encrypted) VALUES (
9393
}'::jsonb
9494
);
9595

96+
-- npo kind
97+
INSERT INTO users (name_encrypted) VALUES (
98+
'{
99+
"v": 1,
100+
"c": "ciphertext",
101+
"i": {
102+
"t": "users",
103+
"c": "name"
104+
}
105+
}'::jsonb
106+
);
107+
108+
109+
110+
-- bad kind
111+
INSERT INTO users (name_encrypted) VALUES (
112+
'{
113+
"v": 1,
114+
"k": "vtha",
115+
"c": "ciphertext",
116+
"i": {
117+
"t": "users",
118+
"c": "name"
119+
}
120+
}'::jsonb
121+
);
122+
123+
124+
96125
-- pt
97126
INSERT INTO users (name_encrypted) VALUES (
98127
'{

0 commit comments

Comments
 (0)