Skip to content

Commit

Permalink
Always set MASK_SECRETS_IN_LOGS to True while rendering task instance…
Browse files Browse the repository at this point in the history
…s not executed
  • Loading branch information
tirkarthi committed May 4, 2022
1 parent eda76cf commit d176a30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,10 @@ def get_rendered_template_fields(self, session: Session = NEW_SESSION) -> None:

try:
# Task was never executed. Initialize RenderedTaskInstanceFields
# to render template and mask secrets.
# to render template and mask secrets. Set MASK_SECRETS_IN_LOGS
# to True to enable masking similar to task run.
original_value = settings.MASK_SECRETS_IN_LOGS
settings.MASK_SECRETS_IN_LOGS = True
rendered_task_instance = RenderedTaskInstanceFields(self)
rendered_fields = rendered_task_instance.rendered_fields
if rendered_fields:
Expand All @@ -2176,6 +2179,8 @@ def get_rendered_template_fields(self, session: Session = NEW_SESSION) -> None:
"started running, please use 'airflow tasks render' for debugging the "
"rendering of template_fields."
) from e
finally:
settings.MASK_SECRETS_IN_LOGS = original_value

@provide_session
def get_rendered_k8s_spec(self, session=NEW_SESSION):
Expand Down
18 changes: 9 additions & 9 deletions tests/www/views/test_views_rendered.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_user_defined_filter_and_macros_raise_error(admin_client, create_dag_run


@pytest.mark.usefixtures("patch_app")
def test_rendered_template_secret(*args, admin_client, create_dag_run, task_secret):
def test_rendered_template_secret(admin_client, create_dag_run, task_secret):
"""Test that the Rendered View masks values retrieved from secret variables."""
Variable.set("my_secret", "foo")
Variable.set("spam", "egg")
Expand All @@ -196,12 +196,12 @@ def test_rendered_template_secret(*args, admin_client, create_dag_run, task_secr
ti.refresh_from_task(task_secret)
assert ti.state == TaskInstanceState.QUEUED

url = f'rendered-templates?task_id=task_secret&dag_id=testdag&execution_date={quote_plus(str(DEFAULT_DATE))}'
date = {quote_plus(str(DEFAULT_DATE))}
url = f'rendered-templates?task_id=task_secret&dag_id=testdag&execution_date={date}'

with mock.patch('airflow.settings.MASK_SECRETS_IN_LOGS', True):
resp = admin_client.get(url, follow_redirects=True)
check_content_in_response(
'echo</span> *** <span class="o">&amp;&amp;</span> <span class="nb">echo</span> egg', resp
)
ti.refresh_from_task(task_secret)
assert ti.state == TaskInstanceState.QUEUED
resp = admin_client.get(url, follow_redirects=True)
check_content_in_response(
'echo</span> *** <span class="o">&amp;&amp;</span> <span class="nb">echo</span> egg', resp
)
ti.refresh_from_task(task_secret)
assert ti.state == TaskInstanceState.QUEUED

0 comments on commit d176a30

Please sign in to comment.