LeeHanYeong / django-secrets-manager

Django SecretsManager is a package that helps you manage the secrets used by Django through various services.
MIT License
27 stars 7 forks source link

Support local dev setup #11

Open CelestialGuru opened 3 years ago

CelestialGuru commented 3 years ago

I cannot figure out how to write a settings.py which could work in both a development environment and a production environment. On an EC2 staging instance the following works:

# settings.py
import os
from django_secrets import SECRETS

SECRET_KEY = SECRETS.get('SECRET_KEY', os.environ.get('SECRET_KEY'))
> python manage.py runserver 0:8000
...
Starting development server at http://0:8000/
Quit the server with CONTROL-C.

but in a local development environment, I'll get an error:

> SECRET_KEY=thesecret python manage.py runserver 0:8000
Traceback (most recent call last):
...
django_secrets.backends.aws_secrets_manager.CredentialsNotExists: AWS Credentials Not Exists

And it doesn't work if I swap the order; python will evaluate both arguments before calling the function:

SECRET_KEY = os.environ.get('SECRET_KEY', SECRETS.get('SECRET_KEY', "this can be null or set; doesn't matter"))

The only other way I can think to get this to work is to rewrite the settings entirely to have something like prod.settings.py and dev.settings.py, but that doesn't sound very DRY.

Another option might be to extend django_secrets.backends.aws_secrets_manager.AWSSecretsManagerSecrets but I feel like that would be more of a monkeypatch than a solution.

CelestialGuru commented 3 years ago

I found a way to do it but don't like it:

import os
from django_secrets import SECRETS
from objproxies import LazyProxy

SECRET_KEY = os.environ.get('SECRET_KEY', LaxyProxy(lambda: SECRETS.get('SECRET_KEY')))
ifranco14 commented 3 years ago

hey guys! any news on this? or maybe a way to improve it to work in local and staging/production? @CelestialGuru @LeeHanYeong

I could give a hand and create a PR :)