Skip to content

Commit 9caa151

Browse files
authored
Merge pull request #90 from cipherstash/feat/parse-ore-as-json-array
Parse ORE indexes as JSON arrays of hex-encoded strings
2 parents ab9afc1 + 328d533 commit 9caa151

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

sql/011-core-functions.sql

+23-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ DROP CAST IF EXISTS (text AS ore_64_8_v1_term);
156156
CREATE CAST (text AS ore_64_8_v1_term)
157157
WITH FUNCTION _cs_text_to_ore_64_8_v1_term_v1_0(text) AS IMPLICIT;
158158

159+
DROP FUNCTION IF EXISTS jsonb_array_to_ore_64_8_v1(val jsonb);
160+
161+
-- Casts a jsonb array of hex-encoded strings to the `ore_64_8_v1` composite type.
162+
-- In other words, this function takes the ORE index format sent through in the
163+
-- EQL payload from Proxy and decodes it as the composite type that we use for
164+
-- ORE operations on the Postgres side.
165+
CREATE FUNCTION jsonb_array_to_ore_64_8_v1(val jsonb)
166+
RETURNS ore_64_8_v1 AS $$
167+
DECLARE
168+
terms_arr ore_64_8_v1_term[];
169+
BEGIN
170+
IF jsonb_typeof(val) = 'null' THEN
171+
RETURN NULL;
172+
END IF;
173+
174+
SELECT array_agg(ROW(decode(value::text, 'hex'))::ore_64_8_v1_term)
175+
INTO terms_arr
176+
FROM jsonb_array_elements_text(val) AS value;
177+
178+
RETURN ROW(terms_arr)::ore_64_8_v1;
179+
END;
180+
$$ LANGUAGE plpgsql;
159181

160182
-- extracts ore index from an encrypted column
161183
DROP FUNCTION IF EXISTS cs_ore_64_8_v1_v0_0(val jsonb);
@@ -166,7 +188,7 @@ CREATE FUNCTION cs_ore_64_8_v1_v0_0(val jsonb)
166188
AS $$
167189
BEGIN
168190
IF val ? 'o' THEN
169-
RETURN (val->>'o')::ore_64_8_v1;
191+
RETURN jsonb_array_to_ore_64_8_v1(val->'o');
170192
END IF;
171193
RAISE 'Expected an ore index (o) value in json: %', val;
172194
END;

tests/core-functions.sql

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ DO $$
66
ASSERT (SELECT EXISTS (SELECT cs_unique_v1('{"u": "u"}'::jsonb)));
77
ASSERT (SELECT EXISTS (SELECT cs_match_v1('{"m": []}'::jsonb)));
88
ASSERT (SELECT EXISTS (SELECT cs_ste_vec_v1('{"sv": [[]]}'::jsonb)));
9-
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": "()"}'::jsonb)));
9+
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": []}'::jsonb)));
1010

1111
END;
1212
$$ LANGUAGE plpgsql;
1313

1414
DO $$
1515
BEGIN
1616
-- sanity check
17-
PERFORM cs_ore_64_8_v1('{"o": "()"}'::jsonb);
17+
PERFORM cs_ore_64_8_v1('{"o": []}'::jsonb);
1818

1919
BEGIN
2020
PERFORM cs_ore_64_8_v1('{}'::jsonb);
@@ -76,4 +76,3 @@ DO $$
7676
END;
7777
END;
7878
$$ LANGUAGE plpgsql;
79-

0 commit comments

Comments
 (0)