-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description #1551 ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
- Loading branch information
Showing
37 changed files
with
216 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .create_secret import create_secret | ||
from .delete_secret import delete_secret | ||
from .read_secret import read_secret |
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,21 @@ | ||
from uuid import UUID | ||
|
||
from models import get_supabase_client | ||
from utils import build_secret_unique_name | ||
|
||
|
||
def create_secret( | ||
user_id: UUID, brain_id: UUID, secret_name: str, secret_value | ||
) -> UUID | None: | ||
supabase_client = get_supabase_client() | ||
response = supabase_client.rpc( | ||
"insert_secret", | ||
{ | ||
"name": build_secret_unique_name( | ||
user_id=user_id, brain_id=brain_id, secret_name=secret_name | ||
), | ||
"secret": secret_value, | ||
}, | ||
).execute() | ||
|
||
return response.data |
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,18 @@ | ||
from uuid import UUID | ||
|
||
from models import get_supabase_client | ||
from utils import build_secret_unique_name | ||
|
||
|
||
def delete_secret(user_id: UUID, brain_id: UUID, secret_name: str) -> bool: | ||
supabase_client = get_supabase_client() | ||
response = supabase_client.rpc( | ||
"delete_secret", | ||
{ | ||
"name": build_secret_unique_name( | ||
user_id=user_id, brain_id=brain_id, secret_name=secret_name | ||
), | ||
}, | ||
).execute() | ||
|
||
return response.data |
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,20 @@ | ||
from uuid import UUID | ||
|
||
from models import get_supabase_client | ||
from utils import build_secret_unique_name | ||
|
||
|
||
def read_secret( | ||
user_id: UUID, brain_id: UUID, secret_name: str, secret_value | ||
) -> UUID | None: | ||
supabase_client = get_supabase_client() | ||
response = supabase_client.rpc( | ||
"read_secret", | ||
{ | ||
"secret_name": build_secret_unique_name( | ||
user_id=user_id, brain_id=brain_id, secret_name=secret_name | ||
), | ||
}, | ||
).execute() | ||
|
||
return response.data |
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,5 @@ | ||
from uuid import UUID | ||
|
||
|
||
def build_secret_unique_name(user_id: UUID, brain_id: UUID, secret_name: str): | ||
return f"{user_id}-{brain_id}-{secret_name}" |
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,53 @@ | ||
CREATE OR REPLACE FUNCTION insert_secret(name text, secret text) | ||
returns uuid | ||
language plpgsql | ||
security definer | ||
set search_path = public | ||
as $$ | ||
begin | ||
return vault.create_secret(secret, name); | ||
end; | ||
$$; | ||
|
||
|
||
create or replace function read_secret(secret_name text) | ||
returns text | ||
language plpgsql | ||
security definer set search_path = public | ||
as $$ | ||
declare | ||
secret text; | ||
begin | ||
select decrypted_secret from vault.decrypted_secrets where name = | ||
secret_name into secret; | ||
return secret; | ||
end; | ||
$$; | ||
|
||
create or replace function delete_secret(secret_name text) | ||
returns text | ||
language plpgsql | ||
security definer set search_path = public | ||
as $$ | ||
declare | ||
deleted_rows int; | ||
begin | ||
delete from vault.decrypted_secrets where name = secret_name; | ||
get diagnostics deleted_rows = row_count; | ||
if deleted_rows = 0 then | ||
return false; | ||
else | ||
return true; | ||
end if; | ||
end; | ||
$$; | ||
|
||
-- Insert a migration record if it doesn't exist | ||
INSERT INTO migrations (name) | ||
SELECT '20231107104700_setup_vault' | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM migrations WHERE name = '20231107104700_setup_vault' | ||
); | ||
|
||
-- Commit the changes | ||
COMMIT; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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