Skip to content

Commit ce3acf8

Browse files
author
Pandora
committed
Add search API endpoint
1 parent bafb9f3 commit ce3acf8

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

bazaar/core/api_view.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from django.core.files.storage import default_storage
55
from elasticsearch import Elasticsearch
66
from rest_framework.authentication import TokenAuthentication
7-
from rest_framework.decorators import api_view, authentication_classes, permission_classes
7+
from rest_framework.decorators import api_view, authentication_classes, permission_classes, throttle_classes
88
from rest_framework.permissions import IsAuthenticated
99
from rest_framework.response import Response
10+
from rest_framework.throttling import UserRateThrottle
1011

1112
from bazaar.core.tasks import analyze
1213
from bazaar.core.utils import get_sha256_of_file
14+
from bazaar.front.utils import transform_hl_results
1315

1416

1517
@api_view(['GET', 'POST'])
@@ -65,3 +67,40 @@ def analysis_tasks_status(request, sha256):
6567
except Exception as e:
6668
logging.exception(e)
6769
return Response({"error": "Object of type NotFoundError is not JSON serializable"})
70+
71+
72+
@api_view(['POST'])
73+
@authentication_classes([TokenAuthentication])
74+
@permission_classes([IsAuthenticated])
75+
@throttle_classes([UserRateThrottle])
76+
def search(request):
77+
user_query = request.data
78+
if not user_query or 'q' not in user_query:
79+
return Response([])
80+
q = user_query.get('q')
81+
query = {
82+
"query": {
83+
"query_string": {
84+
"default_field": "sha256",
85+
"query": q
86+
}
87+
},
88+
"highlight": {
89+
"fields": {
90+
"*": {"pre_tags": ["<mark>"], "post_tags": ["</mark>"]}
91+
}
92+
},
93+
"sort": {"analysis_date": "desc"},
94+
"_source": ["sha256", "uploaded_at", "handle", "app_name",
95+
"version_code", "size", "dexofuzzy.apk", "quark.threat_level", "vt", "malware_bazaar",
96+
"is_signed", "frosting_data.is_frosted", "features"],
97+
"size": 150,
98+
}
99+
100+
es = Elasticsearch(settings.ELASTICSEARCH_HOSTS)
101+
try:
102+
raw_results = es.search(index=settings.ELASTICSEARCH_APK_INDEX, body=query)
103+
results = transform_hl_results(raw_results)
104+
return Response(results)
105+
except Exception as e:
106+
return Response([])

bazaar/core/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
path("hello", hello_world, name="bazaar-api"),
88
path("upload", apk_upload, name="bazaar-api"),
99
path("report/<str:sha256>", apk_analysis_report, name="bazaar-api"),
10-
path("status/<str:sha256>", analysis_tasks_status, name="bazaar-api")
10+
path("status/<str:sha256>", analysis_tasks_status, name="bazaar-api"),
11+
path("search/", search, name="bazaar-api"),
1112
]

config/settings/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,15 @@
304304
"rest_framework.authentication.TokenAuthentication",
305305
"rest_framework.authentication.SessionAuthentication",
306306
),
307-
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
307+
"DEFAULT_PERMISSION_CLASSES": (
308+
"rest_framework.permissions.IsAuthenticated",
309+
),
310+
'DEFAULT_THROTTLE_CLASSES': [
311+
'rest_framework.throttling.UserRateThrottle'
312+
],
313+
'DEFAULT_THROTTLE_RATES': {
314+
'user': '10/minute'
315+
},
308316
}
309317

310318
# django-cors-headers - https://github.com/adamchainz/django-cors-headers#setup

0 commit comments

Comments
 (0)