Repository: incubator-airflow
Updated Branches:
  refs/heads/master ea86895d5 -> d22340aab


[AIRFLOW-1516] Fix error handling getting fernet

There were unhandled cases for exceptions when
importing fernet in
models.py. This seems to be a remanent of a
previous refactor,
replacing logic that would depend on the
definition of a global variable
for Fernet if it was imported correctly.

Generally catching all exceptions from get_fernet
function, given that
other functions are already handling it that way
and the only
error handling case here is to not use encryption.

Closes #2527 from edgarRd/erod-fernet-error-
handling


Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/d22340aa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/d22340aa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/d22340aa

Branch: refs/heads/master
Commit: d22340aab01ed684c6b056f2b54f60e0602cabd6
Parents: ea86895
Author: Edgar Rodriguez <[email protected]>
Authored: Fri Aug 18 11:31:27 2017 -0700
Committer: Alex Guziel <[email protected]>
Committed: Fri Aug 18 11:31:27 2017 -0700

----------------------------------------------------------------------
 airflow/models.py | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/d22340aa/airflow/models.py
----------------------------------------------------------------------
diff --git a/airflow/models.py b/airflow/models.py
index bf308e5..d83bc9a 100755
--- a/airflow/models.py
+++ b/airflow/models.py
@@ -94,9 +94,18 @@ def get_fernet():
 
     This function could fail either because Cryptography is not installed
     or because the Fernet key is invalid.
+    :return: Fernet object
+    :raises: AirflowException if there's a problem trying to load Fernet
     """
-    from cryptography.fernet import Fernet
-    return Fernet(configuration.get('core', 'FERNET_KEY').encode('utf-8'))
+    try:
+        from cryptography.fernet import Fernet
+    except ImportError:
+        raise AirflowException('Failed to import Fernet, it may not be 
installed')
+    try:
+        return Fernet(configuration.get('core', 'FERNET_KEY').encode('utf-8'))
+    except ValueError as ve:
+        raise AirflowException("Could not create Fernet object: {}"
+                               .format(ve.message))
 
 
 if 'mysql' in settings.SQL_ALCHEMY_CONN:
@@ -608,7 +617,9 @@ class Connection(Base):
                 fernet = get_fernet()
                 self._password = fernet.encrypt(bytes(value, 'utf-8')).decode()
                 self.is_encrypted = True
-            except NameError:
+            except AirflowException:
+                self.logger.exception("Failed to load fernet while encrypting 
value, "
+                                      "using non-encrypted value.")
                 self._password = value
                 self.is_encrypted = False
 
@@ -635,7 +646,9 @@ class Connection(Base):
                 fernet = get_fernet()
                 self._extra = fernet.encrypt(bytes(value, 'utf-8')).decode()
                 self.is_extra_encrypted = True
-            except NameError:
+            except AirflowException:
+                self.logger.exception("Failed to load fernet while encrypting 
value, "
+                                      "using non-encrypted value.")
                 self._extra = value
                 self.is_extra_encrypted = False
 
@@ -3846,7 +3859,9 @@ class Variable(Base):
                 fernet = get_fernet()
                 self._val = fernet.encrypt(bytes(value, 'utf-8')).decode()
                 self.is_encrypted = True
-            except NameError:
+            except AirflowException:
+                self.logger.exception("Failed to load fernet while encrypting 
value, "
+                                      "using non-encrypted value.")
                 self._val = value
                 self.is_encrypted = False
 

Reply via email to