app-search-python icon indicating copy to clipboard operation
app-search-python copied to clipboard

Client.create_signed_search_key() returns a byte type value causing signed key searches to fail authentication

Open TheRiffRafi opened this issue 5 years ago • 0 comments

Steps to reproduce

  • Have an environment with Python version 3.4 or above.
  • Install app-search-python
  • Try the following script:
from elastic_app_search import Client

endpoint = 'yourcoolentsearchcloudendpointhere.io/api/as/v1'
engine_name = 'test'

# Docs: https://github.com/elastic/app-search-python#create-a-signed-search-key
signed_search_key = Client.create_signed_search_key(
    'search-xxxxxxxxxxxx',  
    'search-key',
    {'search_fields': { 'description': {}}})

client = Client(
    base_endpoint=endpoint,
    api_key=signed_search_key,
    use_https=True
)

result  = client.search(engine_name, 'into')
print (result)

You will get this:

elastic_app_search.exceptions.InvalidCredentials: Unauthorized

And rightly so, because if you were to print the created encoded key you will get something like:

b' eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzZWFyY2hfZmllbGRzIjp7ImRlc2NyaXB0aW9uIjp7fX0sImFwaV9rZXlfbmFtZSI6InNlYXJjaC1rZXkifQ.JezVQ4D6L76FadCvplQnCv8QpVJzxqy0dz-i_X4C4s0 '

indicating that this is a byte type value.

Work around The workaround is to add .decode('utf8') to the signed_search_key variable in order to pass the key as a string type value and not a byte type one, like this:

client = Client(
    base_endpoint=endpoint,
    api_key=signed_search_key.decode('utf8'),
    use_https=True
)

And now you will be able to search to your heart's desire.

This doesn't happen on Python 3.3 (our current dependency). But Python 3.3 is already EOL, and ent-search users would rather not have to run EOL code in production in order to use this nifty client.

Cheers!

TheRiffRafi avatar Sep 11 '20 02:09 TheRiffRafi