|
@@ -1,20 +1,32 @@
|
|
|
import ssl
|
|
import ssl
|
|
|
|
|
+import base64
|
|
|
|
|
|
|
|
from elasticsearch import AsyncElasticsearch
|
|
from elasticsearch import AsyncElasticsearch
|
|
|
from elasticsearch.helpers import async_bulk
|
|
from elasticsearch.helpers import async_bulk
|
|
|
|
|
|
|
|
|
|
+from app.core.config import GlobalConfigSettings
|
|
|
|
|
+
|
|
|
|
|
|
|
|
class AsyncElasticSearchClient:
|
|
class AsyncElasticSearchClient:
|
|
|
- def __init__(self, index_):
|
|
|
|
|
- self.password = "nkvvASQuQ0XUGRq5OLvm"
|
|
|
|
|
- self.hosts = ["https://192.168.205.85:9200", "https://192.168.205.85:9300"]
|
|
|
|
|
- self.ctx = ssl.create_default_context(
|
|
|
|
|
- cafile="app/core/config/cert/es_certs.crt"
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ def __init__(self, global_config: GlobalConfigSettings):
|
|
|
|
|
+ config = global_config.elasticsearch
|
|
|
|
|
+ self.hosts = config.hosts
|
|
|
|
|
+
|
|
|
|
|
+ # 优先使用 cert_content (base64 编码的证书内容),其次使用 cert_path
|
|
|
|
|
+ if config.cert_content:
|
|
|
|
|
+ cert_data = base64.b64decode(config.cert_content).decode("utf-8")
|
|
|
|
|
+ self.ctx = ssl.create_default_context(cadata=cert_data)
|
|
|
|
|
+ elif config.cert_path:
|
|
|
|
|
+ self.ctx = ssl.create_default_context(cafile=config.cert_path)
|
|
|
|
|
+ else:
|
|
|
|
|
+ self.ctx = ssl.create_default_context()
|
|
|
|
|
+
|
|
|
self.es = AsyncElasticsearch(
|
|
self.es = AsyncElasticsearch(
|
|
|
- self.hosts, basic_auth=("elastic", self.password), ssl_context=self.ctx
|
|
|
|
|
|
|
+ self.hosts,
|
|
|
|
|
+ basic_auth=(config.username, config.password),
|
|
|
|
|
+ ssl_context=self.ctx,
|
|
|
)
|
|
)
|
|
|
- self.index_name = index_
|
|
|
|
|
|
|
+ self.index_name = config.index_name
|
|
|
|
|
|
|
|
async def create_index(self, settings, mappings):
|
|
async def create_index(self, settings, mappings):
|
|
|
exists = await self.es.indices.exists(index=self.index_name)
|
|
exists = await self.es.indices.exists(index=self.index_name)
|