You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "juan-pablo-guereca (via GitHub)" <gi...@apache.org> on 2026/06/25 09:10:16 UTC

[PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

juan-pablo-guereca opened a new pull request, #68975:
URL: https://github.com/apache/airflow/pull/68975

   When var.json is used in a Jinja template (e.g. to pass secrets as env vars to a KubernetesPodOperator), _get_variable deserialised the JSON before masking. The isinstance(var_val, str) guard evaluated to False for the resulting dict, so mask_secret was never called. The secret was stored unredacted in the rendered_task_instance_fields table and shown as plain text in the Rendered Templates view.
   
   Both the SecretCache hit path and the secrets-backend path receive the same fix.
   
    <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull request.
   Write a good git commit message following this guide: http://chris.beams.io/posts/git-commit/
   
   Please make sure that your code changes are covered with tests.
   And in case of new features or big changes remember to adjust the documentation.
   
   For user-facing UI changes, please attach before/after screenshots (or a short
   screen recording) so reviewers can assess the visual impact.
   
   Feel free to ping (in general) for the review if you do not see reaction for a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, please
   change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
   -->
   
   - [x] Yes, claude
   
   <!--
   Generated-by: Claude
   -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #68975:
URL: https://github.com/apache/airflow/pull/68975#issuecomment-4797618367

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our [Contributors' Guide](https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [prek-hooks]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-prek-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example Dag that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "juan-pablo-guereca (via GitHub)" <gi...@apache.org>.
juan-pablo-guereca commented on code in PR #68975:
URL: https://github.com/apache/airflow/pull/68975#discussion_r3504271068


##########
task-sdk/tests/task_sdk/execution_time/test_context.py:
##########
@@ -360,6 +360,59 @@ def test_get_method_with_default(self, mock_supervisor_comms):
         val = accessor.get("nonexistent_var_key", default="default_value")
         assert val == "default_value"
 
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_masks_from_cache(self, mock_mask_secret):
+        """SecretCache hit path applies the same masking as the backends path."""
+        from airflow.sdk.execution_time.cache import SecretCache
+
+        raw_json = '{"password": "s3cr3t", "host": "db.example.com"}'
+        with mock.patch.object(SecretCache, "get_variable", return_value=raw_json):
+            accessor = VariableAccessor(deserialize_json=True)
+            val = accessor.db_config
+
+        assert val == {"password": "s3cr3t", "host": "db.example.com"}
+        mock_mask_secret.assert_any_call(raw_json, "db_config")
+        mock_mask_secret.assert_any_call({"password": "s3cr3t", "host": "db.example.com"})
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_value_masks_secret(self, mock_mask_secret, mock_supervisor_comms):
+        """var.value.<key> masks the string value when the key name is sensitive."""
+        accessor = VariableAccessor(deserialize_json=False)
+        mock_supervisor_comms.send.return_value = VariableResult(key="my_password", value="s3cr3t")
+
+        val = accessor.my_password
+
+        assert val == "s3cr3t"
+        mock_mask_secret.assert_called_once_with("s3cr3t", "my_password")
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_masks_raw_string_and_dict_values(self, mock_mask_secret, mock_supervisor_comms):
+        """var.json.<key> masks both the raw JSON string and the deserialized dict's sensitive fields."""
+        accessor = VariableAccessor(deserialize_json=True)
+        raw_json = '{"password": "s3cr3t", "host": "db.example.com"}'
+        mock_supervisor_comms.send.return_value = VariableResult(key="db_config", value=raw_json)
+
+        val = accessor.db_config
+
+        assert val == {"password": "s3cr3t", "host": "db.example.com"}
+        # First call: raw JSON string with variable key (masks if key name is sensitive)
+        mock_mask_secret.assert_any_call(raw_json, "db_config")
+        # Second call: deserialized dict so internal sensitive fields like "password" get masked
+        mock_mask_secret.assert_any_call({"password": "s3cr3t", "host": "db.example.com"})
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_sensitive_key_masks_raw_json(self, mock_mask_secret, mock_supervisor_comms):
+        """var.json.<sensitive_key> masks the entire raw JSON string because the variable key is sensitive."""
+        accessor = VariableAccessor(deserialize_json=True)
+        raw_json = '{"endpoint": "https://api.example.com", "token": "abc123"}'
+        mock_supervisor_comms.send.return_value = VariableResult(key="my_secret", value=raw_json)
+
+        val = accessor.my_secret
+
+        assert val == {"endpoint": "https://api.example.com", "token": "abc123"}
+        mock_mask_secret.assert_any_call(raw_json, "my_secret")
+        mock_mask_secret.assert_any_call({"endpoint": "https://api.example.com", "token": "abc123"})
+

Review Comment:
   Thanks for your feedback.
   
   Added 2 more unit tests to covert the 2 branches introduced by the helper.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #68975:
URL: https://github.com/apache/airflow/pull/68975#issuecomment-5050261925

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 merged PR #68975:
URL: https://github.com/apache/airflow/pull/68975


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "juan-pablo-guereca (via GitHub)" <gi...@apache.org>.
juan-pablo-guereca commented on code in PR #68975:
URL: https://github.com/apache/airflow/pull/68975#discussion_r3629451971


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -317,6 +317,16 @@ async def _async_get_connection(conn_id: str) -> Connection:
     raise AirflowNotFoundException(f"The conn_id `{conn_id}` isn't defined")
 
 
+def _mask_and_deserialize_variable(raw: str, key: str, deserialize_json: bool) -> Any:
+    mask_secret(raw, key)
+    if not deserialize_json:
+        return raw
+    val = json.loads(raw)
+    if not isinstance(val, str):
+        mask_secret(val)

Review Comment:
   I've pushed a new commit with the fixes and more unit tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 commented on code in PR #68975:
URL: https://github.com/apache/airflow/pull/68975#discussion_r3625448896


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -317,6 +317,16 @@ async def _async_get_connection(conn_id: str) -> Connection:
     raise AirflowNotFoundException(f"The conn_id `{conn_id}` isn't defined")
 
 
+def _mask_and_deserialize_variable(raw: str, key: str, deserialize_json: bool) -> Any:
+    mask_secret(raw, key)
+    if not deserialize_json:
+        return raw
+    val = json.loads(raw)
+    if not isinstance(val, str):
+        mask_secret(val)

Review Comment:
   This guard might regresse masking for string scalars. For `var.json.my_password` storing `'"s3cr3t"'`, only the quoted raw is registered as a pattern - it never matches the unquoted `s3cr3t` that lands in `rendered_task_instance_fields`, so the secret renders in plain text (this case works on main today). The other branch has the opposite problem: `mask_secret(val)` without name is the unconditional-masking mode, so a non-sensitive list variable like var.json.regions gets all its elements globally redacted from task logs.
   
   
   Drafted by: Code Claude, reviewed by myself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 commented on code in PR #68975:
URL: https://github.com/apache/airflow/pull/68975#discussion_r3625448896


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -317,6 +317,16 @@ async def _async_get_connection(conn_id: str) -> Connection:
     raise AirflowNotFoundException(f"The conn_id `{conn_id}` isn't defined")
 
 
+def _mask_and_deserialize_variable(raw: str, key: str, deserialize_json: bool) -> Any:
+    mask_secret(raw, key)
+    if not deserialize_json:
+        return raw
+    val = json.loads(raw)
+    if not isinstance(val, str):
+        mask_secret(val)

Review Comment:
   This guard might regress masking for string scalars. For `var.json.my_password` storing `'"s3cr3t"'`, only the quoted raw is registered as a pattern - it never matches the unquoted `s3cr3t` that lands in `rendered_task_instance_fields`, so the secret renders in plain text (this case works on main today). The other branch has the opposite problem: `mask_secret(val)` without name is the unconditional-masking mode, so a non-sensitive list variable like var.json.regions gets all its elements globally redacted from task logs.
   
   
   Drafted by: Code Claude, reviewed by myself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "juan-pablo-guereca (via GitHub)" <gi...@apache.org>.
juan-pablo-guereca commented on PR #68975:
URL: https://github.com/apache/airflow/pull/68975#issuecomment-4912664763

   @potiuk totally agree passing secrets via pod env vars isn't safe practice, and this doesn't change that guidance at all, k8s level visibility stays the same either way.
   
   The reason I'd still love to see this merged: this masking already works fine in Airflow 2. In AF3 it quietly stopped working for var.json values in some cases, so **this is really just restoring existing behavior, not adding anything new**. I worry that teams who tested and trusted this on AF2 might not realize it's silently different on AF3.
   
   One more bit of context in case it's useful: I'm working on an AF2 to AF3 migration with a lot of existing DAGs, some of which already use this pattern (not by choice, just the reality of a large existing codebase). Losing masking silently on upgrade is a real regression for that kind of migration, not just a hypothetical, but happy to defer to whatever the security team decides either way.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "SameerMesiah97 (via GitHub)" <gi...@apache.org>.
SameerMesiah97 commented on code in PR #68975:
URL: https://github.com/apache/airflow/pull/68975#discussion_r3501776006


##########
task-sdk/tests/task_sdk/execution_time/test_context.py:
##########
@@ -360,6 +360,59 @@ def test_get_method_with_default(self, mock_supervisor_comms):
         val = accessor.get("nonexistent_var_key", default="default_value")
         assert val == "default_value"
 
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_masks_from_cache(self, mock_mask_secret):
+        """SecretCache hit path applies the same masking as the backends path."""
+        from airflow.sdk.execution_time.cache import SecretCache
+
+        raw_json = '{"password": "s3cr3t", "host": "db.example.com"}'
+        with mock.patch.object(SecretCache, "get_variable", return_value=raw_json):
+            accessor = VariableAccessor(deserialize_json=True)
+            val = accessor.db_config
+
+        assert val == {"password": "s3cr3t", "host": "db.example.com"}
+        mock_mask_secret.assert_any_call(raw_json, "db_config")
+        mock_mask_secret.assert_any_call({"password": "s3cr3t", "host": "db.example.com"})
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_value_masks_secret(self, mock_mask_secret, mock_supervisor_comms):
+        """var.value.<key> masks the string value when the key name is sensitive."""
+        accessor = VariableAccessor(deserialize_json=False)
+        mock_supervisor_comms.send.return_value = VariableResult(key="my_password", value="s3cr3t")
+
+        val = accessor.my_password
+
+        assert val == "s3cr3t"
+        mock_mask_secret.assert_called_once_with("s3cr3t", "my_password")
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_masks_raw_string_and_dict_values(self, mock_mask_secret, mock_supervisor_comms):
+        """var.json.<key> masks both the raw JSON string and the deserialized dict's sensitive fields."""
+        accessor = VariableAccessor(deserialize_json=True)
+        raw_json = '{"password": "s3cr3t", "host": "db.example.com"}'
+        mock_supervisor_comms.send.return_value = VariableResult(key="db_config", value=raw_json)
+
+        val = accessor.db_config
+
+        assert val == {"password": "s3cr3t", "host": "db.example.com"}
+        # First call: raw JSON string with variable key (masks if key name is sensitive)
+        mock_mask_secret.assert_any_call(raw_json, "db_config")
+        # Second call: deserialized dict so internal sensitive fields like "password" get masked
+        mock_mask_secret.assert_any_call({"password": "s3cr3t", "host": "db.example.com"})
+
+    @mock.patch("airflow.sdk.execution_time.context.mask_secret")
+    def test_var_json_sensitive_key_masks_raw_json(self, mock_mask_secret, mock_supervisor_comms):
+        """var.json.<sensitive_key> masks the entire raw JSON string because the variable key is sensitive."""
+        accessor = VariableAccessor(deserialize_json=True)
+        raw_json = '{"endpoint": "https://api.example.com", "token": "abc123"}'
+        mock_supervisor_comms.send.return_value = VariableResult(key="my_secret", value=raw_json)
+
+        val = accessor.my_secret
+
+        assert val == {"endpoint": "https://api.example.com", "token": "abc123"}
+        mock_mask_secret.assert_any_call(raw_json, "my_secret")
+        mock_mask_secret.assert_any_call({"endpoint": "https://api.example.com", "token": "abc123"})
+

Review Comment:
   I think you should expand test coverage abit to cover 2 more branches introdcued by the helper:
   1) a JSON value that deserializes to a string (e.g. `"secret"`), to verify the second `mask_secret()` call is skipped.
   2) invalid JSON, just to ensure existing `JSONDecodeError` behaviour is preserved after the refactor.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix secrets not masked in Rendered Templates view with KubernetesPodOperator [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #68975:
URL: https://github.com/apache/airflow/pull/68975#issuecomment-4903059291

   It's an interesting one - I am not sure if we want to merge this one. This gives false sense of security - you know you are not supposed to pass secrets in K8S Pod Operator Env Vars ? 
   
   > Never use environment variables to pass secrets (for example connection authentication information) to Kubernetes Pod Operator. Such environment variables will be visible to anyone who has access to see and describe PODs in Kubernetes. Instead, pass your secrets via native Kubernetes Secrets or use Connections and Variables from Airflow. For the latter, you need to have apache-airflow package installed in your image in the same version as Airflow you run your Kubernetes Pod Operator from).
   
   [Reference](https://airflow.apache.org/docs/apache-airflow-providers-cncf-kubernetes/stable/operators.html#id18)[¶](https://airflow.apache.org/docs/apache-airflow-providers-cncf-kubernetes/stable/operators.html#reference)
   
   I will psss that to the security team for consideration - but I think this one - especially that it addresses something we explicitly told is a bad practice is not a good idea


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org