Removing import OpenSSL hack.
No longer needed since the cryptography 1.0 release has fixed the slow import (due to changes in the way cffi was used).
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import six
|
import six
|
||||||
|
from OpenSSL import crypto
|
||||||
|
|
||||||
from oauth2client._helpers import _parse_pem_key
|
from oauth2client._helpers import _parse_pem_key
|
||||||
|
|
||||||
@@ -43,7 +44,6 @@ class OpenSSLVerifier(object):
|
|||||||
True if message was signed by the private key associated with the public
|
True if message was signed by the private key associated with the public
|
||||||
key that this object was constructed with.
|
key that this object was constructed with.
|
||||||
"""
|
"""
|
||||||
from OpenSSL import crypto # Delay import due to 0.5s import time.
|
|
||||||
if isinstance(message, six.text_type):
|
if isinstance(message, six.text_type):
|
||||||
message = message.encode('utf-8')
|
message = message.encode('utf-8')
|
||||||
if isinstance(signature, six.text_type):
|
if isinstance(signature, six.text_type):
|
||||||
@@ -69,7 +69,6 @@ class OpenSSLVerifier(object):
|
|||||||
Raises:
|
Raises:
|
||||||
OpenSSL.crypto.Error if the key_pem can't be parsed.
|
OpenSSL.crypto.Error if the key_pem can't be parsed.
|
||||||
"""
|
"""
|
||||||
from OpenSSL import crypto # Delay import due to 0.5s import time.
|
|
||||||
if is_x509_cert:
|
if is_x509_cert:
|
||||||
pubkey = crypto.load_certificate(crypto.FILETYPE_PEM, key_pem)
|
pubkey = crypto.load_certificate(crypto.FILETYPE_PEM, key_pem)
|
||||||
else:
|
else:
|
||||||
@@ -97,7 +96,6 @@ class OpenSSLSigner(object):
|
|||||||
Returns:
|
Returns:
|
||||||
string, The signature of the message for the given key.
|
string, The signature of the message for the given key.
|
||||||
"""
|
"""
|
||||||
from OpenSSL import crypto # Delay import due to 0.5s import time.
|
|
||||||
if isinstance(message, six.text_type):
|
if isinstance(message, six.text_type):
|
||||||
message = message.encode('utf-8')
|
message = message.encode('utf-8')
|
||||||
return crypto.sign(self._key, message, 'sha256')
|
return crypto.sign(self._key, message, 'sha256')
|
||||||
@@ -116,7 +114,6 @@ class OpenSSLSigner(object):
|
|||||||
Raises:
|
Raises:
|
||||||
OpenSSL.crypto.Error if the key can't be parsed.
|
OpenSSL.crypto.Error if the key can't be parsed.
|
||||||
"""
|
"""
|
||||||
from OpenSSL import crypto # Delay import due to 0.5s import time.
|
|
||||||
parsed_pem_key = _parse_pem_key(key)
|
parsed_pem_key = _parse_pem_key(key)
|
||||||
if parsed_pem_key:
|
if parsed_pem_key:
|
||||||
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key)
|
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key)
|
||||||
@@ -137,7 +134,6 @@ def pkcs12_key_as_pem(private_key_text, private_key_password):
|
|||||||
Returns:
|
Returns:
|
||||||
String. PEM contents of ``private_key_text``.
|
String. PEM contents of ``private_key_text``.
|
||||||
"""
|
"""
|
||||||
from OpenSSL import crypto # Delay import due to 0.5s import time.
|
|
||||||
decoded_body = base64.b64decode(private_key_text)
|
decoded_body = base64.b64decode(private_key_text)
|
||||||
if isinstance(private_key_password, six.text_type):
|
if isinstance(private_key_password, six.text_type):
|
||||||
private_key_password = private_key_password.encode('ascii')
|
private_key_password = private_key_password.encode('ascii')
|
||||||
|
|||||||
@@ -15,10 +15,8 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
"""Crypto-related routines for oauth2client."""
|
"""Crypto-related routines for oauth2client."""
|
||||||
|
|
||||||
import imp
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from oauth2client._helpers import _json_encode
|
from oauth2client._helpers import _json_encode
|
||||||
@@ -38,37 +36,7 @@ class AppIdentityError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _TryOpenSslImport():
|
|
||||||
"""Import OpenSSL, avoiding the explicit import where possible.
|
|
||||||
|
|
||||||
Importing OpenSSL 0.14 can take up to 0.5s, which is a large price
|
|
||||||
to pay at module import time. However, it's also possible for
|
|
||||||
``imp.find_module`` to fail to find the module, even when it's
|
|
||||||
installed. (This is the case in various exotic environments,
|
|
||||||
including some relevant for Google.) So we first try a fast-path,
|
|
||||||
and fall back to the slow import as needed.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
None
|
|
||||||
Returns:
|
|
||||||
None
|
|
||||||
Raises:
|
|
||||||
ImportError if OpenSSL is unavailable.
|
|
||||||
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
_, _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.crypto
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_TryOpenSslImport()
|
|
||||||
from oauth2client._openssl_crypt import OpenSSLVerifier
|
from oauth2client._openssl_crypt import OpenSSLVerifier
|
||||||
from oauth2client._openssl_crypt import OpenSSLSigner
|
from oauth2client._openssl_crypt import OpenSSLSigner
|
||||||
from oauth2client._openssl_crypt import pkcs12_key_as_pem
|
from oauth2client._openssl_crypt import pkcs12_key_as_pem
|
||||||
|
|||||||
@@ -66,54 +66,6 @@ class Test_pkcs12_key_as_pem(unittest.TestCase):
|
|||||||
password = u'notasecret'
|
password = u'notasecret'
|
||||||
self._succeeds_helper(password)
|
self._succeeds_helper(password)
|
||||||
|
|
||||||
def test_without_openssl(self):
|
|
||||||
import imp
|
|
||||||
imp_find_module = imp.find_module
|
|
||||||
orig_sys_path = sys.path
|
|
||||||
def find_module(module_name):
|
|
||||||
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
|
|
||||||
reload(crypt)
|
|
||||||
self.assertRaises(NotImplementedError, crypt.pkcs12_key_as_pem,
|
|
||||||
'FOO', 'BAR')
|
|
||||||
finally:
|
|
||||||
sys.path = orig_sys_path
|
|
||||||
imp.find_module = imp_find_module
|
|
||||||
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):
|
def test_with_nonsense_key(self):
|
||||||
from OpenSSL import crypto
|
from OpenSSL import crypto
|
||||||
credentials = self._make_signed_jwt_creds(private_key=b'NOT_A_KEY')
|
credentials = self._make_signed_jwt_creds(private_key=b'NOT_A_KEY')
|
||||||
|
|||||||
28
tox.ini
28
tox.ini
@@ -4,13 +4,14 @@ envlist = py26,py27,py33,py34,pypy,cover
|
|||||||
[testenv]
|
[testenv]
|
||||||
basedeps = keyring
|
basedeps = keyring
|
||||||
mock==1.0.1
|
mock==1.0.1
|
||||||
pycrypto==2.6
|
pycrypto>=2.6
|
||||||
|
cryptography>=1.0
|
||||||
|
pyopenssl>=0.14
|
||||||
webtest
|
webtest
|
||||||
nose
|
nose
|
||||||
flask
|
flask
|
||||||
deps = {[testenv]basedeps}
|
deps = {[testenv]basedeps}
|
||||||
django
|
django
|
||||||
pyopenssl==0.14
|
|
||||||
setenv = PYTHONPATH=../google_appengine
|
setenv = PYTHONPATH=../google_appengine
|
||||||
commands = nosetests --ignore-files=test_appengine\.py {posargs}
|
commands = nosetests --ignore-files=test_appengine\.py {posargs}
|
||||||
|
|
||||||
@@ -47,19 +48,6 @@ commands = {toxinidir}/scripts/build-docs
|
|||||||
basepython = python2.6
|
basepython = python2.6
|
||||||
deps = {[testenv]basedeps}
|
deps = {[testenv]basedeps}
|
||||||
django>=1.5,<1.6
|
django>=1.5,<1.6
|
||||||
pyopenssl==0.14
|
|
||||||
|
|
||||||
[testenv:py26openssl13]
|
|
||||||
basepython = python2.6
|
|
||||||
deps = {[testenv]basedeps}
|
|
||||||
django>=1.5,<1.6
|
|
||||||
pyopenssl<0.14
|
|
||||||
|
|
||||||
[testenv:py27openssl13]
|
|
||||||
basepython = python2.7
|
|
||||||
deps = {[testenv]basedeps}
|
|
||||||
django>=1.5,<1.6
|
|
||||||
pyopenssl<0.14
|
|
||||||
|
|
||||||
[testenv:system-tests]
|
[testenv:system-tests]
|
||||||
basepython =
|
basepython =
|
||||||
@@ -67,8 +55,9 @@ basepython =
|
|||||||
commands =
|
commands =
|
||||||
{toxinidir}/scripts/run_system_tests.sh
|
{toxinidir}/scripts/run_system_tests.sh
|
||||||
deps =
|
deps =
|
||||||
pycrypto==2.6
|
pycrypto>=2.6
|
||||||
pyopenssl==0.14
|
cryptography>=1.0
|
||||||
|
pyopenssl>=0.14
|
||||||
passenv = GOOGLE_* OAUTH2CLIENT_* TRAVIS*
|
passenv = GOOGLE_* OAUTH2CLIENT_* TRAVIS*
|
||||||
|
|
||||||
[testenv:system-tests3]
|
[testenv:system-tests3]
|
||||||
@@ -77,6 +66,7 @@ basepython =
|
|||||||
commands =
|
commands =
|
||||||
{toxinidir}/scripts/run_system_tests.sh
|
{toxinidir}/scripts/run_system_tests.sh
|
||||||
deps =
|
deps =
|
||||||
pycrypto==2.6
|
pycrypto>=2.6
|
||||||
pyopenssl==0.14
|
cryptography>=1.0
|
||||||
|
pyopenssl>=0.14
|
||||||
passenv = {[testenv:system-tests]passenv}
|
passenv = {[testenv:system-tests]passenv}
|
||||||
|
|||||||
Reference in New Issue
Block a user