Skip to content

Commit

Permalink
fixed advanced search (#2748)
Browse files Browse the repository at this point in the history
* fixed advanced search

* fixed tests
  • Loading branch information
drosetti authored Feb 18, 2025
1 parent a1a5dbf commit 3198062
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 119 deletions.
81 changes: 0 additions & 81 deletions api_app/documents.py

This file was deleted.

3 changes: 1 addition & 2 deletions api_app/management/commands/elastic_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.conf import settings
from django.core.management import BaseCommand
from elasticsearch import ApiError
from elasticsearch_dsl import connections

logger = logging.getLogger(__name__)

Expand All @@ -22,7 +21,7 @@ def handle(self, *args, **options):
settings.CONFIG_ROOT / "elastic_search_mappings" / "plugin_report.json"
) as file_content:
try:
connections.get_connection().indices.put_template(
settings.ELASTICSEARCH_DSL_CLIENT.indices.put_template(
name="plugin-report", body=json.load(file_content)
)
success_msg = (
Expand Down
5 changes: 4 additions & 1 deletion api_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,10 @@ def get(self, request):

# 3 return data
elastic_response = (
Search(index=f"plugin-report-{get_environment()}*")
Search(
using=settings.ELASTICSEARCH_DSL_CLIENT,
index=f"plugin-report-{get_environment()}*",
)
.query(QElastic("bool", filter=filter_list))
.extra(size=10000) # max allowed size
.execute()
Expand Down
2 changes: 0 additions & 2 deletions docker/env_file_app_ci
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ AWS_SECRET_ACCESS_KEY=
ELASTICSEARCH_DSL_ENABLED=False
ELASTICSEARCH_DSL_HOST=
ELASTICSEARCH_DSL_PASSWORD=changeme
ELASTICSEARCH_DSL_NO_OF_SHARDS=1
ELASTICSEARCH_DSL_NO_OF_REPLICAS=0

ELASTICSEARCH_BI_ENABLED=False
ELASTICSEARCH_BI_HOST=
Expand Down
2 changes: 0 additions & 2 deletions docker/env_file_app_template
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ ELASTICSEARCH_DSL_ENABLED=False
ELASTICSEARCH_DSL_HOST=
ELASTICSEARCH_DSL_PASSWORD=
# consult to: https://django-elasticsearch-dsl.readthedocs.io/en/latest/settings.html
ELASTICSEARCH_DSL_NO_OF_SHARDS=1
ELASTICSEARCH_DSL_NO_OF_REPLICAS=0

ELASTICSEARCH_BI_ENABLED=False
ELASTICSEARCH_BI_HOST=
Expand Down
1 change: 0 additions & 1 deletion intel_owl/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"prettyjson",
# celery, elasticsearch
"django_celery_results",
"django_elasticsearch_dsl",
# rest framework libs
"rest_framework",
"rest_framework_filters",
Expand Down
45 changes: 19 additions & 26 deletions intel_owl/settings/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

from intel_owl import secrets

# business intelligence (bi)
ELASTICSEARCH_BI_ENABLED = (
secrets.get_secret("ELASTICSEARCH_BI_ENABLED", False) == "True"
)
if ELASTICSEARCH_BI_ENABLED:
ELASTICSEARCH_BI_HOST = secrets.get_secret("ELASTICSEARCH_BI_HOST").split(",")
ELASTICSEARCH_BI_INDEX = secrets.get_secret("ELASTICSEARCH_BI_INDEX")

if not ELASTICSEARCH_BI_HOST or not ELASTICSEARCH_BI_INDEX:
print("Elasticsearch not correctly configured")

else:
if ELASTICSEARCH_BI_HOST and ELASTICSEARCH_BI_INDEX:
ELASTICSEARCH_BI_CLIENT = Elasticsearch(
ELASTICSEARCH_BI_HOST,
maxsize=20,
Expand All @@ -27,44 +24,40 @@
print(
f"ELASTICSEARCH BI client configuration did not connect correctly: {ELASTICSEARCH_BI_CLIENT.info()}"
)
else:
print("Elasticsearch not correctly configured")


# advanced search
ELASTICSEARCH_DSL_ENABLED = (
secrets.get_secret("ELASTICSEARCH_DSL_ENABLED", False) == "True"
)
if ELASTICSEARCH_DSL_ENABLED:
ELASTICSEARCH_DSL_HOST = secrets.get_secret("ELASTICSEARCH_DSL_HOST")
if ELASTICSEARCH_DSL_HOST:
elastic_client_settings = {"hosts": ELASTICSEARCH_DSL_HOST}
elastic_search_conf = {"hosts": ELASTICSEARCH_DSL_HOST}

ELASTICSEARCH_DSL_PASSWORD = secrets.get_secret("ELASTICSEARCH_DSL_PASSWORD")
if ELASTICSEARCH_DSL_PASSWORD:
elastic_client_settings["basic_auth"] = (
elastic_search_conf["basic_auth"] = (
"elastic",
ELASTICSEARCH_DSL_PASSWORD,
)
ca_path = "/opt/deploy/intel_owl/certs/elastic_ca/ca.crt"
cert_path = "/opt/deploy/intel_owl/certs/elastic_instance/elasticsearch.crt"
if "elasticsearch:9200" in ELASTICSEARCH_DSL_HOST:
# in case we use Elastic as container we need the generated
# in case we use Elastic as external service it should have a valid cert
elastic_client_settings["verify_certs"] = cert_path
elastic_client_settings["ca_certs"] = ca_path
ELASTICSEARCH_DSL = {"default": elastic_client_settings}

ELASTICSEARCH_DSL_INDEX_SETTINGS = {
"number_of_shards": int(
secrets.get_secret("ELASTICSEARCH_DSL_NO_OF_SHARDS")
),
"number_of_replicas": int(
secrets.get_secret("ELASTICSEARCH_DSL_NO_OF_REPLICAS")
),
}
elastic_search_conf["verify_certs"] = (
"/opt/deploy/intel_owl/certs/elastic_instance/elasticsearch.crt"
)
elastic_search_conf["ca_certs"] = (
"/opt/deploy/intel_owl/certs/elastic_ca/ca.crt"
)
ELASTICSEARCH_DSL_CLIENT = Elasticsearch(**elastic_search_conf)
if not ELASTICSEARCH_DSL_CLIENT.ping():
print(
f"ELASTICSEARCH DSL client configuration did not connect correctly: {ELASTICSEARCH_DSL_CLIENT.info()}"
)
else:
print(
"you have to configure ELASTIC_HOST with the URL of your ElasticSearch instance"
)
else:
ELASTICSEARCH_DSL_AUTOSYNC = False
ELASTICSEARCH_DSL = {
"default": {"hosts": ""},
}
3 changes: 1 addition & 2 deletions intel_owl/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from django_celery_beat.models import PeriodicTask
from elasticsearch import ApiError
from elasticsearch.helpers import bulk
from elasticsearch_dsl import connections

from api_app.choices import ReportStatus, Status
from intel_owl import secrets
Expand Down Expand Up @@ -496,7 +495,7 @@ def _convert_report_to_elastic_document(
)
logger.info(f"documents to add to elastic: {len(all_report_document_list)}")
try:
bulk(connections.get_connection(), all_report_document_list)
bulk(settings.ELASTICSEARCH_DSL_CLIENT, all_report_document_list)
except ApiError as error:
logger.critical(error)
else:
Expand Down
1 change: 0 additions & 1 deletion requirements/project-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ django-ses == 4.4.0
django-iam-dbauth==0.2.1
django-prettyjson==0.4.1
django-silk==5.3.2
django-elasticsearch-dsl==8.0
django-treebeard==4.7
django-solo==2.4.0
django_extensions==3.2.3
Expand Down
1 change: 1 addition & 0 deletions tests/api_app/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ def test_get_403(self):


@override_settings(ELASTICSEARCH_DSL_ENABLED=True)
@override_settings(ELASTICSEARCH_DSL_CLIENT=None)
class ElasticTestCase(CustomViewSetTestCase):
uri = reverse("plugin-report-queries")

Expand Down
2 changes: 1 addition & 1 deletion tests/intel_owl/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@patch("intel_owl.tasks.get_environment", return_value="unittest")
@patch("intel_owl.tasks.now", return_value=_now)
@patch("intel_owl.tasks.connections.get_connection")
@override_settings(ELASTICSEARCH_DSL_CLIENT=None)
class SendElasticTestCase(CustomTestCase):

def setUp(self):
Expand Down

0 comments on commit 3198062

Please # to comment.