jedcunningham commented on code in PR #77472:
URL: https://github.com/apache/airflow/pull/77472#discussion_r4684428164


##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -1206,6 +1206,17 @@ def _record_missing_used_by(trigger: Trigger) -> None:
             trigger.dag_id = callback.get_dag_id() or dag_run.get("dag_id")
             trigger.used_by_ref = {"callback_id": str(callback.id)}
 
+    @staticmethod
+    def _encrypt_plaintext_kwargs(trigger: Trigger) -> None:
+        """
+        Encrypt plaintext trigger kwargs in place when picked up by the 
triggerer.
+
+        Written with the session that builds the workload so legacy 
unencrypted rows are
+        persisted encrypted at rest.
+        """
+        if trigger.encrypted_kwargs.startswith("{"):
+            trigger.encrypted_kwargs = trigger.encrypt_kwargs(trigger.kwargs)

Review Comment:
   `trigger.kwargs` deserializes the kwargs here in the supervisor, where 
nothing catches an error. If a row can't be deserialized (say a kwarg's class 
isn't in `allowed_deserialization_classes`), the triggerer exits. The row stays 
plaintext and gets reassigned, so the next triggerer exits too. Today the 
runner catches this per trigger.
   
   The row is already serialized JSON, so it can be encrypted without 
deserializing it. Skipping the write when nothing changed also avoids rewriting 
the row on every pickup when no `fernet_key` is set (encryption is a no-op 
then). Needs `import json` at the top.
   
   ```suggestion
           if trigger.encrypted_kwargs.startswith("{"):
               encrypted = 
Trigger._encrypt_serialized_kwargs(json.loads(trigger.encrypted_kwargs))
               if encrypted != trigger.encrypted_kwargs:
                   trigger.encrypted_kwargs = encrypted
   ```



##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -6195,3 +6195,34 @@ def test_caller_finished_ids_not_mutated(self):
         runner.process_trigger_events(finished_ids=finished_ids)
 
         assert finished_ids == [10]
+
+
+class TestEncryptPlaintextTriggerKwargs:

Review Comment:
   Could you also cover:
   - a deferred-task trigger with a serde-format payload (the execution API 
stores these for every deferral);
   - a row that can't be deserialized, checking `build_trigger_workloads` 
doesn't raise;
   - an already-encrypted row, checking it's left unchanged.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to