This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 5c9c4d85e89 Move core Fernet helpers out of database models (#76605)
5c9c4d85e89 is described below

commit 5c9c4d85e892a933ad82dd2438b3949020ba674d
Author: Shahar Epstein <[email protected]>
AuthorDate: Mon Nov 16 02:13:18 2026 +0200

    Move core Fernet helpers out of database models (#76605)
    
    Keep generic encryption helpers separate from SQLAlchemy models while 
preserving existing imports and the shared Fernet cache.
---
 airflow-core/src/airflow/{models => }/crypto.py    |  57 -----------
 airflow-core/src/airflow/models/connection.py      |   3 +-
 airflow-core/src/airflow/models/connection_test.py |   3 +-
 airflow-core/src/airflow/models/crypto.py          | 106 ++-------------------
 airflow-core/src/airflow/models/trigger.py         |   6 +-
 airflow-core/src/airflow/models/variable.py        |   2 +-
 .../cli/commands/test_rotate_fernet_key_command.py |   2 +-
 airflow-core/tests/unit/jobs/test_triggerer_job.py |   2 +-
 airflow-core/tests/unit/models/test_connection.py  |   2 +-
 airflow-core/tests/unit/models/test_crypto.py      |  28 ++++++
 generated/known_airflow_exceptions.txt             |   2 +-
 11 files changed, 47 insertions(+), 166 deletions(-)

diff --git a/airflow-core/src/airflow/models/crypto.py 
b/airflow-core/src/airflow/crypto.py
similarity index 60%
copy from airflow-core/src/airflow/models/crypto.py
copy to airflow-core/src/airflow/crypto.py
index fb6ece8084c..c62446b7631 100644
--- a/airflow-core/src/airflow/models/crypto.py
+++ b/airflow-core/src/airflow/crypto.py
@@ -21,9 +21,6 @@ import logging
 from functools import cache
 from typing import Protocol
 
-from sqlalchemy import Boolean, Text
-from sqlalchemy.orm import Mapped, declared_attr, mapped_column, synonym
-
 from airflow.configuration import conf
 from airflow.exceptions import AirflowException
 
@@ -96,60 +93,6 @@ class _RealFernet:
         return self._fernet.rotate(msg)
 
 
-class FernetFieldsMixin:
-    """Mixin providing Fernet-encrypted ``password`` and ``extra`` fields."""
-
-    _password: Mapped[str | None] = mapped_column("password", Text(), 
nullable=True)
-    _extra: Mapped[str | None] = mapped_column("extra", Text(), nullable=True)
-    is_encrypted: Mapped[bool] = mapped_column(Boolean, unique=False, 
default=False, nullable=False)
-    is_extra_encrypted: Mapped[bool] = mapped_column(Boolean, unique=False, 
default=False, nullable=False)
-
-    def get_password(self) -> str | None:
-        """Decrypt and return password."""
-        if self._password and self.is_encrypted:
-            fernet = get_fernet()
-            if not fernet.is_encrypted:
-                raise ValueError("Can't decrypt encrypted password, FERNET_KEY 
configuration is missing")
-            return fernet.decrypt(bytes(self._password, "utf-8")).decode()
-        return self._password
-
-    def set_password(self, value: str | None):
-        """Encrypt and store password."""
-        if value:
-            fernet = get_fernet()
-            self._password = fernet.encrypt(bytes(value, "utf-8")).decode()
-            self.is_encrypted = fernet.is_encrypted
-
-    @declared_attr
-    def password(cls):
-        """Password. The value is decrypted/encrypted when reading/setting the 
value."""
-        return synonym("_password", descriptor=property(cls.get_password, 
cls.set_password))
-
-    def get_extra(self) -> str | None:
-        """Decrypt and return extra data."""
-        if self._extra and self.is_extra_encrypted:
-            fernet = get_fernet()
-            if not fernet.is_encrypted:
-                raise ValueError("Can't decrypt `extra` params, FERNET_KEY 
configuration is missing")
-            return fernet.decrypt(bytes(self._extra, "utf-8")).decode()
-        return self._extra
-
-    def set_extra(self, value: str | None):
-        """Encrypt and store extra data."""
-        if value:
-            fernet = get_fernet()
-            self._extra = fernet.encrypt(bytes(value, "utf-8")).decode()
-            self.is_extra_encrypted = fernet.is_encrypted
-        else:
-            self._extra = value
-            self.is_extra_encrypted = False
-
-    @declared_attr
-    def extra(cls):
-        """Extra data. The value is decrypted/encrypted when reading/setting 
the value."""
-        return synonym("_extra", descriptor=property(cls.get_extra, 
cls.set_extra))
-
-
 @cache
 def get_fernet() -> FernetProtocol:
     """
diff --git a/airflow-core/src/airflow/models/connection.py 
b/airflow-core/src/airflow/models/connection.py
index 413902030f9..237bda0ca39 100644
--- a/airflow-core/src/airflow/models/connection.py
+++ b/airflow-core/src/airflow/models/connection.py
@@ -32,9 +32,10 @@ from sqlalchemy.orm import Mapped, mapped_column, 
reconstructor, validates
 from airflow._shared.module_loading import import_string
 from airflow._shared.secrets_backend.base import call_secrets_backend_method
 from airflow._shared.secrets_masker import mask_secret
+from airflow.crypto import get_fernet
 from airflow.exceptions import AirflowException, AirflowNotFoundException
 from airflow.models.base import ID_LEN, Base
-from airflow.models.crypto import FernetFieldsMixin, get_fernet
+from airflow.models.crypto import FernetFieldsMixin
 from airflow.process_context import should_use_task_sdk_api_path
 
 # AirflowSecretsBackendAccessDenied was added to task-sdk in 1.2.2. When
diff --git a/airflow-core/src/airflow/models/connection_test.py 
b/airflow-core/src/airflow/models/connection_test.py
index e9c0a3ec9b9..16ef4aafffc 100644
--- a/airflow-core/src/airflow/models/connection_test.py
+++ b/airflow-core/src/airflow/models/connection_test.py
@@ -38,9 +38,10 @@ from sqlalchemy import (
 from sqlalchemy.orm import Mapped, mapped_column, validates
 
 from airflow._shared.timezones import timezone
+from airflow.crypto import get_fernet
 from airflow.models.base import Base
 from airflow.models.connection import Connection
-from airflow.models.crypto import FernetFieldsMixin, get_fernet
+from airflow.models.crypto import FernetFieldsMixin
 from airflow.utils.sqlalchemy import UtcDateTime
 
 if TYPE_CHECKING:
diff --git a/airflow-core/src/airflow/models/crypto.py 
b/airflow-core/src/airflow/models/crypto.py
index fb6ece8084c..ee88226750e 100644
--- a/airflow-core/src/airflow/models/crypto.py
+++ b/airflow-core/src/airflow/models/crypto.py
@@ -17,83 +17,16 @@
 # under the License.
 from __future__ import annotations
 
-import logging
-from functools import cache
-from typing import Protocol
-
 from sqlalchemy import Boolean, Text
 from sqlalchemy.orm import Mapped, declared_attr, mapped_column, synonym
 
-from airflow.configuration import conf
-from airflow.exceptions import AirflowException
-
-log = logging.getLogger(__name__)
-
-
-class FernetProtocol(Protocol):
-    """This class is only used for TypeChecking (for IDEs, mypy, etc)."""
-
-    is_encrypted: bool
-
-    def decrypt(self, msg: bytes | str, ttl: int | None = None) -> bytes:
-        """Decrypt with Fernet."""
-        ...
-
-    def encrypt(self, msg: bytes) -> bytes:
-        """Encrypt with Fernet."""
-        ...
-
-
-class _NullFernet:
-    """
-    A "Null" encryptor class that doesn't encrypt or decrypt but that presents 
a similar interface to Fernet.
-
-    The purpose of this is to make the rest of the code not have to know the
-    difference, and to only display the message once, not 20 times when
-    `airflow db migrate` is run.
-    """
-
-    is_encrypted = False
-
-    def decrypt(self, msg: bytes | str, ttl: int | None = None) -> bytes:
-        """Decrypt with Fernet."""
-        if isinstance(msg, bytes):
-            return msg
-        if isinstance(msg, str):
-            return msg.encode("utf-8")
-        raise ValueError(f"Expected bytes or str, got {type(msg)}")
-
-    def encrypt(self, msg: bytes) -> bytes:
-        """Encrypt with Fernet."""
-        return msg
-
-
-class _RealFernet:
-    """
-    A wrapper around the real Fernet to set is_encrypted to True.
-
-    This class is only used internally to avoid changing the interface of
-    the get_fernet function.
-    """
-
-    from cryptography.fernet import Fernet, MultiFernet
-
-    is_encrypted = True
-
-    def __init__(self, fernet: MultiFernet):
-        self._fernet = fernet
-
-    def decrypt(self, msg: bytes | str, ttl: int | None = None) -> bytes:
-        """Decrypt with Fernet."""
-        return self._fernet.decrypt(msg, ttl)
-
-    def encrypt(self, msg: bytes) -> bytes:
-        """Encrypt with Fernet."""
-        return self._fernet.encrypt(msg)
-
-    def rotate(self, msg: bytes | str) -> bytes:
-        """Rotate the Fernet key for the given message."""
-        return self._fernet.rotate(msg)
+# Preserve imports from the original module, including the shared Fernet cache.
+from airflow.crypto import (
+    FernetProtocol as FernetProtocol,
+    _NullFernet as _NullFernet,
+    _RealFernet as _RealFernet,
+    get_fernet as get_fernet,
+)
 
 
 class FernetFieldsMixin:
@@ -148,28 +81,3 @@ class FernetFieldsMixin:
     def extra(cls):
         """Extra data. The value is decrypted/encrypted when reading/setting 
the value."""
         return synonym("_extra", descriptor=property(cls.get_extra, 
cls.set_extra))
-
-
-@cache
-def get_fernet() -> FernetProtocol:
-    """
-    Deferred load of Fernet key.
-
-    This function could fail either because Cryptography is not installed
-    or because the Fernet key is invalid.
-
-    :return: Fernet object
-    :raises: airflow.exceptions.AirflowException if there's a problem trying 
to load Fernet
-    """
-    from cryptography.fernet import Fernet, MultiFernet
-
-    try:
-        fernet_key = conf.get("core", "FERNET_KEY")
-        if not fernet_key:
-            log.warning("empty cryptography key - values will not be stored 
encrypted.")
-            return _NullFernet()
-
-        fernet = MultiFernet([Fernet(fernet_part.encode("utf-8")) for 
fernet_part in fernet_key.split(",")])
-        return _RealFernet(fernet)
-    except (ValueError, TypeError) as value_error:
-        raise AirflowException(f"Could not create Fernet object: 
{value_error}")
diff --git a/airflow-core/src/airflow/models/trigger.py 
b/airflow-core/src/airflow/models/trigger.py
index a095e4ef9fa..8dd3ad35419 100644
--- a/airflow-core/src/airflow/models/trigger.py
+++ b/airflow-core/src/airflow/models/trigger.py
@@ -236,7 +236,7 @@ class Trigger(Base):
     def _encrypt_serialized_kwargs(serialized_kwargs: dict[str, Any]) -> str:
         import json
 
-        from airflow.models.crypto import get_fernet
+        from airflow.crypto import get_fernet
 
         return 
get_fernet().encrypt(json.dumps(serialized_kwargs).encode("utf-8")).decode("utf-8")
 
@@ -245,7 +245,7 @@ class Trigger(Base):
         """Decrypt the kwargs of the trigger."""
         import json
 
-        from airflow.models.crypto import get_fernet
+        from airflow.crypto import get_fernet
         from airflow.sdk.serde import deserialize
 
         # We weren't able to encrypt the kwargs in all migration paths,
@@ -271,7 +271,7 @@ class Trigger(Base):
 
     def rotate_fernet_key(self):
         """Encrypts data with a new key. See: :ref:`security/fernet`."""
-        from airflow.models.crypto import get_fernet
+        from airflow.crypto import get_fernet
 
         self.encrypted_kwargs = 
get_fernet().rotate(self.encrypted_kwargs.encode("utf-8")).decode("utf-8")
 
diff --git a/airflow-core/src/airflow/models/variable.py 
b/airflow-core/src/airflow/models/variable.py
index bf7138545f3..f8ecc73a8eb 100644
--- a/airflow-core/src/airflow/models/variable.py
+++ b/airflow-core/src/airflow/models/variable.py
@@ -30,8 +30,8 @@ from sqlalchemy.orm import Mapped, declared_attr, 
mapped_column, reconstructor,
 from airflow._shared.secrets_backend.base import accepts_kwarg, 
call_secrets_backend_method
 from airflow._shared.secrets_masker import mask_secret
 from airflow.configuration import conf, ensure_secrets_loaded
+from airflow.crypto import get_fernet
 from airflow.models.base import ID_LEN, Base
-from airflow.models.crypto import get_fernet
 from airflow.process_context import should_use_task_sdk_api_path
 
 # AirflowSecretsBackendAccessDenied was added to task-sdk in 1.2.2. When
diff --git 
a/airflow-core/tests/unit/cli/commands/test_rotate_fernet_key_command.py 
b/airflow-core/tests/unit/cli/commands/test_rotate_fernet_key_command.py
index ec661a156f2..0592cc535f2 100644
--- a/airflow-core/tests/unit/cli/commands/test_rotate_fernet_key_command.py
+++ b/airflow-core/tests/unit/cli/commands/test_rotate_fernet_key_command.py
@@ -25,8 +25,8 @@ from sqlalchemy import select
 
 from airflow.cli import cli_parser
 from airflow.cli.commands import rotate_fernet_key_command
+from airflow.crypto import get_fernet
 from airflow.models import Connection, Variable
-from airflow.models.crypto import get_fernet
 from airflow.sdk import BaseHook
 from airflow.utils.session import provide_session
 
diff --git a/airflow-core/tests/unit/jobs/test_triggerer_job.py 
b/airflow-core/tests/unit/jobs/test_triggerer_job.py
index 03adf3ade98..4cfff466ce1 100644
--- a/airflow-core/tests/unit/jobs/test_triggerer_job.py
+++ b/airflow-core/tests/unit/jobs/test_triggerer_job.py
@@ -2261,7 +2261,7 @@ class TestTriggerRunner:
     ):
         import json
 
-        from airflow.models.crypto import get_fernet
+        from airflow.crypto import get_fernet
         from airflow.sdk.serde import serialize
 
         SerializedKwargsTrigger.constructed.clear()
diff --git a/airflow-core/tests/unit/models/test_connection.py 
b/airflow-core/tests/unit/models/test_connection.py
index c4b7a728310..cd5d92079bb 100644
--- a/airflow-core/tests/unit/models/test_connection.py
+++ b/airflow-core/tests/unit/models/test_connection.py
@@ -44,7 +44,7 @@ class TestConnection:
     @pytest.fixture(autouse=True)
     def clear_fernet_cache(self):
         """Clear the fernet cache before each test to avoid encryption 
issues."""
-        from airflow.models.crypto import get_fernet
+        from airflow.crypto import get_fernet
 
         get_fernet.cache_clear()
         yield
diff --git a/airflow-core/tests/unit/models/test_crypto.py 
b/airflow-core/tests/unit/models/test_crypto.py
new file mode 100644
index 00000000000..327c35ed31b
--- /dev/null
+++ b/airflow-core/tests/unit/models/test_crypto.py
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import pytest
+
+from airflow import crypto
+from airflow.models import crypto as model_crypto
+
+
[email protected]("name", ["FernetProtocol", "_NullFernet", 
"_RealFernet", "get_fernet"])
+def test_model_crypto_compatibility_imports(name):
+    assert getattr(model_crypto, name) is getattr(crypto, name)
+    assert getattr(crypto, name).__module__ == "airflow.crypto"
diff --git a/generated/known_airflow_exceptions.txt 
b/generated/known_airflow_exceptions.txt
index 1484c68ce3f..4fcd2656dc1 100644
--- a/generated/known_airflow_exceptions.txt
+++ b/generated/known_airflow_exceptions.txt
@@ -4,11 +4,11 @@ airflow-core/src/airflow/cli/cli_parser.py::1
 airflow-core/src/airflow/cli/commands/dag_command.py::3
 airflow-core/src/airflow/cli/commands/db_command.py::1
 airflow-core/src/airflow/config_templates/airflow_local_settings.py::1
+airflow-core/src/airflow/crypto.py::1
 airflow-core/src/airflow/dag_processing/dagbag.py::1
 airflow-core/src/airflow/jobs/base_job_runner.py::1
 airflow-core/src/airflow/jobs/job.py::1
 airflow-core/src/airflow/models/connection.py::4
-airflow-core/src/airflow/models/crypto.py::1
 airflow-core/src/airflow/models/dagrun.py::2
 airflow-core/src/airflow/models/pool.py::2
 airflow-core/src/airflow/secrets/local_filesystem.py::4

Reply via email to