|
| 1 | +CREATE OR REPLACE FUNCTION @extschema@.encrypted_columns( |
| 2 | + relid OID |
| 3 | +) |
| 4 | +RETURNS TEXT AS |
| 5 | +$$ |
| 6 | +DECLARE |
| 7 | + m RECORD; |
| 8 | + expression TEXT; |
| 9 | + comma TEXT; |
| 10 | +BEGIN |
| 11 | + expression := ''; |
| 12 | + comma := E' '; |
| 13 | + FOR m IN SELECT * FROM @extschema@.mask_columns(relid) LOOP |
| 14 | + IF m.key_id IS NULL AND m.key_id_column is NULL THEN |
| 15 | + CONTINUE; |
| 16 | + ELSE |
| 17 | + expression := expression || comma; |
| 18 | + expression := expression || format( |
| 19 | + $f$%s = pg_catalog.encode( |
| 20 | + @extschema@.crypto_aead_det_encrypt( |
| 21 | + pg_catalog.convert_to(%s, 'utf8'), |
| 22 | + pg_catalog.convert_to(%s::text, 'utf8'), |
| 23 | + %s::uuid, |
| 24 | + %s |
| 25 | + ), |
| 26 | + 'base64')$f$, |
| 27 | + 'new.' || quote_ident(m.attname), |
| 28 | + 'new.' || quote_ident(m.attname), |
| 29 | + COALESCE('new.' || quote_ident(m.associated_column), quote_literal('')), |
| 30 | + COALESCE('new.' || quote_ident(m.key_id_column), quote_literal(m.key_id)), |
| 31 | + COALESCE('new.' || quote_ident(m.nonce_column), 'NULL') |
| 32 | + ); |
| 33 | + END IF; |
| 34 | + comma := E';\n '; |
| 35 | + END LOOP; |
| 36 | + RETURN expression; |
| 37 | +END |
| 38 | +$$ |
| 39 | + LANGUAGE plpgsql |
| 40 | + VOLATILE |
| 41 | + SET search_path='' |
| 42 | + ; |
| 43 | + |
| 44 | +CREATE OR REPLACE FUNCTION @extschema@.decrypted_columns( |
| 45 | + relid OID |
| 46 | +) |
| 47 | +RETURNS TEXT AS |
| 48 | +$$ |
| 49 | +DECLARE |
| 50 | + m RECORD; |
| 51 | + expression TEXT; |
| 52 | + comma TEXT; |
| 53 | + padding text = ' '; |
| 54 | +BEGIN |
| 55 | + expression := E'\n'; |
| 56 | + comma := padding; |
| 57 | + FOR m IN SELECT * FROM @extschema@.mask_columns(relid) LOOP |
| 58 | + expression := expression || comma; |
| 59 | + IF m.key_id IS NULL AND m.key_id_column IS NULL THEN |
| 60 | + expression := expression || padding || quote_ident(m.attname); |
| 61 | + ELSE |
| 62 | + expression := expression || padding || quote_ident(m.attname) || E',\n'; |
| 63 | + expression := expression || format( |
| 64 | + $f$ |
| 65 | + pg_catalog.convert_from( |
| 66 | + @extschema@.crypto_aead_det_decrypt( |
| 67 | + pg_catalog.decode(%s, 'base64'), |
| 68 | + pg_catalog.convert_to(%s::text, 'utf8'), |
| 69 | + %s::uuid, |
| 70 | + %s |
| 71 | + ), |
| 72 | + 'utf8') AS %s$f$, |
| 73 | + quote_ident(m.attname), |
| 74 | + coalesce(quote_ident(m.associated_column), quote_literal('')), |
| 75 | + coalesce(quote_ident(m.key_id_column), quote_literal(m.key_id)), |
| 76 | + coalesce(quote_ident(m.nonce_column), 'NULL'), |
| 77 | + 'decrypted_' || quote_ident(m.attname) |
| 78 | + ); |
| 79 | + END IF; |
| 80 | + comma := E', \n'; |
| 81 | + END LOOP; |
| 82 | + RETURN expression; |
| 83 | +END |
| 84 | +$$ |
| 85 | + LANGUAGE plpgsql |
| 86 | + VOLATILE |
| 87 | + SET search_path='' |
| 88 | +; |
0 commit comments