-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[AlertZero] Action catalog #290705
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
Merged
Merged
[AlertZero] Action catalog #290705
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4a7240c
[AlertZero] Action catalog: API and Agent Builder tool for category-s…
patrykkopycinski 5ae826f
fix(alertzero): map invalid categories param to 400 and fix stale too…
patrykkopycinski 3ee5a09
Changes from node scripts/check
kibanamachine e1333af
fix(alertzero-common): re-export ActionApprovalPolicy/ActionCategory/…
patrykkopycinski 983d485
Merge remote-tracking branch 'origin/main' into alertzero/action-catalog
patrykkopycinski 8c8730b
chore: dedupe license headers and narrow tool dependency type
patrykkopycinski 33ed9be
fix(alertzero): quote the Pick type argument and narrow the test help…
patrykkopycinski d389f09
Changes from node scripts/check
kibanamachine 810f238
chore: dedupe license headers in actions routes and service files
patrykkopycinski dee2d63
refactor(alertzero): rename tool to security.alertzero.actions.list p…
patrykkopycinski ba36b0f
[AO] fix CI failure on #290705
patrykkopycinski eddf11f
[AO] address review feedback on #290705
patrykkopycinski b6604e2
[AO] fix CI failure on #290705
patrykkopycinski 935c294
Merge branch 'main' of https://github.com/elastic/kibana into ao-pr-e…
patrykkopycinski 3edc16b
chore: remove changelog entry (release_note:skip)
patrykkopycinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
x-pack/solutions/security/packages/kbn-alertzero-common/action_catalog_types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { ActionApprovalPolicy, ActionCategory, ActionImpact } from '@kbn/workflows'; | ||
|
|
||
| export type { ActionApprovalPolicy, ActionCategory, ActionImpact }; | ||
|
|
||
| /** | ||
| * One entry of the action catalog: the lightweight, agent-facing projection of | ||
| * an installed action workflow. Mirrors `consts.actionMetadata` on the | ||
| * workflow definition plus the workflow id, so an agent can propose the action | ||
| * without reading the full YAML. | ||
| */ | ||
| export interface ActionCatalogEntry { | ||
| workflowId: string; | ||
| name: string; | ||
| description?: string; | ||
| category?: ActionCategory; | ||
| impact?: ActionImpact; | ||
| approvalPolicy?: ActionApprovalPolicy; | ||
| } | ||
|
|
||
| /** Response of `GET /internal/alertzero/actions`. */ | ||
| export interface ListActionsResponse { | ||
| actions: ActionCatalogEntry[]; | ||
| total: number; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,5 +18,6 @@ | |
| "@kbn/zod", | ||
| "@kbn/i18n", | ||
| "@kbn/deeplinks-security", | ||
| "@kbn/workflows", | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...solutions/security/plugins/alertzero/server/agent_builder_tools/list_actions_tool.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { listActionsTool } from './list_actions_tool'; | ||
| import type { ActionsService } from '../services/actions/actions_service'; | ||
| import { ToolResultType } from '@kbn/agent-builder-common/tools/tool_result'; | ||
|
|
||
| const logger = () => ({ error: jest.fn(), warn: jest.fn(), info: jest.fn(), debug: jest.fn() }); | ||
|
|
||
| const serviceWith = (list: jest.Mock) => ({ list } as Pick<ActionsService, 'list'>); | ||
|
|
||
| const run = async ( | ||
| service: Pick<ActionsService, 'list'>, | ||
| input: { categories?: string[] } = {} | ||
| ) => { | ||
| const tool = listActionsTool(() => service); | ||
| const result = await tool.handler(input, { logger: logger() } as never); | ||
| if (!('results' in result)) { | ||
| throw new Error('expected a standard tool result'); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| const ACTION = (over: Partial<Record<string, unknown>> = {}) => ({ | ||
| workflowId: 'system-alertzero-action-create-rule', | ||
| name: 'Create detection rule', | ||
| category: 'tune', | ||
| ...over, | ||
| }); | ||
|
|
||
| describe('listActionsTool', () => { | ||
| it('lists all actions when called without categories', async () => { | ||
| const list = jest.fn().mockResolvedValue({ | ||
| actions: [ACTION(), ACTION({ workflowId: 'a2', name: 'Isolate host', category: 'contain' })], | ||
| total: 2, | ||
| }); | ||
| const result = await run(serviceWith(list)); | ||
| expect(list).toHaveBeenCalledWith('default', undefined); | ||
| expect(result.results[0].type).toBe(ToolResultType.other); | ||
| expect(result.results[0].data).toMatchObject({ total: 2 }); | ||
| }); | ||
|
|
||
| it('forwards categories to the service and reports empty results explicitly', async () => { | ||
| const list = jest.fn().mockResolvedValue({ actions: [], total: 0 }); | ||
| const result = await run(serviceWith(list), { categories: ['escalate'] }); | ||
| expect(list).toHaveBeenCalledWith('default', ['escalate']); | ||
| expect(result.results[0].data).toMatchObject({ | ||
| total: 0, | ||
| message: 'No actions found in categories: escalate.', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns an error result instead of throwing when the service fails', async () => { | ||
| const list = jest.fn().mockRejectedValue(new Error('workflows management down')); | ||
| const result = await run(serviceWith(list)); | ||
| expect(result.results[0].type).not.toBe(ToolResultType.other); | ||
| expect(JSON.stringify(result.results[0])).toContain('workflows management down'); | ||
| }); | ||
|
|
||
| it('declares the documented tool id and read-only annotations', () => { | ||
| const tool = listActionsTool(() => serviceWith(jest.fn())); | ||
| expect(tool.id).toBe('security.alertzero.actions.list'); | ||
| expect(tool.annotations).toMatchObject({ | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| idempotentHint: true, | ||
| }); | ||
| expect(tool.type).toBe('builtin'); | ||
| }); | ||
| }); |
80 changes: 80 additions & 0 deletions
80
x-pack/solutions/security/plugins/alertzero/server/agent_builder_tools/list_actions_tool.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod/v4'; | ||
| import { createErrorResult } from '@kbn/agent-builder-server'; | ||
| import { ToolResultType } from '@kbn/agent-builder-common/tools/tool_result'; | ||
| import type { BuiltinToolDefinition } from '@kbn/agent-builder-server/tools'; | ||
| import { ToolType } from '@kbn/agent-builder-common'; | ||
| import { actionCategorySchema } from '@kbn/workflows/managed'; | ||
| import { ALERTZERO_ACTIONS_LIST_TOOL_ID } from '@kbn/alertzero-common'; | ||
| import type { ActionsService } from '../services/actions/actions_service'; | ||
|
|
||
| const listByCategorySchema = z.object({ | ||
| categories: z | ||
| .array(actionCategorySchema) | ||
| .max(20) | ||
| .optional() | ||
| .describe( | ||
| 'Category keywords to filter on (e.g. ["contain", "escalate"]). An action is returned when its declared category matches ANY of these. Omit to list every available action.' | ||
| ), | ||
| }); | ||
|
|
||
| /** | ||
| * `security.alertzero.actions.list` — lets an agent discover the | ||
| * installed action workflows at runtime instead of hard-coding workflow ids. | ||
| * | ||
| * Registered by the AlertZero plugin (setup), reads the catalog through | ||
| * {@link ActionsService} — the same service backing the HTTP API — so the tool | ||
| * and the API can never drift. | ||
| */ | ||
| export const listActionsTool = ( | ||
| getActionsService: () => Pick<ActionsService, 'list'> | ||
| ): BuiltinToolDefinition<typeof listByCategorySchema> => ({ | ||
| id: ALERTZERO_ACTIONS_LIST_TOOL_ID, | ||
| type: ToolType.builtin, | ||
| description: | ||
| 'List available AlertZero actions, optionally filtered by category. Each result includes the workflowId to reference when proposing the action, plus its name, description, category, impact (low/medium/high/critical) and approvalPolicy (always-gate/autonomy-dependent). Call this before proposing an action so the proposal references a real, installed workflow.', | ||
| annotations: { | ||
| title: 'List AlertZero Actions', | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| idempotentHint: true, | ||
| openWorldHint: false, | ||
| }, | ||
| schema: listByCategorySchema, | ||
| tags: ['alertzero'], | ||
| handler: async ({ categories }, { logger }) => { | ||
| try { | ||
| const { actions, total } = await getActionsService().list('default', categories); | ||
| const message = | ||
| total === 0 | ||
| ? categories | ||
| ? `No actions found in categories: ${categories.join(', ')}.` | ||
| : 'No actions are installed.' | ||
| : undefined; | ||
| return { | ||
| results: [ | ||
| { | ||
| type: ToolResultType.other, | ||
| data: { | ||
| total, | ||
| actions, | ||
| ...(message && { message }), | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
| logger.error(`[List Actions Tool] Error listing actions: ${errorMessage}`); | ||
| return { | ||
| results: [createErrorResult(`Error listing actions: ${errorMessage}`)], | ||
| }; | ||
| } | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
x-pack/solutions/security/plugins/alertzero/server/routes/actions/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| /** Max number of values accepted in the `categories` query param (each also length-capped upstream by actionCategorySchema). */ | ||
| export const ACTION_CATEGORIES_QUERY_PARAM_MAX_ITEMS = 20; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionis bumped to3, but the fingerprint guard inmanaged_workflow_definitions.test.tsexpects2:faf316b1. That test builds`${definition.version}:${createContentFingerprint(importedYaml)}`, so it will compute3:faf316b1and fail (expected 2:faf316b1). The version was1onmainand the YAML changed once here, so this should be2to match the guard (the PR description also says "managed version 1 → 2"). If3is intentional, the expected fingerprint must be updated to3:faf316b1instead.