Unable to connect to elasticsearch using elasticsearch-py when password contains special characters


(rory) #1

I have a python script and when I try to connect to an elasticsearch cluster that is using shield and the user's password contains special characters I am unable to connect.

python script is unable to connect to es cluster due to problems parsing the special characters.

connection string is https://test:%23passw0rd%23@myhost:9200

I've tried using it with and without quote_plus from urllib but have been unable to connect.
without urllib.quote_plus I get the following error:
ValueError: invalid literal for int() with base 10: ''

with urlib.quote_plus I get a connection error:

/usr/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py:54: UserWarning: Connecting to myhost using SSL with verify_certs=False is insecure.
'Connecting to %s using SSL with verify_certs=False is insecure.' % host)
Traceback (most recent call last):
File "./es.py", line 65, in
indices = es.indices.get(index='*')
File "/usr/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/lib/python2.7/site-packages/elasticsearch/client/indices.py", line 130, in get
feature), params=params)
File "/usr/lib/python2.7/site-packages/elasticsearch/transport.py", line 307, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/usr/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 89, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x1e37fd0>: Failed to establish a new connection: [Errno -2] Name or service not known) caused by: NewConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x1e37fd0>: Failed to establish a new connection: [Errno -2] Name or service not known)

script is below:

username = 'test'
password = urllib.quote_plus('#passw0rd#')
hostname = 'myhost'
port = '9200'

if '--ssl' in args:
proto = 'https://'
else:
proto = 'http://'

url = proto + username + ':' + password + '@' + hostname + ':' + port
es = elasticsearch([url])

try:
es.ping()
except Exception, err:
print 'error: connection failed'
sys.exit(1)

....


(rory) #2

I fixed it. the server I was running the script from didn't have the ca cert installed =(


(system) #3