Skip to content

Commit 9c0db65

Browse files
pierrejeambrundadonnelly316
authored andcommitted
Move secret_key config to api section (apache#50839)
* Move secret_key config to api section * Fix CI * Fix CI
1 parent 1c99be9 commit 9c0db65

File tree

17 files changed

+42
-34
lines changed

17 files changed

+42
-34
lines changed

airflow-core/docs/administration-and-deployment/logging-monitoring/logging-tasks.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Most task handlers send logs upon completion of a task. In order to view logs in
178178
In triggerer, logs are served unless the service is started with option ``--skip-serve-logs``.
179179

180180
The server is running on the port specified by ``worker_log_server_port`` option in ``[logging]`` section, and option ``triggerer_log_server_port`` for triggerer. Defaults are 8793 and 8794, respectively.
181-
Communication between the webserver and the worker is signed with the key specified by ``secret_key`` option in ``[webserver]`` section. You must ensure that the key matches so that communication can take place without problems.
181+
Communication between the webserver and the worker is signed with the key specified by ``secret_key`` option in ``[api]`` section. You must ensure that the key matches so that communication can take place without problems.
182182

183183
We are using `Gunicorn <https://gunicorn.org/>`__ as a WSGI server. Its configuration options can be overridden with the ``GUNICORN_CMD_ARGS`` env variable. For details, see `Gunicorn settings <https://docs.gunicorn.org/en/latest/settings.html#settings>`__.
184184

airflow-core/docs/cli-and-env-variables-ref.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Environment Variables
7777
* ``result_backend`` in ``[celery]`` section
7878
* ``password`` in ``[atlas]`` section
7979
* ``smtp_password`` in ``[smtp]`` section
80-
* ``secret_key`` in ``[webserver]`` section
80+
* ``secret_key`` in ``[api]`` section
8181

8282
.. envvar:: AIRFLOW__{SECTION}__{KEY}_SECRET
8383

airflow-core/docs/configurations-ref.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ can set in ``airflow.cfg`` file or using environment variables.
2424

2525
Use the same configuration across all the Airflow components. While each component
2626
does not require all, some configurations need to be same otherwise they would not
27-
work as expected. A good example for that is :ref:`secret_key<config:webserver__secret_key>` which
27+
work as expected. A good example for that is :ref:`secret_key<config:api__secret_key>` which
2828
should be same on the Webserver and Worker to allow Webserver to fetch logs from Worker.
2929

3030
The webserver key is also used to authorize requests to Celery workers when logs are retrieved. The token

airflow-core/docs/howto/set-config.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ The following config options support this ``_cmd`` and ``_secret`` version:
105105
* ``result_backend`` in ``[celery]`` section
106106
* ``password`` in ``[atlas]`` section
107107
* ``smtp_password`` in ``[smtp]`` section
108-
* ``secret_key`` in ``[webserver]`` section
108+
* ``secret_key`` in ``[api]`` section
109109

110110
The ``_cmd`` config options can also be set using a corresponding environment variable
111111
the same way the usual config options can. For example:
@@ -159,7 +159,7 @@ the example below.
159159
.. note::
160160
Use the same configuration across all the Airflow components. While each component
161161
does not require all, some configurations need to be same otherwise they would not
162-
work as expected. A good example for that is :ref:`secret_key<config:webserver__secret_key>` which
162+
work as expected. A good example for that is :ref:`secret_key<config:api__secret_key>` which
163163
should be same on the Webserver and Worker to allow Webserver to fetch logs from Worker.
164164

165165
The webserver key is also used to authorize requests to Celery workers when logs are retrieved. The token

airflow-core/src/airflow/api_fastapi/core_api/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def init_config(app: FastAPI) -> None:
155155
# and 9 (slowest, most compression)
156156
app.add_middleware(GZipMiddleware, minimum_size=1024, compresslevel=5)
157157

158-
app.state.secret_key = get_signing_key("webserver", "secret_key")
158+
app.state.secret_key = get_signing_key("api", "secret_key")
159159

160160

161161
def init_error_handlers(app: FastAPI) -> None:

airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get_timetable_summary(cls, tts: str | None) -> str | None:
107107
@property
108108
def file_token(self) -> str:
109109
"""Return file token."""
110-
serializer = URLSafeSerializer(conf.get_mandatory_value("webserver", "secret_key"))
110+
serializer = URLSafeSerializer(conf.get_mandatory_value("api", "secret_key"))
111111
payload = {
112112
"bundle_name": self.bundle_name,
113113
"relative_fileloc": self.relative_fileloc,

airflow-core/src/airflow/cli/commands/config_command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ def message(self) -> str | None:
410410
config=ConfigParameter("webserver", "base_url"),
411411
renamed_to=ConfigParameter("api", "base_url"),
412412
),
413+
ConfigChange(
414+
config=ConfigParameter("webserver", "secret_key"),
415+
renamed_to=ConfigParameter("api", "secret_key"),
416+
),
413417
ConfigChange(
414418
config=ConfigParameter("webserver", "web_server_host"),
415419
renamed_to=ConfigParameter("api", "host"),

airflow-core/src/airflow/config_templates/config.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,20 @@ secrets:
12841284
api:
12851285
description: ~
12861286
options:
1287+
secret_key:
1288+
description: |
1289+
Secret key used to run your api server. It should be as random as possible. However, when running
1290+
more than 1 instances of the api, make sure all of them use the same ``secret_key`` otherwise
1291+
one of them will error with "CSRF session token is missing".
1292+
The api key is also used to authorize requests to Celery workers when logs are retrieved.
1293+
The token generated using the secret key has a short expiry time though - make sure that time on
1294+
ALL the machines that you run airflow components on is synchronized (for example using ntpd)
1295+
otherwise you might get "forbidden" errors when the logs are accessed.
1296+
version_added: ~
1297+
type: string
1298+
sensitive: true
1299+
example: ~
1300+
default: "{SECRET_KEY}"
12871301
expose_config:
12881302
description: |
12891303
Expose the configuration file in the web server. Set to ``non-sensitive-only`` to show all values
@@ -1748,20 +1762,6 @@ operators:
17481762
webserver:
17491763
description: ~
17501764
options:
1751-
secret_key:
1752-
description: |
1753-
Secret key used to run your api server. It should be as random as possible. However, when running
1754-
more than 1 instances of webserver, make sure all of them use the same ``secret_key`` otherwise
1755-
one of them will error with "CSRF session token is missing".
1756-
The webserver key is also used to authorize requests to Celery workers when logs are retrieved.
1757-
The token generated using the secret key has a short expiry time though - make sure that time on
1758-
ALL the machines that you run airflow components on is synchronized (for example using ntpd)
1759-
otherwise you might get "forbidden" errors when the logs are accessed.
1760-
version_added: ~
1761-
type: string
1762-
sensitive: true
1763-
example: ~
1764-
default: "{SECRET_KEY}"
17651765
instance_name:
17661766
description: |
17671767
Sets a custom page title for the DAGs overview page and site title for all pages

airflow-core/src/airflow/configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def sensitive_config_values(self) -> set[tuple[str, str]]:
362362
("fab", "navbar_text_color"): ("webserver", "navbar_text_color", "3.0.2"),
363363
("fab", "navbar_hover_color"): ("webserver", "navbar_hover_color", "3.0.2"),
364364
("fab", "navbar_text_hover_color"): ("webserver", "navbar_text_hover_color", "3.0.2"),
365+
("api", "secret_key"): ("webserver", "secret_key", "3.0.2"),
365366
("api", "grid_view_sorting_order"): ("webserver", "grid_view_sorting_order", "3.1.0"),
366367
("api", "log_fetch_timeout_sec"): ("webserver", "log_fetch_timeout_sec", "3.1.0"),
367368
("api", "hide_paused_dags_by_default"): ("webserver", "hide_paused_dags_by_default", "3.1.0"),

airflow-core/src/airflow/utils/log/file_task_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _fetch_logs_from_service(url, log_relative_path):
109109

110110
timeout = conf.getint("api", "log_fetch_timeout_sec", fallback=None)
111111
generator = JWTGenerator(
112-
secret_key=get_signing_key("webserver", "secret_key"),
112+
secret_key=get_signing_key("api", "secret_key"),
113113
# Since we are using a secret key, we need to be explicit about the algorithm here too
114114
algorithm="HS512",
115115
private_key=None,

airflow-core/src/airflow/utils/serve_logs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def create_app():
7272
raise ImportError(f"Unable to load {log_config_class} due to error: {e}")
7373
signer = JWTValidator(
7474
issuer=None,
75-
secret_key=get_signing_key("webserver", "secret_key"),
75+
secret_key=get_signing_key("api", "secret_key"),
7676
algorithm="HS512",
7777
leeway=leeway,
7878
audience="task-instance-logs",

airflow-core/tests/unit/core/test_configuration.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ def test_get_section_should_respect_cmd_env_variable(self, tmp_path, monkeypatch
609609
cmd_file.write_text("#!/usr/bin/env bash\necho -n difficult_unpredictable_cat_password\n")
610610
cmd_file.chmod(0o0555)
611611

612-
monkeypatch.setenv("AIRFLOW__WEBSERVER__SECRET_KEY_CMD", str(cmd_file))
613-
content = conf.getsection("webserver")
612+
monkeypatch.setenv("AIRFLOW__API__SECRET_KEY_CMD", str(cmd_file))
613+
content = conf.getsection("api")
614614
assert content["secret_key"] == "difficult_unpredictable_cat_password"
615615

616616
@pytest.mark.parametrize(
@@ -628,11 +628,13 @@ def test_config_value_types(self, key, type):
628628
assert isinstance(section_dict[key], type)
629629

630630
def test_command_from_env(self):
631-
test_cmdenv_config = textwrap.dedent("""\
631+
test_cmdenv_config = textwrap.dedent(
632+
"""\
632633
[testcmdenv]
633634
itsacommand=NOT OK
634635
notacommand=OK
635-
""")
636+
"""
637+
)
636638
test_cmdenv_conf = AirflowConfigParser()
637639
test_cmdenv_conf.read_string(test_cmdenv_config)
638640
test_cmdenv_conf.sensitive_config_values.add(("testcmdenv", "itsacommand"))
@@ -1712,7 +1714,7 @@ def test_sensitive_values():
17121714
("database", "sql_alchemy_conn"),
17131715
("core", "fernet_key"),
17141716
("api_auth", "jwt_secret"),
1715-
("webserver", "secret_key"),
1717+
("api", "secret_key"),
17161718
("secrets", "backend_kwargs"),
17171719
("sentry", "sentry_dsn"),
17181720
("database", "sql_alchemy_engine_args"),
@@ -1723,6 +1725,7 @@ def test_sensitive_values():
17231725
("celery", "result_backend"),
17241726
("opensearch", "username"),
17251727
("opensearch", "password"),
1728+
("webserver", "secret_key"),
17261729
}
17271730
all_keys = {(s, k) for s, v in conf.configuration_description.items() for k in v.get("options")}
17281731
suspected_sensitive = {(s, k) for (s, k) in all_keys if k.endswith(("password", "kwargs"))}

contributing-docs/testing/k8s_tests.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ Should show the status of current KinD cluster.
469469
unnecessary restarts of your Airflow components.
470470
471471
Information on how to set a static webserver secret key can be found here:
472-
https://airflow.apache.org/docs/helm-chart/stable/production-guide.html#webserver-secret-key
472+
https://airflow.apache.org/docs/helm-chart/stable/production-guide.html#api-secret-key
473473
Deployed kind-airflow-python-3.9-v1.24.2 with Airflow Helm Chart.
474474
475475
Airflow for Python 3.9 and K8S version v1.24.2 has been successfully deployed.

dev/breeze/src/airflow_breeze/params/shell_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def env_variables_for_docker_commands(self) -> dict[str, str]:
528528
"AIRFLOW__CORE__SIMPLE_AUTH_MANAGER_PASSWORDS_FILE",
529529
"/opt/airflow/dev/breeze/src/airflow_breeze/files/simple_auth_manager_passwords.json",
530530
)
531-
_set_var(_env, "AIRFLOW__WEBSERVER__SECRET_KEY", b64encode(os.urandom(16)).decode("utf-8"))
531+
_set_var(_env, "AIRFLOW__API__SECRET_KEY", b64encode(os.urandom(16)).decode("utf-8"))
532532
if self.executor == EDGE_EXECUTOR:
533533
_set_var(
534534
_env,

devel-common/src/tests_common/pytest_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,10 +1765,10 @@ def secret_key() -> str:
17651765
"""Return secret key configured."""
17661766
from airflow.configuration import conf
17671767

1768-
the_key = conf.get("webserver", "SECRET_KEY")
1768+
the_key = conf.get("api", "SECRET_KEY")
17691769
if the_key is None:
17701770
raise RuntimeError(
1771-
"The secret key SHOULD be configured as `[webserver] secret_key` in the "
1771+
"The secret key SHOULD be configured as `[api] secret_key` in the "
17721772
"configuration/environment at this stage! "
17731773
)
17741774
return the_key

providers/fab/src/airflow/providers/fab/www/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def create_app(enable_plugins: bool):
5656
from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager
5757

5858
flask_app = Flask(__name__)
59-
flask_app.secret_key = conf.get("webserver", "SECRET_KEY")
59+
flask_app.secret_key = conf.get("api", "SECRET_KEY")
6060
flask_app.config["SQLALCHEMY_DATABASE_URI"] = conf.get("database", "SQL_ALCHEMY_CONN")
6161
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
6262
flask_app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(minutes=get_session_lifetime_config())

providers/fab/www-hash.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f962d6f103d24518ac6d3b902b0b30652e44f24293142f94f28684ab72e10d64
1+
e6d566ed6f8c2b19eda800f6bb53b2bf7b3ede110978d3628e55f1e63f201704

0 commit comments

Comments
 (0)