Skip to content

Commit

Permalink
[SecuritySolution] Add enrichPolicyExecutionInterval to entity enable…
Browse files Browse the repository at this point in the history
…ment and init APIs (elastic#207374)

## Summary

Add `enrichPolicyExecutionInterval`param to entity enablement and init
APIs

### How to test?
* Start kibana
* Call the entity store enablement API with a short value for
`enrichPolicyExecutionInterval` param
* Check in the logs if the enrichment process is running frequently
* Clear the entity store
* Call the entity store enablement API without
`enrichPolicyExecutionInterval` param
* Check in the logs if the enrichment process is running less frequently

**Enable Entity store API call:**
```
POST kbn:/api/entity_store/enable {
  "enrichPolicyExecutionInterval": "10s"
}
```

**Init Entity store API call:**
```
POST kbn:/api/entity_store/engines/user/init {
  "enrichPolicyExecutionInterval": "10s"
}

```

**Enrich policy log message:**
```
   │ info [o.e.x.e.EnrichPolicyRunner] [...] Policy [entity_store_field_retention_user_default_v1.0.0]: Running enrich policy
```

### Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [ ] [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
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <[email protected]>
(cherry picked from commit 1ca4d96)

# Conflicts:
#	oas_docs/output/kibana.serverless.yaml
#	oas_docs/output/kibana.yaml
#	x-pack/solutions/security/plugins/security_solution/common/api/entity_analytics/entity_store/enable.gen.ts
#	x-pack/solutions/security/plugins/security_solution/common/api/entity_analytics/entity_store/enable.schema.yaml
#	x-pack/solutions/security/plugins/security_solution/docs/openapi/ess/security_solution_entity_analytics_api_2023_10_31.bundled.schema.yaml
#	x-pack/solutions/security/plugins/security_solution/docs/openapi/serverless/security_solution_entity_analytics_api_2023_10_31.bundled.schema.yaml
#	x-pack/solutions/security/plugins/security_solution/server/lib/entity_analytics/entity_store/entity_store_data_client.ts
  • Loading branch information
machadoum committed Jan 24, 2025
1 parent bd39b06 commit 7850b61
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 8 deletions.
7 changes: 7 additions & 0 deletions oas_docs/output/kibana.serverless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7567,6 +7567,8 @@ paths:
schema:
type: object
properties:
enrichPolicyExecutionInterval:
$ref: '#/components/schemas/Security_Entity_Analytics_API_Interval'
fieldHistoryLength:
default: 10
description: The number of historical values to keep for each field.
Expand Down Expand Up @@ -46875,6 +46877,11 @@ components:
required:
- dsl
- response
Security_Entity_Analytics_API_Interval:
description: Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
example: 1h
pattern: ^[1-9]\d*[smh]$
type: string
Security_Entity_Analytics_API_RiskEngineScheduleNowErrorResponse:
type: object
properties:
Expand Down
7 changes: 7 additions & 0 deletions oas_docs/output/kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13144,6 +13144,8 @@ paths:
schema:
type: object
properties:
enrichPolicyExecutionInterval:
$ref: '#/components/schemas/Security_Entity_Analytics_API_Interval'
fieldHistoryLength:
default: 10
description: The number of historical values to keep for each field.
Expand Down Expand Up @@ -35275,6 +35277,11 @@ components:
required:
- dsl
- response
Security_Entity_Analytics_API_Interval:
description: Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
example: 1h
pattern: ^[1-9]\d*[smh]$
type: string
Security_Entity_Analytics_API_RiskEngineScheduleNowErrorResponse:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ export const InspectQuery = z.object({
response: z.array(z.string()),
dsl: z.array(z.string()),
});

/**
* Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
*/
export type Interval = z.infer<typeof Interval>;
export const Interval = z.string().regex(/^[1-9]\d*[smh]$/);
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,8 @@ components:
required:
- dsl
- response
Interval:
type: string
description: Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
pattern: '^[1-9]\d*[smh]$' # any number except zero followed by one of the suffixes 's', 'm', 'h'
example: '1h'
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ paths:
$ref: './common.schema.yaml#/components/schemas/IndexPattern'
filter:
type: string
entityTypes:
type: array
items:
$ref: './common.schema.yaml#/components/schemas/EntityType'
enrichPolicyExecutionInterval:
$ref: './common.schema.yaml#/components/schemas/Interval'
responses:
'200':
description: Successful response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { z } from '@kbn/zod';

import { EntityType, IndexPattern, EngineDescriptor } from '../common.gen';
import { EntityType, IndexPattern, Interval, EngineDescriptor } from '../common.gen';

export type InitEntityEngineRequestParams = z.infer<typeof InitEntityEngineRequestParams>;
export const InitEntityEngineRequestParams = z.object({
Expand All @@ -35,6 +35,7 @@ export const InitEntityEngineRequestBody = z.object({
fieldHistoryLength: z.number().int().optional().default(10),
indexPattern: IndexPattern.optional(),
filter: z.string().optional(),
enrichPolicyExecutionInterval: Interval.optional(),
});
export type InitEntityEngineRequestBodyInput = z.input<typeof InitEntityEngineRequestBody>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ paths:
$ref: '../common.schema.yaml#/components/schemas/IndexPattern'
filter:
type: string
enrichPolicyExecutionInterval:
$ref: '../common.schema.yaml#/components/schemas/Interval'
responses:
'200':
description: Successful response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ paths:
schema:
type: object
properties:
enrichPolicyExecutionInterval:
$ref: '#/components/schemas/Interval'
fieldHistoryLength:
default: 10
description: The number of historical values to keep for each field.
Expand Down Expand Up @@ -1134,6 +1136,13 @@ components:
required:
- dsl
- response
Interval:
description: >-
Interval in which enrich policy runs. For example, `"1h"` means the rule
runs every hour.
example: 1h
pattern: ^[1-9]\d*[smh]$
type: string
RiskEngineScheduleNowErrorResponse:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ paths:
schema:
type: object
properties:
<<<<<<< HEAD
=======
enrichPolicyExecutionInterval:
$ref: '#/components/schemas/Interval'
entityTypes:
items:
$ref: '#/components/schemas/EntityType'
type: array
>>>>>>> 1ca4d967d92 ([SecuritySolution] Add enrichPolicyExecutionInterval to entity enablement and init APIs (#207374))
fieldHistoryLength:
default: 10
description: The number of historical values to keep for each field.
Expand Down Expand Up @@ -418,6 +427,8 @@ paths:
schema:
type: object
properties:
enrichPolicyExecutionInterval:
$ref: '#/components/schemas/Interval'
fieldHistoryLength:
default: 10
description: The number of historical values to keep for each field.
Expand Down Expand Up @@ -1134,6 +1145,13 @@ components:
required:
- dsl
- response
Interval:
description: >-
Interval in which enrich policy runs. For example, `"1h"` means the rule
runs every hour.
example: 1h
pattern: ^[1-9]\d*[smh]$
type: string
RiskEngineScheduleNowErrorResponse:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
import { CRITICALITY_VALUES } from '../asset_criticality/constants';
import { createEngineDescription } from './installation/engine_description';
import { convertToEntityManagerDefinition } from './entity_definitions/entity_manager_conversion';
import { DEFAULT_INTERVAL } from './task/constants';

// Workaround. TransformState type is wrong. The health type should be: TransformHealth from '@kbn/transform-plugin/common/types/transform_stats'
export interface TransformHealth extends estypes.TransformGetTransformStatsTransformStatsHealth {
Expand Down Expand Up @@ -200,7 +201,13 @@ export class EntityStoreDataClient {
}

public async enable(
{ indexPattern = '', filter = '', fieldHistoryLength = 10 }: InitEntityStoreRequestBody,
{
indexPattern = '',
filter = '',
fieldHistoryLength = 10,
entityTypes,
enrichPolicyExecutionInterval,
}: InitEntityStoreRequestBody,
{ pipelineDebugMode = false }: { pipelineDebugMode?: boolean } = {}
): Promise<InitEntityStoreResponse> {
if (!this.options.taskManager) {
Expand All @@ -216,7 +223,11 @@ export class EntityStoreDataClient {

const promises = enginesTypes.map((entity) =>
run(() =>
this.init(entity, { indexPattern, filter, fieldHistoryLength }, { pipelineDebugMode })
this.init(
entity,
{ indexPattern, filter, fieldHistoryLength, enrichPolicyExecutionInterval },
{ pipelineDebugMode }
)
)
);

Expand Down Expand Up @@ -274,7 +285,12 @@ export class EntityStoreDataClient {

public async init(
entityType: EntityType,
{ indexPattern = '', filter = '', fieldHistoryLength = 10 }: InitEntityEngineRequestBody,
{
indexPattern = '',
filter = '',
fieldHistoryLength = 10,
enrichPolicyExecutionInterval = DEFAULT_INTERVAL,
}: InitEntityEngineRequestBody,
{ pipelineDebugMode = false }: { pipelineDebugMode?: boolean } = {}
): Promise<InitEntityEngineResponse> {
const { experimentalFeatures } = this.options;
Expand Down Expand Up @@ -330,6 +346,7 @@ export class EntityStoreDataClient {
this.asyncSetup(
entityType,
fieldHistoryLength,
enrichPolicyExecutionInterval,
this.options.taskManager,
indexPattern,
filter,
Expand All @@ -345,6 +362,7 @@ export class EntityStoreDataClient {
private async asyncSetup(
entityType: EntityType,
fieldHistoryLength: number,
enrichPolicyExecutionInterval: string,
taskManager: TaskManagerStartContract,
indexPattern: string,
filter: string,
Expand Down Expand Up @@ -425,6 +443,7 @@ export class EntityStoreDataClient {
namespace,
logger,
taskManager,
interval: enrichPolicyExecutionInterval,
});
this.log(`debug`, entityType, `Started entity store field retention enrich task`);
this.log(`info`, entityType, `Entity store initialized`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
export const SCOPE = ['securitySolution'];
export const TYPE = 'entity_store:field_retention:enrichment';
export const VERSION = '1.0.0';
export const INTERVAL = '1h';
export const DEFAULT_INTERVAL = '1h';
export const TIMEOUT = '10m';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
stateSchemaByVersion,
type LatestTaskStateSchema as EntityStoreFieldRetentionTaskState,
} from './state';
import { INTERVAL, SCOPE, TIMEOUT, TYPE, VERSION } from './constants';
import { SCOPE, TIMEOUT, TYPE, VERSION } from './constants';
import type { EntityAnalyticsRoutesDeps } from '../../types';

import { executeFieldRetentionEnrichPolicy } from '../elasticsearch_assets';
Expand Down Expand Up @@ -120,10 +120,12 @@ export const startEntityStoreFieldRetentionEnrichTask = async ({
logger,
namespace,
taskManager,
interval,
}: {
logger: Logger;
namespace: string;
taskManager: TaskManagerStartContract;
interval: string;
}) => {
const taskId = getTaskId(namespace);
const log = logFactory(logger, taskId);
Expand All @@ -136,7 +138,7 @@ export const startEntityStoreFieldRetentionEnrichTask = async ({
taskType: getTaskName(),
scope: SCOPE,
schedule: {
interval: INTERVAL,
interval,
},
state: { ...defaultState, namespace },
params: { version: VERSION },
Expand Down Expand Up @@ -234,7 +236,7 @@ export const runTask = async ({

telemetry.reportEvent(FIELD_RETENTION_ENRICH_POLICY_EXECUTION_EVENT.eventType, {
duration: taskDurationInSeconds,
interval: INTERVAL,
interval: taskInstance.schedule?.interval,
});

// Track entity store usage
Expand Down

0 comments on commit 7850b61

Please sign in to comment.