Unable to access ElasticSearch AWS through Python
I am trying to access ElasticSearch AWS from my localhost via Python (I can access it via my browser).
from elasticsearch import Elasticsearch ELASTIC_SEARCH_ENDPOINT = 'https://xxx' es = Elasticsearch([ELASTIC_SEARCH_ENDPOINT])
I am getting this error:
ImproperlyConfigured('Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to use it automatically.',)
How can I access it? I have not configured any certificate, I have only freed up the IPs that can access the ElasticSearch service.
+3
source to share
4 answers
elasticsearch-py does not come with a default set of root certificates. To have a valid SSL certificate validation, you either need to specify your own as ca_certs or install a certifi which will be automatically downloaded.
from elasticsearch import Elasticsearch
# you can use RFC-1738 to specify the url
es = Elasticsearch(['https://user:secret@localhost:443'])
# ... or specify common parameters as kwargs
# use certifi for CA certificates
import certifi
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
port=443,
use_ssl=True
)
# SSL client authentication using client_cert and client_key
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
port=443,
use_ssl=True,
ca_certs='/path/to/cacert.pem',
client_cert='/path/to/client_cert.pem',
client_key='/path/to/client_key.pem',
)
+4
source to share
I did it this way and it worked:
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
host = 'YOURHOST.us-east-1.es.amazonaws.com'
awsauth = AWS4Auth(YOUR_ACCESS_KEY, YOUR_SECRET_KEY, REGION, 'es')
es = Elasticsearch(
hosts=[{'host': host, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
print(es.info())
+2
source to share
You can also use boto3 to generate an access key and private key.
import boto3 region = 'ap-southeast-2' service = 'es' session = boto3.Session() credentials = session.get_credentials() awsauth = AWS4Auth(credentials.access_key, credentials.secret_key,region, service,session_token=credentials.token) es = Elasticsearch( hosts = [{'host': host, 'port': 443}], http_auth = awsauth, use_ssl = True, verify_certs = True, connection_class = RequestsHttpConnection )
https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-indexing.html
0
source to share