-
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.
- Loading branch information
Showing
4 changed files
with
92 additions
and
30 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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from secured.attribute import AttrDict | ||
from secured.secured import Secured | ||
|
||
secured = Secured('examples/config-secrets.yaml', secure=True) | ||
secured = Secured('examples/config-secrets.yaml', secure=True, message="<Custom Secured>") | ||
|
||
print(secured.config_secrets.name) | ||
print(secured.config_secrets["name"]) | ||
|
||
ad = AttrDict(secure=True) | ||
ad = AttrDict(secure=True, message="<Custom Secured>") | ||
ad['password'] = 'my_secret' | ||
print((ad.password)) | ||
print((ad['password'])) |
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 |
---|---|---|
@@ -1,35 +1,88 @@ | ||
from typing import Union | ||
|
||
|
||
class Secure(str): | ||
""" | ||
A class for securing sensitive data. | ||
This class is designed to add a thing layer of protection by obscuring sensitive data, such as database URLs or API keys, | ||
in order to prevent accidental exposure in logs or debug output. | ||
This class is designed to add a thin layer of protection by obscuring sensitive data, such as database URLs or API keys, | ||
in order to prevent accidental exposure in logs or debug output. The representation of the secured data can be customized | ||
with a specific message. | ||
Attributes: | ||
message (str): Custom message to represent the secured data when printed or logged. | ||
Example: | ||
>>> DATABASE_URL = "your_actual_database_url" | ||
>>> sensitive_data = Secure(DATABASE_URL) | ||
>>> sensitive_data = Secure(DATABASE_URL, "<Data Hidden>") | ||
>>> print(sensitive_data) | ||
'<Sensitive data secured>' | ||
'<Data Hidden>' | ||
""" | ||
|
||
def __repr__(self): | ||
return "<Sensitive data secured>" | ||
def __new__(cls, original: str, message: str = "<Sensitive data secured>"): | ||
""" | ||
Create a new Secure instance that appears as a custom message. | ||
Args: | ||
original (str): The original string to secure. | ||
message (str): A placeholder message to display instead of the original content. | ||
Returns: | ||
Secure: A new Secure instance displaying the placeholder message. | ||
""" | ||
# Initialize the Secure instance with the message instead of the original content. | ||
return super(Secure, cls).__new__(cls, message) | ||
|
||
def __init__(self, original: str, message: str = "<Sensitive data secured>"): | ||
""" | ||
Initializes a Secure object. The initialization logic is handled by __new__; __init__ does not | ||
need to handle the data directly. | ||
Args: | ||
original (str): The original data to secure. | ||
message (str): A custom message to use for representing the secured data. | ||
""" | ||
super().__init__() | ||
self.original = original | ||
self.message = message | ||
|
||
def __repr__(self) -> str: | ||
""" | ||
Represent the Secure object using the custom message. | ||
Returns: | ||
str: The custom message representing the secured data. | ||
""" | ||
return self.message | ||
|
||
def __str__(self) -> str: | ||
return repr(self) | ||
""" | ||
Convert the Secure object to string using the custom message. | ||
Returns: | ||
str: The custom message representing the secured data. | ||
""" | ||
return self.__repr__() | ||
|
||
def to_int(self) -> Union[int, str]: | ||
""" | ||
Try converting the original secured data to an integer. | ||
Returns: | ||
Union[int, str]: The integer value of the original data, or the custom message if conversion fails. | ||
""" | ||
try: | ||
return int(self) | ||
return int(self.original) | ||
except ValueError: | ||
return self | ||
return self.message | ||
|
||
def to_float(self) -> Union[float, str]: | ||
""" | ||
Try converting the original secured data to a float. | ||
Returns: | ||
Union[float, str]: The float value of the original data, or the custom message if conversion fails. | ||
""" | ||
try: | ||
return float(self) | ||
return float(self.original) | ||
except ValueError: | ||
return self | ||
return self.message |
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