-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Preperation for customer encryption at rest (#671)
- Loading branch information
Showing
18 changed files
with
321 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use std::env; | ||
|
||
// export CUSTOMER_KEY='190a5bf4b3cbb6c0991967ab1c48ab30790af876720f1835cbbf3820f4f5d949' | ||
pub fn get_customer_key() -> Option<String> { | ||
env::var("CUSTOMER_KEY").ok() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod authz; | ||
pub mod customer_keys; | ||
pub mod vector_search; | ||
|
||
use std::str::FromStr; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- migrate:up | ||
CREATE EXTENSION IF NOT EXISTS pgcrypto; | ||
|
||
-- Encrypt function | ||
CREATE OR REPLACE FUNCTION encrypt_text(data text) RETURNS text AS $$ | ||
DECLARE | ||
key text; | ||
encrypted text; | ||
BEGIN | ||
-- Retrieve the 'app.root_key' parameter, allowing it to be missing (returns NULL if not set) | ||
key := current_setting('encryption.root_key', true); | ||
|
||
IF key IS NULL THEN | ||
RETURN data; | ||
ELSE | ||
BEGIN | ||
-- Encrypt the data using pgp_sym_encrypt with 'armor' option | ||
encrypted := pgp_sym_encrypt(data, key, 'compress-algo=1, cipher-algo=aes256'); | ||
RETURN encrypted; | ||
EXCEPTION WHEN others THEN | ||
-- Return the exception message if encryption fails | ||
RETURN SQLERRM; | ||
END; | ||
END IF; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
-- Decrypt function | ||
|
||
CREATE OR REPLACE FUNCTION decrypt_text(data text) RETURNS text AS $$ | ||
DECLARE | ||
key text; | ||
decrypted text; | ||
BEGIN | ||
-- Retrieve the 'app.root_key' parameter, allowing it to be missing (returns NULL if not set) | ||
key := current_setting('encryption.root_key', true); | ||
|
||
IF key IS NULL THEN | ||
RETURN data; | ||
ELSE | ||
BEGIN | ||
-- Decrypt the data using pgp_sym_decrypt | ||
decrypted := pgp_sym_decrypt(data::bytea, key); | ||
RETURN decrypted; | ||
EXCEPTION WHEN others THEN | ||
-- Return the exception message if decryption fails | ||
RETURN data; | ||
END; | ||
END IF; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- migrate:down | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.