Skip to main content

A library that offers a simple method of loading and accessing environmental variables and `.env` file values.

Project description

secretbox

Code style: black pre-commit pre-commit.ci status

A library that offers a simple method of loading and accessing environmental variables, .env file values, and other sources of secrets. The class stores values to state when load methods are called.

Loaded values are also injected into the local environ. This is to assist with adjacent libraries that reference os.environ values by default. Required values can be kept in a .env file instead of managing a script to load them into the environment.

Requirements

  • Python >= 3.6 <= 3.9

Optional Dependencies

  • boto3
  • boto3-stubs[secretsmanager]

Install

$ pip install secretbox

Optional AWS Secret Manager support

$ pip install secretbox[aws]

Example use

from secretbox.loadenv import LoadEnv

secrets = LoadEnv()


def main() -> int:
    """Main function"""
    secrets.load()

    my_sevice_password = secrets.get("SERVICE_PW")
    # More code

    return 0


if __name__ == "__main__":
    exit(main())

LoadEnv arguments:

Default: (shown above)

  • On initialization the LoadEnv() class does nothing. By calling .load() we cause the class to load all the currently available environ variables. It also looks for and loads, if found, a .env file in the working directory. From there we can access those values with .get("KEY_NAME").

auto_load

  • Alternatively, you can initialize with LoadEnv(auto_load=True) and the load() method will be executed on creation of the class instance.

filename

  • You can specify a .env formatted file and location, overriding the default behavior to load the .env from the working directory.

aws_sstore_name

  • When provided, an attempt to load values from named AWS secrets manager will be made. Requires aws_region to be provided. Requires boto3 and boto3-stubs[secretsmanager] to be installed.

aws_region

  • When provided, an attempt to load values from the given AWS secrets manager found in this region will be made. Requires aws_sstore_name to be provided. Requires boto3 and `boto3-stubs[secretsmanager] to be installed.

LoadEnv methods:

.get("[Key Name]")

  • Returns the string value of the loaded value by key name. If the key does not exist, an empty string will be returned "".

.load()

  • Runs all importer methods. If optional dependencies are not installed, e.g. boto3, the importer is skipped.

.load_env_vars()

  • Loads all existing os.environ values into state.

.load_env_file()

  • Loads .env file or any file provided with the filename argument on initialization.

.load_aws_store()

  • Loads secrets from AWS secret manager. Requires aws_sstore_name and aws_region to have been provided. Will raise NotImplementedError if library requirements are missing.

Local developer installation

It is highly recommended to use a venv for installation. Leveraging a venv will ensure the installed dependency files will not impact other python projects.

The instruction below make use of a bash shell and a Makefile. All commands should be able to be run individually of your shell does not support make

Clone this repo and enter root directory of repo:

$ git clone https://github.com/Preocts/secretbox
$ cd secretbox

Create and activate venv:

$ python3 -m venv venv
$ . venv/bin/activate

Your command prompt should now have a (venv) prefix on it.

Install editable library and development requirements:

(venv) $ pip install -r requirements-dev.txt
(venv) $ pip install --editable .[aws,tests]

Run tests

(venv) $ tox

To exit the venv:

(venv) $ deactivate

Makefile

This repo has a Makefile with some quality of life scripts if your system supports make.

  • update : Clean all artifacts, update pip, update requirements, install everything
  • clean-pyc : Deletes python/mypy artifacts
  • clean-tests : Deletes tox, coverage, and pytest artifacts

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

secretbox-1.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

secretbox-1.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file secretbox-1.0.1.tar.gz.

File metadata

  • Download URL: secretbox-1.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for secretbox-1.0.1.tar.gz
Algorithm Hash digest
SHA256 25ca555fe00edfee2792d2135fa9d8ad67dfca3b9dad52406e39d913354a06ce
MD5 eae32a2a9ff11ef46cd98d495b69dd54
BLAKE2b-256 dc7116fd643843efe557bbaee46eeeff2b0f3a504fa67cf16ea4c0b8b353cfb7

See more details on using hashes here.

File details

Details for the file secretbox-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: secretbox-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for secretbox-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9a06445df81eb83ba6bc156ffde81539fc064f172d03167dc30e828c29cd163
MD5 3b48478a3680adf799072a1fe291b963
BLAKE2b-256 29199a6437c05a4f786a03959209db324dfe8980f27ef43f403a632744fc54a6

See more details on using hashes here.

Supported by

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