Skip to content

Commit df8f98e

Browse files
committed
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 googleapis#190.
1 parent c61bdf4 commit df8f98e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

oauth2client/crypt.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ def _TryOpenSslImport():
5757
5858
"""
5959
try:
60-
_ = imp.find_module('OpenSSL')
60+
_, _package_dir, _ = imp.find_module('OpenSSL')
61+
if not (os.path.isfile(os.path.join(_package_dir, 'crypto.py')) or
62+
os.path.isfile(os.path.join(_package_dir, 'crypto.so')) or
63+
os.path.isdir(os.path.join(_package_dir, 'crypto'))):
64+
raise ImportError('No module named OpenSSL.crypto')
6165
return
6266
except ImportError:
63-
import OpenSSL
67+
import OpenSSL.crypto
6468

6569

6670
try:

tests/test_crypt.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,34 @@ def find_module(module_name):
7575
finally:
7676
sys.path = orig_sys_path
7777
imp.find_module = imp_find_module
78-
import OpenSSL
78+
import OpenSSL.crypto
79+
reload(crypt)
80+
81+
def test_without_openssl_crypto(self):
82+
import imp
83+
imp_find_module = imp.find_module
84+
orig_sys_path = sys.path
85+
orig_isfile = os.path.isfile
86+
openssl_module = imp.find_module('OpenSSL')
87+
def find_module(module_name):
88+
if module_name == 'OpenSSL':
89+
return openssl_module
90+
raise ImportError('No module named %s' % module_name)
91+
try:
92+
for m in list(sys.modules):
93+
if m.startswith('OpenSSL'):
94+
sys.modules.pop(m)
95+
sys.path = []
96+
imp.find_module = find_module
97+
os.path.isfile = lambda filename: False
98+
reload(crypt)
99+
self.assertRaises(NotImplementedError, crypt.pkcs12_key_as_pem,
100+
'FOO', 'BAR')
101+
finally:
102+
sys.path = orig_sys_path
103+
imp.find_module = imp_find_module
104+
os.path.isfile = orig_isfile
105+
import OpenSSL.crypto
79106
reload(crypt)
80107

81108
def test_with_nonsense_key(self):

0 commit comments

Comments
 (0)