-
Notifications
You must be signed in to change notification settings - Fork 981
Feature pii #7322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John-Weak
wants to merge
23
commits into
newarchitecture
Choose a base branch
from
feature-pii
base: newarchitecture
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,013
−50
Open
Feature pii #7322
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
dd9b9bf
add support for pii in alerts
John-Weak a6a7734
enable prefill of drawer
John-Weak 3d11059
fix test
John-Weak d962961
add alert test
John-Weak e3bcb04
improve naming
John-Weak 6810170
ui padding fix
John-Weak a3e975a
fix test
John-Weak 38cfe44
processCoreMetrics is called in returnRequestMetrics() during /sdk/pr…
John-Weak 82030e5
Merge branch 'newarchitecture' into feature-pii
can-angun bbc4012
Changes for app_users export download
c978149
Merge pull request #7350 from Countly/feature/ingestion
Cookiezaurs 949ec59
add support for pii in alerts
John-Weak 8a7489a
enable prefill of drawer
John-Weak 5de8fe2
fix test
John-Weak cbf4aa7
add alert test
John-Weak 408639c
improve naming
John-Weak 49052b5
ui padding fix
John-Weak e374d39
fix test
John-Weak e0c3dc4
processCoreMetrics is called in returnRequestMetrics() during /sdk/pr…
John-Weak 3941c39
Merge branch 'feature-pii' of github.com:Countly/countly-server into …
John-Weak 0b98f58
add clickhouse for pii
John-Weak 6f22cd7
update alerts test to support pii clickhouse
John-Weak 61e160b
exec time
John-Weak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * @typedef {import('../parts/common-lib.js').App} App | ||
| */ | ||
|
|
||
| const log = require('../../../../api/utils/log.js')('alert:pii'); | ||
| const moment = require('moment-timezone'); | ||
| const common = require('../../../../api/utils/common.js'); | ||
| const commonLib = require("../parts/common-lib.js"); | ||
| const { ObjectId } = require('mongodb'); | ||
|
|
||
| module.exports.triggerByEvent = triggerByEvent; | ||
| /** | ||
| * Checks if given payload contains PII incidents and triggers matching alerts. | ||
| * @param {object} payload - { incidents, app } | ||
| */ | ||
| async function triggerByEvent(payload) { | ||
| const incidents = payload?.incidents; | ||
| const app = payload?.app; | ||
|
|
||
| if (!incidents || !Array.isArray(incidents) || !incidents.length || !app) { | ||
| return; | ||
| } | ||
|
|
||
| // Find alerts for this specific app AND alerts targeting all apps | ||
| const [appAlerts, allAppsAlerts] = await Promise.all([ | ||
| common.readBatcher.getMany("alerts", { | ||
| selectedApps: app._id.toString(), | ||
| alertDataType: "pii", | ||
| alertDataSubType: commonLib.TRIGGERED_BY_EVENT.pii, | ||
| enabled: true, | ||
| }), | ||
| common.readBatcher.getMany("alerts", { | ||
| selectedApps: "all", | ||
| alertDataType: "pii", | ||
| alertDataSubType: commonLib.TRIGGERED_BY_EVENT.pii, | ||
| enabled: true, | ||
| }), | ||
| ]); | ||
|
|
||
| const alerts = [...(appAlerts || []), ...(allAppsAlerts || [])]; | ||
| if (!alerts.length) { | ||
| return; | ||
| } | ||
|
|
||
| await Promise.all(alerts.map(alert => { | ||
| // If alert targets a specific rule, only trigger for incidents matching that rule | ||
| if (alert.alertDataSubType2) { | ||
| const matchingIncidents = incidents.filter(inc => inc.ruleId === alert.alertDataSubType2); | ||
| if (!matchingIncidents.length) { | ||
| return Promise.resolve(); | ||
| } | ||
| return commonLib.trigger({ | ||
| alert, | ||
| app, | ||
| date: new Date(), | ||
| extra: { | ||
| incidentCount: matchingIncidents.length, | ||
| firstIncident: matchingIncidents[0], | ||
| } | ||
| }, log); | ||
| } | ||
| return commonLib.trigger({ | ||
| alert, | ||
| app, | ||
| date: new Date(), | ||
| extra: { | ||
| incidentCount: incidents.length, | ||
| firstIncident: incidents[0], | ||
| } | ||
| }, log); | ||
| })); | ||
| } | ||
|
|
||
|
|
||
| module.exports.check = async function({ alertConfigs: alert, scheduledTo: date }) { | ||
| const selectedApp = alert.selectedApps[0]; | ||
| let app = null; | ||
| let appId = null; | ||
|
|
||
| if (selectedApp !== "all") { | ||
| app = await common.readBatcher.getOne("apps", { _id: new ObjectId(selectedApp) }); | ||
| if (!app) { | ||
| log.e(`App ${selectedApp} couldn't be found`); | ||
| return; | ||
| } | ||
| appId = app._id.toString(); | ||
| } | ||
|
|
||
| let { period, compareType, compareValue, filterValue, alertDataSubType2 } = alert; | ||
| compareValue = Number(compareValue); | ||
|
|
||
| const metricValue = await countIncidents(date, period, appId, filterValue, alertDataSubType2) || 0; | ||
|
|
||
| if (compareType === commonLib.COMPARE_TYPE_ENUM.MORE_THAN) { | ||
| if (metricValue > compareValue) { | ||
| await commonLib.trigger({ alert, app, metricValue, date }, log); | ||
| } | ||
| } | ||
| else { | ||
| const before = moment(date).subtract(1, commonLib.PERIOD_TO_DATE_COMPONENT_MAP[period]).toDate(); | ||
| const metricValueBefore = await countIncidents(before, period, appId, filterValue, alertDataSubType2); | ||
| if (!metricValueBefore) { | ||
| return; | ||
| } | ||
|
|
||
| const change = (metricValue / metricValueBefore - 1) * 100; | ||
| const shouldTrigger = compareType === commonLib.COMPARE_TYPE_ENUM.INCREASED_BY | ||
| ? change >= compareValue | ||
| : change <= -compareValue; | ||
|
|
||
| if (shouldTrigger) { | ||
| await commonLib.trigger({ alert, app, date, metricValue, metricValueBefore }, log); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Count PII incidents within a time period. | ||
| * @param {Date} date - end date of the period | ||
| * @param {string} period - hourly|daily|monthly | ||
| * @param {string|null} appId - app ID to filter by, or null for all apps | ||
| * @param {string|undefined} actionFilter - optional action filter (NOTIFY|OBFUSCATE|BLOCK) | ||
| * @param {string|undefined} ruleId - optional PII rule ID to filter by | ||
| * @returns {Promise<number>} - incident count | ||
| */ | ||
| async function countIncidents(date, period, appId, actionFilter, ruleId) { | ||
| const periodMs = { | ||
| hourly: 60 * 60 * 1000, | ||
| daily: 24 * 60 * 60 * 1000, | ||
| monthly: 30 * 24 * 60 * 60 * 1000, | ||
| }; | ||
|
|
||
| const endTs = date.getTime(); | ||
| const startTs = endTs - (periodMs[period] || periodMs.daily); | ||
|
|
||
| const query = { | ||
| ts: { $gte: startTs, $lte: endTs }, | ||
| }; | ||
|
|
||
| if (appId) { | ||
| query.app_id = appId; | ||
| } | ||
|
|
||
| if (actionFilter) { | ||
| query.action = actionFilter; | ||
| } | ||
|
|
||
| if (ruleId) { | ||
| query.ruleId = ruleId; | ||
| } | ||
|
|
||
| return common.db.collection("pii_incidents").countDocuments(query); | ||
| } | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When selectedApp is "all", the app variable remains null but is still passed to commonLib.trigger at line 96. The trigger function expects an app object to generate email content (it accesses app.name and app._id). This will cause a runtime error when trying to trigger alerts for "all" apps. Consider either fetching a list of all apps when selectedApp is "all", or handling the null app case in the trigger function.