Azure/azure-cosmos-python

Running unit tests with bad master key raises unfriendly exception

Closed this issue · 6 comments

I ran the unit tests with an intentionally invalid master key. The tests fail with:

Error
Traceback (most recent call last):
  File "C:\Users\clam\Documents\azure-documentdb-python\test\crud_tests.py", line 40, in setUp
    databases = list(client.ReadDatabases())
  File "C:\Users\clam\Documents\azure-documentdb-python\pydocumentdb\query_iterable.py", line 68, in next
    callback, self._resource_throttle_retry_policy)
  File "C:\Users\clam\Documents\azure-documentdb-python\pydocumentdb\backoff_retry_utility.py", line 27, in Execute
    raise e
Error: Incorrect padding

Some tests also log WARNING:root:Operation will NOT be retried. Exception: Incorrect padding.

Hi Carson,

I tried tampering with the master key by deleting some of the chars and I was getting:

HTTPFailure: Status code: 401
{"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ndbs\n\nwed, 11 nov 2015 06:30:31 gmt\n\n'\r\nActivityId: ef1b4818-5541-4b92-8455-786735288abd"}

Can you give an example of the invalid master key that gives you that exception?

Regards,
Rajesh

Hey Rajesh - something like "xxxxx" or the existing dummy value of "[YOUR_KEY_HERE]" triggers the exception.

I get the same Error: Incorrect padding when I haven't entered the [YOUR_KEY_HERE] & [YOUR_ENDPOINT_HERE].

@rnagpal it's because of auth.py __GetAuthorizationTokenUsingMasterKey here it does:
key = master_key.decode('base64')

If the invalid master key doesn't decode from base64 correctly, then you get the error: Error: Incorrect padding, but the stacktrace is not correct due to the try/except backoff_retry_utility.Execute
which catches any exception and doesn't re-raise the caught exception e and instead does an empty raise.

@RedCraig We had fixed this stack trace issue sometimes in Jan this year, so the stack trace shown in the initial thread now shows as:

File "F:\DocDB-Main1\Product\SDK\python\python\pydocumentdb\auth.py", line 37, in GetAuthorizationHeader
document_client.master_key)
File "F:\DocDB-Main1\Product\SDK\python\python\pydocumentdb\auth.py", line 61, in __GetAuthorizationTokenUsingMasterKey
key = master_key.decode('base64')
File "C:\Python27\lib\encodings\base64_codec.py", line 42, in base64_decode
output = base64.decodestring(input)
File "C:\Python27\lib\base64.py", line 321, in decodestring
return binascii.a2b_base64(s)
Error: Incorrect padding

We were doing "raise e" earlier which didn't showed the complete stack trace. But now that has been changed to "raise"(empty raise) which does shows the complete stack trace that I pasted above.

I see that you have a PR to fix this annoying message for someone just coming in and running the tests as is. Thanks!

@rbcarson : @RedCraig had fixed this issue by submitting a PR which has been merged. Closing this issue.