Browse code

Fix exceptions thrown from cryptography import (#16723)

A simple import of cryptography can throw several types of errors. For example,
if `setuptools` is less than cryptography's minimum requirement of 11.3, then
this import of cryptography will throw a VersionConflict here. An earlier case
threw a DistributionNotFound exception.

An optional dependency should not stop ansible. If the error is more than
an ImportError, log a warning, so that errors can be fixed in ansible or
elsewhere.

Connor Osborn authored on 2016/07/20 19:32:23
Showing 1 changed files
... ...
@@ -30,6 +30,12 @@ from hashlib import sha256
30 30
 from binascii import hexlify
31 31
 from binascii import unhexlify
32 32
 
33
+try:
34
+    from __main__ import display
35
+except ImportError:
36
+    from ansible.utils.display import Display
37
+    display = Display()
38
+
33 39
 # Note: Only used for loading obsolete VaultAES files.  All files are written
34 40
 # using the newer VaultAES256 which does not require md5
35 41
 from hashlib import md5
... ...
@@ -71,10 +77,9 @@ try:
71 71
 except ImportError:
72 72
     pass
73 73
 except Exception as e:
74
-    if e.__module__ == 'pkg_resources' and e.__class__.__name__ == 'DistributionNotFound':
75
-        pass
76
-    else:
77
-        raise
74
+    display.warning("Optional dependency 'cryptography' raised an exception, falling back to 'Crypto'")
75
+    import traceback
76
+    traceback.print_exc()
78 77
 
79 78
 from ansible.compat.six import PY3
80 79
 from ansible.utils.unicode import to_unicode, to_bytes