stavxyz/requests-chef

TypeError: Expected instance of hashes.HashAlgorithm.

Closed this issue · 3 comments

Attempted to use your python module, however The example isn't working as requested. I've also not been able to install cryptography==1.3.1f, so I tried with both 1.7 and 1.3.2, and the result below is the same. I'm not sure what's happening however I'm following the example as provided and it isn't working.

Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests, requests_chef
>>> auth = requests_chef.ChefAuth('user', '~/.chef/user.pem')
>>> print auth
ChefAuth(user)
>>> response = requests.get('https://chef.domain.com/users/user', headers={'X-Chef-Version': '12.0.2'}, auth=auth)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 407, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 306, in prepare
    self.prepare_auth(auth, url)
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 527, in prepare_auth
    r = auth(self)
  File "/usr/local/lib/python2.7/site-packages/requests_chef/mixlib_auth.py", line 108, in __call__
    signed = self.private_key.sign(canonical_request, b64=True)
  File "/usr/local/lib/python2.7/site-packages/requests_chef/mixlib_auth.py", line 185, in sign
    signer = self.private_key.signer(padder, None)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 531, in signer
    return _RSASignatureContext(self._backend, self, padding, algorithm)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 207, in __init__
    self._hash_ctx = hashes.Hash(self._algorithm, self._backend)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/primitives/hashes.py", line 76, in __init__
    raise TypeError("Expected instance of hashes.HashAlgorithm.")
TypeError: Expected instance of hashes.HashAlgorithm.
>>> response = requests.get('https://chef.domain.com/users/user', headers={'X-Chef-Version': '12.0.2'}, auth=auth)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 407, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 306, in prepare
    self.prepare_auth(auth, url)
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 527, in prepare_auth
    r = auth(self)
  File "/usr/local/lib/python2.7/site-packages/requests_chef/mixlib_auth.py", line 108, in __call__
    signed = self.private_key.sign(canonical_request, b64=True)
  File "/usr/local/lib/python2.7/site-packages/requests_chef/mixlib_auth.py", line 185, in sign
    signer = self.private_key.signer(padder, None)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 531, in signer
    return _RSASignatureContext(self._backend, self, padding, algorithm)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 207, in __init__
    self._hash_ctx = hashes.Hash(self._algorithm, self._backend)
  File "/usr/local/lib/python2.7/site-packages/cryptography/hazmat/primitives/hashes.py", line 76, in __init__
    raise TypeError("Expected instance of hashes.HashAlgorithm.")
TypeError: Expected instance of hashes.HashAlgorithm.

Unfortunately, this library will only work with my fork of cryptography. A few weeks ago the outstanding issue in cryptography was solved, so in theory, I should be able to update this project to use a legitimate release of the cryptography library after some modifications. See pyca/cryptography#1648 for more info. And feel free to try to get this working and submit a PR ! :)

What is the error you get when installing?

$ pip install git+https://github.com/samstav/cryptography.git@rsa-bypass-hash-on-signer

Is it something like this?

    error: command 'clang' failed with exit status 1

    ----------------------------------------
...

If so, and you are using homebrew installed python, try this:

$ env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" pip install git+https://github.com/samstav/cryptography.git@rsa-bypass-hash-on-signer

From the cryptography documentation

FWIW, the real cryptography doesn't allow this line:

https://github.com/samstav/requests-chef/blob/a0bf013b925abd0cf76eeaf6300cf32659632773/requests_chef/mixlib_auth.py#L185

i.e. passing None as the hashing algorithm. I modified cryptography to accept this pattern.

Made a quick attempt at this: #22

Still not working, but it might be close.