Skip to main content

A merge conflict-less solution to committing an encrypted configuration to the repo with secrets and non-secrets side-by-side.

Project description

Quick start

This package features an opinionated, python configuration management system, focused on combining both secret and non-secret keys in the same configuration file. The values for secret keys are encrypted and can be committed to the repo, but since each key is separated on a line-by-line basis, merge conflicts shouldn't cause much trouble.

Install

pip install python-configuration-management

cli

Generate a key

In a terminal, enter:

pycm generate-key

Follow the instructions printed to the console. For example, if you're setting up a production configuration, make a file called .env-production in the root of your project. Inside of it, save the key generated above to a variable called ENC_KEY.

Upsert a secret

To insert or update a secret, enter:

pycm upsert --environment <your environment>

And follow the prompts.

Insert a non-secret

Simply open the .yml file for the generated stage (the naming scheme is config-<environment>.yaml), and insert a row. It should look like this:

USERNAME: whatsup1994 # non-secret
PASSWORD:
  secret: true
  value: gAAAAABf2_kxEgWXQzJ0SlRmDy6lbXe-d3dWD68W4aM26yiA0EO2_4pA5FhV96uMWCLwpt7N6Y32zXQq-gTJ3sREbh1GOvNh5Q==

Manually editing the file

You can change the values of non-secrets by hand, as well as the keynames, but clearly you must not change the value of secrets by hand, as they're encrypted. Changing the order of any of the keys is perfectly fine.

Print secrets to the console

To show the decrypted values of all the secrets in the console, enter:

pycm reveal --environment <your-environment>

Re-encrypt a config file

To re-encrypt all secret values for a given environment's config file, pass

pycm reencrypt --environment <your-environment> --new-key <your-new-key>

If you do not provide a key, a new one will be generated for you.

Extras

In the root of your project, you can create a file called config-required.json.

The JSON object can be a list or a dictionary. This is useful for validating the presence of your keys on start-up.

Using the config in your python code

There are two ways to use this library. You can either have a dotenv file with your ENC_KEY, or you can place the ENC_KEY in your environment variables. If you use a dotenv, make sure the file follows this naming scheme: .env-[environment].

As for accessing the config, if you don't mind a little magic, you can use inject_config.

# settings.py
from python_configuration_management import inject_config

# development is the environment name
inject_config("development", sys.modules[__name__], use_dotenv=True)

If you want more verbosity, you can import the following function which will return the config as a normalized dictionary that's flat and has all secrets decrypted.

# settings.py
from python_configuration_management import get_config

# config = {"USERNAME": "helloworld", "PASSWORD": "im decrypted}
config = get_config("development", use_dotenv=True)

USERNAME = config["USERNAME"]
# ...

This project uses poetry for dependency management and packaging.

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

python_configuration_management-1.0.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for python_configuration_management-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d03f3c5ba4fc5e1486a1e123ece01fa832ea4b604246d3e8145b7c061ffdb5f1
MD5 dd6910a8dab55e89b0149dc872b6bde7
BLAKE2b-256 6c0fb8158dd6380e039f2d14dcc9412fd25d5fe874901c89a46b7b7cc75637dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_configuration_management-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d3823027326d58bdef27b64b41601548ad8d44073773ec6f9a975e7d6b8f1d
MD5 1f39cec70fae4e1f80033b8b6ecd742a
BLAKE2b-256 fd9b28e1cdea8f1f15f5f61d72867d2e8fac63ad7619742b935f86d44fe2e16a

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