Skip to main content

A ConfigParser subclass that can read values stored with the keyring pypi package.

Project description

keyring_configparser

A ConfigParser subclass that can read values stored with the keyring pypi package.

Installation

pip install keyring_configparser

Usage

It is recommended to be familiar with the ConfigParser module and the keyring pypi package.

KeryingConfigParser is identical to ConfigParser except when it reads a specific token as a configuration value ("$." by default) it uses the keyring package to resolve the value. This enables using secret values in configuration files without storing the value as plain-text within the file.

Basic Example

#/tmp/app.config
[EXAMPLE_SECTION]
non_secret = hello world
some_secret = $.
import keyring

keyring.set_password("EXAMPLE_SECTION", "some_secret", "very_secret_value")
from keyring_configparser import KeyringConfigParser

config = KeyringConfigParser()
config.read("/tmp/app.config")
config.get('EXAMPLE_SECTION', 'non_secret')
> "hello world"
sec = config.get('EXAMPLE_SECTION', 'some_secret')
> "very_secret_value"

Additional Examples

Configured Keyring Instances

A configured keyring instance can be supplied to the KeyringConfigParser constructor. This allows using non-default backends or any other non-default keyring settings when looking up values in keyring.

For example, to use the keyrings.cryptfile backend:

from keyrings.cryptfile.cryptfile import CryptFileKeyring

kr = CryptFileKeyring()
kr.keyring_key = "CRYPTFILE_PASSWORD"
kr.set_password("EXAMPLE_SECTION", "some_secret", "very_secret_value")
from keyring_configparser import KeyringConfigParser
from keyrings.cryptfile.cryptfile import CryptFileKeyring

kr = CryptFileKeyring()
kr.keyring_key = "CRYPTFILE_PASSWORD"

config = KeyringConfigParser(keyring=kr)
config.read("/tmp/app.config")
config.get('EXAMPLE_SECTION', 'some_secret')
> "very_secret_value"

Custom Config Token

A token can be supplied to the KeyringConfigParser constructor to override the default token "$.". When the custom token is encountered in the configuration file the value will be resolved with keyring.

Given the following configuration file:

#/tmp/app.config
[EXAMPLE_SECTION]
non_secret = hello world
some_secret = !~!
default_token = $.
from keyring_configparser import KeyringConfigParser

config = KeyringConfigParser(token="!~!")
config.read("/tmp/app.config")
config.get('EXAMPLE_SECTION', 'some_secret')
> "very_secret_value"
config.get('EXAMPLE_SECTION', 'default_token')
> "$."

Questions / Issues

Please raise any questions in the Discussions page of the repository.

Please document any issues encountered in the Issues page of the repository.

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

keyring_configparser-0.1.0.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

keyring_configparser-0.1.0-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

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