mattes
mattes

Reputation: 9429

No module named OpenSSL.crypto and ImportError: SignedJwtAssertionCredentials

I am trying to connect to the BigQuery API locally with my local dev_appserver, following this tutorial: https://developers.google.com/bigquery/authorization?hl=de#service-accounts Running the code mentioned on this site, returns an ImportError:

ImportError: cannot import name SignedJwtAssertionCredentials

So I followed the error and spotted (in oauth2client/client.py):

if HAS_CRYPTO:
  # PyOpenSSL and PyCrypto are not prerequisites for oauth2client, so if it is
  # missing then don't create the SignedJwtAssertionCredentials or the
  # verify_id_token() method.

  class SignedJwtAssertionCredentials(AssertionCredentials):

But I need "SignedJwtAssertionCredentials"! So I isolated the error further and found (in oauth2client/crypt.py) that this line is actually causing this issue:

from OpenSSL import crypto

I tried:

$ python
>>> import OpenSSL
>>> OpenSSL.__file__
'/usr/local/lib/python2.7/site-packages/OpenSSL/__init__.pyc'
>>> from OpenSSL import crypto
>>> crypto.__file__
'/usr/local/lib/python2.7/site-packages/OpenSSL/crypto.so'

which looks promising and also checked the sys.path of my code:

['/Users/mattes/Developer/gae-projects/project123', 
 '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine', 
 '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine', 
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.1.1', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml-3.10']

Anyway, neither adding "/usr/local/lib/python2.7/site-packages/OpenSSL" to the sys.path nor symlinking /usr/local/lib/python2.7/site-packages/OpenSSL under /Users/mattes/Developer/gae-projects/project123 fixes this issue.

/usr/local/lib/python2.7/site-packages/OpenSSL looks like:

├── SSL.so
├── __init__.py
├── __init__.pyc
├── crypto.so
├── rand.so
├── test
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── test_crypto.py
│   ├── test_crypto.pyc
│   ├── test_rand.py
│   ├── test_rand.pyc
│   ├── test_ssl.py
│   ├── test_ssl.pyc
│   ├── util.py
│   └── util.pyc
├── tsafe.py
├── tsafe.pyc
├── version.py
└── version.pyc

using Mac 10.9 Mavericks, Python 2.7.5

Can somebody help?

Upvotes: 2

Views: 8821

Answers (2)

theScarletManuka
theScarletManuka

Reputation: 96

To get this running on the GAE servers I found that three steps were necessary:

  1. Install the up-to-date version of the Google API Client (or at least the oauth2client module). Note that they provide a GAE targetted download.

  2. Convert my .p12 key file to a .pem format (using the openssl command line tools)

    openssl pkcs12  -nocerts -in cert.p12 -out cert.pem
    
  3. Add the PyCrypto library to the app.yaml.

    libraries:
      - name: pycrypto
        version: "2.6"  # this could be "latest" if you are daring
    

For dev_appserver it was also necessary to install the PyCrypto library locally, as it is not contained in the SDK. (OpenSSL is also supported by the API Client library, but I assume that using PyCrypto stays closer to the runtime environment.)

Upvotes: 5

mattes
mattes

Reputation: 9429

Fixed the issue by adding pycrypto to libraries section in my app.yaml:

libraries:

- name: pycrypto
  version: "latest"

Upvotes: 3

Related Questions