I use Python and Django to create web applications, which we store in source control. The way Django is normally set up, the passwords are in plain text within the settings.py f
Although environment variables are convenient for a lot of configuration, putting passwords in environment variables is not secure. With the alternative being a configuration file outside regular version control, here are some various cons:
Environment variables might accidentally leak (through debugging channels that might get transmitted via plaintext, to end-users, or to unexpected places in the filesystem like ~/.*sh_history).
Configuration files might accidentally get added to version control and end up in repositories accessible to people without deployment privileges.
Read the blog post Environment Variables Considered Harmful for Your Secrets for more arguments: The environment is accessible to the entire process, is inherited to child (and possibly 3rd-party) processes, and there exists no clear assumption among external developers to treat environment variables as confidential.
The simplest configuration file format in Python is simply a Python module.
Having something like this in your settings.py:
db_user = 'my_db_user'
db_password = 'my_db_password'
Hard codes valuable information in your code and does pose a security risk. An alternative is to store your valuable information (Api keys, database passwords etc.) on your local machine as environment variables. E.g. on linux you could add:
export DB_USER = "my_db_user"
export DB_PASS = "my_db_password"
to your .bash_profile. Or there is usually an option with your hosting provider to set environment variables e.g. with AWS elastic beanstalk you can add env variables under your configuration on console.
Then to retrieve your information import os:
import os
db_user = os.environ.get['DB_USER']
db_password = os.environ.get['DB_PASS']
Although I wasn't able to come across anything Python-specific on stackoverflow, I did find a website that was helpful, and thought I'd share the solution with the rest of the community.
The solution: environment variables.
Note: Although environment variables are similar in both Linux/Unix/OS X and in the Windows worlds, I haven't tested this code on a Windows machine. Please let me know if it works.
In your bash/sh shell, type:
export MYAPP_DB_USER='myapp'
export MYAPP_DB_PASSWORD='testing123'
And in your Django settings.py file:
DATABASE_USER = os.environ.get("MYAPP_DB_USER", '')
DATABASE_PASSWORD = os.environ.get("MYAPP_DB_PASSWORD", '')
In this case, the username and password would default to an empty string if the environment variable didn't exist.
This is an old question and the person who asked I'm sure has found a way to deal with this, but I was looking this up myself and figured since the answers here weren't quite the solution I was looking for I might add what I did for any other people potentially asking the same question.
What I did was use getpass() to have the settings file ask for the password when run at startup.
from getpass import getpass
#[...]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #or whatever DB you use
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': getpass(),
'HOST': '',
'PORT': '',
}
}