-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Migrate remaining public Detection Engine APIs to…
… OpenAPI and code generation (#170330) **Related to: elastic/security-team#7491 ## Summary Migrated remaining public Detection Engine endpoints to OpenAPI schema and code generation: - `POST /api/detection_engine/rules/_bulk_action` - `GET /api/detection_engine/rules/_find` Also completed the migration of internal APIs: - `GET /internal/detection_engine/rules/{ruleId}/execution/events` - `GET /internal/detection_engine/rules/{ruleId}/execution/results` ### Other notable changes - Changed how we compose Zod error messages for unions, see `packages/kbn-zod-helpers/src/stringify_zod_error.ts`. Now we are trying to list the validation errors of all union members but limiting the total number of validation errors displayed to users. - Addressed some remaining `TODO https://github.com/elastic/security-team/issues/7491` - Removed dependencies of the risk engine and timelines on detection engine schemas - Removed outdated legacy rule schemas that are no longer in use - Added new schema helpers that work with query params: `BooleanFromString` and `ArrayFromString` ![image](https://github.com/elastic/kibana/assets/1938181/f4898f11-04e2-4c82-bce9-e662ba78f724) ![image](https://github.com/elastic/kibana/assets/1938181/235234e7-c86c-49a1-b39f-6f9f8dc780e7)
- Loading branch information
Showing
164 changed files
with
2,566 additions
and
2,582 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,34 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ArrayFromString } from './array_from_string'; | ||
import * as z from 'zod'; | ||
|
||
describe('ArrayFromString', () => { | ||
const itemsSchema = z.string(); | ||
|
||
it('should return an array when input is a string', () => { | ||
const result = ArrayFromString(itemsSchema).parse('a,b,c'); | ||
expect(result).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
it('should return an empty array when input is an empty string', () => { | ||
const result = ArrayFromString(itemsSchema).parse(''); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return the input as is when it is not a string', () => { | ||
const input = ['a', 'b', 'c']; | ||
const result = ArrayFromString(itemsSchema).parse(input); | ||
expect(result).toEqual(input); | ||
}); | ||
|
||
it('should throw an error when input is not a string or an array', () => { | ||
expect(() => ArrayFromString(itemsSchema).parse(123)).toThrow(); | ||
}); | ||
}); |
This file contains 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,24 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as z from 'zod'; | ||
|
||
/** | ||
* This is a helper schema to convert comma separated strings to arrays. Useful | ||
* for processing query params. | ||
* | ||
* @param schema Array items schema | ||
* @returns Array schema that accepts a comma-separated string as input | ||
*/ | ||
export function ArrayFromString<T extends z.ZodTypeAny>(schema: T) { | ||
return z.preprocess( | ||
(value: unknown) => | ||
typeof value === 'string' ? (value === '' ? [] : value.split(',')) : value, | ||
z.array(schema) | ||
); | ||
} |
This file contains 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,32 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { BooleanFromString } from './boolean_from_string'; | ||
|
||
describe('BooleanFromString', () => { | ||
it('should return true when input is "true"', () => { | ||
expect(BooleanFromString.parse('true')).toBe(true); | ||
}); | ||
|
||
it('should return false when input is "false"', () => { | ||
expect(BooleanFromString.parse('false')).toBe(false); | ||
}); | ||
|
||
it('should return true when input is true', () => { | ||
expect(BooleanFromString.parse(true)).toBe(true); | ||
}); | ||
|
||
it('should return false when input is false', () => { | ||
expect(BooleanFromString.parse(false)).toBe(false); | ||
}); | ||
|
||
it('should throw an error when input is not a boolean or "true" or "false"', () => { | ||
expect(() => BooleanFromString.parse('not a boolean')).toThrow(); | ||
expect(() => BooleanFromString.parse(42)).toThrow(); | ||
}); | ||
}); |
This file contains 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,24 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import * as z from 'zod'; | ||
|
||
/** | ||
* This is a helper schema to convert a boolean string ("true" or "false") to a | ||
* boolean. Useful for processing query params. | ||
* | ||
* Accepts "true" or "false" as strings, or a boolean. | ||
*/ | ||
export const BooleanFromString = z | ||
.enum(['true', 'false']) | ||
.or(z.boolean()) | ||
.transform((value) => { | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
return value === 'true'; | ||
}); |
This file contains 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 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,28 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as z from 'zod'; | ||
|
||
/** | ||
* Safely parse a payload against a schema, returning the output or undefined. | ||
* This method does not throw validation errors and is useful for validating | ||
* optional objects when we don't care about errors. | ||
* | ||
* @param payload Schema payload | ||
* @param schema Validation schema | ||
* @returns Schema output or undefined | ||
*/ | ||
export function safeParseResult<T extends z.ZodTypeAny>( | ||
payload: unknown, | ||
schema: T | ||
): T['_output'] | undefined { | ||
const result = schema.safeParse(payload); | ||
if (result.success) { | ||
return result.data; | ||
} | ||
} |
This file contains 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 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 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
35 changes: 35 additions & 0 deletions
35
x-pack/plugins/security_solution/common/api/detection_engine/model/pagination.gen.ts
This file contains 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,35 @@ | ||
/* | ||
* 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 'zod'; | ||
|
||
/* | ||
* NOTICE: Do not edit this file manually. | ||
* This file is automatically generated by the OpenAPI Generator, @kbn/openapi-generator. | ||
*/ | ||
|
||
/** | ||
* Page number | ||
*/ | ||
export type Page = z.infer<typeof Page>; | ||
export const Page = z.number().int().min(1); | ||
|
||
/** | ||
* Number of items per page | ||
*/ | ||
export type PerPage = z.infer<typeof PerPage>; | ||
export const PerPage = z.number().int().min(0); | ||
|
||
export type PaginationResult = z.infer<typeof PaginationResult>; | ||
export const PaginationResult = z.object({ | ||
page: Page, | ||
per_page: PerPage, | ||
/** | ||
* Total number of items | ||
*/ | ||
total: z.number().int().min(0), | ||
}); |
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/security_solution/common/api/detection_engine/model/pagination.schema.yaml
This file contains 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 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Pagination Schema | ||
version: 'not applicable' | ||
paths: {} | ||
components: | ||
x-codegen-enabled: true | ||
schemas: | ||
Page: | ||
type: integer | ||
minimum: 1 | ||
description: Page number | ||
PerPage: | ||
type: integer | ||
minimum: 0 | ||
description: Number of items per page | ||
PaginationResult: | ||
type: object | ||
properties: | ||
page: | ||
$ref: '#/components/schemas/Page' | ||
per_page: | ||
$ref: '#/components/schemas/PerPage' | ||
total: | ||
type: integer | ||
minimum: 0 | ||
description: Total number of items | ||
required: | ||
- page | ||
- per_page | ||
- total |
28 changes: 0 additions & 28 deletions
28
x-pack/plugins/security_solution/common/api/detection_engine/model/pagination.ts
This file was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.