Skip to content

Commit e81ae17

Browse files
committed
Fix add service API
1 parent 75bee07 commit e81ae17

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

assemblyline_ui/api/v4/service.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,32 @@ def add_service(**_):
5959

6060
new_heuristics = []
6161
if heuristics:
62+
plan = STORAGE.heuristic.get_bulk_plan()
6263
for index, heuristic in enumerate(heuristics):
6364
try:
6465
# Append service name to heuristic ID
6566
heuristic['heur_id'] = f"{service.name.upper()}.{str(heuristic['heur_id'])}"
6667

68+
# Attack_id field is now a list, make it a list if we receive otherwise
69+
attack_id = heuristic.get('attack_id', None)
70+
if isinstance(attack_id, str):
71+
heuristic['attack_id'] = [attack_id]
72+
6773
heuristic = Heuristic(heuristic)
68-
if not STORAGE.heuristic.get_if_exists(heuristic.heur_id):
69-
STORAGE.heuristic.save(heuristic.heur_id, heuristic)
70-
STORAGE.heuristic.commit()
71-
new_heuristics.append(heuristic.heur_id)
74+
heuristic_id = heuristic.heur_id
75+
plan.add_upsert_operation(heuristic_id, heuristic)
7276
except Exception as e:
73-
raise ValueError(f"Error parsing heuristics: {str(e)}")
77+
raise ValueError("Error parsing heuristics")
78+
79+
for item in STORAGE.heuristic.bulk(plan)['items']:
80+
if item['update']['result'] != "noop":
81+
new_heuristics.append(item['update']['_id'])
82+
83+
STORAGE.heuristic.commit()
7484

7585
return make_api_response(dict(
76-
service_name=service.name
86+
service_name=service.name,
87+
new_heuristics=new_heuristics
7788
))
7889
except ValueError as e: # Catch errors when building Service or Heuristic model(s)
7990
return make_api_response("", err=str(e), status_code=400)

0 commit comments

Comments
 (0)