To directly answer your two questions:
- The secret keys are stored external to the source code so that they are not commit to revision control.
- The secret key isn't created by exporting the environment variable, nor is it created using the value in the environment variable.
The information you probably are really after:
First, keep in mind that I am not a cryptology expert! With that out of the way…
What you need to do is to generate some secret of appropriate size and cryptographic security for your application, then set the environment variable to be that value.
I'm making a guess that the secret key is being used in relation with the flask.ext.login.make_secure_token method of the Flask-Login module you linked to. If this is the case, looking at the source code, the key is being used with HMAC for a SHA-512. Ideally, the key should be the same as the blocksize used by the algorithm which, in this case, as indicated by the source for the Python 2.7 hmac implementation is 64 for 512-bit HMAC. If the key is smaller than the blocksize, it will be padded with zeros; if larger, it will hashed down to the blocksize.
The Flask quickstart documentation section for sessions provides an example backed up by Python's os.urandom documentation for generating crytographically suitable random bytes to use for the secret key. I would alter their example as we want a key for a blocksize of 64, rather than 24, to be:
import os
os.urandom(64)
Take the result of that and set the environment variable to the value. Using the Flask example directly (don't use these values for your code):
>>> import os
>>> os.urandom(24)
'\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
You would take the output, and set the environment variable to that value:
export BLOGFUL_SECRET_KEY='\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
Answer from lagweezle on Stack OverflowTo directly answer your two questions:
- The secret keys are stored external to the source code so that they are not commit to revision control.
- The secret key isn't created by exporting the environment variable, nor is it created using the value in the environment variable.
The information you probably are really after:
First, keep in mind that I am not a cryptology expert! With that out of the way…
What you need to do is to generate some secret of appropriate size and cryptographic security for your application, then set the environment variable to be that value.
I'm making a guess that the secret key is being used in relation with the flask.ext.login.make_secure_token method of the Flask-Login module you linked to. If this is the case, looking at the source code, the key is being used with HMAC for a SHA-512. Ideally, the key should be the same as the blocksize used by the algorithm which, in this case, as indicated by the source for the Python 2.7 hmac implementation is 64 for 512-bit HMAC. If the key is smaller than the blocksize, it will be padded with zeros; if larger, it will hashed down to the blocksize.
The Flask quickstart documentation section for sessions provides an example backed up by Python's os.urandom documentation for generating crytographically suitable random bytes to use for the secret key. I would alter their example as we want a key for a blocksize of 64, rather than 24, to be:
import os
os.urandom(64)
Take the result of that and set the environment variable to the value. Using the Flask example directly (don't use these values for your code):
>>> import os
>>> os.urandom(24)
'\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
You would take the output, and set the environment variable to that value:
export BLOGFUL_SECRET_KEY='\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
Didn't realize the actual command to set the secret key is:
export BLOGFUL_SECRET_KEY="your_secret_key_here"
And the key can be generated with something like this using the Python interpreter:
>>> import os
>>> os.urandom(24)
'\x072\xe7E|ns\x18g\x80& \xa3\xbf\xed\x91\xe6\x08+\x88\xd6\xafe['
Hiding secret keys in .env file
python - Where do I get SECRET_KEY for Flask? - Stack Overflow
I don't know how set SECRET_KEY
python - How to get Flask app to run locally after exporting secret key as an environment variable? - Stack Overflow
I am not sure that this might be the correct subreddit, but if anyone could help or at least point me in the correct subreddit, it would be great!
So here it is. I have my website made from Flask and hosted on Heroku. Now the website uses google APIs and thus have a credentials.JSON file in my root folder.
Heroku is building the site from a git repository (is private due to the presence of the .JSON file). But I want to make it public and thus would be required to hide the credentials.JSON file in such a manner that GitHub ignores that file but Heroku doesn't.
I know it sounds ridiculous to do so, but when I asked my friend, he told me that I can store it as an environment variable in a .env file. Can anyone help how to achieve this? TIA
Get the random string for secret key:
Method 1: Use os in Python 2/3:
>>> import os
>>> os.urandom(12)
'\xf0?a\x9a\\\xff\xd4;\x0c\xcbHi'
Method 2: Use uuid in Python 2/3:
>>> import uuid
>>> uuid.uuid4().hex
'3d6f45a5fc12445dbac2f59c3b6c7cb1'
Method 3: Use secrets in Python >= 3.6:
>>> import secrets
>>> secrets.token_urlsafe(16)
'Drmhze6EPcv0fN_81Bj-nA'
>>> secrets.token_hex(16)
'8f42a73054b1749f8f58848be5e6502c'
Method 4: Use os in Python 3:
>>> import os
>>> os.urandom(12).hex()
'f3cfe9ed8fae309f02079dbf'
Set secret key in Flask
Method 1: Use app.secret_key:
app.secret_key = 'the random string'
Method 2: Use app.config:
app.config['SECRET_KEY'] = 'the random string'
Method 3: Put it in your config file:
SECRET_KEY = 'the random string'
Then load the config form config file:
app.config.from_pyfile('config.py') # if your config file's name is config.py
The secret key is needed to keep the client-side sessions secure. You can generate some random key as below:
>>> import os
>>> os.urandom(24)
'\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
Just take that key and copy/paste it into your config file
SECRET_KEY = '\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
See Sessions documentation
Which of the two ways is correct?
SECRET_KEY = os.environ.get('SECRET_KEY') or 'myKey'or
SECRET_KEY = os.environ.get('SECRET_KEY') or os.urandom(24)python-dotenv actually has nothing to do with Flask. It is for your .env file to be translated into actual env variables. So if you're going to have actual env variables without it, your os.getenv should still work.
Sidenote: You can also use os.environ:
os.environ.get("SECRET")
Set your environment variable in the interpreter:
export SECRET_KEY=123
Call the variable with environ.get():
from os import environ
from flask import Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = environ.get('SECRET_KEY')
I'm writing some of my first real-world code and have stumbled upon a project of setting up a simple Stripe payment gateway.
It's running fine locally using virtualenv when I launch it with
PUBLISHABLE_KEY=pk_test_co4ooReepa SECRET_KEY=sk_test_co4ooReepa python app.py
I've set up virtualenv in Webfaction following this guide and can get it working if I set the secret_key and publishable_key in the app.py which I know is unsanitary.
I therefore want to maintain the current code in app.py
Stripe_keys = {
'secret_key': os.environ['SECRET_KEY'],
'publishable_key': os.environ['PUBLISHABLE_KEY']
}and pass in the environment variable.
I've tried putting them in httpd.conf using SetEnv with no luck.
I've tried appending them to virtualenv's bin/activate with no luck.
Apache just continually throws me this error
[wsgi:error] mod_wsgi (pid=10968): Target WSGI script '/home/myname/webapps/myapp/htdocs/index.py' cannot be loaded as Python module. [wsgi:error] mod_wsgi (pid=10968): Exception occurred processing WSGI script '/home/myname/webapps/myapp/htdocs/index.py'. [wsgi:error] Traceback (most recent call last): [wsgi:error] File "/home/myname/webapps/myapp/htdocs/index.py", line 17, in <module> [wsgi:error] from app import app as application [wsgi:error] File "/home/myname/webapps/myapp/src/app.py", line 6, in <module> [wsgi:error] 'secret_key': os.environ['SECRET_KEY'], [wsgi:error] File "/usr/local/lib/python3.5/os.py", line 725, in __getitem__ [wsgi:error] raise KeyError(key) from None [wsgi:error] KeyError: 'SECRET_KEY'
What am I doing dumb?
I'm on my phone and I've never used Webfaction but a quick search showed that it doesn't seem trivial to set environment variables there.
What I suggest instead is that you load sensitive configuration values from a separate file that you don't check into version control.
a flask snippet that explains how to set up a secret key in a file: http://flask.pocoo.org/snippets/104/