Hide Your Secret Keys Using Environment Variables | by AnBento | Towards Data Science

A course I would strongly recommend to my readers:
Command Line Automation In Python (DataCamp) → Learn to automate many common file system tasks and be able to manage and communicate with processes.
Suppose you were working on a data science project and you decided to pull data from the company database by establishing a connection using Python. What if there was a need to share your script via GitHub for the wider team to review? Well, in that case, better your secret details were hidden in order to avoid other people misusing them.
In this article I will show how to use environment variables through the command line to keep username, password and database name safe, avoiding undesired information leakage.
towardsdatascience.com
Environment variables are KEY=value pairs that like Shell variables can be created through the command line, but more powerful as, once declared, they can be used by any programming language since they are linked to your environment/operating system.
In the command line environment:
the variable KEY has to be written in capital letters and can be a mix of characters, numbers and underscores.
on the other hand, any VALUE data type can be assigned to them and quotes are optional when using strings in Bash, unless the string contains a space. This is because Bash is sensitive to spaces, so strings that have them won’t work properly unless surrounded with quotes.
Lastly, no space should be left before and after the “=” symbol as shown below:
MY_VARIABLE=”my_value” --> works(capital, no space, quotes)MY_VARIABLE=my_value --> works(capital, no space, no quotes but underscore)MY_VARIABLE=my value --> fails(capital, no space, space in value)MY_VARIABLE = ”my_value” --> fails(capital, space between equal sign, quotes)
towardsdatascience.com
An easy way to permanently declare an environment variable while using Mac OS, is to add it to the .bash_profile that is an hidden file defined in your home directory used to configure the Bash Shell in your system. To access the file, first run your terminal, navigate to the home directory with cd ~, then open the file editor with open .bash_profile command:
$ cd ~$ open .bash_profile
In the file editor type the KEY=value pairs for the secret info you want to hide, preceded by the export command that indicates that you wish for these variables to be also available outside the shell:
export USERNAME=”Insert Your User Name”export DBPW=”Insert Your PW"export DBNAME=”Insert Your DB Name"
Once all the variables have been added, save the text file and refresh the bash profile with the source .bash_profile command:
$ source .bash_profile
If you omit running the command above, you will not be able to immediately see the environment variables just created and you will need to exit and log back in the command line.
Now, before attempting a database connection, let’s make sure that the variables can actually be used in Python by opening the interactive Python console in the command line and importing the os module. Among other things, the os module enables Python to use your operating system environment variables. You can list them with os.environ, a method that returns a Python dictionary including all the variables that are currently available in your system or select each key as shown below:
$ ipythonIn [1]: import osIn [2]: os.environ['USERNAME']Out[2]: 'Insert Your User Name'In [3]: os.environ['DBPW']Out[3]: 'Insert Your PW'In [4]: os.environ['DBNAME']Out[4]: 'Insert Your DB Name'
Terrific! It seems that you can now replace secret info strings with environment variables KEYS in the database connection settings. If you like me use psycopg2.connect() and pd.read_sql() methods to connect to Redshift, you will end up with something like this:
In this tutorial, I have tried to solve a practical problem that I faced a few times in the recent past: hiding secret keys while working on shared Python scripts and I hope you will find environment variables as useful for your workflow as they are for me. Please note that this is only an introductory guide and there is a lot more to learn about environment variables. For instance, a course that I found particularly enlightening is Command Line Automation In Python by DataCamp.
Disclaimer: This post includes affiliate links for which I may make a small commission at no extra cost to you should, you make a purchase.