On 10 December 2020 at 01:18:16, Pankaj Jangid (pan...@codeisgreat.org) wrote:
Fenrir Sivar <fenrir...@gmail.com> writes:
> I inherited a django app that calls private APIs in a view to fetch some
> data. The credentials or api keys are currently hardcoded in the source,
> making it difficult to share.
> What is the preferred way to store these kind of keys in a secure way only
> accessible to the django server?
As Kasper suggests, I also use environment variables in development
environment. But a few more things add up to convenience.
Not only convenience, but a very good idea. First and foremost, keep secrets out of configuration even gitignore'd ones, with .env files being the exception.
The environment is not a perfect place to store these but they are better then placing them in code or configuration.
For more info and a very tried and tested approach, check out https://12factor.net/
1. I use ‘direnv’ for managing per directory environment variables. As I
enter a perticular directory (on terminal), it echos the newly set
environment variables.
Seconded ... and thirded!
If you use .env files you can even suck these in automatically, set your virtualenv and any configuration information in your .envrc. It is a really good approach especially so i you work on multiple projects and don't want to clutter your .*profile or Windows global env.
2. It might be overkill but I use Docker on my development machine. And
I use the same environment variables in my docker-compose.yml. This
is also useful in production setup where you are deploying in Docker.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPvfA9A1zjp76qNf%2BN0QgacHuSzT0LQ246hFHoJCb7Bj1m1tYg%40mail.gmail.com.
I use multiple settings.py files to do this. Specifically, in my main one, I have what I consider the only acceptable instance of import *.
Slightly off-topic (subject changed to reflect that), but there are other exceptions as well that conform to a similar pattern.
For example, refactoring models.py to a package requires adding an import into __init__.py (unless you want to change all dependent code and often needlessly cluttering import statements). Importing `*` instead if individual submodules into `__init__.py` is often the way to go and leads to less changes when you add models in new modules.
PEP8 doesn't explicitly disallow this, but only strongly recommends against it, but the concern is very project specific. I just use it in django for this use case but rarely otherwise.
Regards,
/d