简体   繁体   English

无法通过Python访问ElasticSearch AWS

[英]Unable to access ElasticSearch AWS through Python

I'm trying to access ElasticSearch AWS from my localhost through Python (I can access it through my browser). 我正在尝试通过Python从本地主机访问ElasticSearch AWS(可以通过浏览器访问它)。

from elasticsearch import Elasticsearch
ELASTIC_SEARCH_ENDPOINT = 'https://xxx'
es = Elasticsearch([ELASTIC_SEARCH_ENDPOINT])

I'm receiving 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 only liberated the IPs that can access ElasticSearch Service. 我没有配置任何证书,只释放了可以访问ElasticSearch Service的IP。

for python 3.5 install certifi and use ca_certs=certifi.where() this will pass the certificates 对于python 3.5安装certifi并使用ca_certs = certifi.where()这将通过证书

import certifi
from elasticsearch import Elasticsearch

host = 'https://###########.ap-south-1.es.amazonaws.com'

es = Elasticsearch([host], use_ssl=True, ca_certs=certifi.where())

elasticsearch-py doesn't ship with default set of root certificates. elasticsearch-py不附带默认的根证书集。 To have working SSL certificate validation you need to either specify your own as ca_certs or install certifi which will be picked up automatically. 要进行有效的SSL证书验证,您需要将自己的证书指定为ca_certs或安装将自动提取的证书。

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',
)

https://elasticsearch-py.readthedocs.io/en/master/ https://elasticsearch-py.readthedocs.io/en/master/

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())

You can also use boto3 to generate tempory access key & secret key. 您还可以使用boto3生成临时访问密钥和秘密密钥。

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 https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-indexing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM