Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s file-mounted secrets #1092

Open
zalmane opened this issue Apr 21, 2024 · 1 comment
Open

k8s file-mounted secrets #1092

zalmane opened this issue Apr 21, 2024 · 1 comment

Comments

@zalmane
Copy link

zalmane commented Apr 21, 2024

In K8S, it is common to mount secrets to a file such as tmpfs.
The typical format puts each secret into a separate file inside a mounted volume, where the key is the filename and the content is the value.
Is there a way to achieve loading this in Dynaconf?
If not, what is the best way to add this functionality (using hooks or plugin, or contribute a PR?).

@rochacbruno
Copy link
Member

rochacbruno commented Apr 22, 2024

Right now, the easiest path is to create a hook function and attach it as shown here: https://www.dynaconf.com/advanced/?h=hooks#instance-approach

import os

def load_mounted_secrets(settings):
    data = {"dynaconf_merge": True}
    folder_path = "the/path/to/the/secrets" # or take from settings
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        if os.path.isfile(file_path):
            with open(file_path, 'r') as file:
                data[filename] = file.read().strip()
    return data

settings = Dynaconf(post_hooks=[load_mounted_secrets])

It would be great to have this feature built-in in a better standardized way, so A PR also welcome.

How I would do it:

  1. Include a new variable on https://github.com/dynaconf/dynaconf/blob/master/dynaconf/default_settings.py something like SECRET_MOUNT_PATH_FOR_DYNACONF which will become the secret_mount_path= argument to the Dynaconf class.
  2. Add a mounted_secrets_loader.py to https://github.com/dynaconf/dynaconf/tree/master/dynaconf/loaders, that loader will do what the function above does (following the loader Base class style)
  3. On execute_loaders function invoke the loader (need to decide which order is best to load it)
  4. Add an app example as a functional test on tests_functional folder
  5. Add unit tests to the tests/ folder
  6. Add docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants