Skip to content

Commit

Permalink
[AIRFLOW-2886] Secure Flask SECRET_KEY (#3738)
Browse files Browse the repository at this point in the history
The Flask SECRET_KEY should be as random as possible.

On the other hand, we can nott genrate random value when
we launch the webserver (the secret_key will be
inconsistent across the workers).

We can generate a random one in the configuration file
airflow.cfg, just like how we deal with FERNET_KEY.

The SECRET_KEY is generated using os.urandom, as
recommended by Flask community.
  • Loading branch information
XD-DENG authored and Tao Feng committed Aug 14, 2018
1 parent 9d68fa3 commit f7602f8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
5 changes: 2 additions & 3 deletions airflow/config_templates/default_airflow.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,8 @@ worker_refresh_batch_size = 1
worker_refresh_interval = 30

# Secret key used to run your flask app
# If default value is given ("temporary_key"), a random secret_key will be generated
# when you launch your webserver for security reason
secret_key = temporary_key
# It should be as random as possible
secret_key = {SECRET_KEY}

# Number of workers to run the Gunicorn web server
workers = 4
Expand Down
3 changes: 3 additions & 0 deletions airflow/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import print_function
from __future__ import unicode_literals

from base64 import b64encode
from builtins import str
from collections import OrderedDict
import copy
Expand Down Expand Up @@ -478,6 +479,8 @@ def parameterized_config(template):
else:
FERNET_KEY = ''

SECRET_KEY = b64encode(os.urandom(16)).decode('utf-8')

TEMPLATE_START = (
'# ----------------------- TEMPLATE BEGINS HERE -----------------------')
if not os.path.isfile(TEST_CONFIG_FILE):
Expand Down
8 changes: 1 addition & 7 deletions airflow/www/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ def create_app(config=None, testing=False):

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)

if configuration.conf.get('webserver', 'SECRET_KEY') == "temporary_key":
log.info("SECRET_KEY for Flask App is not specified. Using a random one.")
app.secret_key = os.urandom(16)
else:
app.secret_key = configuration.conf.get('webserver', 'SECRET_KEY')

app.secret_key = configuration.conf.get('webserver', 'SECRET_KEY')
app.config['LOGIN_DISABLED'] = not configuration.conf.getboolean(
'webserver', 'AUTHENTICATE')

Expand Down
5 changes: 1 addition & 4 deletions airflow/www_rbac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ def create_app(config=None, session=None, testing=False, app_name="Airflow"):
global app, appbuilder
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
if conf.get('webserver', 'SECRET_KEY') == "temporary_key":
app.secret_key = os.urandom(16)
else:
app.secret_key = conf.get('webserver', 'SECRET_KEY')
app.secret_key = conf.get('webserver', 'SECRET_KEY')

airflow_home_path = conf.get('core', 'AIRFLOW_HOME')
webserver_config_path = airflow_home_path + '/webserver_config.py'
Expand Down

0 comments on commit f7602f8

Please sign in to comment.