IBM / ibm-cos-sdk-python-core

ibm-cos-sdk-python-core
Apache License 2.0
6 stars 14 forks source link

Error while generating presigned url with new apikeys #13

Closed kundanone closed 2 years ago

kundanone commented 2 years ago

When I generate a service API key, it has "-" in the string which fails the validation : e.g. api key: ***V4kt4-OH4klr_qtoXsF Stack trace:

aws_secret_access_key=config["cos_hmac_keys"]["secret_access_key"])
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ibm_boto3/__init__.py", line 97, in client
    return _get_default_session().client(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ibm_boto3/session.py", line 316, in client
    return self._session.create_client(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ibm_botocore/session.py", line 842, in create_client
    region_name = self._resolve_region_name(region_name, config)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ibm_botocore/session.py", line 935, in _resolve_region_name
    validate_region_name(region_name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ibm_botocore/utils.py", line 1007, in validate_region_name
    raise InvalidRegionError(region_name=region_name)
ibm_botocore.exceptions.InvalidRegionError: Provided region_name '****EZV4kt4-OH4klr_qtoXsF' doesn't match a supported format.

I can see that the root cause is its validator method: link: https://github.com/IBM/ibm-cos-sdk-python-core/blob/18e8a854a2a4fdca7fbb50d5b80fb666734def1f/ibm_botocore/utils.py#L1004

def validate_region_name(region_name):
    """Provided region_name must be a valid host label."""
    if region_name is None:
        return
    valid_host_label = re.compile(r'^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?<!-)$')
    valid = valid_host_label.match(region_name)
    if not valid:
        raise InvalidRegionError(region_name=region_name)

Workaround: comment out last two lines:

# if not valid:
# raise InvalidRegionError(region_name=region_name)

the validator needs to be updated as it is not accepting the new API key which has "-'' char in it.

IBMeric commented 2 years ago

Could you provide more detail or perhaps a code sample? Looking up the failure, this check is for validating the region name, not the API key used for authentication: https://github.com/IBM/ibm-cos-sdk-python-core/blob/18e8a854a2a4fdca7fbb50d5b80fb666734def1f/ibm_botocore/session.py#L752. Are you sure you are passing the correct values into the correct fields?

kundanone commented 2 years ago

Below is the sample code for generating presigned URL which throws this error.

import ibm_boto3
myserviceapikey = "abcA6raWVUVEdfGXG4IpKC1EZV4kt4-OH4klr_qtoXyZ"
myendpointurl = "https://s3.us-south.cloud-object-storage.appdomain.cloud"
config = {"cos_hmac_keys": {}}
config["cos_hmac_keys"]["access_key_id"] = "1231104d3bd348c283c98f99fddf0xyz"
config["cos_hmac_keys"]["secret_access_key"] = "abc9fa48ee0e5e27e079d365c5aa3c19d6a48a2064669xyz"
mybucket = "mybucket"
myobject = "myfile.txt"
cos = ibm_boto3.client('s3', myserviceapikey, endpoint_url=myendpointurl, aws_access_key_id=config["cos_hmac_keys"]["access_key_id"], aws_secret_access_key=config["cos_hmac_keys"]["secret_access_key"])
theURL=cos.generate_presigned_url('get_object', Params = {'Bucket': mybucket, 'Key': myobject}, ExpiresIn = 3600)
print(theURL)

PS: I have replaced some sensitive characters with different ones.

IBMeric commented 2 years ago

Thank you for the sample. I can reproduce your issue using Python 3.9.6, version 2.10.0 of the SDK, and valid credentials. The problem has nothing to do with presigned URLs; the exception happens when creating the client. Your function call is populating region_name with myserviceapikey because it is the next argument in the function signature: https://github.com/IBM/ibm-cos-sdk-python/blob/master/ibm_boto3/session.py#L223. You want to pass that value to ibm_api_key_id= instead.

If this resolves your issue, please close this ticket.

kundanone commented 2 years ago

Thank you. Closing th ticket.