cloudfoundry-community / cf-python-client

Small cloudfoundry client implemented in python
Apache License 2.0
52 stars 50 forks source link

Since v1.4.x Authorization fails #191

Closed frruit closed 1 year ago

frruit commented 1 year ago

Hi, by updating the library from 1.33.0 to 1.4.x an authentication error 401 occurs

Here is the code snippet which is using the library

# run with python 3.9.7
class MyCloudFoundryWrapper(object):
    def __init__(self, target_endpoint: str, auth: Tuple[str, str], org_name: str, user_guid: UUID, **kwargs):
        self.target_endpoint = target_endpoint
        self.client = CloudFoundryClient(self.target_endpoint, **kwargs)
        self.cf_user_guid: UUID = user_guid
        self.client.init_with_user_credentials(auth[0], auth[1])
        ....

auth contains username and password.

After the upgrade during the initialisation the following error occures: oauth2_client.credentials_manager.OAuthError: 401 - unauthorized : Bad credentials

See stacktrace

Traceback (most recent call last):
  File "/home/frruit/projects/test_project/.venv/lib/python3.9/site-packages/celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/frruit/projects/test_project/.venv/lib/python3.9/site-packages/celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/frruit/projects/test_project/test_project/celery_app/tasks.py", line 82, in request_open_stack_environment
    bcf = BodeCloudFoundry(target_endpoint=config.CF_URL, auth=(config.CF_USER, config.CF_PASSWORD),
  File "/home/frruit/projects/test_project/test_project/bode_cloud_foundry/bode_cloud_foundry.py", line 18, in __init__
    self.client.init_with_user_credentials(auth[0], auth[1])
  File "/home/frruit/projects/test_project/.venv/lib/python3.9/site-packages/oauth2_client/credentials_manager.py", line 146, in init_with_user_credentials
    self._token_request(self._grant_password_request(login, password), True)
  File "/home/frruit/projects/test_project/.venv/lib/python3.9/site-packages/oauth2_client/credentials_manager.py", line 200, in _token_request
    CredentialManager._handle_bad_response(response)
  File "/home/frruit/projects/test_project/.venv/lib/python3.9/site-packages/oauth2_client/credentials_manager.py", line 82, in _handle_bad_response
    raise OAuthError(HTTPStatus(response.status_code), error.get('error'), error.get('error_description'))
oauth2_client.credentials_manager.OAuthError: 401  - unauthorized : Bad credentials

I have realised that in the meantime also some dependencies has been upgraded:

• Updating oauth2-client (1.2.1 -> 1.4.0) • Updating cloudfoundry-client (1.33.0 -> 1.34.0)

Any idea and thanks for support?

Thanhphan1147 commented 1 year ago

@antechrestos I made a PR to fix the missing client_secret field in the json body for oauth2 token requests:

https://github.com/antechrestos/OAuth2Client/pull/13

Thanhphan1147 commented 1 year ago

In the meantime I'd suggest @frruit to move back to 1.33.0

frruit commented 1 year ago

Thanks a lot for the quick reaction. I will follow also your recommendation and wait for the next release :-)

antechrestos commented 1 year ago

Fixed in 1.34.2