diff --git a/connector_elasticsearch/components/adapter.py b/connector_elasticsearch/components/adapter.py index 4ce8bf83..3f940582 100644 --- a/connector_elasticsearch/components/adapter.py +++ b/connector_elasticsearch/components/adapter.py @@ -4,6 +4,7 @@ import logging from odoo import _, exceptions +from odoo.exceptions import UserError from odoo.addons.component.core import Component @@ -37,20 +38,31 @@ def _es_connection_class(self): def _get_es_client(self): backend = self.backend_record - api_key = ( - (backend.api_key_id, backend.api_key) - if backend.api_key_id and backend.api_key - else None - ) - if not backend.es_server_host: - raise exceptions.UserError(_("No ElasticSearch host defined")) - # UserError to be consistent with - # se_backend_elasticsearch.py - return elasticsearch.Elasticsearch( - [backend.es_server_host], - connection_class=self._es_connection_class, - api_key=api_key, - ) + + if backend.is_http_authentication: + if backend.es_user and backend.es_password: + auth = (backend.es_user, backend.es_password) + es = elasticsearch.Elasticsearch( + [backend.es_server_host], http_auth=auth + ) + else: + es = elasticsearch.Elasticsearch([backend.es_server_host]) + + if not es.ping(): # pragma: no cover + raise UserError(_("Connect Exception with elasticsearch")) + + return es + else: + api_key = ( + (backend.api_key_id, backend.api_key) + if backend.api_key_id and backend.api_key + else None + ) + return elasticsearch.Elasticsearch( + [backend.es_server_host], + connection_class=self._es_connection_class, + api_key=api_key, + ) def index(self, records): es = self._get_es_client() diff --git a/connector_elasticsearch/models/se_backend_elasticsearch.py b/connector_elasticsearch/models/se_backend_elasticsearch.py index 6cc42c1e..00a14ecd 100644 --- a/connector_elasticsearch/models/se_backend_elasticsearch.py +++ b/connector_elasticsearch/models/se_backend_elasticsearch.py @@ -1,6 +1,5 @@ # Copyright 2019 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - from elasticsearch import AuthenticationException, NotFoundError from odoo import _, fields, models @@ -28,29 +27,39 @@ class SeBackendElasticsearch(models.Model): tech_name = fields.Char( related="se_backend_id.tech_name", store=True, readonly=False ) + + is_http_authentication = fields.Boolean( + string="use http authentication", default=False + ) + api_key_id = fields.Char(help="Elasticsearch Api Key ID", string="Api Key ID") api_key = fields.Char(help="Elasticsearch Api Key") + es_user = fields.Char(help="Leave blank if not using http authentication.") + es_password = fields.Char(help="Leave blank if not using http authentication.") + @property def _server_env_fields(self): env_fields = super()._server_env_fields - env_fields.update({"es_server_host": {}}) + env_fields.update({"es_server_host": {}, "es_user": {}, "es_password": {}}) return env_fields def action_test_connection(self): with self.specific_backend.work_on(self._name) as work: adapter = work.component(usage="se.backend.adapter") es = adapter._get_es_client() - try: - es.security.authenticate() - except NotFoundError: - raise UserError(_("Unable to reach host.")) - except AuthenticationException: - raise UserError(_("Unable to authenticate. Check credentials.")) - except Exception as e: - raise UserError( - _("Unable to connect to ElasticSearch:") + "\n\n" + repr(e) - ) + + if not self.is_http_authentication: + try: + es.security.authenticate() + except NotFoundError: + raise UserError(_("Unable to reach host.")) + except AuthenticationException: + raise UserError(_("Unable to authenticate. Check credentials.")) + except Exception as e: + raise UserError( + _("Unable to connect to ElasticSearch:") + "\n\n" + repr(e) + ) return { "type": "ir.actions.client", "tag": "display_notification", diff --git a/connector_elasticsearch/views/se_backend_elasticsearch.xml b/connector_elasticsearch/views/se_backend_elasticsearch.xml index c38c22dd..3ca05bbf 100644 --- a/connector_elasticsearch/views/se_backend_elasticsearch.xml +++ b/connector_elasticsearch/views/se_backend_elasticsearch.xml @@ -18,8 +18,24 @@ string="Host" placeholder="http://elastic:9200" /> - - + + + + +