Skip to content

Commit ef338b8

Browse files
authored
add admiralty filters #417 (#420)
1 parent 81b2b1f commit ef338b8

5 files changed

Lines changed: 104 additions & 1 deletion

File tree

stixify/web/views.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@
7575
from drf_spectacular.views import SpectacularAPIView
7676
from rest_framework.response import Response
7777

78+
ADMIRALTY_MARKING_MAPPING = {
79+
"SOURCE": {
80+
"A": "marking-definition--cf438540-077a-56c7-b68e-82fcc2bb0208",
81+
"B": "marking-definition--b3cd9dd0-9081-5cbe-84d0-ef5bc11b8b13",
82+
"C": "marking-definition--3545f856-c5f5-5d2f-a1ae-102e0b6028b2",
83+
"D": "marking-definition--223ecfcc-22ce-5ece-b91c-a05a53a91959",
84+
"E": "marking-definition--9eff5f66-33b9-5e54-9868-72179b28ae12",
85+
"F": "marking-definition--adebda39-90c9-5ac0-9107-c26d86a6c3d8",
86+
},
87+
"INFORMATION": {
88+
"1": "marking-definition--2462b621-0825-5879-917c-082e0394bcf4",
89+
"2": "marking-definition--9cf59b27-57f8-5250-98f4-16c462d5652c",
90+
"3": "marking-definition--4c76ec83-d905-5ada-b0bf-8ae2fb9e9f4d",
91+
"4": "marking-definition--c36a018d-bc8e-57a4-a39d-9e7e31d1bc17",
92+
"5": "marking-definition--0a48adab-e7d5-5354-8a41-abf199fe2628",
93+
"6": "marking-definition--2244db4b-ee29-5b8c-bed4-c7ac784c647a",
94+
},
95+
}
96+
7897

7998
class SchemaViewCached(SpectacularAPIView):
8099
_schema = None
@@ -250,6 +269,15 @@ class filterset_class(FilterSet):
250269
choices=JobState.choices,
251270
)
252271

272+
admiralty_source_reliability = filters.ChoiceFilter(
273+
choices=File._meta.get_field("admiralty_source_reliability").choices,
274+
help_text="Filter Files by the Admiralty source reliability rating assigned to them (e.g. `A`).",
275+
)
276+
admiralty_information_credibility = filters.ChoiceFilter(
277+
choices=File._meta.get_field("admiralty_information_credibility").choices,
278+
help_text="Filter Files by the Admiralty information credibility rating assigned to them (e.g. `1`).",
279+
)
280+
253281
ai_describes_incident = filters.BooleanFilter(
254282
help_text="If `ai_content_check_provider` set in profile used to process report, AI will answer if file describes security incident. Default will show all reports, can filter those that only describe incident by setting to true."
255283
)
@@ -669,6 +697,16 @@ def get_report(cls, report_id, request=None):
669697
description="Filter the results by TLP marking of the Report object (set at file upload time).",
670698
enum=[f[0] for f in TLP_Levels.choices],
671699
),
700+
OpenApiParameter(
701+
"admiralty_source_reliability",
702+
description="Filter the results by the Admiralty source reliability marking applied to the Report object (set at file upload time). Checks the `object_marking_refs` of the Report object for the marking definition `id` matching the rating selected.",
703+
enum=[f[0] for f in File._meta.get_field("admiralty_source_reliability").choices],
704+
),
705+
OpenApiParameter(
706+
"admiralty_information_credibility",
707+
description="Filter the results by the Admiralty information credibility marking applied to the Report object (set at file upload time). Checks the `object_marking_refs` of the Report object for the marking definition `id` matching the rating selected.",
708+
enum=[f[0] for f in File._meta.get_field("admiralty_information_credibility").choices],
709+
),
672710
OpenApiParameter(
673711
"description",
674712
description="Filter by the content in a report `description` (which contains the markdown version of the report). Will search for descriptions that contain the value entered. Search is wildcard so `exploit` will match `exploited`, `exploits`, etc.",
@@ -867,6 +905,14 @@ def get_reports(self, id=None):
867905
bind_vars["tlp_level_stix_id"] = TLP_LEVEL_STIX_ID_MAPPING.get(tlp_level)
868906
filters.append("FILTER @tlp_level_stix_id IN doc.object_marking_refs")
869907

908+
if rating := helper.query.get("admiralty_source_reliability"):
909+
bind_vars["admiralty_source_reliability_stix_id"] = ADMIRALTY_MARKING_MAPPING["SOURCE"].get(rating)
910+
filters.append("FILTER @admiralty_source_reliability_stix_id IN doc.object_marking_refs")
911+
912+
if rating := helper.query.get("admiralty_information_credibility"):
913+
bind_vars["admiralty_information_credibility_stix_id"] = ADMIRALTY_MARKING_MAPPING["INFORMATION"].get(rating)
914+
filters.append("FILTER @admiralty_information_credibility_stix_id IN doc.object_marking_refs")
915+
870916
if q := helper.query.get("name"):
871917
bind_vars["name"] = q.lower()
872918
filters.append("FILTER CONTAINS(LOWER(doc.name), @name)")

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def more_files(stixifier_profile, identity):
115115
ai_describes_incident=True,
116116
name="First file, special",
117117
identity=identity,
118+
admiralty_source_reliability="A",
119+
admiralty_information_credibility="1",
118120
),
119121
models.File.objects.create(
120122
id="aadbe23d-192c-488d-8ce9-96aa2613453f",
@@ -124,6 +126,8 @@ def more_files(stixifier_profile, identity):
124126
ai_incident_classification=["other", "apt_group", "data_leak"],
125127
name="second file, not breakable",
126128
identity=identity,
129+
admiralty_source_reliability="B",
130+
admiralty_information_credibility="1",
127131
),
128132
models.File.objects.create(
129133
id="bd5c8992-e1f2-42ef-8ad2-8003bc4fcedb",
@@ -136,5 +140,7 @@ def more_files(stixifier_profile, identity):
136140
ai_incident_classification=["data_leak", "vulnerability"],
137141
name="Forth file, special, breakable",
138142
identity=identity,
143+
admiralty_source_reliability="B",
144+
admiralty_information_credibility="3",
139145
),
140146
]

tests/src/views/bundles.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"object_marking_refs": [
8484
"marking-definition--94868c89-83c2-464b-929b-a1a8aa3c8487",
8585
"marking-definition--f92e15d9-6afc-5ae2-bb3e-85a1fd83a3b5",
86+
"marking-definition--cf438540-077a-56c7-b68e-82fcc2bb0208",
8687
],
8788
},
8889
{
@@ -284,7 +285,8 @@
284285
],
285286
"object_marking_refs": [
286287
"marking-definition--55d920b0-5e8b-4f79-9ee9-91f868d9b421",
287-
"marking-definition--f92e15d9-6afc-5ae2-bb3e-85a1fd83a3b5"
288+
"marking-definition--f92e15d9-6afc-5ae2-bb3e-85a1fd83a3b5",
289+
"marking-definition--9cf59b27-57f8-5250-98f4-16c462d5652c"
288290
]
289291
},
290292
{

tests/src/views/test_file_view.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,41 @@ def search_files(stixifier_profile, identity):
368368
"bd5c8992-e1f2-42ef-8ad2-8003bc4fcedb",
369369
],
370370
),
371+
(
372+
dict(admiralty_source_reliability="A"),
373+
[
374+
"f3848d80-b14d-4aa6-b3a6-94bce54b217e",
375+
],
376+
),
377+
(
378+
dict(admiralty_source_reliability="B"),
379+
[
380+
"aadbe23d-192c-488d-8ce9-96aa2613453f",
381+
"bd5c8992-e1f2-42ef-8ad2-8003bc4fcedb",
382+
],
383+
),
384+
(
385+
dict(admiralty_information_credibility="1"),
386+
[
387+
"f3848d80-b14d-4aa6-b3a6-94bce54b217e",
388+
"aadbe23d-192c-488d-8ce9-96aa2613453f",
389+
],
390+
),
391+
(
392+
dict(admiralty_information_credibility="3"),
393+
[
394+
"bd5c8992-e1f2-42ef-8ad2-8003bc4fcedb",
395+
],
396+
),
397+
(
398+
dict(
399+
admiralty_source_reliability="B",
400+
admiralty_information_credibility="3",
401+
),
402+
[
403+
"bd5c8992-e1f2-42ef-8ad2-8003bc4fcedb",
404+
],
405+
),
371406
],
372407
)
373408
@pytest.mark.django_db

tests/src/views/test_report_view.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ def test_report_objects_types(client, report_id, types, api_schema):
318318
(dict(name="oThER"), ["report--ed758a1b-34fe-4fca-8178-0c30d93a03ab"]),
319319
(dict(tlp_level="clear", name="other"), []),
320320
(dict(tlp_level="amber"), ["report--ed758a1b-34fe-4fca-8178-0c30d93a03ab"]),
321+
(
322+
dict(admiralty_source_reliability="A"),
323+
["report--52d2146c-798a-440f-942f-6fe039fb8995"],
324+
),
325+
(dict(admiralty_source_reliability="B"), []),
326+
(
327+
dict(admiralty_information_credibility="2"),
328+
["report--ed758a1b-34fe-4fca-8178-0c30d93a03ab"],
329+
),
330+
(dict(admiralty_information_credibility="1"), []),
331+
(
332+
dict(admiralty_source_reliability="A", name="rig"),
333+
["report--52d2146c-798a-440f-942f-6fe039fb8995"],
334+
),
321335
(dict(labels="ploit"), ["report--ed758a1b-34fe-4fca-8178-0c30d93a03ab"]),
322336
(dict(labels="steal"), ["report--52d2146c-798a-440f-942f-6fe039fb8995"]),
323337
(

0 commit comments

Comments
 (0)