Skip to content

Commit

Permalink
Improve FileAwareEnv docs and add a docstring to the class
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileyChris committed Sep 15, 2021
1 parent ce98b28 commit 50fa40e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
34 changes: 26 additions & 8 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@ Tips
Docker-style file based variables
=================================

To enable Docker-style file based variables (appended with ``_FILE``), use
``environ.FileAwareEnv`` rather than ``environ.Env``:
Docker (swarm) and Kubernetes are two widely used platforms that store their
secrets in tmpfs inside containers as individual files, providing a secure way
to be able to share configuration data between containers.

Use ``environ.FileAwareEnv`` rather than ``environ.Env`` to first look for
environment variables with ``_FILE`` appended. If found, their contents will be
read from the file system and used instead.

For example, given an app with the following in its settings module:

.. code-block:: python
import environ
import environ
env = environ.FileAwareEnv()
SECRET_KEY = env("SECRET_KEY")
the example ``docker-compose.yml`` for would contain:

.. code-block:: yaml
env = environ.FileAwareEnv()
secrets:
secret_key:
external: true
# If a ``SECRET_KEY_FILE`` environment variable exists, its contents will be
# read from the file system and used instead of the ``SECRET_KEY``
# environment variable.
SECRET_KEY = env('SECRET_KEY')
services:
app:
secrets:
- secret_key
environment:
- SECRET_KEY_FILE=/run/secrets/secret_key
Using unsafe characters in URLs
Expand Down
14 changes: 14 additions & 0 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,20 @@ def read_env(cls, env_file=None, **overrides):


class FileAwareEnv(Env):
"""
First look for environment variables with ``_FILE`` appended. If found,
their contents will be read from the file system and used instead.
Use as a drop-in replacement for the standard ``environ.Env``:
.. code-block:: python
python env = environ.FileAwareEnv()
For example, if a ``SECRET_KEY_FILE`` environment variable was set,
``env("SECRET_KEY")`` would find the related variable, returning the file
contents rather than ever looking up a ``SECRET_KEY`` environment variable.
"""
ENVIRON = FileAwareMapping()


Expand Down

0 comments on commit 50fa40e

Please sign in to comment.