Skip to content

Commit 6b06584

Browse files
XD-DENGashb
authored andcommitted
[AIRFLOW-2886] Generate random Flask SECRET_KEY in default config (apache#3738)
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)
1 parent a8900fa commit 6b06584

File tree

5 files changed

+9
-17
lines changed

5 files changed

+9
-17
lines changed

airflow/config_templates/config.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,11 @@
737737
- name: secret_key
738738
description: |
739739
Secret key used to run your flask app
740-
If default value is given ("temporary_key"), a random secret_key will be generated
741-
when you launch your webserver for security reason
740+
It should be as random as possible
742741
version_added: ~
743742
type: string
744743
example: ~
745-
default: "temporary_key"
744+
default: "{SECRET_KEY}"
746745
- name: workers
747746
description: |
748747
Number of workers to run the Gunicorn web server

airflow/config_templates/default_airflow.cfg

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,8 @@ worker_refresh_interval = 30
362362
reload_on_plugin_change = False
363363

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

369368
# Number of workers to run the Gunicorn web server
370369
workers = 4

airflow/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from __future__ import print_function
2323
from __future__ import unicode_literals
2424

25+
from base64 import b64encode
2526
from builtins import str
2627
from collections import OrderedDict
2728
import copy
@@ -706,6 +707,8 @@ def get_airflow_test_config(airflow_home):
706707
else:
707708
FERNET_KEY = ''
708709

710+
SECRET_KEY = b64encode(os.urandom(16)).decode('utf-8')
711+
709712
TEMPLATE_START = (
710713
'# ----------------------- TEMPLATE BEGINS HERE -----------------------')
711714
if not os.path.isfile(TEST_CONFIG_FILE):

airflow/www/app.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,11 @@ def create_app(config=None, testing=False):
6161
x_port=conf.getint("webserver", "PROXY_FIX_X_PORT", fallback=1),
6262
x_prefix=conf.getint("webserver", "PROXY_FIX_X_PREFIX", fallback=1)
6363
)
64-
app.secret_key = conf.get('webserver', 'SECRET_KEY')
6564
app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(minutes=settings.get_session_lifetime_config())
6665
app.config['LOGIN_DISABLED'] = not conf.getboolean(
6766
'webserver', 'AUTHENTICATE')
6867

69-
if configuration.conf.get('webserver', 'SECRET_KEY') == "temporary_key":
70-
log.info("SECRET_KEY for Flask App is not specified. Using a random one.")
71-
app.secret_key = os.urandom(16)
72-
else:
73-
app.secret_key = configuration.conf.get('webserver', 'SECRET_KEY')
68+
app.secret_key = conf.get('webserver', 'SECRET_KEY')
7469

7570
app.config['SESSION_COOKIE_HTTPONLY'] = True
7671
app.config['SESSION_COOKIE_SECURE'] = conf.getboolean('webserver', 'COOKIE_SECURE')

airflow/www_rbac/app.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,9 @@ def create_app(config=None, session=None, testing=False, app_name="Airflow"):
6161
x_port=conf.getint("webserver", "PROXY_FIX_X_PORT", fallback=1),
6262
x_prefix=conf.getint("webserver", "PROXY_FIX_X_PREFIX", fallback=1)
6363
)
64-
app.secret_key = conf.get('webserver', 'SECRET_KEY')
6564
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=settings.get_session_lifetime_config())
6665

67-
if conf.get('webserver', 'SECRET_KEY') == "temporary_key":
68-
app.secret_key = os.urandom(16)
69-
else:
70-
app.secret_key = conf.get('webserver', 'SECRET_KEY')
66+
app.secret_key = conf.get('webserver', 'SECRET_KEY')
7167

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

0 commit comments

Comments
 (0)