openzim / python-storagelib

S3 Cache wrapper to use within Kiwix/OpenZIM/Offspot projects
GNU General Public License v3.0
1 stars 1 forks source link

"failsafe" parameter not working for check_credentials #1

Closed nabinkhadka closed 6 months ago

nabinkhadka commented 4 years ago

For case with incorrect creds in the URL, I can see the following

>>> from kiwixstorage import KiwixStorage
>>> s3_storage = KiwixStorage('https://s3.eu-central-1.wasabisys.com/?keyId=XXXXX&secretAccessKey=XXXXXX&bucketName=testbucket-nabin')
>>>s3_storage.check_credentials(list_buckets=True, failsafe=True)
False

In this case, the failsafe command works as expected.

Now , if I check for access to the bucket in URL,

>>> s3_storage.check_credentials(bucket=True, failsafe=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 331, in check_credentials
    self.test_access_bucket(bucket_name=bucket_name)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 277, in test_access_bucket
    if self.get_bucket(bucket_name).creation_date is None:
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/boto3/resources/factory.py", line 339, in property_loader
    self.load()
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/boto3/s3/inject.py", line 72, in bucket_load
    response = self.meta.client.list_buckets()
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (SignatureDoesNotMatch) when calling the ListBuckets operation: The request signature we calculated does not match the signature you provided. Check your key and signing method.

The failsafe parameter does not work as expected. Similar behavior can be seen for other checks as well.

nabinkhadka commented 4 years ago

While working at feedback on this, I wanted to be sure if the creds can read and write a bucket. So I now need to add another try-except for achieving that which would have been good if failsafe helped.

rgaudin commented 4 years ago

you can check this with write=True, read=True.

I’ve identified other cases where it raises instead of returning False that are similar but different to the one you reported. Will fix and inform you. If your credentials are Ok, it won’t fail though.

nabinkhadka commented 4 years ago

@rgaudin Throws exception with write=True and read=True

>>> s3_storage.check_credentials(write=True, read=True, failsafe=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 334, in check_credentials
    self.test_access_write(
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 286, in test_access_write
    self.put_text_object(key, key, bucket_name=bucket_name)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 453, in put_text_object
    self.client.put_object(
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAccessKeyId) when calling the PutObject operation: The AWS Access Key Id you provided does not exist in our records.
>>> s3_storage.check_credentials(write=True, failsafe=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 334, in check_credentials
    self.test_access_write(
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 286, in test_access_write
    self.put_text_object(key, key, bucket_name=bucket_name)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/kiwixstorage/__init__.py", line 453, in put_text_object
    self.client.put_object(
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/nabin/upwork/kiwix/v3.8/lib/python3.8/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAccessKeyId) when calling the PutObject operation: The AWS Access Key Id you provided does not exist in our records.
rgaudin commented 4 years ago

That's exactly what I said. there are uncaught cases at the moment. But, it means your credentials are not OK.

I just pushed a script that simplifies testing. You can use it like:

PYTHONPATH=$(pwd)/src/ python src/kiwixstorage/test_credentials.py "https://xxxxx"

I see in your URL above that you use the eu-central-1 region in your URL. If the bucket you created is on another region (you had to choose where to create it and you can see it in your buckets list), then it won't work. That may be your problem.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be now be reviewed manually. Thank you for your contributions.