-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Security solution] Security AI prompts integration #216106
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
stephmilovic
merged 28 commits into
elastic:main
from
stephmilovic:security_ai_prompts_integration
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b4cbaf9
- (temp) enable in mgmt
andrew-goldstein 593512a
beta version and feature flag
stephmilovic a49e283
rm pirate talk
stephmilovic f10e13a
Merge branch 'main' into security_ai_prompts_integration
stephmilovic e66000e
Merge branch 'main' into security_ai_prompts_integration
stephmilovic 987e9fa
more fix
stephmilovic ce93e88
fix test
stephmilovic 1bff213
fix types
stephmilovic bd13130
Merge branch 'main' into security_ai_prompts_integration
stephmilovic c37ebf4
test fixing
stephmilovic 77e9927
type
stephmilovic ab70229
rm tagging
stephmilovic cb06cb2
Merge branch 'main' into security_ai_prompts_integration
stephmilovic fc38ea6
maybe fixing?
stephmilovic 21097de
fix install remove assets test
stephmilovic 43d11cf
rm tag assets test
stephmilovic d0c4af3
fix epm test
stephmilovic 9b5f519
fix snapshot
stephmilovic 25a997a
Merge branch 'main' into security_ai_prompts_integration
stephmilovic b4b0b43
update comment
stephmilovic 4832e69
Merge branch 'main' into security_ai_prompts_integration
elasticmachine 5bb9f94
exclude package from tests
stephmilovic b7d574e
Merge branch 'security_ai_prompts_integration' of github.com:stephmil…
stephmilovic 596ee6c
rm from bundled packages
stephmilovic 49a237d
Merge branch 'main' into security_ai_prompts_integration
elasticmachine 90e3f69
fix error handling
stephmilovic 02eefe4
Merge branch 'main' into security_ai_prompts_integration
elasticmachine bb76dc3
Merge branch 'main' into security_ai_prompts_integration
elasticmachine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,4 +58,4 @@ | |
| "name": "security_detection_engine", | ||
| "version": "9.0.3" | ||
| } | ||
| ] | ||
| ] | ||
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/elastic_assistant/scripts/generate_security_ai_prompts.js
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. | ||
| */ | ||
|
|
||
| require('../../../../../../src/setup_node_env'); | ||
| require('./generate_security_ai_prompts_script').generateSecurityAiPrompts(); |
117 changes: 117 additions & 0 deletions
117
...lutions/security/plugins/elastic_assistant/scripts/generate_security_ai_prompts_script.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,117 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* eslint-disable no-console */ | ||
|
|
||
| import { Prompt } from '@kbn/security-ai-prompts'; | ||
| import * as fs from 'fs/promises'; | ||
| import { existsSync, mkdirSync } from 'fs'; | ||
| import * as path from 'path'; | ||
| import globby from 'globby'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| import { localPrompts } from '../server/lib/prompt/local_prompt_object'; | ||
| import { localToolPrompts } from '../server/lib/prompt/tool_prompts'; | ||
|
|
||
| export const OUTPUT_DIR = '../../../../../target/security_ai_prompts'; | ||
| export const DELETE_FILES_PATTERN = '*.json'; | ||
| export const SAVED_OBJECT_ID_PREFIX = 'security_ai_prompts-'; | ||
|
|
||
| interface SecurityAiPromptSavedObject { | ||
| attributes: Prompt; | ||
| id: string; | ||
| type: 'security-ai-prompt'; | ||
| } | ||
|
|
||
| export const createOutputDir = () => { | ||
| if (!existsSync(OUTPUT_DIR)) { | ||
| console.log(`Creating output directory: ${OUTPUT_DIR}`); | ||
| mkdirSync(OUTPUT_DIR); | ||
| } | ||
| }; | ||
|
|
||
| export const deleteFilesByPattern = async ({ | ||
| directoryPath, | ||
| pattern, | ||
| }: { | ||
| directoryPath: string; | ||
| pattern: string; | ||
| }): Promise<void> => { | ||
| try { | ||
| console.log(`Deleting files matching pattern "${pattern}" in directory "${directoryPath}"`); | ||
| const files = await globby(pattern, { cwd: directoryPath }); | ||
|
|
||
| if (files.length === 0) { | ||
| console.log(`No files found matching pattern "${pattern}" in directory "${directoryPath}".`); | ||
| return; | ||
| } | ||
|
|
||
| for (const file of files) { | ||
| const filePath = path.join(directoryPath, file); | ||
| await fs.unlink(filePath); | ||
| console.log(`Deleted file: "${filePath}"`); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error deleting files: ${error.message}`); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| export const writeSavedObjects = async ({ | ||
| direcotryPath, | ||
| savedObjects, | ||
| }: { | ||
| direcotryPath: string; | ||
| savedObjects: SecurityAiPromptSavedObject[]; | ||
| }) => { | ||
| for (const savedObject of savedObjects) { | ||
| const filePath = path.join(direcotryPath, `${savedObject.id}.json`); | ||
|
|
||
| await fs.writeFile(filePath, `${JSON.stringify(savedObject, null, 2)}\n`); | ||
| console.log(`Wrote saved object to file: "${filePath}"`); | ||
| } | ||
| }; | ||
|
|
||
| export const generateSavedObject = (prompt: Prompt): SecurityAiPromptSavedObject => ({ | ||
| attributes: { | ||
| ...prompt, | ||
| prompt: { | ||
| default: `${prompt.prompt.default}`, | ||
| }, | ||
| }, | ||
| id: `${SAVED_OBJECT_ID_PREFIX}${uuidv4()}`, | ||
| type: 'security-ai-prompt', | ||
| }); | ||
|
|
||
| export const generateSavedObjects = (prompts: Prompt[]): SecurityAiPromptSavedObject[] => | ||
| prompts.map(generateSavedObject); | ||
|
|
||
| export const generateSecurityAiPrompts = async () => { | ||
| console.log('Generating Security AI prompts'); | ||
|
|
||
| createOutputDir(); | ||
|
|
||
| await deleteFilesByPattern({ | ||
| directoryPath: OUTPUT_DIR, | ||
| pattern: DELETE_FILES_PATTERN, | ||
| }); | ||
|
|
||
| const prompts = [...localPrompts, ...localToolPrompts]; | ||
|
|
||
| console.log('--> prompts', JSON.stringify(prompts, null, 2)); | ||
|
|
||
| const savedObjects = generateSavedObjects(prompts); | ||
|
|
||
| console.log('--> savedObjects', JSON.stringify(savedObjects, null, 2)); | ||
|
|
||
| await writeSavedObjects({ | ||
| direcotryPath: OUTPUT_DIR, | ||
| savedObjects, | ||
| }); | ||
|
|
||
| console.log('Done'); | ||
| }; |
1 change: 1 addition & 0 deletions
1
x-pack/solutions/security/plugins/elastic_assistant/tsconfig.json
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
27 changes: 27 additions & 0 deletions
27
...ution/server/lib/detection_engine/prebuilt_rules/logic/integrations/install_ai_prompts.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,27 @@ | ||
| /* | ||
| * 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 { SECURITY_AI_PROMPTS_PACKAGE_NAME } from '../../../../../../common/detection_engine/constants'; | ||
| import type { SecuritySolutionApiRequestHandlerContext } from '../../../../../types'; | ||
| import type { ConfigType } from '../../../../../config'; | ||
| import { findLatestPackageVersion } from './find_latest_package_version'; | ||
|
|
||
| export async function installSecurityAiPromptsPackage( | ||
| config: ConfigType, | ||
| context: SecuritySolutionApiRequestHandlerContext | ||
| ) { | ||
| try { | ||
| const pkgVersion = await findLatestPackageVersion(context, SECURITY_AI_PROMPTS_PACKAGE_NAME); | ||
| return context.getInternalFleetServices().packages.ensureInstalledPackage({ | ||
| pkgName: SECURITY_AI_PROMPTS_PACKAGE_NAME, | ||
| pkgVersion, | ||
| }); | ||
| } catch (e) { | ||
| // fail silently | ||
| return null; | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
x-pack/test/fleet_api_integration/apis/epm/__snapshots__/bulk_get_assets.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
12 changes: 12 additions & 0 deletions
12
.../test_packages/all_assets/0.1.0/kibana/security_ai_prompt/sample_security_ai_prompts.json
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,12 @@ | ||
| { | ||
| "attributes": { | ||
| "promptId": "systemPrompt", | ||
| "promptGroupId": "aiAssistant", | ||
| "provider": "openai", | ||
| "prompt": { | ||
| "default": "You always talk like a pirate." | ||
| } | ||
| }, | ||
| "id": "sample_security_ai_prompt", | ||
| "type": "security-ai-prompt" | ||
| } |
12 changes: 12 additions & 0 deletions
12
.../test_packages/all_assets/0.2.0/kibana/security_ai_prompt/sample_security_ai_prompts.json
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,12 @@ | ||
| { | ||
| "attributes": { | ||
| "promptId": "systemPrompt", | ||
| "promptGroupId": "aiAssistant", | ||
| "provider": "openai", | ||
| "prompt": { | ||
| "default": "You always talk like a pirate." | ||
| } | ||
| }, | ||
| "id": "sample_security_ai_prompt", | ||
| "type": "security-ai-prompt" | ||
| } |
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.
Is it possible to have conflicts if multiple packages have prompts with the same id?
I think that for other assets we prefix their ids with the name of the package to avoid these issues.
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.
The ids will be UUID autogenerated by saved object client, so we will not have this issue. this id was just for tests following other examples