Secret
Secret
Bases: Generic[T]
The Secret class is a Python class that encapsulates a secret value and provides methods to access it. The secret value can be initialized either with a direct value or with a function that generates the value. The class allows for limiting the number of times the secret value can be exposed.
The expose_secret() method returns the inner secret value and increments the exposure count, expose_count.
If the exposure count, expose_count is larger than the maximum exposure count, max_expose_count, an AttributeError is raised.
The apply() method applies a function to the inner secret value and returns a new Secret object with the result encapsulated within.
The original Secret object is not modified.
This class provides a way to control access to sensitive data by encapsulating it within a class and allowing access only through well-defined methods. This can help prevent accidental exposure of sensitive data as much as possible.
Source code in src/sosecrets/secrets.pyi
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
__init__(value=Ellipsis, *, func=Ellipsis, func_args=Ellipsis, func_kwargs=Ellipsis, max_expose_count=Ellipsis)
Initialize a Secret object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value |
Optional[T]
|
The initial value of the Secret object. If provided, it takes precedence over |
Ellipsis
|
func |
Optional[Callable[..., T]]
|
A function used to generate the initial value of the Secret object. Ignored if |
Ellipsis
|
func_args |
Tuple[Any, ...]
|
Positional arguments to pass to the |
Ellipsis
|
func_kwargs |
Dict[str, Any]
|
Keyword arguments to pass to the |
Ellipsis
|
max_expose_count |
int
|
The maximum number of times the Secret object can be exposed. Set to -1 for unlimited. |
Ellipsis
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Source code in src/sosecrets/secrets.pyi
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
apply(func, *, func_args=Ellipsis, func_kwargs=Ellipsis)
Apply a function to the inner secret value and return a new Secret object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
Callable[..., Any]
|
The function to apply to the inner secret value. |
required |
func_args |
Tuple[Any, ...]
|
Positional arguments to pass to the |
Ellipsis
|
func_kwargs |
Dict[str, Any]
|
Keyword arguments to pass to the |
Ellipsis
|
Returns:
| Type | Description |
|---|---|
Secret
|
A new Secret object with the result of applying the function to the inner secret value. |
Source code in src/sosecrets/secrets.pyi
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
expose_secret()
Expose the secret value.
Returns:
| Type | Description |
|---|---|
T
|
The inner secret value. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the Secret object has reached the maximum exposure count. |
Source code in src/sosecrets/secrets.pyi
47 48 49 50 51 52 53 54 55 56 57 58 | |