Skip to main content

Simple wrapper to secure your secrets.

Reason this release was yanked:

Cannot Build

Project description

sosecrets

sosecrets is a Python module that provides a secure way to handle sensitive data by encapsulating it and only exposing it through a controlled interface.

Version: 0.1.1

Installation

To install sosecrets, you can use pip:

pip install sosecrets

Usage

Here's are the examples of how to use sosecrets:

Secret

Here's an example of how to use Secret:

from sosecrets import Secret

# Create a secret value
secret_value = Secret("my secret value")

# Use the secret value while keeping it encapsulated
result = secret_value.apply(len)
print(result)  # Output: 14

# Get the value of the secret
value = secret_value.expose_secret()
print(value)  # Output: "my secret value"

In this example, we create a Secret object with the value "my secret value". We then use the apply method to apply the len function to the secret value while keeping it encapsulated. Finally, we use the expose_secret method to retrieve the value of the secret.

Here's an example of how to use ImmutableSecretMapping:

from pathlib import Path
from sosecrets import ImmutableSecretMapping
from dotenv import dotenv_values

# Define the path to the `.env` file
THIS_SCRIPT_FILE_PATH = Path(__file__)
EXAMPLES = THIS_SCRIPT_FILE_PATH.parent / '.env'

# Load the environment variables and store them securely
secret_env_dict = ImmutableSecretMapping.from_func(dotenv_values, dotenv_path=EXAMPLES)

# Get a dictionary of the exposed values of the secret variables
exposed_dict = secret_env_dict.expose_dict()
print("exposed_dict: ", exposed_dict)

# Print the `ImmutableSecretMapping` object itself
print("secret_env_dict: ", secret_env_dict)

# Get the value associated with a key using the `get()` method
value0 = secret_env_dict.get('value0')
print("value0: ", value0)

# Get the exposed value of a key using the `get_exposed()` method
value1 = secret_env_dict.get_exposed('value1')
print("value1: ", value1)

The example code does the following:

  1. Imports the necessary packages: Path from pathlib, src from the sosecrets module, ImmutableSecretMapping from secretdicts in the sosecrets module, and dotenv_values from the dotenv package.
  2. Defines the path to a .env file that contains environment variables to be loaded.
  3. Uses ImmutableSecretMapping.from_func(dotenv_values, dotenv_path=EXAMPLES) to load the environment variables from the .env file and store them in an instance of ImmutableSecretMapping.
  4. Prints the exposed dictionary of the ImmutableSecretMapping object using the expose_dict() method.
  5. Prints the ImmutableSecretMapping object itself.
  6. Gets a value from the ImmutableSecretMapping object using the get() method and prints it.
  7. Gets the exposed value of a key using the get_exposed() method## The Output

MutableSecretMapping

Similar to ImmutableSecretMapping.

Use Cases

sosecrets can be used in a variety of scenarios where sensitive data needs to be securely handled. Here are some common use cases:

Storing API keys, passwords, and other credentials: sosecrets can be used to securely store sensitive information such as API keys, passwords, and other credentials that are required for authentication or authorization in an application.

Handling personal identifiable information (PII): sosecrets can be used to protect personal identifiable information (PII) such as names, addresses, social security numbers, and other sensitive data that needs to be kept confidential.

API Reference

Secret

The Secret class encapsulates a secret value and only exposes it through a controlled interface.

Secret(value: Optional[T] = None, func: Optional[Callable[[Any], T]] = None, func_args: Tuple[Any] = tuple(), func_kwargs: Dict[str, Any] = dict()) -> SecretType
  • value: The secret value to encapsulate.
  • func: A function to generate the secret value.
  • func_args: The positional arguments to pass to the func function.
  • func_kwargs: The keyword arguments to pass to the func function.

apply

The apply method applies a function to the secret value while keeping it encapsulated.

apply(self, func: Callable[[Any], Any], *args: Tuple[Any], **kwargs: Dict[str, Any]) -> SecretType:
  • func: The function to apply to the secret value.
  • args: The positional arguments to pass to the func function.
  • kwargs: The keyword arguments to pass to the func function.

expose_secret

The expose_secret method returns the value of the secret.

def expose_secret(self) -> T:

Exceptions

sosecrets defines the following exceptions:

CannotInstantiateExposeSecret

Raised when attempting to instantiate the __expose_secret__ class.

CannotInheritFromSecret

Raised when attempting to subclass the Secret class.

FuncAndValueCannotBeBothPassed

Raised when both value and func arguments are passed to the Secret constructor.

Contributing

Contributions are welcome! Let me know if you need help with anything else.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sosecrets-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

sosecrets-0.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file sosecrets-0.1.1.tar.gz.

File metadata

  • Download URL: sosecrets-0.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Windows/10

File hashes

Hashes for sosecrets-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7e9352f5d0ca79a3d08337b2fe4674a72ae24c33fb76b182b5885e20bc3611b0
MD5 a2c2cb1217f9e1b685febf324f40e6e0
BLAKE2b-256 3b97b1fedf705c58b66e534fe1aaddc9ccd36e3d0513d6e3358fa345408da145

See more details on using hashes here.

File details

Details for the file sosecrets-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sosecrets-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Windows/10

File hashes

Hashes for sosecrets-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9cde4ebf27b0fa32b6be32b3f0861e33e22123dcf788a66ee6133bab7f0c4250
MD5 8e86a89957a8e0dcf531ce151d9f0b70
BLAKE2b-256 a84a59bcd590e3e1ed937af79c92f1f82d9df59feb092247e3a1d04975066808

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page