fix(topology): scope alerts to the owning application before creating/updating incidents - #6734
Open
Prabal864 wants to merge 1 commit into
Open
Conversation
…/updating incidents Closes keephq#6701. _process_tenant() builds one tenant-wide services_to_alerts dict, then passed that same dict unscoped into _create_application_based_incident and _update_application_based_incident for every application. Both methods just iterate every key in whatever dict they're given (see the loop in _update_application_based_incident), so an application's incident ends up containing alerts from every other application's services too, not just its own. Fix: build application_services_to_alerts, scoped to the current application's own services (services_with_alerts, which was already being computed correctly a few lines above but never used for this), and pass that scoped dict into both incident methods instead of the raw tenant-wide one. Testing: - Added tests/test_topology_processor.py, mocking every DB-touching collaborator (_get_topology_data, _get_applications_data, _get_application_based_incident, get_last_alerts, convert_db_alerts_to_dto_alerts) so the scoping logic in _process_tenant can be exercised without a live database. Covers both the create-incident and update-incident paths. - Could not run the suite here: tests/conftest.py imports pytest_docker and requires live Docker/MySQL fixtures at collection time regardless of which test is targeted, on this Windows sandbox. - Verified instead: python -m py_compile on both changed files; black --check and isort --check report no new issues; extracted the exact before/after scoping logic into a standalone script (using plain dicts/lists standing in for the DB models) and confirmed the old logic leaks a second application's alerts into the first application's incident while the new logic does not.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6734 +/- ##
==========================================
- Coverage 46.42% 46.41% -0.01%
==========================================
Files 178 178
Lines 18694 18698 +4
==========================================
Hits 8679 8679
- Misses 10015 10019 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
shahargl
requested changes
Aug 24, 2026
Comment on lines
+29
to
+35
| # Run migrations synchronously first so the database schema is fully initialized | ||
| # before ARQ workers and API gunicorn start querying tables. | ||
| python -c "from keep.api.core.db_on_start import migrate_db; migrate_db()" || { | ||
| echo "Failed to run migrations before starting workers, exiting" | ||
| exit 1 | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
this has many implications, not sure we want to do that as part of this fix, why its mandatory?
Contributor
Author
There was a problem hiding this comment.
Good catch, that's not related to this fix, removing it.
Prabal864
force-pushed
the
fix-6701-topology-cross-application-leak
branch
from
August 24, 2026 05:54
e6f412b to
a354bbc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #6701.
TopologyProcessor._process_tenantbuilds one tenant-wideservices_to_alertsdict (every alert for every service in the tenant), then passes that same, unscoped dict into_create_application_based_incident/_update_application_based_incidentfor every application in the loop:Both of those methods just iterate every key in whatever dict they're handed (see the loop building
alertsin_update_application_based_incident), so an application's incident ends up including alerts from every other application's services too - not just its own.The method already computes
services_with_alerts, the list of this application's own services that have alerts, a few lines above - it's just never used to scope what gets passed on.Fix
Build
application_services_to_alerts, restricted toservices_with_alerts, and pass that into both incident methods instead of the raw tenant-wide dict.Testing
Added
tests/test_topology_processor.py, mocking every DB-touching collaborator (_get_topology_data,_get_applications_data,_get_application_based_incident,get_last_alerts,convert_db_alerts_to_dto_alerts) so the scoping logic in_process_tenantcan be exercised without a live database. Covers both the create-incident and update-incident paths, asserting each application's incident only ever sees its own service's alerts.Same environment limitation as my previous PRs here: I couldn't run the suite on this Windows sandbox -
tests/conftest.pyimportspytest_dockerand requires live Docker/MySQL fixtures at collection time, regardless of which test is targeted. What I did verify:python -m py_compilepasses on both changed files.black --checkandisort --checkreport no new issues on the changed files.A maintainer running this on Linux/macOS should see
tests/test_topology_processor.pypass outright.Checklist