-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repurpose the project to use database for secret storage
- Loading branch information
1 parent
de4c2b6
commit 543d92a
Showing
11 changed files
with
276 additions
and
43 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 |
---|---|---|
|
@@ -163,3 +163,4 @@ cython_debug/ | |
|
||
*.env | ||
*.json | ||
*.db |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from . import models | ||
|
||
|
||
def get_secret(key: str, table_name: str) -> str | None: | ||
"""Function to retrieve secret from database. | ||
Args: | ||
key: Name of the secret to retrieve. | ||
table_name: Name of the table where the secret is stored. | ||
Returns: | ||
str: | ||
Returns the secret value. | ||
""" | ||
with models.database.connection: | ||
cursor = models.database.connection.cursor() | ||
state = cursor.execute( | ||
f'SELECT value FROM "{table_name}" WHERE key=(?)', (key,) | ||
).fetchone() | ||
if state and state[0]: | ||
return state[0] | ||
|
||
|
||
def put_secret(key: str, value: str, table_name: str) -> None: | ||
"""Function to add secret to the database. | ||
Args: | ||
key: Name of the secret to be stored. | ||
value: Value of the secret to be stored | ||
table_name: Name of the table where the secret is stored. | ||
""" | ||
with models.database.connection: | ||
cursor = models.database.connection.cursor() | ||
cursor.execute( | ||
f'INSERT INTO "{table_name}" (key, value) VALUES (?,?)', | ||
(key, value), | ||
) | ||
models.database.connection.commit() | ||
|
||
|
||
def remove_secret(key: str, table_name: str) -> None: | ||
"""Function to remove a secret from the database. | ||
Args: | ||
key: Name of the secret to be removed. | ||
table_name: Name of the table where the secret is stored. | ||
""" | ||
with models.database.connection: | ||
cursor = models.database.connection.cursor() | ||
cursor.execute(f'DELETE FROM "{table_name}" WHERE key=(?)', (key,)) | ||
models.database.connection.commit() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pydantic import BaseModel, FilePath, DirectoryPath | ||
|
||
|
||
class DeleteSecret(BaseModel): | ||
"""Payload for delete-secret API call. | ||
>>> DeleteSecret | ||
""" | ||
|
||
key: str | ||
table_name: str = "default" | ||
|
||
|
||
class PutSecret(BaseModel): | ||
"""Payload for put-secret API call. | ||
>>> DeleteSecret | ||
""" | ||
|
||
key: str | ||
value: str | ||
table_name: str = "default" |
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.