Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Helpful error message in P12 factory in absence of pyOpenSSL. #424

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions oauth2client/_pycrypto_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ def from_string(key, password='notasecret'):
pkey = RSA.importKey(parsed_pem_key)
else:
raise NotImplementedError(
'PKCS12 format is not supported by the PyCrypto library. '
'Try converting to a "PEM" '
'(openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > '
'privatekey.pem) '
'or using PyOpenSSL if native code is an option.')
'No key in PEM format was detected. This implementation '
'can only use the PyCrypto library for keys in PEM '
'format.')
return PyCryptoSigner(pkey)
2 changes: 2 additions & 0 deletions oauth2client/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ def _from_p12_keyfile_contents(cls, service_account_email,
"""
if private_key_password is None:
private_key_password = _PASSWORD_DEFAULT
if crypt.Signer is not crypt.OpenSSLSigner:

This comment was marked as spam.

This comment was marked as spam.

raise NotImplementedError(_PKCS12_ERROR)
signer = crypt.Signer.from_string(private_key_pkcs12,
private_key_password)
credentials = cls(service_account_email, signer, scopes=scopes)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ def _from_p12_keyfile_helper(self, private_key_password=None, scopes=''):
self.assertEqual(creds._private_key_password, private_key_password)
self.assertEqual(creds._scopes, ' '.join(scopes))

def _p12_not_implemented_helper(self):
service_account_email = '[email protected]'
filename = data_filename('privatekey.p12')
with self.assertRaises(NotImplementedError):
ServiceAccountCredentials.from_p12_keyfile(
service_account_email, filename)

@mock.patch('oauth2client.crypt.Signer', new=crypt.PyCryptoSigner)
def test_from_p12_keyfile_with_pycrypto(self):
self._p12_not_implemented_helper()

@mock.patch('oauth2client.crypt.Signer', new=crypt.RsaSigner)
def test_from_p12_keyfile_with_rsa(self):
self._p12_not_implemented_helper()

def test_from_p12_keyfile_defaults(self):
self._from_p12_keyfile_helper()

Expand Down