Skip to content

Commit 94f820e

Browse files
author
abhishekmauryaKsolves
committed
Resolve team scoped spelling of per-key secrets-backend-kwarg options as sensitive
_is_per_key_sensitive_option and _mask_per_key_sensitive_options matched literal section names ('secrets', 'workers') only, so a team scoped spelling of the same option -- the [<team>=secrets] config-file section, or the AIRFLOW__<TEAM>___SECRETS__BACKEND_KWARG__* env var, both of which are reported under a section named after the team -- was never recognised as sensitive. Resolve the section to its base section via base_section_name (the same helper AirflowConfigParser.is_sensitive_option uses for registered options, introduced in apache#70755) before deciding sensitivity, in both _is_per_key_sensitive_option and _mask_per_key_sensitive_options. closes apache#71037
1 parent 5f77c84 commit 94f820e

3 files changed

Lines changed: 97 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Redact team scoped spellings of the per-key secrets-backend-kwarg options (``[<team>=secrets]`` / ``[<team>=workers]`` config sections, and the corresponding ``AIRFLOW__<TEAM>___...`` environment variables) in ``GET /config`` and ``GET /config/section/{section}/option/{option}``, the same way the non-team-scoped spelling is already redacted.

‎airflow-core/src/airflow/api_fastapi/core_api/services/public/config.py‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,52 @@
2424
from airflow.api_fastapi.common.types import Mimetype
2525
from airflow.api_fastapi.core_api.datamodels.config import Config
2626
from airflow.configuration import conf
27+
from airflow_shared.configuration.parser import base_section_name
2728

2829
# Per-key environment-variable overrides for secrets-backend kwargs are
2930
# surfaced by ``conf.as_dict`` as synthetic options under the ``secrets``
3031
# and ``workers`` sections. They carry the same secrets-backend material
3132
# (e.g. Vault role_id / secret_id) as the registered ``backend_kwargs``
3233
# option, so they need the same redaction treatment when
3334
# ``display_sensitive=False``.
34-
# These match literal section names only, so a team scoped spelling of the same option -- the
35-
# ``[<team>=secrets]`` section, or ``AIRFLOW__<TEAM>___SECRETS__BACKEND_KWARG__*``, which is
36-
# reported under a section named after the team -- is not recognised. Nothing leaks today because
37-
# the secrets backend is not team aware; tracked at
38-
# https://github.com/apache/airflow/issues/71037
35+
# A team scoped spelling of the same option -- the ``[<team>=secrets]`` config-file section, or
36+
# ``AIRFLOW__<TEAM>___SECRETS__BACKEND_KWARG__*``, which is reported under a section named after
37+
# the team -- is resolved back to its base section via ``base_section_name`` before matching, so
38+
# it is masked the same way. See https://github.com/apache/airflow/issues/71037
3939
_PER_KEY_SENSITIVE_PREFIXES: dict[str, str] = {
4040
"secrets": "backend_kwarg__",
4141
"workers": "secrets_backend_kwarg__",
4242
}
4343

4444

4545
def _is_per_key_sensitive_option(section: str, option: str) -> bool:
46-
"""Return True for synthetic per-key secrets-backend-kwarg options."""
47-
prefix = _PER_KEY_SENSITIVE_PREFIXES.get(section)
46+
"""
47+
Return True for synthetic per-key secrets-backend-kwarg options.
48+
49+
Resolves a team scoped section (e.g. ``myteam=secrets``) back to its base
50+
section (``secrets``) before matching, so a team scoped spelling of the
51+
same option is treated exactly like the base one.
52+
"""
53+
prefix = _PER_KEY_SENSITIVE_PREFIXES.get(base_section_name(section))
4854
return prefix is not None and option.startswith(prefix)
4955

5056

5157
def _mask_per_key_sensitive_options(conf_dict: dict) -> None:
52-
"""Mask synthetic per-key secrets-backend-kwarg options in-place."""
53-
for section, prefix in _PER_KEY_SENSITIVE_PREFIXES.items():
54-
options = conf_dict.get(section)
58+
"""
59+
Mask synthetic per-key secrets-backend-kwarg options in-place.
60+
61+
Iterates every section actually present in ``conf_dict`` -- rather than
62+
only the literal ``secrets`` / ``workers`` keys -- and resolves each one
63+
to its base section, so a team scoped section (config-file spelling) or a
64+
team scoped env var (which ``conf.as_dict`` reports under a
65+
team-derived section) is masked as well.
66+
"""
67+
for section, options in conf_dict.items():
5568
if not options:
5669
continue
70+
prefix = _PER_KEY_SENSITIVE_PREFIXES.get(base_section_name(section))
71+
if prefix is None:
72+
continue
5773
for option in list(options):
5874
if option.startswith(prefix):
5975
current = options[option]

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_config.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,73 @@ def test_get_config_value_keeps_team_scoped_non_sensitive_option(self, test_clie
623623
)
624624
assert response.status_code == 200
625625
assert response.json()["sections"][0]["options"][0]["value"] == OPTION_VALUE_TEAM_PARALLELISM
626+
627+
628+
SECTION_TEAM_SECRETS = f"{TEAM_NAME}={SECTION_SECRETS}"
629+
SECTION_TEAM_WORKERS = f"{TEAM_NAME}={SECTION_WORKERS}"
630+
631+
632+
class TestTeamScopedPerKeyBackendKwargMasking(TestConfigEndpoint):
633+
"""A team scoped spelling of a per-key secrets-backend-kwarg option -- the
634+
``[<team>=secrets]`` config-file section, or an
635+
``AIRFLOW__<TEAM>___SECRETS__BACKEND_KWARG__*`` env var -- is reported by
636+
``conf.as_dict`` under a section named after the team (``team_a=secrets``)
637+
rather than under the literal ``secrets`` / ``workers`` section. Both
638+
spellings collapse to that same shape once ``conf.as_dict`` has parsed
639+
them, so a single team scoped section covers both. The Config API must
640+
resolve that section back to its base section and redact the option the
641+
same way it redacts the non-team-scoped one. See
642+
https://github.com/apache/airflow/issues/71037"""
643+
644+
@pytest.fixture(autouse=True)
645+
def setup_team_scoped_per_key(self) -> Generator[None, None, None]:
646+
per_key_dict = {
647+
SECTION_CORE: {OPTION_KEY_PARALLELISM: OPTION_VALUE_PARALLELISM},
648+
SECTION_TEAM_SECRETS: {PER_KEY_OPTION_SECRETS: PER_KEY_VALUE},
649+
SECTION_TEAM_WORKERS: {PER_KEY_OPTION_WORKERS: PER_KEY_VALUE},
650+
}
651+
652+
def _mock_conf_as_dict(display_sensitive: bool, **_):
653+
return {section: options.copy() for section, options in per_key_dict.items()}
654+
655+
def _mock_has_option(section: str, option: str) -> bool:
656+
return option in per_key_dict.get(section, {})
657+
658+
with (
659+
conf_vars(AIRFLOW_CONFIG_ENABLE_EXPOSE_CONFIG),
660+
patch(
661+
"airflow.api_fastapi.core_api.routes.public.config.conf.as_dict",
662+
new=_mock_conf_as_dict,
663+
),
664+
patch(
665+
"airflow.api_fastapi.core_api.routes.public.config.conf.has_option",
666+
new=_mock_has_option,
667+
),
668+
):
669+
yield
670+
671+
def test_get_config_masks_team_scoped_per_key_secrets_backend_kwargs(self, test_client):
672+
"""``GET /config`` must redact a team scoped per-key option under
673+
both the ``secrets`` and ``workers`` team scoped sections when
674+
``display_sensitive=False`` (the API-server default)."""
675+
response = test_client.get("/config", headers=HEADERS_JSON)
676+
assert response.status_code == 200
677+
678+
sections = {
679+
s["name"]: {o["key"]: o["value"] for o in s["options"]} for s in response.json()["sections"]
680+
}
681+
assert sections[SECTION_TEAM_SECRETS][PER_KEY_OPTION_SECRETS] == OPTION_VALUE_SENSITIVE_HIDDEN
682+
assert sections[SECTION_TEAM_WORKERS][PER_KEY_OPTION_WORKERS] == OPTION_VALUE_SENSITIVE_HIDDEN
683+
# Non-sensitive option in the same response must remain untouched.
684+
assert sections[SECTION_CORE][OPTION_KEY_PARALLELISM] == OPTION_VALUE_PARALLELISM
685+
686+
def test_get_config_value_masks_team_scoped_per_key_secrets_backend_kwarg(self, test_client):
687+
"""``GET /config/section/{section}/option/{option}`` must redact a
688+
team scoped per-key synthetic option the same way it redacts the
689+
non-team-scoped one."""
690+
response = test_client.get(
691+
f"/config/section/{SECTION_TEAM_SECRETS}/option/{PER_KEY_OPTION_SECRETS}",
692+
headers=HEADERS_JSON,
693+
)
694+
assert response.status_code == 200
695+
assert response.json()["sections"][0]["options"][0]["value"] == OPTION_VALUE_SENSITIVE_HIDDEN

0 commit comments

Comments
 (0)