You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
+ created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
1148
+
+ updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
1149
+
+);
1150
+
+
1151
+
+COMMENT ON TABLE vault.secrets IS 'Table with encrypted `secret` column for storing sensitive information on disk.';
1152
+
+
1153
+
+CREATE UNIQUE INDEX ON vault.secrets USING btree (name) WHERE name IS NOT NULL;
1154
+
+
1155
+
+DROP VIEW IF EXISTS vault.decrypted_secrets;
1156
+
+CREATE VIEW vault.decrypted_secrets AS
1157
+
+SELECT s.id,
1158
+
+ s.name,
1159
+
+ s.description,
1160
+
+ s.secret,
1161
+
+ convert_from(
1162
+
+ vault._crypto_aead_det_decrypt(
1163
+
+ message := decode(s.secret, 'base64'::text),
1164
+
+ additional := convert_to(s.id || s.description || s.created_at at time zone 'utc' || s.updated_at at time zone 'utc', 'utf8'),
1165
+
+ key_id := 0,
1166
+
+ context := 'pgsodium'::bytea,
1167
+
+ nonce := s.nonce
1168
+
+ ),
1169
+
+ 'utf8'::name
1170
+
+ ) AS decrypted_secret,
1171
+
+ s.key_id,
1172
+
+ s.nonce,
1173
+
+ s.created_at,
1174
+
+ s.updated_at
1175
+
+FROM vault.secrets s;
1176
+
+
1177
+
+GRANT ALL ON SCHEMA vault TO pgsodium_keyiduser;
1178
+
+GRANT ALL ON TABLE vault.secrets TO pgsodium_keyiduser;
1179
+
+GRANT ALL ON vault.decrypted_secrets TO pgsodium_keyiduser;
1180
+
+
1181
+
+CREATE OR REPLACE FUNCTION vault.create_secret(
1182
+
+ new_secret text,
1183
+
+ new_name text = NULL,
1184
+
+ new_description text = '',
1185
+
+ -- unused
1186
+
+ new_key_id uuid = NULL
1187
+
+)
1188
+
+RETURNS uuid
1189
+
+LANGUAGE plpgsql
1190
+
+SET search_path = ''
1191
+
+AS $$
1192
+
+DECLARE
1193
+
+ rec record;
1194
+
+BEGIN
1195
+
+ INSERT INTO vault.secrets (secret, name, description)
1196
+
+ VALUES (
1197
+
+ new_secret,
1198
+
+ new_name,
1199
+
+ new_description
1200
+
+ )
1201
+
+ RETURNING * INTO rec;
1202
+
+ UPDATE vault.secrets s
1203
+
+ SET secret = encode(vault._crypto_aead_det_encrypt(
1204
+
+ message := convert_to(rec.secret, 'utf8'),
1205
+
+ additional := convert_to(s.id || s.description || s.created_at at time zone 'utc' || s.updated_at at time zone 'utc', 'utf8'),
1206
+
+ key_id := 0,
1207
+
+ context := 'pgsodium'::bytea,
1208
+
+ nonce := rec.nonce
1209
+
+ ), 'base64')
1210
+
+ WHERE id = rec.id;
1211
+
+ RETURN rec.id;
1212
+
+END
1213
+
+$$;
1214
+
+
1215
+
+CREATE OR REPLACE FUNCTION vault.update_secret(
1216
+
+ secret_id uuid,
1217
+
+ new_secret text = NULL,
1218
+
+ new_name text = NULL,
1219
+
+ new_description text = NULL,
1220
+
+ -- unused
1221
+
+ new_key_id uuid = NULL
1222
+
+)
1223
+
+RETURNS void
1224
+
+LANGUAGE plpgsql
1225
+
+SET search_path = ''
1226
+
+AS $$
1227
+
+DECLARE
1228
+
+ decrypted_secret text := (SELECT decrypted_secret FROM vault.decrypted_secrets WHERE id = secret_id);
1229
+
+BEGIN
1230
+
+ UPDATE vault.secrets s
1231
+
+ SET
1232
+
+ secret = CASE WHEN new_secret IS NULL THEN s.secret
1233
+
+ ELSE encode(vault._crypto_aead_det_encrypt(
1234
+
+ message := convert_to(new_secret, 'utf8'),
1235
+
+ additional := convert_to(s.id || coalesce(new_description, s.description) || (s.created_at at time zone 'utc') || (now() at time zone 'utc'), 'utf8'),
0 commit comments