Skip to content

fix(misp): eliminate N+1 HTTP requests in MISP connector. Closes #3571#3579

Merged
mlodic merged 2 commits intointelowlproject:developfrom
jagapathi20:fix/misp-n-plus-one-add-attribute
Apr 14, 2026
Merged

fix(misp): eliminate N+1 HTTP requests in MISP connector. Closes #3571#3579
mlodic merged 2 commits intointelowlproject:developfrom
jagapathi20:fix/misp-n-plus-one-add-attribute

Conversation

@jagapathi20
Copy link
Copy Markdown
Contributor

Description

The MISP.run() method called misp_instance.add_attribute() in a loop
after add_event(), issuing one HTTP request per attribute (N+1 pattern).

Fixed by pre-attaching all attributes to the local MISPEvent object
before calling add_event(), so MISP persists the event and all its
attributes in a single request.

Closes #3571

Type of change

  • Bug fix (non-breaking change which fixes an issue).

Checklist

  • I have read and understood the rules about how to Contribute to this project
  • The pull request is for the branch develop
  • I have inserted the copyright banner at the start of the file
  • Please avoid adding new libraries as requirements whenever it is possible. Use new libraries only if strictly needed to solve the issue you are working for.
  • Linters (Ruff) gave 0 errors.

Copilot AI review requested due to automatic review settings March 31, 2026 05:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the MISP connector to avoid an N+1 HTTP request pattern when creating events with multiple attributes, reducing connector runtime/network overhead and addressing issue #3571.

Changes:

  • Pre-attaches all generated attributes to the local pymisp.MISPEvent before calling add_event(), so event + attributes are sent in a single request.
  • Avoids recomputing the base attribute by caching it once in run().
  • Fixes hash type normalization by correctly applying replace("-", "") when mapping hash classifications.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 121 to 133
# bulk: attach all attributes to the event object before sending
for attr in attributes:
misp_instance.add_attribute(misp_event.id, attr)
event.add_attribute(
attr.type,
attr.value,
**{k: v for k, v in attr.to_dict().items() if k not in ("type", "value", "uuid")},
)

# add event + all attributes in a single request (bulk optimization)
misp_event = misp_instance.add_event(event, pythonify=True)

result = misp_instance.get_event(misp_event.id)

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: add a regression test for this bulk optimization (e.g., patch pymisp.PyMISP and assert add_event is called once and add_attribute is not invoked per-attribute) to help prevent the N+1 pattern from being reintroduced later.

Copilot uses AI. Check for mistakes.
@jagapathi20 jagapathi20 force-pushed the fix/misp-n-plus-one-add-attribute branch from fb35091 to 5ab8e7c Compare March 31, 2026 05:27
Copilot AI review requested due to automatic review settings March 31, 2026 05:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +121 to +130
# bulk: attach all attributes to the event object before sending
for attr in attributes:
misp_instance.add_attribute(misp_event.id, attr)
event.add_attribute(
attr.type,
attr.value,
**{k: v for k, v in attr.to_dict().items() if k not in ("type", "value", "uuid")},
)

# single request — event + all attributes sent together
misp_event = misp_instance.add_event(event, pythonify=True)
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactor changes the connector’s behavior from multiple PyMISP.add_attribute() calls to a single add_event() payload. There are unit tests for other connectors under tests/api_app/connectors_manager/, but none covering the MISP connector’s request pattern. Please add a test that mocks pymisp.PyMISP and asserts add_event() is called once and add_attribute() is not called, so the N+1 regression is prevented.

Copilot uses AI. Check for mistakes.
@jagapathi20 jagapathi20 force-pushed the fix/misp-n-plus-one-add-attribute branch from 4eb3f22 to 34da343 Compare March 31, 2026 06:06
@jagapathi20
Copy link
Copy Markdown
Contributor Author

hi @mlodic, as proposed in issue #3571, I have made changes. can you review them, when you get a chance?

Copy link
Copy Markdown
Member

@mlodic mlodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without unittests this won't be merged. I also need proof that this work with a MISP instance otherwise this is all theoretical only

@jagapathi20
Copy link
Copy Markdown
Contributor Author

without unittests this won't be merged. I also need proof that this work with a MISP instance otherwise this is all theoretical only

okay. Understood. I will update this PR shortly

Copilot AI review requested due to automatic review settings April 10, 2026 10:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +121 to +125
# bulk: attach all attributes to the event object before sending
for attr in attributes:
misp_instance.add_attribute(misp_event.id, attr)
event.add_attribute(
attr.type,
attr.value,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change materially alters how attributes are sent to MISP (bundled into the initial add_event call). There don’t appear to be unit tests covering the MISP connector; please add tests to assert that PyMISP.add_event is called once with the event containing all expected attributes and that PyMISP.add_attribute is no longer called (preventing regressions back to N+1 requests).

Copilot uses AI. Check for mistakes.
Comment on lines 118 to +121
# append attribute name to event info
event.info += f": {self._base_attr_obj.value}"

# add event to MISP Instance
misp_event = misp_instance.add_event(event, pythonify=True)
# add attributes to event on MISP Instance
# bulk: attach all attributes to the event object before sending
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event.info += f": {self._base_attr_obj.value}" recomputes _base_attr_obj even though it was already computed when building attributes above. Since _base_attr_obj performs a DB query to build the analyzers list, consider computing it once (e.g., base_attr = self._base_attr_obj) and reusing it for both event.info and the attributes list to avoid duplicate queries and keep values consistent.

Copilot uses AI. Check for mistakes.
@jagapathi20 jagapathi20 force-pushed the fix/misp-n-plus-one-add-attribute branch from a374c35 to 416b018 Compare April 10, 2026 11:02
Copilot AI review requested due to automatic review settings April 10, 2026 12:40
@jagapathi20 jagapathi20 force-pushed the fix/misp-n-plus-one-add-attribute branch from 416b018 to 8f8b906 Compare April 10, 2026 12:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +8 to +13
from api_app.analyzables_manager.models import Analyzable
from api_app.choices import Classification
from api_app.connectors_manager.connectors.misp import MISP
from api_app.connectors_manager.models import ConnectorConfig, ConnectorReport
from api_app.models import Job, Parameter, PluginConfig
from tests import CustomTestCase
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginConfig is imported but never used in this test module. Removing unused imports avoids lint noise and keeps the test focused.

Copilot uses AI. Check for mistakes.
@jagapathi20 jagapathi20 force-pushed the fix/misp-n-plus-one-add-attribute branch from 8f8b906 to d74da6f Compare April 10, 2026 13:20
@jagapathi20
Copy link
Copy Markdown
Contributor Author

Proof

Jobs created in IntelOwl
IntelOwl1
IntelOwl2
IntelOwl3
Jobs got in MISP
MISP
Number of requests made by IntelOwl to MISP
Requests
Unittests
Screenshot 2026-04-10 at 7 05 22 PM

@jagapathi20
Copy link
Copy Markdown
Contributor Author

Hi @mlodic, sorry for the delay, was busy in college work.As asked I implemented unittests, and attached proof that this works with MISP Instance, can you review them, when you get a chance.

@mlodic mlodic merged commit a6a7246 into intelowlproject:develop Apr 14, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants