Package: python-configuration-management
Quick startThis 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.Installpip install python-configuration-managementcliGenerate a keyIn a terminal, enter:pycmgenerate-keyFollow the instructions printed to the console. For example, if you're setting up a production configuration,
make a file called.env-productionin the root of your project. Inside of it, save the key generated
above to a variable calledENC_KEY.Upsert a secretTo insert or update a secret, enter:pycmupsert--environment<yourenvironment>And follow the prompts.Insert a non-secretSimply open the .yml file for the generated stage (the naming scheme isconfig-<environment>.yaml),
and insert a row. It should look like this:USERNAME:whatsup1994# non-secretPASSWORD:secret:truevalue:gAAAAABf2_kxEgWXQzJ0SlRmDy6lbXe-d3dWD68W4aM26yiA0EO2_4pA5FhV96uMWCLwpt7N6Y32zXQq-gTJ3sREbh1GOvNh5Q==Manually editing the fileYou 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 consoleTo show the decrypted values of all the secrets in the console, enter:pycmreveal--environment<your-environment>Re-encrypt a config fileTo re-encrypt all secret values for a given environment's config file, passpycmreencrypt--environment<your-environment>--new-key<your-new-key>If you do not provide a key, a new one will be generated for you.ExtrasIn the root of your project, you can create a file calledconfig-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 codeThere are two ways to use this library. You can either have a dotenv file with yourENC_KEY,
or you can place theENC_KEYin 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 useinject_config.# settings.pyfrompycmimportinject_config# development is the environment nameinject_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.pyfrompycmimportget_config# config = {"USERNAME": "helloworld", "PASSWORD": "im decrypted"}config=get_config("development")USERNAME=config["USERNAME"]# ...Advanced usageAll file paths within the libary are relative to root by default. To change this
behaviour, set an environment variable calledPYCM_ROOTwhich stores the relative
path to the root of your project (where your.env-[environment]andconfig-[environment].ymlfiles are stored).Running testsYou'll need the following encryption key to run testsrj10mXFq-JTDlsSa-5GxYzcx4KAF6TQpXWcl1LLbUTU=This belongs either in your environment variables underENC_KEYor in.env-test.This project usespoetryfor dependency management
and packaging.