Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] connector_elasticsearch: add http auth + use server env #171

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions connector_elasticsearch/models/se_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class SeBackend(models.Model):
string="ElasticSearch host",
groups="connector_search_engine.group_connector_search_engine_manager",
)
auth_type = fields.Selection(
selection=[("http", "HTTP"), ("api_key", "API key")], default="api_key"
)
api_key_id = fields.Char(
help="Elasticsearch Api Key ID",
string="Api Key ID",
Expand All @@ -28,6 +31,28 @@ class SeBackend(models.Model):
help="Elasticsearch Api Key",
groups="connector_search_engine.group_connector_search_engine_manager",
)
es_user = fields.Char(help="Leave blank if not using http authentication.")
es_password = fields.Char(help="Leave blank if not using http authentication.")
ssl = fields.Boolean(
default=True,
help="Verify SSL certificates. Only set to False in development environments.",
)

@property
def _server_env_fields(self):
env_fields = super()._server_env_fields
env_fields.update(
{
"es_server_host": {},
"auth_type": {},
"es_user": {},
"es_password": {},
"ssl": {},
"api_key_id": {},
"api_key": {},
}
)
return env_fields

def _get_adapter_class(self):
if self.backend_type == "elasticsearch":
Expand Down
26 changes: 16 additions & 10 deletions connector_elasticsearch/tools/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,22 @@

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
)
return elasticsearch.Elasticsearch(
[backend.es_server_host],
connection_class=self._es_connection_class,
api_key=api_key,
)
if backend.auth_type == "api_key":
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,
)
if backend.auth_type == "http":
auth = (backend.es_user, backend.es_password)
return elasticsearch.Elasticsearch(

Check warning on line 67 in connector_elasticsearch/tools/adapter.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/tools/adapter.py#L66-L67

Added lines #L66 - L67 were not covered by tests
[backend.es_server_host], http_auth=auth, use_ssl=backend.ssl
)

def test_connection(self):
es = self._es_client
Expand Down
23 changes: 21 additions & 2 deletions connector_elasticsearch/views/se_backend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@
placeholder="http://elastic:9200"
attrs="{'required': [('backend_type', '=', 'elasticsearch')]}"
/>
<field name="api_key_id" />
<field name="api_key" />
<field name="auth_type" />
<field
name="es_user"
attrs="{'invisible': [('auth_type', '!=', 'http')]}"
/>
<field
name="es_password"
attrs="{'invisible': [('auth_type', '!=', 'http')]}"
/>
<field
name="ssl"
attrs="{'invisible': [('auth_type', '!=', 'http')]}"
/>
<field
name="api_key_id"
attrs="{'invisible': [('auth_type', '!=', 'api_key')]}"
/>
<field
name="api_key"
attrs="{'invisible': [('auth_type', '!=', 'api_key')]}"
/>
</group>
</group>
</field>
Expand Down
2 changes: 1 addition & 1 deletion connector_search_engine/models/se_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _compute_binding_count(self):

@property
def _server_env_fields(self):
return {"index_prefix_name": {}}
return {"index_prefix_name": {}, "backend_type": {}}

@api.onchange("tech_name", "index_prefix_name")
def _onchange_tech_name(self):
Expand Down
Loading