Skip to main content

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 pycm import inject_config

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

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 pycm import get_config

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

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

Advanced usage

All file paths within the libary are relative to root by default. To change this behaviour, set an environment variable called PYCM_ROOT which stores the relative path to the root of your project (where your .env-[environment] and config-[environment].yml files are stored).

Running tests

You'll need the following encryption key to run tests

rj10mXFq-JTDlsSa-5GxYzcx4KAF6TQpXWcl1LLbUTU=

This belongs either in your environment variables under ENC_KEY or in .env-test.


This project uses poetry for dependency management and packaging.

Metadata

Release files for python-configuration-management 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-configuration-management 4.0.0
File Size Uploaded Provenance Actions
python-configuration-management-4.0.0.tar.gz 6.7 kB Without provenance Details for python-configuration-management-4.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-configuration-management 4.0.0
File Interpreter ABI Platform Actions
python_configuration_management-4.0.0-py3-none-any.whl Python 3 none any Details for python_configuration_management-4.0.0-py3-none-any.whl

Total release size: 13.9 kB

Release files / python-configuration-management-4.0.0.tar.gz

Download URL python-configuration-management-4.0.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f42f4d91aefc49eb5ac49aaa47d5f87a582fe2794c3007879e63a37175cdba15
BLAKE2b-256 checksum
How to use checksums
19c609680ca4db63b8b59655d82ea8b9fe7a6f19aafb6a70ae45c99569584c13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.12 Linux/5.4.0-1063-azure

Release files / python_configuration_management-4.0.0-py3-none-any.whl

Download URL python_configuration_management-4.0.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f54e8abfbbf05034b091567182a674ac045c924d3527d151141ebff85ac1cee
BLAKE2b-256 checksum
How to use checksums
c784c8e516ee3f94d859634716a796166fe315c6a5d3c0907e2e477b624baa00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.12 Linux/5.4.0-1063-azure

Security for python-configuration-management 4.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page