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

[Telemetry][Security Solution] Index metadata collector #194004

Merged
merged 62 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
1b9ffa5
feat(wip): Telemetry index metadata
szaffarano Sep 25, 2024
3007763
Add testing route to trigger new task
szaffarano Sep 26, 2024
34cfa87
Manual pagination
szaffarano Sep 27, 2024
4aefe23
Parameterize testing api
szaffarano Sep 27, 2024
4ebeb00
Update common prefixes logic
szaffarano Sep 30, 2024
5a7092c
Code style
szaffarano Sep 30, 2024
ee68d1a
Fix lint error
szaffarano Oct 1, 2024
ba8ca5f
Fix failing test
szaffarano Oct 1, 2024
bb5e854
Fix failing test
szaffarano Oct 1, 2024
0f7707d
Update logging
szaffarano Oct 2, 2024
90de950
make testing api public
szaffarano Oct 3, 2024
e0c2ca2
Add task configuration
szaffarano Oct 3, 2024
8aa3c9c
Send cluster stats and compute all indices
szaffarano Oct 11, 2024
5846a2a
Add filters option to ftr_helper api
szaffarano Oct 17, 2024
1f395ae
Add analytics_ftr_helpers plugin to kibana server config
szaffarano Oct 17, 2024
1bc08d5
Define kibana_ebt_server service in ftr configs
szaffarano Oct 17, 2024
cb384a1
Add indices_metadata task FTR
szaffarano Oct 17, 2024
90052d9
Move FTR to its test suite
szaffarano Oct 17, 2024
b82f57b
Add more tests
szaffarano Oct 18, 2024
920a444
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Oct 18, 2024
840328e
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 18, 2024
cd3ea60
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 18, 2024
4d27414
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 18, 2024
77a1404
Improve error handling
szaffarano Oct 18, 2024
371b49f
Add more tests
szaffarano Oct 18, 2024
fbd7d8e
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 21, 2024
57bca8b
Improve logging
szaffarano Oct 21, 2024
922f7bc
Apply config to index stats
szaffarano Oct 21, 2024
16a3e29
More tests
szaffarano Oct 21, 2024
9f57dd3
Update common prefixes function
szaffarano Oct 22, 2024
95905a9
Fix typo
szaffarano Oct 22, 2024
125ce49
Add integration tests
szaffarano Oct 23, 2024
8be093d
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 23, 2024
259f5ff
Merge branch 'main' into szaffarano/datastreams-metadata-poc
szaffarano Oct 24, 2024
acc7731
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Oct 24, 2024
09c088e
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 24, 2024
651c3e9
Remove testing route
szaffarano Oct 24, 2024
9d612f9
Remove TODOs
szaffarano Oct 24, 2024
1f212b9
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Oct 28, 2024
10be30a
Not send cluster stats events
szaffarano Oct 24, 2024
8d5e88b
Merge branch 'main' into szaffarano/datastreams-metadata-poc
szaffarano Nov 4, 2024
6192f3d
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 4, 2024
0dcec8e
Add import
szaffarano Nov 4, 2024
a43ecb8
Use indices api instead of _cat
szaffarano Nov 6, 2024
4aab9bd
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 6, 2024
342703c
Add preview route
szaffarano Nov 7, 2024
5b746f3
Update EBT schema
szaffarano Nov 11, 2024
7605489
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 11, 2024
19f476a
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 18, 2024
a3416c8
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 25, 2024
22cc6ce
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Nov 26, 2024
c43ec74
Use alternative approach to paginate
szaffarano Dec 3, 2024
02650b0
Code style
szaffarano Dec 3, 2024
7ac516a
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Dec 3, 2024
932f89c
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Dec 9, 2024
d79fd7f
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Dec 11, 2024
ea57c6f
Add testing endpoint (revert this commit before merge)
szaffarano Dec 11, 2024
18b06e7
Add parameters to the endpoint
szaffarano Dec 11, 2024
4fb6ea3
Merge branch 'main' into szaffarano/datastreams-metadata-poc
elasticmachine Dec 13, 2024
ef230cb
Remove testing endpoint
szaffarano Dec 13, 2024
4cd915f
Update default indices threshold
szaffarano Dec 13, 2024
e6a9cbf
Reduce logging noise
szaffarano Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .buildkite/ftr_security_serverless_configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ enabled:
- x-pack/test/security_solution_api_integration/test_suites/detections_response/rules_management/rule_read/trial_license_complete_tier/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/rules_management/rule_read/basic_license_essentials_tier/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/telemetry/trial_license_complete_tier/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/telemetry/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/user_roles/trial_license_complete_tier/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/genai/nlp_cleanup_task/trial_license_complete_tier/configs/serverless.config.ts
- x-pack/test/security_solution_api_integration/test_suites/genai/nlp_cleanup_task/basic_license_essentials_tier/configs/serverless.config.ts
Expand Down
1 change: 1 addition & 0 deletions .buildkite/ftr_security_stateful_configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enabled:
- x-pack/test/security_solution_api_integration/test_suites/detections_response/rules_management/rule_read/trial_license_complete_tier/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/rules_management/rule_read/basic_license_essentials_tier/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/telemetry/trial_license_complete_tier/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/telemetry/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/detections_response/user_roles/trial_license_complete_tier/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/trial_license_complete_tier/configs/ess.config.ts
- x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/basic_license_essentials_tier/configs/ess.config.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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 Path from 'path';
import axios from 'axios';

import { cloneDeep } from 'lodash';

import { telemetryConfiguration } from '../lib/telemetry/configuration';
import {
TaskManagerPlugin,
type TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server/plugin';

import {
setupTestServers,
removeFile,
mockAxiosPost,
DEFAULT_GET_ROUTES,
mockAxiosGet,
getRandomInt,
} from './lib/helpers';

import {
type TestElasticsearchUtils,
type TestKibanaUtils,
} from '@kbn/core-test-helpers-kbn-server';
import { Plugin as SecuritySolutionPlugin } from '../plugin';
import { getTelemetryTasks, runSoonConfigTask } from './lib/telemetry_helpers';
import type { SecurityTelemetryTask } from '../lib/telemetry/task';

jest.mock('axios');

const logFilePath = Path.join(__dirname, 'config.logs.log');
const taskManagerStartSpy = jest.spyOn(TaskManagerPlugin.prototype, 'start');
const securitySolutionStartSpy = jest.spyOn(SecuritySolutionPlugin.prototype, 'start');

const mockedAxiosGet = jest.spyOn(axios, 'get');
const mockedAxiosPost = jest.spyOn(axios, 'post');

const securitySolutionPlugin = jest.spyOn(SecuritySolutionPlugin.prototype, 'start');

describe('configuration', () => {
let esServer: TestElasticsearchUtils;
let kibanaServer: TestKibanaUtils;
let taskManagerPlugin: TaskManagerStartContract;
let tasks: SecurityTelemetryTask[];

beforeAll(async () => {
await removeFile(logFilePath);

const servers = await setupTestServers(logFilePath);

esServer = servers.esServer;
kibanaServer = servers.kibanaServer;

expect(taskManagerStartSpy).toHaveBeenCalledTimes(1);
taskManagerPlugin = taskManagerStartSpy.mock.results[0].value;

expect(securitySolutionStartSpy).toHaveBeenCalledTimes(1);

tasks = getTelemetryTasks(securitySolutionStartSpy);

expect(securitySolutionPlugin).toHaveBeenCalledTimes(1);
});

afterAll(async () => {
if (kibanaServer) {
await kibanaServer.stop();
}
if (esServer) {
await esServer.stop();
}
});

beforeEach(async () => {
jest.clearAllMocks();
mockAxiosPost(mockedAxiosPost);
});

afterEach(async () => {});

describe('configuration task', () => {
it('should keep default values when no new config was provided', async () => {
const before = cloneDeep(telemetryConfiguration);

await runSoonConfigTask(tasks, taskManagerPlugin);

expect(telemetryConfiguration).toEqual(before);
});

it('should update values with new manifest', async () => {
const expected = {
telemetry_max_buffer_size: getRandomInt(1, 100),
max_security_list_telemetry_batch: getRandomInt(1, 100),
max_endpoint_telemetry_batch: getRandomInt(1, 100),
max_detection_rule_telemetry_batch: getRandomInt(1, 100),
max_detection_alerts_batch: getRandomInt(1, 100),
use_async_sender: true,
pagination_config: {
max_page_size_bytes: getRandomInt(1, 100),
num_docs_to_sample: getRandomInt(1, 100),
},
sender_channels: {
default: {
buffer_time_span_millis: getRandomInt(1, 100),
inflight_events_threshold: getRandomInt(1, 100),
max_payload_size_bytes: getRandomInt(1, 100),
},
},
indices_metadata_config: {
indices_threshold: getRandomInt(1, 100),
datastreams_threshold: getRandomInt(1, 100),
max_prefixes: getRandomInt(1, 100),
max_group_size: getRandomInt(1, 100),
},
};

mockAxiosGet(mockedAxiosGet, [
...DEFAULT_GET_ROUTES,
[/.*telemetry-buffer-and-batch-sizes-v1.*/, { status: 200, data: cloneDeep(expected) }],
]);

await runSoonConfigTask(tasks, taskManagerPlugin);

expect(telemetryConfiguration.telemetry_max_buffer_size).toEqual(
expected.telemetry_max_buffer_size
);
expect(telemetryConfiguration.max_security_list_telemetry_batch).toEqual(
expected.max_security_list_telemetry_batch
);
expect(telemetryConfiguration.max_endpoint_telemetry_batch).toEqual(
expected.max_endpoint_telemetry_batch
);
expect(telemetryConfiguration.max_detection_rule_telemetry_batch).toEqual(
expected.max_detection_rule_telemetry_batch
);
expect(telemetryConfiguration.max_detection_alerts_batch).toEqual(
expected.max_detection_alerts_batch
);
expect(telemetryConfiguration.use_async_sender).toEqual(expected.use_async_sender);
expect(telemetryConfiguration.sender_channels).toEqual(expected.sender_channels);
expect(telemetryConfiguration.pagination_config).toEqual(expected.pagination_config);
expect(telemetryConfiguration.indices_metadata_config).toEqual(
expected.indices_metadata_config
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import Util from 'util';
import type { ElasticsearchClient } from '@kbn/core/server';
import deepmerge from 'deepmerge';
import { createTestServers, createRootWithCorePlugins } from '@kbn/core-test-helpers-kbn-server';

export const DEFAULT_GET_ROUTES: Array<[RegExp, unknown]> = [
[new RegExp('.*/ping$'), { status: 200 }],
[
/.*kibana\/manifest\/artifacts.*/,
{
status: 200,
data: 'x-pack/plugins/security_solution/server/lib/telemetry/__mocks__/kibana-artifacts.zip',
},
],
];

export const DEFAULT_POST_ROUTES: Array<[RegExp, unknown]> = [[/.*/, { status: 200 }]];

const asyncUnlink = Util.promisify(Fs.unlink);

/**
Expand Down Expand Up @@ -127,3 +141,35 @@ export function updateTimestamps(data: object[]): object[] {
return { ...d, '@timestamp': new Date(currentTimeMillis + (i + 1) * 100) };
});
}

export function mockAxiosPost(
postSpy: jest.SpyInstance,
routes: Array<[RegExp, unknown]> = DEFAULT_POST_ROUTES
) {
postSpy.mockImplementation(async (url: string) => {
for (const [route, returnValue] of routes) {
if (route.test(url)) {
return returnValue;
}
}
return { status: 404 };
});
}

export function mockAxiosGet(
getSpy: jest.SpyInstance,
routes: Array<[RegExp, unknown]> = DEFAULT_GET_ROUTES
) {
getSpy.mockImplementation(async (url: string) => {
for (const [route, returnValue] of routes) {
if (route.test(url)) {
return returnValue;
}
}
return { status: 404 };
});
}

export function getRandomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import {
deleteExceptionListItem,
} from '@kbn/lists-plugin/server/services/exception_lists';
import { LEGACY_AGENT_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common/constants';
import type { TaskManagerStartContract } from '@kbn/task-manager-plugin/server';

import { packagePolicyService } from '@kbn/fleet-plugin/server/services';

import { ENDPOINT_ARTIFACT_LISTS } from '@kbn/securitysolution-list-constants';
import { DETECTION_TYPE, NAMESPACE_TYPE } from '@kbn/lists-plugin/common/constants.mock';
import { DEFAULT_DIAGNOSTIC_INDEX_PATTERN } from '../../../common/endpoint/constants';
import { bulkInsert, updateTimestamps } from './helpers';
import { bulkInsert, eventually, updateTimestamps } from './helpers';
import { TelemetryEventsSender } from '../../lib/telemetry/sender';
import type {
SecuritySolutionPluginStart,
Expand All @@ -41,6 +41,7 @@ import type { SecurityTelemetryTask } from '../../lib/telemetry/task';
import { Plugin as SecuritySolutionPlugin } from '../../plugin';
import { AsyncTelemetryEventsSender } from '../../lib/telemetry/async_sender';
import { type ITelemetryReceiver, TelemetryReceiver } from '../../lib/telemetry/receiver';
import { DEFAULT_DIAGNOSTIC_INDEX_PATTERN } from '../../../common/endpoint/constants';
import mockEndpointAlert from '../__mocks__/endpoint-alert.json';
import mockedRule from '../__mocks__/rule.json';
import fleetAgents from '../__mocks__/fleet-agents.json';
Expand Down Expand Up @@ -417,3 +418,24 @@ export function getTelemetryTaskType(task: SecurityTelemetryTask): string {
return '';
}
}

export async function runSoonConfigTask(
tasks: SecurityTelemetryTask[],
taskManagerPlugin: TaskManagerStartContract
) {
const configTaskType = 'security:telemetry-configuration';
const configTask = getTelemetryTask(tasks, configTaskType);
const runAfter = new Date();
await eventually(async () => {
await taskManagerPlugin.runSoon(configTask.getTaskId());
});

// wait until the task finishes
await eventually(async () => {
const hasRun = await taskManagerPlugin
.get(configTask.getTaskId())
.then((t) => new Date(t.state.lastExecutionTimestamp) > runAfter)
.catch(() => false);
expect(hasRun).toBe(true);
});
}
Loading