0

In my __init__.py file I am trying to set:

import os
app.config['SECRET_KEY'] = os.environ['MY_KEY']

but am getting the error:

raise KeyError(key)
KeyError: 'MY_KEY'

When I run printenv, the variable MY_KEY is present.

Also in IDLE I tried running:

import os
print os.environ['MY_KEY']

and I get the correct output.

I set MY_KEY in /etc/profile using:

export MY_KEY="1234example_secret_key"

I did restart my computer after making the change to the profile file.

Would anyone know what the issue may be?

Thanks for your help.

  • How are you running your flask app? Not all invocations will evaluate /etc/profile. The system's apache install, for instance, might not, depending on setup. – pvg Jul 12 '15 at 02:48
  • @pvg I am using `Nginx/Gunicorn`. I run the app using `supervisor`. –  Jul 12 '15 at 02:49
  • 1
    http://stackoverflow.com/questions/19054008/how-to-use-environment-variables-with-supervisor-gunicorn-and-django-1-6 Another, potentially simpler approach is to have this info in a python file and import it. Keep out of source control and have your deploy script generate it. Keep a template version under source control with a different name. So, if you have app_config.py with the params, maintain an app_config_sample.py for documentation purposes. If you wan to get fancy, have your build script generate app_config out of sample. – pvg Jul 12 '15 at 02:57
  • @Giri You can pass env variable to supervisor process using environment config param environment = MY_KEY="ABCD",MY_KEY2="EFG". – Boris Jul 12 '15 at 03:08
  • @Giri yet another option is to simply not use Flask's default encrypted cookies for sessions and use something like Beaker. No weird secret keys, no sending sensitive data back and forth to the client, the whole thing just goes away. – pvg Jul 12 '15 at 03:21
  • @pvg Thanks for your help. –  Jul 12 '15 at 06:53

1 Answers1

0

If you are running your process under supervisor and/or gunicorn (judging but you comments you are) you can user supervisor environment config param.

[program:my_app]
...
environment = MY_KEY="ABCD",MY_KEY2="EFG"

You can also use gunicorn --env flag or env config file param.

gunicorn -b 127.0.0.1:8000 --env MY_KEY=ABCD test:app

The downside of the 2nd option is that your key will be visible to anyone who has access to your machine.

The best approach is to use the app.config.from_envvar function and store your config into machine specific config settings location (this could be on encrypted filesystem). In this case your code would look like this:

app = Flask(__name__)
...
app.config.from_envvar('MACHINE_SPECIFIC_SETTINGS')

Your MACHINE_SPECIFIC_SETTINGS env variable could point to the file that will have the MY_KEY value.

MACHINE_SPECIFIC_SETTINGS=/path/to/config.py  
Boris
  • 2,275
  • 20
  • 21
  • Thanks @Boris, I will try this out. If I use your final suggestion, how/where should I be setting the `MACHINE_SPECIFIC_SETTINGS` variable? –  Jul 12 '15 at 04:36
  • @Giri You can use the supervisor environment config param or gunicorn --env flag. – Boris Jul 12 '15 at 04:40