sosecrets


Namesosecrets JSON
Version 0.1.12 PyPI version JSON
download
home_page
SummarySimple wrapper to secure your secrets.
upload_time2023-06-04 15:56:09
maintainer
docs_urlNone
authorJim Chng
requires_python>=3.8,<4.0
licenseMIT
keywords secrets security
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sosecrets

![https://img.shields.io/github/commit-activity/t/jymchng/sosecrets](https://img.shields.io/github/commit-activity/t/jymchng/sosecrets) ![https://img.shields.io/github/actions/workflow/status/jymchng/sosecrets/testing.yml](https://img.shields.io/github/actions/workflow/status/jymchng/sosecrets/testing.yml) ![https://img.shields.io/pypi/pyversions/sosecrets](https://img.shields.io/pypi/pyversions/sosecrets) ![https://img.shields.io/pypi/dm/sosecrets](https://img.shields.io/pypi/dm/sosecrets) ![https://img.shields.io/github/issues/jymchng/sosecrets](https://img.shields.io/github/issues/jymchng/sosecrets) ![https://img.shields.io/github/issues-pr/jymchng/sosecrets](https://img.shields.io/github/issues-pr/jymchng/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.12

[Documentation](https://sosecrets.readthedocs.io/en/latest/)

## Installation

To install `sosecrets`, you can use pip:

```bash
pip install sosecrets
```

## Usage

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

### Secret

Here's an example of how to use `Secret`:

```python
from sosecrets import Secret

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

# Use the secret value while keeping it encapsulated
result: Secret[T] = secret_value.apply(len)
print(result.expose_secret())  # 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.

### ImmutableSecretMapping

Here's an example of how to use `ImmutableSecretMapping`:

```python
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 output of the above codes is shown below:
```
exposed_dict = {
    'value0': '3c976b3c66a2aa1d440d3ad99a9653c7',
    'value1': '8f7047cda0532a374dbc380edad96c25',
...
    'value14': '277103129cbf6050b7cbd6502eca9810'}

secret_env_dict =  {'value0': <secrets.Secret object at 0x000002210A017540>, 
                    'value1': <secrets.Secret object at 0x000002210A017580>, 
                    ...
                    'value14': <secrets.Secret object at 0x000002210A0178C0>}

value0 = <secrets.Secret object at 0x000002210A017540>
value1 = 8f7047cda0532a374dbc380edad96c25
```

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

### 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[..., T]] = None, func_args: Tuple[Any, ...] = tuple(), func_kwargs: Dict[str, Any] = dict()) -> Secret
```

- `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.

```python
apply(self, func: Callable[..., 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.

```python
expose_secret(self) -> T
```

## Contributing

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

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "sosecrets",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "secrets,security",
    "author": "Jim Chng",
    "author_email": "jimchng@outlook.com",
    "download_url": "https://files.pythonhosted.org/packages/2c/14/818a6aa26692af79335c050ad2614f522956796a6671993712af084c5d10/sosecrets-0.1.12.tar.gz",
    "platform": null,
    "description": "# sosecrets\n\n![https://img.shields.io/github/commit-activity/t/jymchng/sosecrets](https://img.shields.io/github/commit-activity/t/jymchng/sosecrets) ![https://img.shields.io/github/actions/workflow/status/jymchng/sosecrets/testing.yml](https://img.shields.io/github/actions/workflow/status/jymchng/sosecrets/testing.yml) ![https://img.shields.io/pypi/pyversions/sosecrets](https://img.shields.io/pypi/pyversions/sosecrets) ![https://img.shields.io/pypi/dm/sosecrets](https://img.shields.io/pypi/dm/sosecrets) ![https://img.shields.io/github/issues/jymchng/sosecrets](https://img.shields.io/github/issues/jymchng/sosecrets) ![https://img.shields.io/github/issues-pr/jymchng/sosecrets](https://img.shields.io/github/issues-pr/jymchng/sosecrets)\n\n`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.\n\nVersion: 0.1.12\n\n[Documentation](https://sosecrets.readthedocs.io/en/latest/)\n\n## Installation\n\nTo install `sosecrets`, you can use pip:\n\n```bash\npip install sosecrets\n```\n\n## Usage\n\nHere's are the examples of how to use `sosecrets`:\n\n### Secret\n\nHere's an example of how to use `Secret`:\n\n```python\nfrom sosecrets import Secret\n\n# Create a secret value\nsecret_value = Secret(\"my secret value\")\n\n# Use the secret value while keeping it encapsulated\nresult: Secret[T] = secret_value.apply(len)\nprint(result.expose_secret())  # Output: 14\n\n# Get the value of the secret\nvalue = secret_value.expose_secret()\nprint(value)  # Output: \"my secret value\"\n```\n\nIn 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.\n\n### ImmutableSecretMapping\n\nHere's an example of how to use `ImmutableSecretMapping`:\n\n```python\nfrom pathlib import Path\nfrom sosecrets import ImmutableSecretMapping\nfrom dotenv import dotenv_values\n\n# Define the path to the `.env` file\nTHIS_SCRIPT_FILE_PATH = Path(__file__)\nEXAMPLES = THIS_SCRIPT_FILE_PATH.parent / '.env'\n\n# Load the environment variables and store them securely\nsecret_env_dict = ImmutableSecretMapping.from_func(dotenv_values, dotenv_path=EXAMPLES)\n\n# Get a dictionary of the exposed values of the secret variables\nexposed_dict = secret_env_dict.expose_dict()\nprint(\"exposed_dict: \", exposed_dict)\n\n# Print the `ImmutableSecretMapping` object itself\nprint(\"secret_env_dict: \", secret_env_dict)\n\n# Get the value associated with a key using the `get()` method\nvalue0 = secret_env_dict.get('value0')\nprint(\"value0: \", value0)\n\n# Get the exposed value of a key using the `get_exposed()` method\nvalue1 = secret_env_dict.get_exposed('value1')\nprint(\"value1: \", value1)\n```\n\nThe output of the above codes is shown below:\n```\nexposed_dict = {\n    'value0': '3c976b3c66a2aa1d440d3ad99a9653c7',\n    'value1': '8f7047cda0532a374dbc380edad96c25',\n...\n    'value14': '277103129cbf6050b7cbd6502eca9810'}\n\nsecret_env_dict =  {'value0': <secrets.Secret object at 0x000002210A017540>, \n                    'value1': <secrets.Secret object at 0x000002210A017580>, \n                    ...\n                    'value14': <secrets.Secret object at 0x000002210A0178C0>}\n\nvalue0 = <secrets.Secret object at 0x000002210A017540>\nvalue1 = 8f7047cda0532a374dbc380edad96c25\n```\n\nThe example code does the following:\n\n1. 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.\n2. Defines the path to a `.env` file that contains environment variables to be loaded.\n3. 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`.\n4. Prints the exposed dictionary of the `ImmutableSecretMapping` object using the `expose_dict()` method.\n5. Prints the `ImmutableSecretMapping` object itself.\n6. Gets a value from the `ImmutableSecretMapping` object using the `get()` method and prints it.\n7. Gets the exposed value of a key using the `get_exposed()` method\n\n### MutableSecretMapping\n\nSimilar to `ImmutableSecretMapping`.\n\n## Use Cases\nsosecrets can be used in a variety of scenarios where sensitive data needs to be securely handled. Here are some common use cases:\n\nStoring 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.\n\nHandling 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.\n\n## API Reference\n\n### `Secret`\n\nThe `Secret` class encapsulates a secret value and only exposes it through a controlled interface.\n\n```\nSecret(value: Optional[T] = None, func: Optional[Callable[..., T]] = None, func_args: Tuple[Any, ...] = tuple(), func_kwargs: Dict[str, Any] = dict()) -> Secret\n```\n\n- `value`: The secret value to encapsulate.\n- `func`: A function to generate the secret value.\n- `func_args`: The positional arguments to pass to the `func` function.\n- `func_kwargs`: The keyword arguments to pass to the `func` function.\n\n#### `apply`\n\nThe `apply` method applies a function to the secret value while keeping it encapsulated.\n\n```python\napply(self, func: Callable[..., Any], *args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> SecretType\n```\n\n- `func`: The function to apply to the secret value.\n- `args`: The positional arguments to pass to the `func` function.\n- `kwargs`: The keyword arguments to pass to the `func` function.\n\n#### `expose_secret`\n\nThe `expose_secret` method returns the value of the secret.\n\n```python\nexpose_secret(self) -> T\n```\n\n## Contributing\n\nContributions are welcome! Let me know if you need help with anything else.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple wrapper to secure your secrets.",
    "version": "0.1.12",
    "project_urls": null,
    "split_keywords": [
        "secrets",
        "security"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50351234d93df5ef35162b0b511fda82fe0674b618db61411a11dd45e5bb24a6",
                "md5": "c1f2b7a0cac5b8360857e015aa46fd78",
                "sha256": "8a515ce422a5f98773001ded9253a307215fbe9f69f259a57028f1cdc089b741"
            },
            "downloads": -1,
            "filename": "sosecrets-0.1.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c1f2b7a0cac5b8360857e015aa46fd78",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6698,
            "upload_time": "2023-06-04T15:56:08",
            "upload_time_iso_8601": "2023-06-04T15:56:08.053976Z",
            "url": "https://files.pythonhosted.org/packages/50/35/1234d93df5ef35162b0b511fda82fe0674b618db61411a11dd45e5bb24a6/sosecrets-0.1.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c14818a6aa26692af79335c050ad2614f522956796a6671993712af084c5d10",
                "md5": "9808d2216d00a65da49893bddae10315",
                "sha256": "f937586e9b281b4eace2dc55fdf1949d3883ae48c4f890c1f5f0501e0f05b93c"
            },
            "downloads": -1,
            "filename": "sosecrets-0.1.12.tar.gz",
            "has_sig": false,
            "md5_digest": "9808d2216d00a65da49893bddae10315",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 6903,
            "upload_time": "2023-06-04T15:56:09",
            "upload_time_iso_8601": "2023-06-04T15:56:09.872931Z",
            "url": "https://files.pythonhosted.org/packages/2c/14/818a6aa26692af79335c050ad2614f522956796a6671993712af084c5d10/sosecrets-0.1.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-04 15:56:09",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "sosecrets"
}
        
Elapsed time: 0.14977s