Skip to content

Commit

Permalink
[AIRFLOW-2886] Generate random Flask SECRET_KEY in default config (#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.

(cherry picked from commit f7602f8)
  • Loading branch information
XD-DENG authored and ashb committed Dec 3, 2020
1 parent a8900fa commit 6b06584
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 17 deletions.
5 changes: 2 additions & 3 deletions airflow/config_templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,11 @@
- name: secret_key
description: |
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
It should be as random as possible
version_added: ~
type: string
example: ~
default: "temporary_key"
default: "{SECRET_KEY}"
- name: workers
description: |
Number of workers to run the Gunicorn web server
Expand Down
5 changes: 2 additions & 3 deletions airflow/config_templates/default_airflow.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,8 @@ worker_refresh_interval = 30
reload_on_plugin_change = False

# 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 @@ -706,6 +707,8 @@ def get_airflow_test_config(airflow_home):
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
7 changes: 1 addition & 6 deletions airflow/www/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ def create_app(config=None, testing=False):
x_port=conf.getint("webserver", "PROXY_FIX_X_PORT", fallback=1),
x_prefix=conf.getint("webserver", "PROXY_FIX_X_PREFIX", fallback=1)
)
app.secret_key = conf.get('webserver', 'SECRET_KEY')
app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(minutes=settings.get_session_lifetime_config())
app.config['LOGIN_DISABLED'] = not conf.getboolean(
'webserver', 'AUTHENTICATE')

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 = conf.get('webserver', 'SECRET_KEY')

app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SECURE'] = conf.getboolean('webserver', 'COOKIE_SECURE')
Expand Down
6 changes: 1 addition & 5 deletions airflow/www_rbac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,9 @@ def create_app(config=None, session=None, testing=False, app_name="Airflow"):
x_port=conf.getint("webserver", "PROXY_FIX_X_PORT", fallback=1),
x_prefix=conf.getint("webserver", "PROXY_FIX_X_PREFIX", fallback=1)
)
app.secret_key = conf.get('webserver', 'SECRET_KEY')
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=settings.get_session_lifetime_config())

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')

app.config.from_pyfile(settings.WEBSERVER_CONFIG, silent=True)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Expand Down

0 comments on commit 6b06584

Please sign in to comment.