1919import six
2020from six .moves .urllib .parse import urlencode # pylint: disable=F0401
2121
22- from OpenSSL import crypto
22+ try :
23+ from OpenSSL import crypto
24+ except ImportError : # pragma: NO COVER
25+ # pyOpenSSL can't be installed on App Engine, but it will not
26+ # be needed there since app_identity is used.
27+ crypto = None
2328
2429from oauth2client import client
2530from oauth2client .client import _get_application_default_credential_from_file
@@ -170,6 +175,7 @@ def _get_pem_key(credentials):
170175 :rtype: :class:`OpenSSL.crypto.PKey`
171176 :returns: A PKey object used to sign text.
172177 :raises: `TypeError` if `credentials` is the wrong type.
178+ `EnvironmentError` if `crypto` did not import successfully.
173179 """
174180 if isinstance (credentials , client .SignedJwtAssertionCredentials ):
175181 # Take our PKCS12 (.p12) text and convert to PEM text.
@@ -181,6 +187,9 @@ def _get_pem_key(credentials):
181187 raise TypeError ((credentials ,
182188 'not a valid service account credentials type' ))
183189
190+ if crypto is None :
191+ raise EnvironmentError (
192+ 'pyOpenSSL must be installed to load a private key' )
184193 return crypto .load_privatekey (crypto .FILETYPE_PEM , pem_text )
185194
186195
@@ -198,6 +207,7 @@ def _get_signature_bytes(credentials, string_to_sign):
198207
199208 :rtype: bytes
200209 :returns: Signed bytes produced by the credentials.
210+ :raises: `EnvironmentError` if `crypto` did not import successfully.
201211 """
202212 if isinstance (credentials , _GAECreds ):
203213 _ , signed_bytes = app_identity .sign_blob (string_to_sign )
@@ -207,6 +217,10 @@ def _get_signature_bytes(credentials, string_to_sign):
207217 pkey = _get_pem_key (credentials )
208218 if not isinstance (string_to_sign , six .binary_type ):
209219 string_to_sign = string_to_sign .encode ('utf-8' )
220+ if crypto is None :
221+ raise EnvironmentError (
222+ 'pyOpenSSL must be installed to sign content using a '
223+ 'private key' )
210224 return crypto .sign (pkey , string_to_sign , 'SHA256' )
211225
212226
0 commit comments