Skip to content
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

[Security Solution] [Elastic AI Assistant] Include acknowledged alerts in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts) #173121

Merged

Conversation

andrew-goldstein
Copy link
Contributor

@andrew-goldstein andrew-goldstein commented Dec 11, 2023

[Security Solution] [Elastic AI Assistant] Include acknowledged alerts in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts)

This PR updates the query used by [Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts #172542 to include alerts with a kibana.alert.workflow_status value of acknowledged.

The query previously only returned alerts with a status of open. This change ensures both open and acknowledged alerts are provided as context to the LLM.

Updated Anonymization defaults

Three fields, detailed below, were added as anonymization defaults because they improve the quality of responses from the LLM when it answers questions about alerts.

For example, the LLM can refer to specific alerts by ID when the _id field is provided.

This PR makes the following additive changes to the Assistant's Anonymization defaults:

Field Allow by default Anonymize by default Value add
_id An anonymized _id field enables responses from the LLM to refer to specific documents (but doesn't provide it the actual document IDs).
kibana.alert.risk_score The getOpenAndAcknowledgedAlertsQuery query sorts alerts by kibana.alert.risk_score to return the n riskiest alerts. Allowing this field (by default) enables the LLM to include actual alert risk scores in responses.
kibana.alert.workflow_status The getOpenAndAcknowledgedAlertsQuery query filters alerts by kibana.alert.workflow_status to ensure only open and acknowledged alerts are provided as context to the LLM. Allowing this field (by default) enables the LLM answer questions about workflow status, and echo the workflow status of alerts in responses.
  • Clicking the Reset button shown in the screenshot below will reset the user's Anonymization defaults, such that they include the additive changes in the table above:

01__id_allowed_and_anonymized_by_default

Updated settings text

The text in the settings below was also updated:

updated_settings

Desk testing

To desk test this change:

  • Enable the assistantRagOnAlerts feature flag described in #172542 must be enabled, per the following example:
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
  • The Alerts feature must be enabled in the assistant settings, per the screenshot below:

alerts_enabled

  1. Navigate to Security > Alerts

  2. Click the AI Assistant button to open the assistant

  3. Click the Settings gear to open the assistant settings

  4. Click the Anonymization category

  5. Click the Reset button, shown in the screenshot below

01__id_allowed_and_anonymized_by_default

Expected results

  • 65 fields are allowed by default, per the screenshot above
  • 12 fields are anonymized by default, per the screenshot above
  • The _id field is allowed by default, per the screenshot above
  • The _id field is anonymized by default, per the screenshot above
  1. Type kibana.alert.risk in the search box

Expected result

  • The kibana.alert.risk_score field is allowed by default
  1. Type kibana.alert.workflow in the search box

Expected result

  • The kibana.alert.workflow_status field is allowed by default
  1. Click Save

  2. Click the X button to clear the conversation

  3. Close the assistant

  4. Add the following two fields as columns to the Alerts page table:

  • kibana.alert.workflow_status
  • _id
  1. Sort the table, first by kibana.alert.risk_score from high to low, and then by @timestamp from new to old, per the screenshot below:

fields_sorted

  1. Filter the alerts page to only show open and acknowledged alerts

Expected result

  • The alerts page has custom columns, sorting, and filtering, per the screenshot below:

alerts_page_custom_filter_sort_and_columns

  1. Click the AI Assistant button to open the assistant

  2. Ask the assistant:

What is the workflow status of my alerts?

Expected result

  • The assistant will report on the workflow status of alerts, per the example response below:
The workflow status for your alerts is currently 'open'. This status was observed on alerts related to processes started by Mimikatz, a known tool used in many cyberattacks, and sequences of processes that are often indicative of malicious activity. The severity of most of these alerts is 'high' or 'critical'. You may want to investigate these alerts further to ensure there's no ongoing threat to your system.

assistant_open_alerts

  1. Close the assistant

  2. Change the workflow status of an alert in the Alerts table from open to acknowledged

Expected result

  • The alerts table shows the updated alert, per the screenshot below:

acknowledged_alert

  1. Once again, open the assistant

  2. Once again, ask the (same) question:

What is the workflow status of my alerts?

Expected result

  • The response from the assistant makes reference to the alert who's workflow status was changed from open to acknowledged, per the example response below:
Based on the latest information, your alerts mainly show 'open' status, indicating that they have not been resolved yet. Some alerts have been acknowledged. Most of these unaddressed alerts have a critical severity rating and are primarily triggered by a Mimikatz process start and an EQL process sequence. You may want to prioritize these if the severity of the threat they pose is truly high or critical. It's also noteworthy that some alerts have a high severity rating. You should review all of these alerts as soon as possible to ensure your systems are secure.

assistant_some_alerts_acknowledged

  1. Ask the assistant for details about the acknowledged alerts:
What are the details of the acknowledged alerts?

Expected result

The assistant for details about the acknowledged alert that, for example, includes the kibana.alert.risk_score, per the example response below:

In response to your previous question, here are the details of the acknowledged alerts:

1. There is a 'mimikatz process started' alert, which is of 'critical' severity and 'acknowledged' status. It has a high risk score of 99. Its threat tactic is 'Command and Control'. The process involved was 'mimikatz.exe' running with arguments '--fo1'.

2. A 'Threshold rule' alert of 'critical' severity and 'open' status has also been detected with a risk score of 99 and threat tactic 'Collection'.

3. Lastly, there are several 'EQL process sequence' alerts of 'high' severity with 'open' status. These alerts involve execution of various processes including 'mimikatz.exe', 'lsass.exe', and 'notepad.exe'. Risk score for these alerts is 73 and the threat tactic involved is 'Execution'.

Please, take appropriate action to address these alerts.

details_of_acknowledged_alert

  1. Ask the assistant for the _id of the acknowledged alert:
What is the id of the acknowledged alert?

Expected results

  • The response from the assistant contains the _id of the acknowledged alert, per the example response below:
The id of the acknowledged alert is 'db9e3dbaf40a37e3b7b95d8015e99c5721b416731e04b9140536675f6e4fd170'. This alert was for a 'mimikatz process started' event with a severity rating of 'critical' and a risk score of 99. The host name associated with this alert is 'Host-terkvbzvtj'.

id_of_acknowledged_alert

  • The _id shown in the assistant is the same _id of the acknowledged alert on the alerts page, per the screeenshot below:

id_of_alert

  1. Click the Show anonymized toggle in the assistant

Expected result

  • The _id shown in the latest result is replaced with the actual anonymized value that was sent to the LLM, per the example screenshot below:

show_anonymized_alert_id

@andrew-goldstein andrew-goldstein added release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Threat Hunting:Investigations Security Solution Investigations Team Feature:Security Assistant Security Assistant v8.12.0 v8.13.0 labels Dec 11, 2023
@andrew-goldstein andrew-goldstein self-assigned this Dec 11, 2023
@andrew-goldstein andrew-goldstein requested a review from a team as a code owner December 11, 2023 23:56
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

Copy link
Member

@spong spong left a comment

Choose a reason for hiding this comment

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

Updated queries to include acknowledged alerts LGTM 👍

Copy link
Contributor

@logeekal logeekal Dec 12, 2023

Choose a reason for hiding this comment

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

@andrew-goldstein
nit: does it make sense to rename folder and file name from open_alerts to eligible_alerts or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andrew-goldstein nit: does it make sense to rename folder and file name from open_alerts to eligible_alerts or something else?

Thanks @logeekal!

  • I implemented your feedback in this commit

  • The name eligible_alerts felt slightly out context as a folder and file name, perhaps because it wasn't clear what eligible meant when looking at the file system. Based purely on feels, and with the aim of avoiding the introduction of a "new" concept like eligible , the renamed function and folders / files now look like this (when imported):

Before:

import { getOpenAlertsTool } from './open_alerts/get_open_alerts_tool';

After:

import { getOpenAndAcknowledgedAlertsTool } from './open_and_acknowledged_alerts/get_open_and_acknowledged_alerts_tool';

Does that feel like a reasonable change based on the feedback?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I agree 100%. this change makes sense. Thanks for implementing it.

@andrew-goldstein andrew-goldstein force-pushed the include_acknowledged_alerts branch from a92be36 to 322707e Compare December 12, 2023 23:21
@@ -7,6 +7,7 @@

/** By default, these fields are allowed to be sent to the assistant */
export const DEFAULT_ALLOW = [
'_id',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Field Allow by default Anonymize by default Value add
_id An anonymized _id field enables responses from the LLM to refer to specific documents (but doesn't provide it the actual document IDs).
kibana.alert.risk_score The getOpenAndAcknowledgedAlertsQuery query sorts alerts by kibana.alert.risk_score to return the n riskiest alerts. Allowing this field (by default) enables the LLM to include actual alert risk scores in responses.
kibana.alert.workflow_status The getOpenAndAcknowledgedAlertsQuery query filters alerts by kibana.alert.workflow_status to ensure only open and acknowledged alerts are provided as context to the LLM. Allowing this field (by default) enables the LLM answer questions about workflow status, and echo the workflow status of alerts in responses.

@logeekal
Copy link
Contributor

@andrew-goldstein ,

I attempted to do the Desk testing but I got stuck with below error. Could it be because of the token I have? I am not sure.

image

Additionally, do you think below text should also be revised, since we are not looking at Open alerts only. Sorry for not noticing it last time.

Screenshot 2023-12-13 at 12 11 02

@andrew-goldstein andrew-goldstein force-pushed the include_acknowledged_alerts branch from 322707e to c0a29ab Compare December 13, 2023 13:53
…alerts in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts)

This PR updates the query used by [[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts elastic#172542](elastic#172542) to include alerts with a `kibana.alert.workflow_status` value of `acknowledged`.

The query previously only returned alerts with a status of `open`. This change ensures both `open` and `acknowledged` alerts are provided as context to the LLM.
@andrew-goldstein andrew-goldstein force-pushed the include_acknowledged_alerts branch from c0a29ab to 0982303 Compare December 13, 2023 16:08
@andrew-goldstein
Copy link
Contributor Author

@andrew-goldstein ,

I attempted to do the Desk testing but I got stuck with below error. Could it be because of the token I have? I am not sure.

image

The error in the first screenshot happened because the number of alerts sent as context to the LLM exceeded the model's maximum context length.

  • The maximum context length will vary from model to model
    • Users may configure multiple connectors and work with multiple models
  • The total number of fields sent per alert (as context) will also vary
    • The user's anonymization settings, which determine which fields will be sent, will vary
    • The fields present in the alerts will vary; some alerts may have more fields, some will have fewer fields

Given the variability above, a slider is provided so users may adjust the total number of alerts sent as context.

The default value for the slider is 20. We expect to increase this default over time.

It appears in the 2nd screenshot that you may have increased the default.

  • Would you be willing to retry with the default value of 20?
  • As a result of the error you reported above, we added additional guidance: Select fewer alerts if the model's maximum context length is frequently exceeded. to the Alerts settings, as detailed below

Additionally, do you think below text should also be revised, since we are not looking at Open alerts only. Sorry for not noticing it last time.

Screenshot 2023-12-13 at 12 11 02

Thanks for noting that this text should also be revised, because the query was updated to include acknowledged alerts.

  • It now reads: Ask questions about the (n) latest and riskiest open and acknowledged alerts in your environment.
  • Additional text Select fewer alerts if the model's maximum context length is frequently exceeded. was added to guide users selecting a lower value for n when they encounter context length limits

The updates are highlighted in the screenshot below:

updated_settings

Copy link
Contributor

@logeekal logeekal left a comment

Choose a reason for hiding this comment

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

Desk Testing work perfectly. I was able to follow the steps mentioned and all of them passed.

I also tried changing the sorting of data in Alert Table and it changed the response from Assistant perfectly.

Great work 🚀 .

One point to note:

I hit the context limit event with 20 alerts so I had to reduce to 10. For future enhancement, I think we should instruct users on what to do for known errors such as below. I guess action will be different in different type of Conversations. Not sure if it is even feasible.

An error occurred sending your message.

ActionsClientLlm: action result status is error: an error occurred while running the action - Status code: 400. Message: API Error: model_error - This model's maximum context length is 8192 tokens. However, your messages resulted in 8996 tokens. Please reduce the length of the messages.

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 13.1MB 13.1MB +3.0KB

History

  • 💛 Build #183412 was flaky c0a29ab1f76fe25e2bb8b421cddfd68f880ee206
  • 💚 Build #183321 succeeded 322707ec682dc9c5a94ddc1f68bd2a27fe011fd7
  • 💛 Build #183062 was flaky a92be36a8a39d1600a5bde1b37a3ceb480dd53fb

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @andrew-goldstein

@andrew-goldstein andrew-goldstein merged commit 0d9c261 into elastic:main Dec 13, 2023
@andrew-goldstein andrew-goldstein deleted the include_acknowledged_alerts branch December 13, 2023 17:39
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Dec 13, 2023
…s in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts) (elastic#173121)

## [Security Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts)

This PR updates the query used by [[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts elastic#172542](elastic#172542) to include alerts with a `kibana.alert.workflow_status` value of `acknowledged`.

The query previously only returned alerts with a status of `open`. This change ensures both `open` and `acknowledged` alerts are provided as context to the LLM.

### Updated Anonymization defaults

Three fields, detailed below, were added as anonymization defaults because they improve the quality of responses from the LLM when it answers questions about alerts.

For example, the LLM can refer to specific alerts by ID when the `_id` field is provided.

This PR makes the following additive changes to the Assistant's `Anonymization` defaults:

| Field                          | Allow by default | Anonymize by default | Value add                                                                                                                                                                                                                                                                                                                         |
|--------------------------------|------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `_id`                          | ✅                | ✅                    | An anonymized `_id` field enables responses from the LLM to refer to specific documents (but doesn't provide it the actual document IDs).                                                                                                                                                                                         |
| `kibana.alert.risk_score`      | ✅                | ❌                    | The `getOpenAndAcknowledgedAlertsQuery` query sorts alerts by `kibana.alert.risk_score` to return the `n` riskiest alerts.  Allowing this field (by default) enables the LLM to include actual alert risk scores in responses.                                                                                                    |
| `kibana.alert.workflow_status` | ✅                | ❌                    | The `getOpenAndAcknowledgedAlertsQuery` query filters alerts by `kibana.alert.workflow_status` to ensure only `open` and `acknowledged` alerts are provided as context to the LLM.  Allowing this field (by default) enables the LLM answer questions about workflow status, and echo the workflow status of alerts in responses. |

- Clicking the `Reset` button shown in the screenshot below will reset the user's `Anonymization` defaults, such that they include the additive changes in the table above:

![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)

### Updated settings text

The text in the settings below was also updated:

![updated_settings](https://github.com/elastic/kibana/assets/4459398/3b2d9f93-f89f-410a-b943-dd4c98e37a29)

### Desk testing

To desk test this change:

- Enable the `assistantRagOnAlerts` feature flag described in [elastic#172542](elastic#172542) must be enabled, per the following example:

```
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
```

- The `Alerts` feature must be enabled in the assistant settings, per the screenshot below:

 ![alerts_enabled](https://github.com/elastic/kibana/assets/4459398/edd273c8-b9e7-4ecf-93bb-81fbf0f3e6b0)

1) Navigate to Security > Alerts

2) Click the `AI Assistant` button to open the assistant

3) Click the `Settings` gear to open the assistant settings

4) Click the `Anonymization` category

5) Click the `Reset` button, shown in the screenshot below

![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)

**Expected results**

- `65` fields are allowed by default, per the screenshot above
- `12` fields are anonymized by default, per the screenshot above
- The `_id` field is allowed by default, per the screenshot above
- The `_id` field is anonymized by default, per the screenshot above

6) Type `kibana.alert.risk` in the search box

**Expected result**

- The `kibana.alert.risk_score` field is allowed by default

7) Type `kibana.alert.workflow` in the search box

**Expected result**

- The `kibana.alert.workflow_status` field is allowed by default

8) Click `Save`

9) Click the `X` button to clear the conversation

10) Close the assistant

11) Add the following two fields as columns to the Alerts page table:

- `kibana.alert.workflow_status`
- `_id`

12) Sort the table, first by `kibana.alert.risk_score` from high to low, and then by `@timestamp` from new to old, per the screenshot below:

![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)

13) Filter the alerts page to only show `open` and `acknowledged` alerts

**Expected result**

- The alerts page has custom columns, sorting, and filtering, per the screenshot below:

![alerts_page_custom_filter_sort_and_columns](https://github.com/elastic/kibana/assets/4459398/ba191f83-ee4c-4c90-be4f-197eff6be68e)

14) Click the `AI Assistant` button to open the assistant

15) Ask the assistant:

```
What is the workflow status of my alerts?
```

**Expected result**

- The assistant will report on the workflow status of alerts, per the example response below:

```
The workflow status for your alerts is currently 'open'. This status was observed on alerts related to processes started by Mimikatz, a known tool used in many cyberattacks, and sequences of processes that are often indicative of malicious activity. The severity of most of these alerts is 'high' or 'critical'. You may want to investigate these alerts further to ensure there's no ongoing threat to your system.
```

![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/93730bfe-9d6a-44f6-8270-14b6dd141514)

16) Close the assistant

17) Change the workflow status of an alert in the Alerts table from `open` to `acknowledged`

**Expected result**

- The alerts table shows the updated alert, per the screenshot below:

![acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/df685381-ffd5-4253-897a-4058f1641747)

18) Once again, open the assistant

19) Once again, ask the (same) question:

```
What is the workflow status of my alerts?
```

**Expected result**

- The response from the assistant makes reference to the alert who's workflow status was changed from `open` to `acknowledged`, per the example response below:

```
Based on the latest information, your alerts mainly show 'open' status, indicating that they have not been resolved yet. Some alerts have been acknowledged. Most of these unaddressed alerts have a critical severity rating and are primarily triggered by a Mimikatz process start and an EQL process sequence. You may want to prioritize these if the severity of the threat they pose is truly high or critical. It's also noteworthy that some alerts have a high severity rating. You should review all of these alerts as soon as possible to ensure your systems are secure.
```

![assistant_some_alerts_acknowledged](https://github.com/elastic/kibana/assets/4459398/53bef5b6-1661-480e-bbf1-bdb14202480e)

20) Ask the assistant for details about the acknowledged alerts:

```
What are the details of the acknowledged alerts?
```

**Expected result**

The assistant for details about the acknowledged alert that, for example, includes the `kibana.alert.risk_score`, per the example response below:

```
In response to your previous question, here are the details of the acknowledged alerts:

1. There is a 'mimikatz process started' alert, which is of 'critical' severity and 'acknowledged' status. It has a high risk score of 99. Its threat tactic is 'Command and Control'. The process involved was 'mimikatz.exe' running with arguments '--fo1'.

2. A 'Threshold rule' alert of 'critical' severity and 'open' status has also been detected with a risk score of 99 and threat tactic 'Collection'.

3. Lastly, there are several 'EQL process sequence' alerts of 'high' severity with 'open' status. These alerts involve execution of various processes including 'mimikatz.exe', 'lsass.exe', and 'notepad.exe'. Risk score for these alerts is 73 and the threat tactic involved is 'Execution'.

Please, take appropriate action to address these alerts.
```

![details_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/191400e3-f123-4a0d-8ebc-9b5c88f242ed)

21) Ask the assistant for the `_id` of the acknowledged alert:

```
What is the id of the acknowledged alert?
```

**Expected results**

- The response from the assistant contains the `_id` of the `acknowledged` alert, per the example response below:

```
The id of the acknowledged alert is 'db9e3dbaf40a37e3b7b95d8015e99c5721b416731e04b9140536675f6e4fd170'. This alert was for a 'mimikatz process started' event with a severity rating of 'critical' and a risk score of 99. The host name associated with this alert is 'Host-terkvbzvtj'.
```

![id_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/b360b957-b198-4c9f-a101-581589b279bd)

- The `_id` shown in the assistant is the same `_id` of the acknowledged alert on the alerts page, per the screeenshot below:

![id_of_alert](https://github.com/elastic/kibana/assets/4459398/b9d4ef55-83ee-46cb-ab32-35bc467f4150)

22) Click the `Show anonymized` toggle in the assistant

**Expected result**

- The `_id` shown in the latest result is replaced with the actual anonymized value that was sent to the LLM, per the example screenshot below:

![show_anonymized_alert_id](https://github.com/elastic/kibana/assets/4459398/8ce47dd6-4bc6-4478-87c9-97d24b6eae82)

(cherry picked from commit 0d9c261)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.12

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

@@ -13,17 +13,17 @@ import { DynamicTool, Tool } from 'langchain/tools';
import { requestHasRequiredAnonymizationParams } from '../../helpers';
import { RequestBody } from '../../types';

import { getOpenAlertsQuery } from './get_open_alerts_query';
import { getOpenAndAcknowledgedAlertsQuery } from './get_open_and_acknowledged_alerts_query';
import { getRawDataOrDefault, sizeIsOutOfRange } from './helpers';

export const OPEN_ALERTS_TOOL_DESCRIPTION =
'Call this for knowledge about the latest n open alerts (sorted by `kibana.alert.risk_score`) in the environment, or when answering questions about open alerts';
Copy link
Member

Choose a reason for hiding this comment

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

nit: open an acknowledged

Found in managing the merge w/ #172234. I got this 👍

kibanamachine added a commit that referenced this pull request Dec 13, 2023
…d alerts in the context sent to the LLM (Retrieval Augmented Generation (RAG) for Alerts) (#173121) (#173310)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[Security Solution] [Elastic AI Assistant] Include acknowledged
alerts in the context sent to the LLM (Retrieval Augmented Generation
(RAG) for Alerts)
(#173121)](#173121)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Andrew
Macri","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-12-13T17:39:08Z","message":"[Security
Solution] [Elastic AI Assistant] Include acknowledged alerts in the
context sent to the LLM (Retrieval Augmented Generation (RAG) for
Alerts) (#173121)\n\n## [Security Solution] [Elastic AI Assistant]
Include `acknowledged` alerts in the context sent to the LLM (Retrieval
Augmented Generation (RAG) for Alerts)\r\n\r\nThis PR updates the query
used by [[Security Solution] [Elastic AI Assistant] Retrieval Augmented
Generation (RAG) for Alerts
#172542](#172542) to include
alerts with a `kibana.alert.workflow_status` value of
`acknowledged`.\r\n\r\nThe query previously only returned alerts with a
status of `open`. This change ensures both `open` and `acknowledged`
alerts are provided as context to the LLM.\r\n\r\n### Updated
Anonymization defaults\r\n\r\nThree fields, detailed below, were added
as anonymization defaults because they improve the quality of responses
from the LLM when it answers questions about alerts.\r\n\r\nFor example,
the LLM can refer to specific alerts by ID when the `_id` field is
provided.\r\n\r\nThis PR makes the following additive changes to the
Assistant's `Anonymization` defaults:\r\n\r\n| Field | Allow by default
| Anonymize by default | Value add
|\r\n|--------------------------------|------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\r\n|
`_id` | ✅ | ✅ | An anonymized `_id` field enables responses from the LLM
to refer to specific documents (but doesn't provide it the actual
document IDs). |\r\n| `kibana.alert.risk_score` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query sorts alerts by
`kibana.alert.risk_score` to return the `n` riskiest alerts. Allowing
this field (by default) enables the LLM to include actual alert risk
scores in responses. |\r\n| `kibana.alert.workflow_status` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query filters alerts by
`kibana.alert.workflow_status` to ensure only `open` and `acknowledged`
alerts are provided as context to the LLM. Allowing this field (by
default) enables the LLM answer questions about workflow status, and
echo the workflow status of alerts in responses. |\r\n\r\n- Clicking the
`Reset` button shown in the screenshot below will reset the user's
`Anonymization` defaults, such that they include the additive changes in
the table
above:\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n###
Updated settings text\r\n\r\nThe text in the settings below was also
updated:\r\n\r\n![updated_settings](https://github.com/elastic/kibana/assets/4459398/3b2d9f93-f89f-410a-b943-dd4c98e37a29)\r\n\r\n###
Desk testing\r\n\r\nTo desk test this change:\r\n\r\n- Enable the
`assistantRagOnAlerts` feature flag described in
[#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![alerts_enabled](https://github.com/elastic/kibana/assets/4459398/edd273c8-b9e7-4ecf-93bb-81fbf0f3e6b0)\r\n\r\n1)
Navigate to Security > Alerts\r\n\r\n2) Click the `AI Assistant` button
to open the assistant\r\n\r\n3) Click the `Settings` gear to open the
assistant settings\r\n\r\n4) Click the `Anonymization`
category\r\n\r\n5) Click the `Reset` button, shown in the screenshot
below\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n**Expected
results**\r\n\r\n- `65` fields are allowed by default, per the
screenshot above\r\n- `12` fields are anonymized by default, per the
screenshot above\r\n- The `_id` field is allowed by default, per the
screenshot above\r\n- The `_id` field is anonymized by default, per the
screenshot above\r\n\r\n6) Type `kibana.alert.risk` in the search
box\r\n\r\n**Expected result**\r\n\r\n- The `kibana.alert.risk_score`
field is allowed by default\r\n\r\n7) Type `kibana.alert.workflow` in
the search box\r\n\r\n**Expected result**\r\n\r\n- The
`kibana.alert.workflow_status` field is allowed by default\r\n\r\n8)
Click `Save`\r\n\r\n9) Click the `X` button to clear the
conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add the following
two fields as columns to the Alerts page table:\r\n\r\n-
`kibana.alert.workflow_status`\r\n- `_id`\r\n\r\n12) Sort the table,
first by `kibana.alert.risk_score` from high to low, and then by
`@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n13)
Filter the alerts page to only show `open` and `acknowledged`
alerts\r\n\r\n**Expected result**\r\n\r\n- The alerts page has custom
columns, sorting, and filtering, per the screenshot
below:\r\n\r\n![alerts_page_custom_filter_sort_and_columns](https://github.com/elastic/kibana/assets/4459398/ba191f83-ee4c-4c90-be4f-197eff6be68e)\r\n\r\n14)
Click the `AI Assistant` button to open the assistant\r\n\r\n15) Ask the
assistant:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The assistant will
report on the workflow status of alerts, per the example response
below:\r\n\r\n```\r\nThe workflow status for your alerts is currently
'open'. This status was observed on alerts related to processes started
by Mimikatz, a known tool used in many cyberattacks, and sequences of
processes that are often indicative of malicious activity. The severity
of most of these alerts is 'high' or 'critical'. You may want to
investigate these alerts further to ensure there's no ongoing threat to
your
system.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/93730bfe-9d6a-44f6-8270-14b6dd141514)\r\n\r\n16)
Close the assistant\r\n\r\n17) Change the workflow status of an alert in
the Alerts table from `open` to `acknowledged`\r\n\r\n**Expected
result**\r\n\r\n- The alerts table shows the updated alert, per the
screenshot
below:\r\n\r\n![acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/df685381-ffd5-4253-897a-4058f1641747)\r\n\r\n18)
Once again, open the assistant\r\n\r\n19) Once again, ask the (same)
question:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response
below:\r\n\r\n```\r\nBased on the latest information, your alerts mainly
show 'open' status, indicating that they have not been resolved yet.
Some alerts have been acknowledged. Most of these unaddressed alerts
have a critical severity rating and are primarily triggered by a
Mimikatz process start and an EQL process sequence. You may want to
prioritize these if the severity of the threat they pose is truly high
or critical. It's also noteworthy that some alerts have a high severity
rating. You should review all of these alerts as soon as possible to
ensure your systems are
secure.\r\n```\r\n\r\n![assistant_some_alerts_acknowledged](https://github.com/elastic/kibana/assets/4459398/53bef5b6-1661-480e-bbf1-bdb14202480e)\r\n\r\n20)
Ask the assistant for details about the acknowledged
alerts:\r\n\r\n```\r\nWhat are the details of the acknowledged
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\nThe assistant for
details about the acknowledged alert that, for example, includes the
`kibana.alert.risk_score`, per the example response
below:\r\n\r\n```\r\nIn response to your previous question, here are the
details of the acknowledged alerts:\r\n\r\n1. There is a 'mimikatz
process started' alert, which is of 'critical' severity and
'acknowledged' status. It has a high risk score of 99. Its threat tactic
is 'Command and Control'. The process involved was 'mimikatz.exe'
running with arguments '--fo1'.\r\n\r\n2. A 'Threshold rule' alert of
'critical' severity and 'open' status has also been detected with a risk
score of 99 and threat tactic 'Collection'.\r\n\r\n3. Lastly, there are
several 'EQL process sequence' alerts of 'high' severity with 'open'
status. These alerts involve execution of various processes including
'mimikatz.exe', 'lsass.exe', and 'notepad.exe'. Risk score for these
alerts is 73 and the threat tactic involved is
'Execution'.\r\n\r\nPlease, take appropriate action to address these
alerts.\r\n```\r\n\r\n![details_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/191400e3-f123-4a0d-8ebc-9b5c88f242ed)\r\n\r\n21)
Ask the assistant for the `_id` of the acknowledged
alert:\r\n\r\n```\r\nWhat is the id of the acknowledged
alert?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The response from the
assistant contains the `_id` of the `acknowledged` alert, per the
example response below:\r\n\r\n```\r\nThe id of the acknowledged alert
is 'db9e3dbaf40a37e3b7b95d8015e99c5721b416731e04b9140536675f6e4fd170'.
This alert was for a 'mimikatz process started' event with a severity
rating of 'critical' and a risk score of 99. The host name associated
with this alert is
'Host-terkvbzvtj'.\r\n```\r\n\r\n![id_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/b360b957-b198-4c9f-a101-581589b279bd)\r\n\r\n-
The `_id` shown in the assistant is the same `_id` of the acknowledged
alert on the alerts page, per the screeenshot
below:\r\n\r\n![id_of_alert](https://github.com/elastic/kibana/assets/4459398/b9d4ef55-83ee-46cb-ab32-35bc467f4150)\r\n\r\n22)
Click the `Show anonymized` toggle in the assistant\r\n\r\n**Expected
result**\r\n\r\n- The `_id` shown in the latest result is replaced with
the actual anonymized value that was sent to the LLM, per the example
screenshot
below:\r\n\r\n![show_anonymized_alert_id](https://github.com/elastic/kibana/assets/4459398/8ce47dd6-4bc6-4478-87c9-97d24b6eae82)","sha":"0d9c261530b102e7a7a87f4d5dcdf423cdd17a2c","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:
SecuritySolution","Team:Threat Hunting:Investigations","Feature:Elastic
AI
Assistant","v8.12.0","v8.13.0"],"number":173121,"url":"https://github.com/elastic/kibana/pull/173121","mergeCommit":{"message":"[Security
Solution] [Elastic AI Assistant] Include acknowledged alerts in the
context sent to the LLM (Retrieval Augmented Generation (RAG) for
Alerts) (#173121)\n\n## [Security Solution] [Elastic AI Assistant]
Include `acknowledged` alerts in the context sent to the LLM (Retrieval
Augmented Generation (RAG) for Alerts)\r\n\r\nThis PR updates the query
used by [[Security Solution] [Elastic AI Assistant] Retrieval Augmented
Generation (RAG) for Alerts
#172542](#172542) to include
alerts with a `kibana.alert.workflow_status` value of
`acknowledged`.\r\n\r\nThe query previously only returned alerts with a
status of `open`. This change ensures both `open` and `acknowledged`
alerts are provided as context to the LLM.\r\n\r\n### Updated
Anonymization defaults\r\n\r\nThree fields, detailed below, were added
as anonymization defaults because they improve the quality of responses
from the LLM when it answers questions about alerts.\r\n\r\nFor example,
the LLM can refer to specific alerts by ID when the `_id` field is
provided.\r\n\r\nThis PR makes the following additive changes to the
Assistant's `Anonymization` defaults:\r\n\r\n| Field | Allow by default
| Anonymize by default | Value add
|\r\n|--------------------------------|------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\r\n|
`_id` | ✅ | ✅ | An anonymized `_id` field enables responses from the LLM
to refer to specific documents (but doesn't provide it the actual
document IDs). |\r\n| `kibana.alert.risk_score` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query sorts alerts by
`kibana.alert.risk_score` to return the `n` riskiest alerts. Allowing
this field (by default) enables the LLM to include actual alert risk
scores in responses. |\r\n| `kibana.alert.workflow_status` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query filters alerts by
`kibana.alert.workflow_status` to ensure only `open` and `acknowledged`
alerts are provided as context to the LLM. Allowing this field (by
default) enables the LLM answer questions about workflow status, and
echo the workflow status of alerts in responses. |\r\n\r\n- Clicking the
`Reset` button shown in the screenshot below will reset the user's
`Anonymization` defaults, such that they include the additive changes in
the table
above:\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n###
Updated settings text\r\n\r\nThe text in the settings below was also
updated:\r\n\r\n![updated_settings](https://github.com/elastic/kibana/assets/4459398/3b2d9f93-f89f-410a-b943-dd4c98e37a29)\r\n\r\n###
Desk testing\r\n\r\nTo desk test this change:\r\n\r\n- Enable the
`assistantRagOnAlerts` feature flag described in
[#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![alerts_enabled](https://github.com/elastic/kibana/assets/4459398/edd273c8-b9e7-4ecf-93bb-81fbf0f3e6b0)\r\n\r\n1)
Navigate to Security > Alerts\r\n\r\n2) Click the `AI Assistant` button
to open the assistant\r\n\r\n3) Click the `Settings` gear to open the
assistant settings\r\n\r\n4) Click the `Anonymization`
category\r\n\r\n5) Click the `Reset` button, shown in the screenshot
below\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n**Expected
results**\r\n\r\n- `65` fields are allowed by default, per the
screenshot above\r\n- `12` fields are anonymized by default, per the
screenshot above\r\n- The `_id` field is allowed by default, per the
screenshot above\r\n- The `_id` field is anonymized by default, per the
screenshot above\r\n\r\n6) Type `kibana.alert.risk` in the search
box\r\n\r\n**Expected result**\r\n\r\n- The `kibana.alert.risk_score`
field is allowed by default\r\n\r\n7) Type `kibana.alert.workflow` in
the search box\r\n\r\n**Expected result**\r\n\r\n- The
`kibana.alert.workflow_status` field is allowed by default\r\n\r\n8)
Click `Save`\r\n\r\n9) Click the `X` button to clear the
conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add the following
two fields as columns to the Alerts page table:\r\n\r\n-
`kibana.alert.workflow_status`\r\n- `_id`\r\n\r\n12) Sort the table,
first by `kibana.alert.risk_score` from high to low, and then by
`@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n13)
Filter the alerts page to only show `open` and `acknowledged`
alerts\r\n\r\n**Expected result**\r\n\r\n- The alerts page has custom
columns, sorting, and filtering, per the screenshot
below:\r\n\r\n![alerts_page_custom_filter_sort_and_columns](https://github.com/elastic/kibana/assets/4459398/ba191f83-ee4c-4c90-be4f-197eff6be68e)\r\n\r\n14)
Click the `AI Assistant` button to open the assistant\r\n\r\n15) Ask the
assistant:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The assistant will
report on the workflow status of alerts, per the example response
below:\r\n\r\n```\r\nThe workflow status for your alerts is currently
'open'. This status was observed on alerts related to processes started
by Mimikatz, a known tool used in many cyberattacks, and sequences of
processes that are often indicative of malicious activity. The severity
of most of these alerts is 'high' or 'critical'. You may want to
investigate these alerts further to ensure there's no ongoing threat to
your
system.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/93730bfe-9d6a-44f6-8270-14b6dd141514)\r\n\r\n16)
Close the assistant\r\n\r\n17) Change the workflow status of an alert in
the Alerts table from `open` to `acknowledged`\r\n\r\n**Expected
result**\r\n\r\n- The alerts table shows the updated alert, per the
screenshot
below:\r\n\r\n![acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/df685381-ffd5-4253-897a-4058f1641747)\r\n\r\n18)
Once again, open the assistant\r\n\r\n19) Once again, ask the (same)
question:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response
below:\r\n\r\n```\r\nBased on the latest information, your alerts mainly
show 'open' status, indicating that they have not been resolved yet.
Some alerts have been acknowledged. Most of these unaddressed alerts
have a critical severity rating and are primarily triggered by a
Mimikatz process start and an EQL process sequence. You may want to
prioritize these if the severity of the threat they pose is truly high
or critical. It's also noteworthy that some alerts have a high severity
rating. You should review all of these alerts as soon as possible to
ensure your systems are
secure.\r\n```\r\n\r\n![assistant_some_alerts_acknowledged](https://github.com/elastic/kibana/assets/4459398/53bef5b6-1661-480e-bbf1-bdb14202480e)\r\n\r\n20)
Ask the assistant for details about the acknowledged
alerts:\r\n\r\n```\r\nWhat are the details of the acknowledged
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\nThe assistant for
details about the acknowledged alert that, for example, includes the
`kibana.alert.risk_score`, per the example response
below:\r\n\r\n```\r\nIn response to your previous question, here are the
details of the acknowledged alerts:\r\n\r\n1. There is a 'mimikatz
process started' alert, which is of 'critical' severity and
'acknowledged' status. It has a high risk score of 99. Its threat tactic
is 'Command and Control'. The process involved was 'mimikatz.exe'
running with arguments '--fo1'.\r\n\r\n2. A 'Threshold rule' alert of
'critical' severity and 'open' status has also been detected with a risk
score of 99 and threat tactic 'Collection'.\r\n\r\n3. Lastly, there are
several 'EQL process sequence' alerts of 'high' severity with 'open'
status. These alerts involve execution of various processes including
'mimikatz.exe', 'lsass.exe', and 'notepad.exe'. Risk score for these
alerts is 73 and the threat tactic involved is
'Execution'.\r\n\r\nPlease, take appropriate action to address these
alerts.\r\n```\r\n\r\n![details_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/191400e3-f123-4a0d-8ebc-9b5c88f242ed)\r\n\r\n21)
Ask the assistant for the `_id` of the acknowledged
alert:\r\n\r\n```\r\nWhat is the id of the acknowledged
alert?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The response from the
assistant contains the `_id` of the `acknowledged` alert, per the
example response below:\r\n\r\n```\r\nThe id of the acknowledged alert
is 'db9e3dbaf40a37e3b7b95d8015e99c5721b416731e04b9140536675f6e4fd170'.
This alert was for a 'mimikatz process started' event with a severity
rating of 'critical' and a risk score of 99. The host name associated
with this alert is
'Host-terkvbzvtj'.\r\n```\r\n\r\n![id_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/b360b957-b198-4c9f-a101-581589b279bd)\r\n\r\n-
The `_id` shown in the assistant is the same `_id` of the acknowledged
alert on the alerts page, per the screeenshot
below:\r\n\r\n![id_of_alert](https://github.com/elastic/kibana/assets/4459398/b9d4ef55-83ee-46cb-ab32-35bc467f4150)\r\n\r\n22)
Click the `Show anonymized` toggle in the assistant\r\n\r\n**Expected
result**\r\n\r\n- The `_id` shown in the latest result is replaced with
the actual anonymized value that was sent to the LLM, per the example
screenshot
below:\r\n\r\n![show_anonymized_alert_id](https://github.com/elastic/kibana/assets/4459398/8ce47dd6-4bc6-4478-87c9-97d24b6eae82)","sha":"0d9c261530b102e7a7a87f4d5dcdf423cdd17a2c"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173121","number":173121,"mergeCommit":{"message":"[Security
Solution] [Elastic AI Assistant] Include acknowledged alerts in the
context sent to the LLM (Retrieval Augmented Generation (RAG) for
Alerts) (#173121)\n\n## [Security Solution] [Elastic AI Assistant]
Include `acknowledged` alerts in the context sent to the LLM (Retrieval
Augmented Generation (RAG) for Alerts)\r\n\r\nThis PR updates the query
used by [[Security Solution] [Elastic AI Assistant] Retrieval Augmented
Generation (RAG) for Alerts
#172542](#172542) to include
alerts with a `kibana.alert.workflow_status` value of
`acknowledged`.\r\n\r\nThe query previously only returned alerts with a
status of `open`. This change ensures both `open` and `acknowledged`
alerts are provided as context to the LLM.\r\n\r\n### Updated
Anonymization defaults\r\n\r\nThree fields, detailed below, were added
as anonymization defaults because they improve the quality of responses
from the LLM when it answers questions about alerts.\r\n\r\nFor example,
the LLM can refer to specific alerts by ID when the `_id` field is
provided.\r\n\r\nThis PR makes the following additive changes to the
Assistant's `Anonymization` defaults:\r\n\r\n| Field | Allow by default
| Anonymize by default | Value add
|\r\n|--------------------------------|------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\r\n|
`_id` | ✅ | ✅ | An anonymized `_id` field enables responses from the LLM
to refer to specific documents (but doesn't provide it the actual
document IDs). |\r\n| `kibana.alert.risk_score` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query sorts alerts by
`kibana.alert.risk_score` to return the `n` riskiest alerts. Allowing
this field (by default) enables the LLM to include actual alert risk
scores in responses. |\r\n| `kibana.alert.workflow_status` | ✅ | ❌ | The
`getOpenAndAcknowledgedAlertsQuery` query filters alerts by
`kibana.alert.workflow_status` to ensure only `open` and `acknowledged`
alerts are provided as context to the LLM. Allowing this field (by
default) enables the LLM answer questions about workflow status, and
echo the workflow status of alerts in responses. |\r\n\r\n- Clicking the
`Reset` button shown in the screenshot below will reset the user's
`Anonymization` defaults, such that they include the additive changes in
the table
above:\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n###
Updated settings text\r\n\r\nThe text in the settings below was also
updated:\r\n\r\n![updated_settings](https://github.com/elastic/kibana/assets/4459398/3b2d9f93-f89f-410a-b943-dd4c98e37a29)\r\n\r\n###
Desk testing\r\n\r\nTo desk test this change:\r\n\r\n- Enable the
`assistantRagOnAlerts` feature flag described in
[#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![alerts_enabled](https://github.com/elastic/kibana/assets/4459398/edd273c8-b9e7-4ecf-93bb-81fbf0f3e6b0)\r\n\r\n1)
Navigate to Security > Alerts\r\n\r\n2) Click the `AI Assistant` button
to open the assistant\r\n\r\n3) Click the `Settings` gear to open the
assistant settings\r\n\r\n4) Click the `Anonymization`
category\r\n\r\n5) Click the `Reset` button, shown in the screenshot
below\r\n\r\n![01__id_allowed_and_anonymized_by_default](https://github.com/elastic/kibana/assets/4459398/7b8bcfa0-deb3-478b-b32f-c7fe3cf039b0)\r\n\r\n**Expected
results**\r\n\r\n- `65` fields are allowed by default, per the
screenshot above\r\n- `12` fields are anonymized by default, per the
screenshot above\r\n- The `_id` field is allowed by default, per the
screenshot above\r\n- The `_id` field is anonymized by default, per the
screenshot above\r\n\r\n6) Type `kibana.alert.risk` in the search
box\r\n\r\n**Expected result**\r\n\r\n- The `kibana.alert.risk_score`
field is allowed by default\r\n\r\n7) Type `kibana.alert.workflow` in
the search box\r\n\r\n**Expected result**\r\n\r\n- The
`kibana.alert.workflow_status` field is allowed by default\r\n\r\n8)
Click `Save`\r\n\r\n9) Click the `X` button to clear the
conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add the following
two fields as columns to the Alerts page table:\r\n\r\n-
`kibana.alert.workflow_status`\r\n- `_id`\r\n\r\n12) Sort the table,
first by `kibana.alert.risk_score` from high to low, and then by
`@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n13)
Filter the alerts page to only show `open` and `acknowledged`
alerts\r\n\r\n**Expected result**\r\n\r\n- The alerts page has custom
columns, sorting, and filtering, per the screenshot
below:\r\n\r\n![alerts_page_custom_filter_sort_and_columns](https://github.com/elastic/kibana/assets/4459398/ba191f83-ee4c-4c90-be4f-197eff6be68e)\r\n\r\n14)
Click the `AI Assistant` button to open the assistant\r\n\r\n15) Ask the
assistant:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The assistant will
report on the workflow status of alerts, per the example response
below:\r\n\r\n```\r\nThe workflow status for your alerts is currently
'open'. This status was observed on alerts related to processes started
by Mimikatz, a known tool used in many cyberattacks, and sequences of
processes that are often indicative of malicious activity. The severity
of most of these alerts is 'high' or 'critical'. You may want to
investigate these alerts further to ensure there's no ongoing threat to
your
system.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/93730bfe-9d6a-44f6-8270-14b6dd141514)\r\n\r\n16)
Close the assistant\r\n\r\n17) Change the workflow status of an alert in
the Alerts table from `open` to `acknowledged`\r\n\r\n**Expected
result**\r\n\r\n- The alerts table shows the updated alert, per the
screenshot
below:\r\n\r\n![acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/df685381-ffd5-4253-897a-4058f1641747)\r\n\r\n18)
Once again, open the assistant\r\n\r\n19) Once again, ask the (same)
question:\r\n\r\n```\r\nWhat is the workflow status of my
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response
below:\r\n\r\n```\r\nBased on the latest information, your alerts mainly
show 'open' status, indicating that they have not been resolved yet.
Some alerts have been acknowledged. Most of these unaddressed alerts
have a critical severity rating and are primarily triggered by a
Mimikatz process start and an EQL process sequence. You may want to
prioritize these if the severity of the threat they pose is truly high
or critical. It's also noteworthy that some alerts have a high severity
rating. You should review all of these alerts as soon as possible to
ensure your systems are
secure.\r\n```\r\n\r\n![assistant_some_alerts_acknowledged](https://github.com/elastic/kibana/assets/4459398/53bef5b6-1661-480e-bbf1-bdb14202480e)\r\n\r\n20)
Ask the assistant for details about the acknowledged
alerts:\r\n\r\n```\r\nWhat are the details of the acknowledged
alerts?\r\n```\r\n\r\n**Expected result**\r\n\r\nThe assistant for
details about the acknowledged alert that, for example, includes the
`kibana.alert.risk_score`, per the example response
below:\r\n\r\n```\r\nIn response to your previous question, here are the
details of the acknowledged alerts:\r\n\r\n1. There is a 'mimikatz
process started' alert, which is of 'critical' severity and
'acknowledged' status. It has a high risk score of 99. Its threat tactic
is 'Command and Control'. The process involved was 'mimikatz.exe'
running with arguments '--fo1'.\r\n\r\n2. A 'Threshold rule' alert of
'critical' severity and 'open' status has also been detected with a risk
score of 99 and threat tactic 'Collection'.\r\n\r\n3. Lastly, there are
several 'EQL process sequence' alerts of 'high' severity with 'open'
status. These alerts involve execution of various processes including
'mimikatz.exe', 'lsass.exe', and 'notepad.exe'. Risk score for these
alerts is 73 and the threat tactic involved is
'Execution'.\r\n\r\nPlease, take appropriate action to address these
alerts.\r\n```\r\n\r\n![details_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/191400e3-f123-4a0d-8ebc-9b5c88f242ed)\r\n\r\n21)
Ask the assistant for the `_id` of the acknowledged
alert:\r\n\r\n```\r\nWhat is the id of the acknowledged
alert?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The response from the
assistant contains the `_id` of the `acknowledged` alert, per the
example response below:\r\n\r\n```\r\nThe id of the acknowledged alert
is 'db9e3dbaf40a37e3b7b95d8015e99c5721b416731e04b9140536675f6e4fd170'.
This alert was for a 'mimikatz process started' event with a severity
rating of 'critical' and a risk score of 99. The host name associated
with this alert is
'Host-terkvbzvtj'.\r\n```\r\n\r\n![id_of_acknowledged_alert](https://github.com/elastic/kibana/assets/4459398/b360b957-b198-4c9f-a101-581589b279bd)\r\n\r\n-
The `_id` shown in the assistant is the same `_id` of the acknowledged
alert on the alerts page, per the screeenshot
below:\r\n\r\n![id_of_alert](https://github.com/elastic/kibana/assets/4459398/b9d4ef55-83ee-46cb-ab32-35bc467f4150)\r\n\r\n22)
Click the `Show anonymized` toggle in the assistant\r\n\r\n**Expected
result**\r\n\r\n- The `_id` shown in the latest result is replaced with
the actual anonymized value that was sent to the LLM, per the example
screenshot
below:\r\n\r\n![show_anonymized_alert_id](https://github.com/elastic/kibana/assets/4459398/8ce47dd6-4bc6-4478-87c9-97d24b6eae82)","sha":"0d9c261530b102e7a7a87f4d5dcdf423cdd17a2c"}}]}]
BACKPORT-->

Co-authored-by: Andrew Macri <[email protected]>
spong added a commit that referenced this pull request Dec 14, 2023
… tool registration (#172234)

## Summary

Adds support for arbitrary tool registration on plugin start, allowing
any plugin consuming the `elastic_assistant` plugin to register tools
specific to their use case.

This was achieved by introducing an `AppContextService`, with functions
`registerTools` and `getRegisteredTools`.

When registering within another plugin's start contract, call
`registerTools`, e.g.


``` ts
// Assistant Tool and Feature Registration
plugins.elasticAssistant.registerTools(APP_UI_ID, getAssistantTools());
```

and to get any tools for a given plugin, call `getRegisteredTools` with
the plugin's name. E.g here within the assistant request handler:

```ts
// Fetch any tools registered by the request's originating plugin
const pluginName = getPluginNameFromRequest({ request, logger });
const assistantTools = (await context.elasticAssistant).getRegisteredTools(pluginName);
```

> [!NOTE]
> Use the plugin name that corresponds to your application as defined in
the `x-kbn-context` header of requests made from your application. This
is the value used to fetch relevant tools, which may be different than
your plugin's registered `APP_ID`.


### Test instructions

Since no new tool functionality has been enabled here, you can just
follow the test instructions from
#173121, and ensure that
enabling/disabling the KB or RAG on Alerts still functions as expected.
You should see the registered tools as a debug logger, e.g.

```
[DEBUG][plugins.elasticAssistant] AppContextService:getRegisteredTools
[DEBUG][plugins.elasticAssistant] pluginName: securitySolutionUI
[DEBUG][plugins.elasticAssistant] tools: AlertCountsTool, ESQLKnowledgeBaseTool, OpenAndAcknowledgedAlertsTool
[DEBUG][plugins.elasticAssistant] applicable tools: "alert-counts, ESQLKnowledgeBaseTool, open-alerts"
```

### Checklist

Delete any items that are not applicable to this PR.

- [ ] ~Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)~
- [ ]
~[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials~
- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
andrew-goldstein added a commit to andrew-goldstein/kibana that referenced this pull request Dec 20, 2023
…alerts in the LangChain `AlertCountsTool` aggregation

This PR updates the LangChain `AlertCountsTool` aggregation, which answers questions like `How many open alerts do I have?`, to include `acknowledged` alerts. The `AlertCountsTool` was introduced as part of [[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts elastic#172542](elastic#172542)

- This PR is similar to <elastic#173121>, where `acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`, which returns the _details_ of alerts
  - In contrast to [elastic#173121](elastic#173121), this PR is focused on the alert counts _aggregation_

- This PR also updates the `range` of **both** the `AlertCountsTool` and the `OpenAndAcknowledgedAlertsTool` queries to standardize on the following syntax, which aligns with the `Last 24 hours` option in the _Commonly used_ section of the Kibana date picker:

```json
          "range": {
            "@timestamp": {
              "gte": "now-24h",
              "lte": "now"
            }
          }
```

### Desk testing

To desk test this change:

- The `assistantRagOnAlerts` feature flag described in [elastic#172542](elastic#172542) must be enabled, per the following example:

```
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
```

- The `Alerts` feature must be enabled in the assistant settings, per the screenshot below:

 ![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)

1) Generate alerts with a variety of severity (e.g. `low`, `medium`, `high`, and `critical`)

2) After the alerts have been generated, disable all detection rules to keep the counts static during testing

3) Navigate to Security > Alerts

4) Select `Last 24 hours` from the _Commonly used_ section of the global date picker

5) Click the `Treemap` button to select the Treemap visualization

6) In the Treemap's `Group by` input, enter `kibana.alert.severity`

7) Next, in the Treemap's `Group by top` input, enter `kibana.alert.workflow_status`

8) Click the `AI Assistant` button to open the assistant

9) Click the `X` button to clear the conversation

10) Close the assistant

11) Add the following two fields as columns to the Alerts page table:

```
kibana.alert.workflow_status
_id
```

12) Sort the Alerts table, first by `kibana.alert.risk_score` from high to low, and then by `@timestamp` from new to old, per the screenshot below:

![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)

**Expected results**

- The alerts page date range is `Last 24 hours`
- The `Treemap` is selected
- The treemap is grouped by `kibana.alert.severity` and then `kibana.alert.workflow_status`
- The alerts table has custom sorting and columns, per the screenshot below:

![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)

13) Click the `AI Assistant` button to open the assistant

14) Ask the assistant:

```
How many open alerts do I have?
```

**Expected results**

- The assistant will report on the counts and workflow status of alerts, per the example response and screenshot below:

```
You have a total of 47 open alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts with medium severity, 7 alerts with high severity, and 4 alerts with critical severity.
```

![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)

15) Make note of the counts shown in the assistant, then close the assistant

Expected result:

- The counts from the assistant match the counts in the treemap legend, per the example screenshot below:

![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)

16) Change the workflow status of an alert in the Alerts table from `open` to `acknowledged`

**Expected result**

- The treemap and alerts table and include the updated (`acknowledged`) alert, per the screenshot below:

![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)

17) Once again, open the assistant

18) Once again, ask the (same) question:

```
How many open alerts do I have?
```

**Expected result**

- The response from the assistant makes reference to the alert who's workflow status was changed from `open` to `acknowledged`, per the example response and screenshot below:

```
Based on the latest data I had received, you have a total of 47 open alerts. Here's the breakdown: 24 alerts are of low severity, 12 alerts are of medium severity, 7 alerts are of high severity, and 4 alerts are of critical severity (Note: One of the critical severity alerts has been acknowledged).
```

![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)
andrew-goldstein added a commit that referenced this pull request Dec 21, 2023
…rts in the LangChain `AlertCountsTool` aggregation (#173701)

## [Security Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the LangChain `AlertCountsTool` aggregation

This PR updates the LangChain `AlertCountsTool` aggregation, which answers questions like `How many open alerts do I have?`, to include `acknowledged` alerts. The `AlertCountsTool` was introduced as part of [[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts #172542](#172542)

- This PR is similar to <#173121>, where `acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`, which returns the _details_ of alerts
  - In contrast to [#173121](#173121), this PR is focused on the alert counts _aggregation_

- This PR also updates the `range` of **both** the `AlertCountsTool` and the `OpenAndAcknowledgedAlertsTool` queries to standardize on the following syntax, which aligns with the `Last 24 hours` option in the _Commonly used_ section of the Kibana date picker:

```json
          "range": {
            "@timestamp": {
              "gte": "now-24h",
              "lte": "now"
            }
          }
```

### Desk testing

To desk test this change:

- The `assistantRagOnAlerts` feature flag described in [#172542](#172542) must be enabled, per the following example:

```
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
```

- The `Alerts` feature must be enabled in the assistant settings, per the screenshot below:

 ![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)

1) Generate alerts with a variety of severity (e.g. `low`, `medium`, `high`, and `critical`)

2) After the alerts have been generated, disable all detection rules to keep the counts static during testing

3) Navigate to Security > Alerts

4) Select `Last 24 hours` from the _Commonly used_ section of the global date picker

5) Click the `Treemap` button to select the Treemap visualization

6) In the Treemap's `Group by` input, enter `kibana.alert.severity`

7) Next, in the Treemap's `Group by top` input, enter `kibana.alert.workflow_status`

8) Click the `AI Assistant` button to open the assistant

9) Click the `X` button to clear the conversation

10) Close the assistant

11) Add the following two fields as columns to the Alerts page table:

```
kibana.alert.workflow_status
_id
```

12) Sort the Alerts table, first by `kibana.alert.risk_score` from high to low, and then by `@timestamp` from new to old, per the screenshot below:

![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)

**Expected results**

- The alerts page date range is `Last 24 hours`
- The `Treemap` is selected
- The treemap is grouped by `kibana.alert.severity` and then `kibana.alert.workflow_status`
- The alerts table has custom sorting and columns, per the screenshot below:

![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)

13) Click the `AI Assistant` button to open the assistant

14) Ask the assistant:

```
How many open alerts do I have?
```

**Expected results**

- The assistant will report on the counts and workflow status of alerts, per the example response and screenshot below:

```
You have a total of 47 open alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts with medium severity, 7 alerts with high severity, and 4 alerts with critical severity.
```

![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)

15) Make note of the counts shown in the assistant, then close the assistant

Expected result:

- The counts from the assistant match the counts in the treemap legend, per the example screenshot below:

![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)

16) Change the workflow status of an alert in the Alerts table from `open` to `acknowledged`

**Expected result**

- The treemap and alerts table and include the updated (`acknowledged`) alert, per the screenshot below:

![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)

17) Once again, open the assistant

18) Once again, ask the (same) question:

```
How many open alerts do I have?
```

**Expected result**

- The response from the assistant makes reference to the alert who's workflow status was changed from `open` to `acknowledged`, per the example response and screenshot below:

```
Based on the latest data I had received, you have a total of 47 open alerts. Here's the breakdown: 24 alerts are of low severity, 12 alerts are of medium severity, 7 alerts are of high severity, and 4 alerts are of critical severity (Note: One of the critical severity alerts has been acknowledged).
```

![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)
andrew-goldstein added a commit to andrew-goldstein/kibana that referenced this pull request Dec 21, 2023
…rts in the LangChain `AlertCountsTool` aggregation (elastic#173701)

This PR updates the LangChain `AlertCountsTool` aggregation, which answers questions like `How many open alerts do I have?`, to include `acknowledged` alerts. The `AlertCountsTool` was introduced as part of [[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts elastic#172542](elastic#172542)

- This PR is similar to <elastic#173121>, where `acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`, which returns the _details_ of alerts
  - In contrast to [elastic#173121](elastic#173121), this PR is focused on the alert counts _aggregation_

- This PR also updates the `range` of **both** the `AlertCountsTool` and the `OpenAndAcknowledgedAlertsTool` queries to standardize on the following syntax, which aligns with the `Last 24 hours` option in the _Commonly used_ section of the Kibana date picker:

```json
          "range": {
            "@timestamp": {
              "gte": "now-24h",
              "lte": "now"
            }
          }
```

To desk test this change:

- The `assistantRagOnAlerts` feature flag described in [elastic#172542](elastic#172542) must be enabled, per the following example:

```
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
```

- The `Alerts` feature must be enabled in the assistant settings, per the screenshot below:

 ![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)

1) Generate alerts with a variety of severity (e.g. `low`, `medium`, `high`, and `critical`)

2) After the alerts have been generated, disable all detection rules to keep the counts static during testing

3) Navigate to Security > Alerts

4) Select `Last 24 hours` from the _Commonly used_ section of the global date picker

5) Click the `Treemap` button to select the Treemap visualization

6) In the Treemap's `Group by` input, enter `kibana.alert.severity`

7) Next, in the Treemap's `Group by top` input, enter `kibana.alert.workflow_status`

8) Click the `AI Assistant` button to open the assistant

9) Click the `X` button to clear the conversation

10) Close the assistant

11) Add the following two fields as columns to the Alerts page table:

```
kibana.alert.workflow_status
_id
```

12) Sort the Alerts table, first by `kibana.alert.risk_score` from high to low, and then by `@timestamp` from new to old, per the screenshot below:

![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)

**Expected results**

- The alerts page date range is `Last 24 hours`
- The `Treemap` is selected
- The treemap is grouped by `kibana.alert.severity` and then `kibana.alert.workflow_status`
- The alerts table has custom sorting and columns, per the screenshot below:

![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)

13) Click the `AI Assistant` button to open the assistant

14) Ask the assistant:

```
How many open alerts do I have?
```

**Expected results**

- The assistant will report on the counts and workflow status of alerts, per the example response and screenshot below:

```
You have a total of 47 open alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts with medium severity, 7 alerts with high severity, and 4 alerts with critical severity.
```

![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)

15) Make note of the counts shown in the assistant, then close the assistant

Expected result:

- The counts from the assistant match the counts in the treemap legend, per the example screenshot below:

![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)

16) Change the workflow status of an alert in the Alerts table from `open` to `acknowledged`

**Expected result**

- The treemap and alerts table and include the updated (`acknowledged`) alert, per the screenshot below:

![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)

17) Once again, open the assistant

18) Once again, ask the (same) question:

```
How many open alerts do I have?
```

**Expected result**

- The response from the assistant makes reference to the alert who's workflow status was changed from `open` to `acknowledged`, per the example response and screenshot below:

```
Based on the latest data I had received, you have a total of 47 open alerts. Here's the breakdown: 24 alerts are of low severity, 12 alerts are of medium severity, 7 alerts are of high severity, and 4 alerts are of critical severity (Note: One of the critical severity alerts has been acknowledged).
```

![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)

(cherry picked from commit 081f52b)
andrew-goldstein added a commit that referenced this pull request Dec 21, 2023
…ed` alerts in the LangChain `AlertCountsTool` aggregation (#173701) (#173801)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[Security Solution] [Elastic AI Assistant] Include `acknowledged`
alerts in the LangChain `AlertCountsTool` aggregation
(#173701)](#173701)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Andrew
Macri","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-12-21T04:41:11Z","message":"[Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation (#173701)\n\n## [Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation\r\n\r\nThis PR updates the
LangChain `AlertCountsTool` aggregation, which answers questions like
`How many open alerts do I have?`, to include `acknowledged` alerts. The
`AlertCountsTool` was introduced as part of [[Security Solution]
[Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts
#172542](https://github.com/elastic/kibana/pull/172542)\r\n\r\n- This PR
is similar to <#173121>, where
`acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`,
which returns the _details_ of alerts\r\n - In contrast to
[#173121](#173121), this PR is
focused on the alert counts _aggregation_\r\n\r\n- This PR also updates
the `range` of **both** the `AlertCountsTool` and the
`OpenAndAcknowledgedAlertsTool` queries to standardize on the following
syntax, which aligns with the `Last 24 hours` option in the _Commonly
used_ section of the Kibana date picker:\r\n\r\n```json\r\n \"range\":
{\r\n \"@timestamp\": {\r\n \"gte\": \"now-24h\",\r\n \"lte\":
\"now\"\r\n }\r\n }\r\n```\r\n\r\n### Desk testing\r\n\r\nTo desk test
this change:\r\n\r\n- The `assistantRagOnAlerts` feature flag described
in [#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)\r\n\r\n1)
Generate alerts with a variety of severity (e.g. `low`, `medium`,
`high`, and `critical`)\r\n\r\n2) After the alerts have been generated,
disable all detection rules to keep the counts static during
testing\r\n\r\n3) Navigate to Security > Alerts\r\n\r\n4) Select `Last
24 hours` from the _Commonly used_ section of the global date
picker\r\n\r\n5) Click the `Treemap` button to select the Treemap
visualization\r\n\r\n6) In the Treemap's `Group by` input, enter
`kibana.alert.severity`\r\n\r\n7) Next, in the Treemap's `Group by top`
input, enter `kibana.alert.workflow_status`\r\n\r\n8) Click the `AI
Assistant` button to open the assistant\r\n\r\n9) Click the `X` button
to clear the conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add
the following two fields as columns to the Alerts page
table:\r\n\r\n```\r\nkibana.alert.workflow_status\r\n_id\r\n```\r\n\r\n12)
Sort the Alerts table, first by `kibana.alert.risk_score` from high to
low, and then by `@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n**Expected
results**\r\n\r\n- The alerts page date range is `Last 24 hours`\r\n-
The `Treemap` is selected\r\n- The treemap is grouped by
`kibana.alert.severity` and then `kibana.alert.workflow_status`\r\n- The
alerts table has custom sorting and columns, per the screenshot
below:\r\n\r\n![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)\r\n\r\n13)
Click the `AI Assistant` button to open the assistant\r\n\r\n14) Ask the
assistant:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The assistant will
report on the counts and workflow status of alerts, per the example
response and screenshot below:\r\n\r\n```\r\nYou have a total of 47 open
alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts
with medium severity, 7 alerts with high severity, and 4 alerts with
critical
severity.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)\r\n\r\n15)
Make note of the counts shown in the assistant, then close the
assistant\r\n\r\nExpected result:\r\n\r\n- The counts from the assistant
match the counts in the treemap legend, per the example screenshot
below:\r\n\r\n![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)\r\n\r\n16)
Change the workflow status of an alert in the Alerts table from `open`
to `acknowledged`\r\n\r\n**Expected result**\r\n\r\n- The treemap and
alerts table and include the updated (`acknowledged`) alert, per the
screenshot
below:\r\n\r\n![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)\r\n\r\n17)
Once again, open the assistant\r\n\r\n18) Once again, ask the (same)
question:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response and screenshot
below:\r\n\r\n```\r\nBased on the latest data I had received, you have a
total of 47 open alerts. Here's the breakdown: 24 alerts are of low
severity, 12 alerts are of medium severity, 7 alerts are of high
severity, and 4 alerts are of critical severity (Note: One of the
critical severity alerts has been
acknowledged).\r\n```\r\n\r\n![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)","sha":"081f52bfe3fbbaf5bb9476c656c308f7f9430df2","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:
SecuritySolution","Team:Threat Hunting:Investigations","Feature:Elastic
AI
Assistant","v8.12.0","v8.13.0"],"number":173701,"url":"https://github.com/elastic/kibana/pull/173701","mergeCommit":{"message":"[Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation (#173701)\n\n## [Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation\r\n\r\nThis PR updates the
LangChain `AlertCountsTool` aggregation, which answers questions like
`How many open alerts do I have?`, to include `acknowledged` alerts. The
`AlertCountsTool` was introduced as part of [[Security Solution]
[Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts
#172542](https://github.com/elastic/kibana/pull/172542)\r\n\r\n- This PR
is similar to <#173121>, where
`acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`,
which returns the _details_ of alerts\r\n - In contrast to
[#173121](#173121), this PR is
focused on the alert counts _aggregation_\r\n\r\n- This PR also updates
the `range` of **both** the `AlertCountsTool` and the
`OpenAndAcknowledgedAlertsTool` queries to standardize on the following
syntax, which aligns with the `Last 24 hours` option in the _Commonly
used_ section of the Kibana date picker:\r\n\r\n```json\r\n \"range\":
{\r\n \"@timestamp\": {\r\n \"gte\": \"now-24h\",\r\n \"lte\":
\"now\"\r\n }\r\n }\r\n```\r\n\r\n### Desk testing\r\n\r\nTo desk test
this change:\r\n\r\n- The `assistantRagOnAlerts` feature flag described
in [#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)\r\n\r\n1)
Generate alerts with a variety of severity (e.g. `low`, `medium`,
`high`, and `critical`)\r\n\r\n2) After the alerts have been generated,
disable all detection rules to keep the counts static during
testing\r\n\r\n3) Navigate to Security > Alerts\r\n\r\n4) Select `Last
24 hours` from the _Commonly used_ section of the global date
picker\r\n\r\n5) Click the `Treemap` button to select the Treemap
visualization\r\n\r\n6) In the Treemap's `Group by` input, enter
`kibana.alert.severity`\r\n\r\n7) Next, in the Treemap's `Group by top`
input, enter `kibana.alert.workflow_status`\r\n\r\n8) Click the `AI
Assistant` button to open the assistant\r\n\r\n9) Click the `X` button
to clear the conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add
the following two fields as columns to the Alerts page
table:\r\n\r\n```\r\nkibana.alert.workflow_status\r\n_id\r\n```\r\n\r\n12)
Sort the Alerts table, first by `kibana.alert.risk_score` from high to
low, and then by `@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n**Expected
results**\r\n\r\n- The alerts page date range is `Last 24 hours`\r\n-
The `Treemap` is selected\r\n- The treemap is grouped by
`kibana.alert.severity` and then `kibana.alert.workflow_status`\r\n- The
alerts table has custom sorting and columns, per the screenshot
below:\r\n\r\n![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)\r\n\r\n13)
Click the `AI Assistant` button to open the assistant\r\n\r\n14) Ask the
assistant:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The assistant will
report on the counts and workflow status of alerts, per the example
response and screenshot below:\r\n\r\n```\r\nYou have a total of 47 open
alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts
with medium severity, 7 alerts with high severity, and 4 alerts with
critical
severity.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)\r\n\r\n15)
Make note of the counts shown in the assistant, then close the
assistant\r\n\r\nExpected result:\r\n\r\n- The counts from the assistant
match the counts in the treemap legend, per the example screenshot
below:\r\n\r\n![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)\r\n\r\n16)
Change the workflow status of an alert in the Alerts table from `open`
to `acknowledged`\r\n\r\n**Expected result**\r\n\r\n- The treemap and
alerts table and include the updated (`acknowledged`) alert, per the
screenshot
below:\r\n\r\n![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)\r\n\r\n17)
Once again, open the assistant\r\n\r\n18) Once again, ask the (same)
question:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response and screenshot
below:\r\n\r\n```\r\nBased on the latest data I had received, you have a
total of 47 open alerts. Here's the breakdown: 24 alerts are of low
severity, 12 alerts are of medium severity, 7 alerts are of high
severity, and 4 alerts are of critical severity (Note: One of the
critical severity alerts has been
acknowledged).\r\n```\r\n\r\n![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)","sha":"081f52bfe3fbbaf5bb9476c656c308f7f9430df2"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173701","number":173701,"mergeCommit":{"message":"[Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation (#173701)\n\n## [Security
Solution] [Elastic AI Assistant] Include `acknowledged` alerts in the
LangChain `AlertCountsTool` aggregation\r\n\r\nThis PR updates the
LangChain `AlertCountsTool` aggregation, which answers questions like
`How many open alerts do I have?`, to include `acknowledged` alerts. The
`AlertCountsTool` was introduced as part of [[Security Solution]
[Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts
#172542](https://github.com/elastic/kibana/pull/172542)\r\n\r\n- This PR
is similar to <#173121>, where
`acknowledged` alerts were added to the `OpenAndAcknowledgedAlertsTool`,
which returns the _details_ of alerts\r\n - In contrast to
[#173121](#173121), this PR is
focused on the alert counts _aggregation_\r\n\r\n- This PR also updates
the `range` of **both** the `AlertCountsTool` and the
`OpenAndAcknowledgedAlertsTool` queries to standardize on the following
syntax, which aligns with the `Last 24 hours` option in the _Commonly
used_ section of the Kibana date picker:\r\n\r\n```json\r\n \"range\":
{\r\n \"@timestamp\": {\r\n \"gte\": \"now-24h\",\r\n \"lte\":
\"now\"\r\n }\r\n }\r\n```\r\n\r\n### Desk testing\r\n\r\nTo desk test
this change:\r\n\r\n- The `assistantRagOnAlerts` feature flag described
in [#172542](#172542) must be
enabled, per the following
example:\r\n\r\n```\r\nxpack.securitySolution.enableExperimental:
['assistantRagOnAlerts']\r\n```\r\n\r\n- The `Alerts` feature must be
enabled in the assistant settings, per the screenshot below:\r\n\r\n
![enable_alerts](https://github.com/elastic/kibana/assets/4459398/f6a3077d-5815-4225-9a8e-7f5b51d5f2d4)\r\n\r\n1)
Generate alerts with a variety of severity (e.g. `low`, `medium`,
`high`, and `critical`)\r\n\r\n2) After the alerts have been generated,
disable all detection rules to keep the counts static during
testing\r\n\r\n3) Navigate to Security > Alerts\r\n\r\n4) Select `Last
24 hours` from the _Commonly used_ section of the global date
picker\r\n\r\n5) Click the `Treemap` button to select the Treemap
visualization\r\n\r\n6) In the Treemap's `Group by` input, enter
`kibana.alert.severity`\r\n\r\n7) Next, in the Treemap's `Group by top`
input, enter `kibana.alert.workflow_status`\r\n\r\n8) Click the `AI
Assistant` button to open the assistant\r\n\r\n9) Click the `X` button
to clear the conversation\r\n\r\n10) Close the assistant\r\n\r\n11) Add
the following two fields as columns to the Alerts page
table:\r\n\r\n```\r\nkibana.alert.workflow_status\r\n_id\r\n```\r\n\r\n12)
Sort the Alerts table, first by `kibana.alert.risk_score` from high to
low, and then by `@timestamp` from new to old, per the screenshot
below:\r\n\r\n![fields_sorted](https://github.com/elastic/kibana/assets/4459398/e84f06d4-790d-4227-afbf-a233d4848178)\r\n\r\n**Expected
results**\r\n\r\n- The alerts page date range is `Last 24 hours`\r\n-
The `Treemap` is selected\r\n- The treemap is grouped by
`kibana.alert.severity` and then `kibana.alert.workflow_status`\r\n- The
alerts table has custom sorting and columns, per the screenshot
below:\r\n\r\n![alerts_page_setup](https://github.com/elastic/kibana/assets/4459398/f4700abc-b2ca-483e-92d8-5a186142e1fb)\r\n\r\n13)
Click the `AI Assistant` button to open the assistant\r\n\r\n14) Ask the
assistant:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected results**\r\n\r\n- The assistant will
report on the counts and workflow status of alerts, per the example
response and screenshot below:\r\n\r\n```\r\nYou have a total of 47 open
alerts. Here's the breakdown: 24 alerts with low severity, 12 alerts
with medium severity, 7 alerts with high severity, and 4 alerts with
critical
severity.\r\n```\r\n\r\n![assistant_open_alerts](https://github.com/elastic/kibana/assets/4459398/45740c07-9317-42e6-943d-fc346b8106e5)\r\n\r\n15)
Make note of the counts shown in the assistant, then close the
assistant\r\n\r\nExpected result:\r\n\r\n- The counts from the assistant
match the counts in the treemap legend, per the example screenshot
below:\r\n\r\n![open_alerts_in_treemap](https://github.com/elastic/kibana/assets/4459398/368fb707-9faf-4b9b-a0b3-81fab4d680b2)\r\n\r\n16)
Change the workflow status of an alert in the Alerts table from `open`
to `acknowledged`\r\n\r\n**Expected result**\r\n\r\n- The treemap and
alerts table and include the updated (`acknowledged`) alert, per the
screenshot
below:\r\n\r\n![updated_treemap_and_table](https://github.com/elastic/kibana/assets/4459398/0b8bedb7-aed7-41f1-abcd-f79a79480739)\r\n\r\n17)
Once again, open the assistant\r\n\r\n18) Once again, ask the (same)
question:\r\n\r\n```\r\nHow many open alerts do I
have?\r\n```\r\n\r\n**Expected result**\r\n\r\n- The response from the
assistant makes reference to the alert who's workflow status was changed
from `open` to `acknowledged`, per the example response and screenshot
below:\r\n\r\n```\r\nBased on the latest data I had received, you have a
total of 47 open alerts. Here's the breakdown: 24 alerts are of low
severity, 12 alerts are of medium severity, 7 alerts are of high
severity, and 4 alerts are of critical severity (Note: One of the
critical severity alerts has been
acknowledged).\r\n```\r\n\r\n![with_acknowledged_alerts](https://github.com/elastic/kibana/assets/4459398/4a8961f2-80eb-457f-b16b-8ea48c5d5c38)","sha":"081f52bfe3fbbaf5bb9476c656c308f7f9430df2"}}]}]
BACKPORT-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Security Assistant Security Assistant release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Threat Hunting:Investigations Security Solution Investigations Team v8.12.0 v8.13.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants