-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Osquery] Add Scout API tests for saved queries, packs, and response actions #258534
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
+1,278
−162
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
390af80
add initial Scout Osquery tests
csr 89ead7f
use correct matcher
csr 3f966b9
fix spacing
csr 1c3bedb
capitalize Osquery
csr 05032cd
use `apiServices` instead of `kbnClient` or `apiClient` for setup and…
csr f89c521
add API tests that test permission bounderies (shoul return unauthori…
csr bc87674
split monolithic API test into three focused tests
csr b26ca4d
define common constants file
csr 58d18b8
split sequential test into multiple parallel tests + global setup hook
csr d457e10
minor tweaks to UI tests
csr b643cf5
remove UI tests (will create a separate follow-up PR)
csr 5bbca9c
remove FTR tests that were migrated to Scout
csr f82900d
Merge branch 'main' into scout-osquery-first-steps
csr a7ed131
Merge branch 'main' into scout-osquery-first-steps
csr e383d22
Fix `createdBy` query parameter encoding for MKI compatibility
csr 10796be
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine 7ae9d3e
Merge branch 'main' into scout-osquery-first-steps
csr e207506
check for 404 not found response
csr c14bfc2
temporarily exclude test suites from ECH test runs
csr ab4cfdb
replace `deploymentAgnostic` tags with Security-specific tags
csr 3d191fd
add `tsconfig.json`
csr 5cdeabb
re-add FTR test suite (without tests that are already migrated)
csr 88ba38c
filter pack by `enabled` status (both `true` and `false`)
csr e300d7e
update comments
csr c9e22d6
update tags
csr 455163d
update comments
csr e381663
remove `tsconfig.json`
csr 7680bf8
add TODO comment
csr 4d281f4
update plugin's `tsconfig.json`
csr 288bb06
Changes from node scripts/lint_ts_projects --fix
kibanamachine 99b381c
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine 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
Some comments aren't visible on the classic Files Changed page.
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
62 changes: 62 additions & 0 deletions
62
x-pack/platform/plugins/shared/osquery/test/scout/api/fixtures/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,62 @@ | ||
| /* | ||
| * 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 { OSQUERY_API_VERSION } from '../../common/constants'; | ||
|
|
||
| export { OSQUERY_API_VERSION }; | ||
|
|
||
| export const COMMON_HEADERS = { | ||
| 'kbn-xsrf': 'some-xsrf-token', | ||
| 'x-elastic-internal-origin': 'kibana', | ||
| 'Content-Type': 'application/json;charset=UTF-8', | ||
| 'elastic-api-version': OSQUERY_API_VERSION, | ||
| } as const; | ||
|
|
||
| export const API_PATHS = { | ||
| DETECTION_RULES: 'api/detection_engine/rules', | ||
| OSQUERY_SAVED_QUERIES: 'api/osquery/saved_queries', | ||
| OSQUERY_PACKS: 'api/osquery/packs', | ||
| } as const; | ||
|
|
||
| const uniqueId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; | ||
|
|
||
| export const getMinimalRule = (overrides: Record<string, unknown> = {}) => ({ | ||
| type: 'query', | ||
| index: ['auditbeat-*'], | ||
| language: 'kuery', | ||
| query: '_id:*', | ||
| name: `Test rule ${uniqueId()}`, | ||
| description: 'Test rule for Osquery response actions', | ||
| risk_score: 21, | ||
| severity: 'low', | ||
| interval: '5m', | ||
| from: 'now-360s', | ||
| to: 'now', | ||
| enabled: false, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| export const getMinimalPack = (overrides: Record<string, unknown> = {}) => ({ | ||
| name: `test-pack-${uniqueId()}`, | ||
| description: 'Test pack for Osquery Scout tests', | ||
| enabled: true, | ||
| queries: { | ||
| testQuery: { | ||
| query: 'select * from uptime;', | ||
| interval: 3600, | ||
| }, | ||
| }, | ||
| shards: {}, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| export const getMinimalSavedQuery = (overrides: Record<string, unknown> = {}) => ({ | ||
| id: `test-saved-query-${uniqueId()}`, | ||
| query: 'select 1;', | ||
| interval: '3600', | ||
| ...overrides, | ||
| }); |
44 changes: 44 additions & 0 deletions
44
x-pack/platform/plugins/shared/osquery/test/scout/api/fixtures/index.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,44 @@ | ||
| /* | ||
| * 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 { ApiServicesFixture, ScoutTestFixtures, ScoutWorkerFixtures } from '@kbn/scout'; | ||
| import { apiTest as baseApiTest } from '@kbn/scout'; | ||
| import { | ||
| getOsqueryApiService, | ||
| type OsqueryApiService, | ||
| } from '../../common/services/osquery_api_service'; | ||
|
|
||
| export interface OsqueryApiServicesFixture extends ApiServicesFixture { | ||
| osquery: OsqueryApiService; | ||
| } | ||
|
|
||
| export const apiTest = baseApiTest.extend< | ||
| ScoutTestFixtures, | ||
| { apiServices: OsqueryApiServicesFixture } | ||
| >({ | ||
| apiServices: [ | ||
| async ( | ||
| { | ||
| apiServices, | ||
| kbnClient, | ||
| log, | ||
| }: { | ||
| apiServices: ApiServicesFixture; | ||
| kbnClient: ScoutWorkerFixtures['kbnClient']; | ||
| log: ScoutWorkerFixtures['log']; | ||
| }, | ||
| use: (extendedApiServices: OsqueryApiServicesFixture) => Promise<void> | ||
| ) => { | ||
| const extendedApiServices = apiServices as OsqueryApiServicesFixture; | ||
| extendedApiServices.osquery = getOsqueryApiService({ kbnClient, log }); | ||
| await use(extendedApiServices); | ||
| }, | ||
| { scope: 'worker' }, | ||
| ], | ||
| }); | ||
|
|
||
| export * as testData from './constants'; |
12 changes: 12 additions & 0 deletions
12
x-pack/platform/plugins/shared/osquery/test/scout/api/playwright.config.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,12 @@ | ||
| /* | ||
| * 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 { createPlaywrightConfig } from '@kbn/scout'; | ||
|
|
||
| export default createPlaywrightConfig({ | ||
| testDir: './tests', | ||
| }); |
61 changes: 61 additions & 0 deletions
61
x-pack/platform/plugins/shared/osquery/test/scout/api/tests/packs_admin.spec.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,61 @@ | ||
| /* | ||
| * 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 { RoleSessionCredentials } from '@kbn/scout'; | ||
| import { expect } from '@kbn/scout/api'; | ||
| import { tags } from '@kbn/scout'; | ||
| import { apiTest, testData } from '../fixtures'; | ||
|
|
||
| // TODO: run on ECH once PR #258866 makes it to prod | ||
| apiTest.describe( | ||
| 'Osquery packs - admin', | ||
| { | ||
| tag: ['@local-stateful-classic', ...tags.serverless.security.complete], | ||
| }, | ||
| () => { | ||
| let adminCredentials: RoleSessionCredentials; | ||
| const createdPackIds: string[] = []; | ||
|
|
||
| apiTest.beforeAll(async ({ samlAuth }) => { | ||
| // TODO: investigate why this test only passes with cookie-based authentication while similar | ||
| // tests (saved_queries_admin.spec.ts) pass with API key-based authentication | ||
| adminCredentials = await samlAuth.asInteractiveUser('admin'); | ||
| }); | ||
|
|
||
| apiTest.afterAll(async ({ apiServices }) => { | ||
| for (const packId of createdPackIds) { | ||
| await apiServices.osquery.packs.delete(packId); | ||
| } | ||
| }); | ||
|
|
||
| apiTest('includes profile_uid fields on create and find', async ({ apiClient }) => { | ||
| const createResponse = await apiClient.post(testData.API_PATHS.OSQUERY_PACKS, { | ||
| headers: { ...testData.COMMON_HEADERS, ...adminCredentials.cookieHeader }, | ||
| body: testData.getMinimalPack(), | ||
| responseType: 'json', | ||
| }); | ||
| expect(createResponse).toHaveStatusCode(200); | ||
| expect(createResponse.body.data).toBeDefined(); | ||
| createdPackIds.push(createResponse.body.data.saved_object_id); | ||
|
|
||
| expect('created_by_profile_uid' in createResponse.body.data).toBe(true); | ||
| expect('updated_by_profile_uid' in createResponse.body.data).toBe(true); | ||
| expect(createResponse.body.data.created_by).toBeDefined(); | ||
|
|
||
| const findResponse = await apiClient.get( | ||
| `${testData.API_PATHS.OSQUERY_PACKS}?search=${createResponse.body.data.name}`, | ||
| { | ||
| headers: { ...testData.COMMON_HEADERS, ...adminCredentials.cookieHeader }, | ||
| responseType: 'json', | ||
| } | ||
| ); | ||
| expect(findResponse).toHaveStatusCode(200); | ||
| expect(findResponse.body.data).toBeDefined(); | ||
| expect('created_by_profile_uid' in findResponse.body.data[0]).toBe(true); | ||
| }); | ||
| } | ||
| ); | ||
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.
🟢 Low
tests/packs_admin.spec.ts:55findResponse.body.data[0]on line 57 throwsTypeErrorwhen the search returns an empty array. The check on line 56 only verifiesdatais defined, not that it contains elements. If the pack isn't found due to eventual consistency,data[0]isundefinedand'created_by_profile_uid' in undefinedthrows. Consider assertingdata.length > 0before the element access, or use optional chaining with a fallback assertion.expect(findResponse).toHaveStatusCode(200); expect(findResponse.body.data).toBeDefined(); - expect('created_by_profile_uid' in findResponse.body.data[0]).toBe(true); + expect(findResponse.body.data.length).toBeGreaterThan(0); + expect('created_by_profile_uid' in findResponse.body.data[0]).toBe(true);🤖 Copy this AI Prompt to have your agent fix this: