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

[Security Solution] Add missing alerts (signals) API endpoints OpenAPI specs #184838

Merged
merged 16 commits into from
Jun 18, 2024

Conversation

maximpn
Copy link
Contributor

@maximpn maximpn commented Jun 5, 2024

Addresses: #183661

Summary

This PR adds missing OpenAPI specs for the following API endpoints (Alerts API and Alerts Migration API) available in both Serverless and ESS

  • POST /api/detection_engine/signals/status
  • POST /api/detection_engine/signals/tags
  • POST /api/detection_engine/signals/search

and API endpoints available only in ESS

  • POST /api/detection_engine/signals/migration_status
  • POST /api/detection_engine/signals/migration
  • POST /api/detection_engine/signals/finalize_migration
  • DELETE /api/detection_engine/signals/migration

@maximpn maximpn added release_note:skip Skip the PR/issue when compiling release notes docs Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Detection Rule Management Security Detection Rule Management Team Team:Detection Engine Security Solution Detection Engine Area v8.15.0 labels Jun 5, 2024
@maximpn maximpn self-assigned this Jun 5, 2024
@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch 2 times, most recently from 6a3688a to 54c379b Compare June 7, 2024 11:04
@maximpn maximpn requested a review from marshallmain June 7, 2024 11:04
@maximpn
Copy link
Contributor Author

maximpn commented Jun 7, 2024

Hey @marcelhallmann,

Could you check added OpenAPI specs for correctness? Don't pay too much attention to summaries and descriptions since it will be further refined in #183702. I used some information from the documentation page.

@maximpn maximpn marked this pull request as ready for review June 7, 2024 11:07
@maximpn maximpn requested review from a team as code owners June 7, 2024 11:07
@maximpn maximpn requested a review from dplumlee June 7, 2024 11:07
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detections-response (Team:Detections and Resp)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-engine (Team:Detection Engine)

@maximpn maximpn removed the request for review from dplumlee June 7, 2024 11:08
@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch from 54c379b to 68e7e95 Compare June 7, 2024 13:37
paths: {}
components:
schemas:
GenericErrorResponse:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused here at first because the SiemResponseFactory uses status_code (https://github.com/elastic/kibana/blob/main/x-pack/plugins/security_solution/server/lib/detection_engine/routes/utils.ts#L184) and message as the response if we throw during the executor and return a 500. Can we add a response schema for the siemResponse as well, and maybe rename this one to PlatformErrorResponse?

conflicts: z.enum(['abort', 'proceed']).optional().default('abort'),
})
.merge(
z.union([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a type error here, can't merge a zod union with another object

});

export type CreateAlertsMigrationResponse = z.infer<typeof CreateAlertsMigrationResponse>;
export const CreateAlertsMigrationResponse = z.object({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migration response can have an error as part of the ok response: https://github.com/elastic/kibana/blob/main/x-pack/plugins/security_solution/server/lib/detection_engine/routes/signals/create_signals_migration_route.ts#L142
Looks like this response should be a union of the error and regular responses


export type CleanupAlertsMigrationRequestBody = z.infer<typeof CleanupAlertsMigrationRequestBody>;
export const CleanupAlertsMigrationRequestBody = z.object({
migration_ids: z.array(z.string()).min(1).optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be required

Comment on lines 37 to 38
message: z.string().optional(),
status_code: z.number().int().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message and status_code within error are not optional, though error is optional

tags:
- Alerts migration API
parameters:
- name: from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the codegen for this parameter?

Copy link
Contributor Author

@maximpn maximpn Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out request response types aren't generated when x-codegen-enabled is omitted but schemas are generated for common schemas. Since a valid spec may produce invalid code I explicitly added x-codegen-enabled: true.

Comment on lines 28 to 32
index: NonEmptyString.optional(),
version: z.number().int().optional(),
signal_versions: z.array(AlertVersion).optional(),
migrations: z.array(MigrationStatus).optional(),
is_outdated: z.boolean().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every field is required


export type GetAlertsMigrationStatusResponse = z.infer<typeof GetAlertsMigrationStatusResponse>;
export const GetAlertsMigrationStatusResponse = z.object({
indices: z.array(IndexMigrationStatus).optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indices is required

Comment on lines 37 to 38
version: z.number().int().optional(),
count: z.number().int().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are required

Comment on lines 43 to 46
id: NonEmptyString.optional(),
status: z.enum(['success', 'failure', 'pending']).optional(),
version: z.number().int().optional(),
updated: z.string().datetime().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All required

@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch from b5ac814 to 6952fc1 Compare June 8, 2024 10:00
@maximpn
Copy link
Contributor Author

maximpn commented Jun 8, 2024

@marshallmain thank you for the review!

I addressed your comments and also found a few things to fix. Explicitly enabled code generation via x-codegen-enabled: true forces code generator to produce request and response Zod schemas. I made it enabled in all added OAS files to verify generated code correctness. A valid OAS may produce an invalid TS file.

@maximpn maximpn requested a review from marshallmain June 8, 2024 10:03
@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch 2 times, most recently from 6759a2e to 25db960 Compare June 10, 2024 18:56
Copy link
Contributor

@marshallmain marshallmain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better, thanks for addressing the comments. I only see one remaining issue.

Comment on lines +24 to +32
migration_id: z.string(),
migration_index: z.string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match the API route behavior these should exist but be null for the error and skipped migration cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marshallmain,

OAS doesn't provide a way to specify explicit null fields. There is a nullable modifier which marks a type as nullable. In this particular case it's possible to declare a single response type instead of AlertsIndexMigrationSuccess, AlertsIndexMigrationError and SkippedAlertsIndexMigration with migration_id and migration_index being string or null. Such schema won't describe the response precisely neither.

What's the approach you expect here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed on slack and the approach is ok as is. Better to leave the properties out of the response schema than have them typed incorrectly or make the rest of the schema less precise.

@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch from 26a8c52 to a6e7e34 Compare June 18, 2024 06:02
@maximpn maximpn marked this pull request as ready for review June 18, 2024 07:56
@maximpn maximpn requested a review from a team as a code owner June 18, 2024 07:56
@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch from a6e7e34 to 5ace3d8 Compare June 18, 2024 08:01
@maximpn maximpn force-pushed the add-missing-alerts-api-oas branch from 5ace3d8 to 9ffe520 Compare June 18, 2024 08:56
@maximpn maximpn requested a review from banderror June 18, 2024 10:50
Copy link
Contributor

@marshallmain marshallmain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 5507 5506 -1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 13.6MB 13.6MB +9.5KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @maximpn

Copy link
Contributor

@banderror banderror left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule Management changes LGTM 👍

I did a high-level review of the whole diff and checked the changes owned by our team.

Can we please add the new folders to the CODEOWNERS file in a separate PR?

@maximpn maximpn merged commit b169a9c into elastic:main Jun 18, 2024
42 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jun 18, 2024
@maximpn maximpn deleted the add-missing-alerts-api-oas branch June 18, 2024 15:54
bhapas pushed a commit to bhapas/kibana that referenced this pull request Jun 18, 2024
…I specs (elastic#184838)

**Addresses:** elastic#183661

## Summary

This PR adds missing OpenAPI specs for the following API endpoints (Alerts API and Alerts Migration API) available in both Serverless and ESS

- `POST /api/detection_engine/signals/status`
- `POST /api/detection_engine/signals/tags`
- `POST /api/detection_engine/signals/search`

and API endpoints available only in ESS

- `POST /api/detection_engine/signals/migration_status`
- `POST /api/detection_engine/signals/migration`
- `POST /api/detection_engine/signals/finalize_migration`
- `DELETE /api/detection_engine/signals/migration`

**Note:** Code generation is enabled for the added specs to verify that it works and produces expected results. Generated Zod schemas and types aren't integrated in the route's code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting docs release_note:skip Skip the PR/issue when compiling release notes Team:Detection Engine Security Solution Detection Engine Area Team:Detection Rule Management Security Detection Rule Management Team Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants