Skip to content

Commit

Permalink
Thread safe tag clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
jshcodes committed Dec 30, 2022
1 parent 923f995 commit 954f582
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cs_misp_import/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ def remove_crowdstrike_tags(self):
fallback="BEGIN DELETE",
hide_cool_banners=self.import_settings["no_banners"]
)
# self.log.info(DELETE_BANNER)
removed = 0
self.log.info("Retrieving list of tags to remove from MISP instance")
self.log.info("Retrieving list of tags to remove from MISP instance (may take several minutes).")
lock = Lock()
with concurrent.futures.ThreadPoolExecutor(self.misp_client.thread_count, thread_name_prefix="thread") as executor:
futures = {
executor.submit(self.misp_client.clear_tag, tg)
executor.submit(self.misp_client.clear_tag, tg, lock)
for tg in self.misp_client.search_tags("CrowdStrike:%", strict_tagname=True)
}
for _ in concurrent.futures.as_completed(futures):
Expand Down
26 changes: 22 additions & 4 deletions cs_misp_import/threaded_misp.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,34 @@ def get_cs_tags(self):
return self.search_tags("CrowdStrike:%", strict_tagname=True)

def clear_tag(self, *args, **kwargs):
# tags = self.search_tags("CrowdStrike:%")
#for tag in kwargstags:
tag = args[0]
if tag:
def perform_tag_clear():
if not self.deleted_tag_count % 50 and self.deleted_tag_count:
self.log.info("%i tags deleted", self.deleted_tag_count, extra={"key": ""})
result = self._retry(self.delete_tag, tag, **kwargs)
if "errors" not in result:
self.deleted_tag_count += 1

thread_lock = kwargs.get("lock", None)
if thread_lock:
kwargs.pop("lock")
tag = args[0]
if tag:
if thread_lock:
with thread_lock:
perform_tag_clear()
# if not self.deleted_tag_count % 50 and self.deleted_tag_count:
# self.log.info("%i tags deleted", self.deleted_tag_count, extra={"key": ""})
# result = self._retry(self.delete_tag, tag, **kwargs)
# if "errors" not in result:
# self.deleted_tag_count += 1
else:
perform_tag_clear()
# if not self.deleted_tag_count % 50 and self.deleted_tag_count:
# self.log.info("%i tags deleted", self.deleted_tag_count, extra={"key": ""})
# result = self._retry(self.delete_tag, tag, **kwargs)
# if "errors" not in result:
# self.deleted_tag_count += 1

return self.deleted_tag_count
#self.log.info("%i tags deleted", self.deleted_tag_count)

Expand Down

0 comments on commit 954f582

Please sign in to comment.