Skip to content

Refactor Index Sync Logic and Improve Component Test Coverage#225783

Draft
CAWilson94 wants to merge 16 commits intoelastic:mainfrom
CAWilson94:ea-225551-refactor-priv-sync
Draft

Refactor Index Sync Logic and Improve Component Test Coverage#225783
CAWilson94 wants to merge 16 commits intoelastic:mainfrom
CAWilson94:ea-225551-refactor-priv-sync

Conversation

@CAWilson94
Copy link
Contributor

@CAWilson94 CAWilson94 commented Jun 30, 2025

This pull request refactors the PrivilegeMonitoringDataClient class to improve code clarity and modularity in line with this issue: #225551

  • Renamed syncUsernamesFromIndex → fetchUsernamesFromIndex — now focused on retrieving usernames from the source index.
  • Added bulkUpsertMonitoredUsers — responsible for create/update logic in the internal index.
  • Added ingestUsersFromIndexSource — wraps fetch + sync logic with error handling and logging.
  • Helper methods added to reduce complexity in plainIndexSync.
  • Better handling for null indexPattern and index_not_found - removed continue statements, now handling explicitly.
  • Component testing for above.
  • Pulled in API Key and Sync Task Updates from this PR: #225551

🚧 Add testing for new types and functionalities from API Key and Sync Task merging.

How To Test

For testing steps, please see the steps on the API and Sync Task PR: #225551

@CAWilson94 CAWilson94 marked this pull request as ready for review July 7, 2025 07:36
@CAWilson94 CAWilson94 requested review from a team as code owners July 7, 2025 07:36
@CAWilson94 CAWilson94 requested a review from machadoum July 7, 2025 07:36
@CAWilson94 CAWilson94 marked this pull request as draft July 7, 2025 07:41
@elasticmachine
Copy link
Contributor

🤖 Jobs for this PR can be triggered through checkboxes. 🚧

ℹ️ To trigger the CI, please tick the checkbox below 👇

  • Click to trigger kibana-pull-request for this PR!
  • Click to trigger kibana-deploy-project-from-pr for this PR!
  • Click to trigger kibana-deploy-cloud-from-pr for this PR!

* 2.0.
*/

describe('Privilege Monitoring API Key Manager', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

WiP

@elasticmachine
Copy link
Contributor

elasticmachine commented Jul 7, 2025

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #4 / MonitoringEntitySourceDataClient init should initialize Monitoring Entity Source Sync Config Successfully
  • [job] [logs] Jest Tests #4 / MonitoringEntitySourceDataClient init should initialize Monitoring Entity Source Sync Config Successfully
  • [job] [logs] FTR Configs #24 / Serverless security API Platform security APIs security/authorization available features composite features
  • [job] [logs] FTR Configs #24 / Serverless security API Platform security APIs security/authorization available features composite features

Metrics [docs]

Unknown metric groups

ESLint disabled line counts

id before after diff
securitySolution 629 627 -2

Total ESLint disabled count

id before after diff
securitySolution 727 725 -2

History

Comment on lines +462 to +463
source: MonitoringEntitySourceDescriptor,
index: string
Copy link
Member

Choose a reason for hiding this comment

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

Why does it receive two params if index is already inside source?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

May be a leftover from a previous iteration, good spot. I don't see a reason to have them separate. Will update this 🫡

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh no, I see why now. I was trying to reduce the checks inside the ingestUsersFromIndexSource method so it can always assume it receives a valid index. If I used source.indexPattern directly in that method, I’d have to move the source.indexPattern === undefined check into it.

I felt it was a bit cleaner and easier to follow with that validation staying in plainIndexSync, since it keeps the top-level flow clear.

My usual preference is to push validation down to the lowest-level method so that ingestUsersFromIndexSource would be more self-contained and flexible for future reuse — but in this case, keeping it in the top-level method just felt simpler.

Let me know if you’d prefer the check moved down into ingestUsersFromIndexSource — happy to refactor if that feels more consistent!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merged in line with: #227420

*/

export type PrivMonUserSource = 'csv' | 'api' | 'index_sync';
export type SyncIntervalConfig = EntityAnalyticsConfig['monitoring']['privileges']['syncInterval'];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be monitoring -> privileges -> developer -> syncInterval


const query = kuery ? toElasticsearchQuery(fromKueryExpression(kuery)) : { match_all: {} };

while (true) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of while true, needs to be more readable. Could rename this to something like 'hits' but look for where we do composite in risk scoring @hop-dev

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