Skip to content

[9.1] Add API Key Management and Enable Index Sync via Scheduled Task (#225551)#226834

Merged
kibanamachine merged 1 commit intoelastic:9.1from
kibanamachine:backport/9.1/pr-225551
Jul 7, 2025
Merged

[9.1] Add API Key Management and Enable Index Sync via Scheduled Task (#225551)#226834
kibanamachine merged 1 commit intoelastic:9.1from
kibanamachine:backport/9.1/pr-225551

Conversation

@kibanamachine
Copy link
Contributor

Backport

This will backport the following commits from main to 9.1:

Questions ?

Please refer to the Backport tool documentation

…tic#225551)

## Summary
This PR adds in API key management and runs plain index sync on task
execution. Thanks to @jaredburgettelastic for the below changes 🚀 :
### Key Changes
Tasks now call plainIndexSync to sync index users at specified intervals
— this is the main functionality change. The following updates support
it:
- Added support for scoped API key usage via getClientfromApiKey in the
getApiKeyManager
- Using createInternalRepository for API key generation, to allow for
internal kibana user to create saved object client.
- API keys now store a snapshot of the user’s privileges by passing {}
to role_descriptors.
- Updated the find method in MonitoringEntitySourceDescriptorClient to
use a namespace-scoped saved objects client.

## ⚠️ Important Note
You must fully restart Elasticsearch and Kibana after pulling these
changes to regenerate the API key with the updated privileges.

While taking a privilege snapshot is known to be problematic (e.g.,
privileges can get out of sync if updated later), it’s a known issue and
consistent with the current behavior in the Entity Store. @tiansivive
raised 🚀

# How to Test:
Steps Taken from
[elastic#221610](elastic#221610)
- Pull branch into local machine
- Security experimental flag enable: 'privilegeMonitoringEnabled'
- Start up ElasticSearch and Kibana

**1. Optional - create the default index, this should just skip if you
don't make it.**

```
POST entity_analytics.privileged_monitoring/_doc
{
  "user": {
    "name": "default name"
  }
}
```

**2. Create test index/ indicies**

```
PUT /tatooine-
{
  "mappings": {
    "properties": {
      "user": {
        "properties": {
          "name": {
            "type": "keyword",
            "fields": {
              "text": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

```
```
POST tatooine-/_bulk
{ "index": {} }
{ "user": { "name": "Luke Skywalker" } }
{ "index": {} }
{ "user": { "name": "Leia Organa" } }
{ "index": {} }
{ "user": { "name": "Han Solo" } }
{ "index": {} }
{ "user": { "name": "Chewbacca" } }
{ "index": {} }
{ "user": { "name": "Obi-Wan Kenobi" } }
{ "index": {} }
{ "user": { "name": "Yoda" } }
{ "index": {} }
{ "user": { "name": "R2-D2" } }
{ "index": {} }
{ "user": { "name": "C-3PO" } }
{ "index": {} }
{ "user": { "name": "Darth Vader" } }

```
**3. Register Monitoring Entity Source Saved Objects**
```
POST kbn:/api/entity_analytics/monitoring/entity_source
{
  "type": "index",
  "name": "StarWars",
  "managed": true,
  "indexPattern": "tatooine-",
  "enabled": true,
 "matchers": [
    {
      "fields": ["user.role"],
      "values": ["admin"]
    }
  ],
  "filter": {}
}

```
**- OPTIONAL: You can check what is in the monitoring entity_source
SO:**

```
GET kbn:/api/entity_analytics/monitoring/entity_source/list
```
**4. Initialise monitoring engine:**
```
POST kbn:/api/entity_analytics/monitoring/engine/init {}
```

**5. Verify Users in Monitoring Index**
- Check the list of synced user, should include:
- - The created users
- - The default user (if you created it)

```
GET kbn:/api/entity_analytics/monitoring/users/list
```

**e.g. output:**

```
[
 {
   "id": "FkMJoZcB7muj1aiwb_eQ",
   "user": {
     "name": "C-3PO",
     "is_privileged": true
   },
   "labels": {
     "sources": [
       "index"
     ],
     "source_indices": [
       "tatooine-"
     ],
   }
 },
 {
   "id": "F0MJoZcB7muj1aiwb_eQ",
   "user": {
     "name": "Chewbacca",
      "is_privileged": true
   },
   "labels": {
     "sources": [
       "index"
     ],
     "source_indices": [
       "tatooine-"
     ],
   }
 },
// ... more here

```

## Testing: Removing Stale Users
The engine should soft delete users from the internal index if they no
longer appear in the synced sources - e.g. label as
monitoring.privileged_users: "not_monitored"
**Example:**

- Delete users in index:
```
POST tatooine-/_delete_by_query
{
 "query": {
   "terms": {
     "user.name": ["Chewbacca", "Han Solo"]
   }
 }
}

```
- re-run engine init

```
POST kbn:/api/entity_analytics/monitoring/engine/init
{}
```
-Fetch the updated user list:

```
GET kbn:/api/entity_analytics/monitoring/users/list
```
You should now only see both Chewbacca and Han Solo are no longer
privileged:
```
// ..
{
   "id": "GUMJoZcB7muj1aiwb_eQ",
   "user": {
     "name": "Han Solo"
     "is_privileged": false
   },
   "labels": {
     "sources": [],
     "source_indices": [],
   }
 }
]
```

## Recurring Sync
Current interval is 10m, change this interval to 30s / shorter interval
for testing -
`
x-pack/solutions/security/plugins/security_solution/server/lib/entity_analytics/privilege_monitoring/constants.ts`
under 'INTERVAL'.

Should be able to add some more indicies/ delete some users from current
indicies and see results from the same users list above, on index sync
as well as this log "[Privilege Monitoring] Running privilege monitoring
task" running every x chosen INTERVAL occurrence.

---------

Co-authored-by: jaredburgettelastic <jared.burgett@elastic.co>
Co-authored-by: Tiago Vila Verde <tiago.vilaverde@elastic.co>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Jeramy Soucy <jeramy.soucy@elastic.co>
(cherry picked from commit 441d2a5)
@kibanamachine kibanamachine merged commit ad8b553 into elastic:9.1 Jul 7, 2025
16 checks passed
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
securitySolution 130 131 +1
Unknown metric groups

API count

id before after diff
securitySolution 199 200 +1

References to deprecated APIs

id before after diff
securitySolution 344 345 +1

cc @CAWilson94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR is a backport of another PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants