From df8f98e09f6f95e9032668b743c6125f22c35a3f Mon Sep 17 00:00:00 2001 From: Craig Citro Date: Fri, 22 May 2015 23:28:00 -0700 Subject: [PATCH] Check for OpenSSL.crypto when detecting OpenSSL. Previously, we assumed that anyone using `OpenSSL` would have installed it via a mechanism like `pip`; this has come back to bite us. We modify our code to look for `OpenSSL.crypto`, not just `OpenSSL`, and add another (unpleasant) test. Fixes #190. --- oauth2client/crypt.py | 8 ++++++-- tests/test_crypt.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/oauth2client/crypt.py b/oauth2client/crypt.py index 19f9d9f..9e3aede 100644 --- a/oauth2client/crypt.py +++ b/oauth2client/crypt.py @@ -57,10 +57,14 @@ def _TryOpenSslImport(): """ try: - _ = imp.find_module('OpenSSL') + _, _package_dir, _ = imp.find_module('OpenSSL') + if not (os.path.isfile(os.path.join(_package_dir, 'crypto.py')) or + os.path.isfile(os.path.join(_package_dir, 'crypto.so')) or + os.path.isdir(os.path.join(_package_dir, 'crypto'))): + raise ImportError('No module named OpenSSL.crypto') return except ImportError: - import OpenSSL + import OpenSSL.crypto try: diff --git a/tests/test_crypt.py b/tests/test_crypt.py index 10fff04..dcc44e0 100644 --- a/tests/test_crypt.py +++ b/tests/test_crypt.py @@ -75,7 +75,34 @@ class Test_pkcs12_key_as_pem(unittest.TestCase): finally: sys.path = orig_sys_path imp.find_module = imp_find_module - import OpenSSL + import OpenSSL.crypto + reload(crypt) + + def test_without_openssl_crypto(self): + import imp + imp_find_module = imp.find_module + orig_sys_path = sys.path + orig_isfile = os.path.isfile + openssl_module = imp.find_module('OpenSSL') + def find_module(module_name): + if module_name == 'OpenSSL': + return openssl_module + raise ImportError('No module named %s' % module_name) + try: + for m in list(sys.modules): + if m.startswith('OpenSSL'): + sys.modules.pop(m) + sys.path = [] + imp.find_module = find_module + os.path.isfile = lambda filename: False + reload(crypt) + self.assertRaises(NotImplementedError, crypt.pkcs12_key_as_pem, + 'FOO', 'BAR') + finally: + sys.path = orig_sys_path + imp.find_module = imp_find_module + os.path.isfile = orig_isfile + import OpenSSL.crypto reload(crypt) def test_with_nonsense_key(self):