Skip to content

Commit

Permalink
[Threat Hunting Investigations][OpenAPI] Use timeline's generated enu…
Browse files Browse the repository at this point in the history
…ms (elastic#189410)

## Summary

Fixes elastic/security-team#10132.

This PR is the first on in a series of PRs to adopt the newly generate
OpenAPI types in the timeline server code base. As a first step, we're
migrating to the newly generated enums only. This has almost no impact
on the schemas and is mostly a one-to-one change.

Despite there being changes in more than 150 files, the review should be
pretty straight-forward. The most changes come from the new distinction
of enums and the actual type of an enum. Meaning a lot of imports and
enum usages needed a simple change.

In some places I found duplicate or unused types and the OpenAPI types
still had a couple of minor mistakes.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent 769fb99 commit 13b15bd
Show file tree
Hide file tree
Showing 169 changed files with 1,398 additions and 1,212 deletions.
158 changes: 50 additions & 108 deletions x-pack/plugins/security_solution/common/api/timeline/model/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ import type { Maybe } from '../../../search_strategy';
import { Direction } from '../../../search_strategy';
import type { PinnedEvent } from '../pinned_events/pinned_events_route';
import { PinnedEventRuntimeType } from '../pinned_events/pinned_events_route';
// TODO https://github.com/elastic/security-team/issues/7491
import { ErrorSchema } from './error_schema';
import type { DataProviderType } from './components.gen';
import {
DataProviderTypeEnum,
RowRendererId,
RowRendererIdEnum,
SortFieldTimeline,
SortFieldTimelineEnum,
TemplateTimelineType,
TemplateTimelineTypeEnum,
TimelineStatus,
TimelineStatusEnum,
TimelineType,
TimelineTypeEnum,
} from './components.gen';

export {
DataProviderType,
DataProviderTypeEnum,
RowRendererId,
RowRendererIdEnum,
SortFieldTimeline,
SortFieldTimelineEnum,
TemplateTimelineType,
TemplateTimelineTypeEnum,
TimelineStatus,
TimelineStatusEnum,
TimelineType,
TimelineTypeEnum,
};

/**
* Outcome is a property of the saved object resolve api
Expand All @@ -40,8 +69,6 @@ export const SavedObjectResolveAliasPurpose = runtimeTypes.union([
runtimeTypes.literal('savedObjectImport'),
]);

import { ErrorSchema } from './error_schema';

export const BareNoteSchema = runtimeTypes.intersection([
runtimeTypes.type({
timelineId: runtimeTypes.string,
Expand Down Expand Up @@ -133,14 +160,9 @@ const SavedDataProviderQueryMatchRuntimeType = runtimeTypes.partial({
queryMatch: unionWithNullType(SavedDataProviderQueryMatchBasicRuntimeType),
});

export enum DataProviderType {
default = 'default',
template = 'template',
}

export const DataProviderTypeLiteralRt = runtimeTypes.union([
runtimeTypes.literal(DataProviderType.default),
runtimeTypes.literal(DataProviderType.template),
runtimeTypes.literal(DataProviderTypeEnum.default),
runtimeTypes.literal(DataProviderTypeEnum.template),
]);

const SavedDataProviderRuntimeType = runtimeTypes.partial({
Expand Down Expand Up @@ -251,93 +273,26 @@ export type Sort = runtimeTypes.TypeOf<typeof SavedSortRuntimeType>;
* Timeline Statuses
*/

export enum TimelineStatus {
active = 'active',
draft = 'draft',
immutable = 'immutable',
}

export const TimelineStatusLiteralRt = runtimeTypes.union([
runtimeTypes.literal(TimelineStatus.active),
runtimeTypes.literal(TimelineStatus.draft),
runtimeTypes.literal(TimelineStatus.immutable),
runtimeTypes.literal(TimelineStatusEnum.active),
runtimeTypes.literal(TimelineStatusEnum.draft),
runtimeTypes.literal(TimelineStatusEnum.immutable),
]);

const TimelineStatusLiteralWithNullRt = unionWithNullType(TimelineStatusLiteralRt);

export type TimelineStatusLiteralWithNull = runtimeTypes.TypeOf<
typeof TimelineStatusLiteralWithNullRt
>;

export enum RowRendererId {
/** event.kind: signal */
alert = 'alert',
/** endpoint alerts (created on the endpoint) */
alerts = 'alerts',
auditd = 'auditd',
auditd_file = 'auditd_file',
library = 'library',
netflow = 'netflow',
plain = 'plain',
registry = 'registry',
suricata = 'suricata',
system = 'system',
system_dns = 'system_dns',
system_endgame_process = 'system_endgame_process',
system_file = 'system_file',
system_fim = 'system_fim',
system_security_event = 'system_security_event',
system_socket = 'system_socket',
threat_match = 'threat_match',
zeek = 'zeek',
}
export const RowRendererCount = Object.keys(RowRendererIdEnum).length;
export const RowRendererValues = Object.values(RowRendererId.Values);

export const RowRendererCount = Object.keys(RowRendererId).length;

const RowRendererIdRuntimeType = stringEnum(RowRendererId, 'RowRendererId');
const RowRendererIdRuntimeType = stringEnum(RowRendererIdEnum, 'RowRendererId');

/**
* Timeline template type
*/

export enum TemplateTimelineType {
elastic = 'elastic',
custom = 'custom',
}

export const TemplateTimelineTypeLiteralRt = runtimeTypes.union([
runtimeTypes.literal(TemplateTimelineType.elastic),
runtimeTypes.literal(TemplateTimelineType.custom),
]);

export const TemplateTimelineTypeLiteralWithNullRt = unionWithNullType(
TemplateTimelineTypeLiteralRt
);

export type TemplateTimelineTypeLiteral = runtimeTypes.TypeOf<typeof TemplateTimelineTypeLiteralRt>;
export type TemplateTimelineTypeLiteralWithNull = runtimeTypes.TypeOf<
typeof TemplateTimelineTypeLiteralWithNullRt
>;

/*
* Timeline Types
* Timeline types
*/

export enum TimelineType {
default = 'default',
template = 'template',
}

export const TimelineTypeLiteralRt = runtimeTypes.union([
runtimeTypes.literal(TimelineType.template),
runtimeTypes.literal(TimelineType.default),
runtimeTypes.literal(TimelineTypeEnum.template),
runtimeTypes.literal(TimelineTypeEnum.default),
]);

export const TimelineTypeLiteralWithNullRt = unionWithNullType(TimelineTypeLiteralRt);

export type TimelineTypeLiteral = runtimeTypes.TypeOf<typeof TimelineTypeLiteralRt>;
export type TimelineTypeLiteralWithNull = runtimeTypes.TypeOf<typeof TimelineTypeLiteralWithNullRt>;

/**
* This is the response type
*/
Expand Down Expand Up @@ -483,18 +438,11 @@ export const TimelineErrorResponseType = runtimeTypes.union([
export type TimelineErrorResponse = runtimeTypes.TypeOf<typeof TimelineErrorResponseType>;
export type TimelineResponse = runtimeTypes.TypeOf<typeof TimelineResponseType>;

export enum SortFieldTimeline {
title = 'title',
description = 'description',
updated = 'updated',
created = 'created',
}

export const sortFieldTimeline = runtimeTypes.union([
runtimeTypes.literal(SortFieldTimeline.title),
runtimeTypes.literal(SortFieldTimeline.description),
runtimeTypes.literal(SortFieldTimeline.updated),
runtimeTypes.literal(SortFieldTimeline.created),
runtimeTypes.literal(SortFieldTimelineEnum.title),
runtimeTypes.literal(SortFieldTimelineEnum.description),
runtimeTypes.literal(SortFieldTimelineEnum.updated),
runtimeTypes.literal(SortFieldTimelineEnum.created),
]);

export const direction = runtimeTypes.union([
Expand Down Expand Up @@ -569,7 +517,6 @@ export const pageInfoTimeline = runtimeTypes.type({

export interface PageInfoTimeline {
pageIndex: number;

pageSize: number;
}

Expand Down Expand Up @@ -661,16 +608,16 @@ export interface SerializedFilterQueryResult {
filterQuery?: Maybe<SerializedKueryQueryResult>;
}

export interface SerializedKueryQueryResult {
kuery?: Maybe<KueryFilterQueryResult>;
serializedQuery?: Maybe<string>;
}

export interface KueryFilterQueryResult {
kind?: Maybe<string>;
expression?: Maybe<string>;
}

export interface SerializedKueryQueryResult {
kuery?: Maybe<KueryFilterQueryResult>;
serializedQuery?: Maybe<string>;
}

export interface TimelineResult {
columns?: Maybe<ColumnHeaderResult[]>;
created?: Maybe<number>;
Expand Down Expand Up @@ -717,11 +664,6 @@ export interface SortTimeline {
sortOrder: Direction;
}

export interface ExportTimelineNotFoundError {
statusCode: number;
message: string;
}

export interface GetAllTimelineVariables {
pageInfo: PageInfoTimeline;
search?: Maybe<string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* version: not applicable
*/

import type { ZodTypeDef } from 'zod';
import { z } from 'zod';

/**
Expand All @@ -33,6 +32,14 @@ export const DataProviderType = z.enum(['default', 'template']);
export type DataProviderTypeEnum = typeof DataProviderType.enum;
export const DataProviderTypeEnum = DataProviderType.enum;

/**
* The type of the timeline template.
*/
export type TemplateTimelineType = z.infer<typeof TemplateTimelineType>;
export const TemplateTimelineType = z.enum(['elastic', 'custom']);
export type TemplateTimelineTypeEnum = typeof TemplateTimelineType.enum;
export const TemplateTimelineTypeEnum = TemplateTimelineType.enum;

export type ColumnHeaderResult = z.infer<typeof ColumnHeaderResult>;
export const ColumnHeaderResult = z.object({
aggregatable: z.boolean().optional(),
Expand All @@ -50,46 +57,33 @@ export const ColumnHeaderResult = z.object({

export type QueryMatchResult = z.infer<typeof QueryMatchResult>;
export const QueryMatchResult = z.object({
field: z.string().optional(),
displayField: z.string().optional(),
value: z.string().optional(),
displayValue: z.string().optional(),
operator: z.string().optional(),
field: z.string().nullable().optional(),
displayField: z.string().nullable().optional(),
value: z.string().nullable().optional(),
displayValue: z.string().nullable().optional(),
operator: z.string().nullable().optional(),
});

export interface DataProviderResult {
id?: string;
name?: string;
enabled?: boolean;
excluded?: boolean;
kqlQuery?: string;
queryMatch?: QueryMatchResult;
and?: DataProviderResult[];
type?: DataProviderType;
}
export interface DataProviderResultInput {
id?: string;
name?: string;
enabled?: boolean;
excluded?: boolean;
kqlQuery?: string;
queryMatch?: QueryMatchResult;
and?: DataProviderResultInput[];
type?: DataProviderType;
}
export const DataProviderResult: z.ZodType<
DataProviderResult,
ZodTypeDef,
DataProviderResultInput
> = z.object({
id: z.string().optional(),
name: z.string().optional(),
enabled: z.boolean().optional(),
excluded: z.boolean().optional(),
kqlQuery: z.string().optional(),
export type DataProviderQueryMatch = z.infer<typeof DataProviderQueryMatch>;
export const DataProviderQueryMatch = z.object({
enabled: z.boolean().nullable().optional(),
excluded: z.boolean().nullable().optional(),
id: z.string().nullable().optional(),
kqlQuery: z.string().nullable().optional(),
name: z.string().nullable().optional(),
queryMatch: QueryMatchResult.optional(),
and: z.array(z.lazy(() => DataProviderResult)).optional(),
type: DataProviderType.optional(),
});

export type DataProviderResult = z.infer<typeof DataProviderResult>;
export const DataProviderResult = z.object({
and: z.array(DataProviderQueryMatch).nullable().optional(),
enabled: z.boolean().nullable().optional(),
excluded: z.boolean().nullable().optional(),
id: z.string().nullable().optional(),
kqlQuery: z.string().nullable().optional(),
name: z.string().nullable().optional(),
queryMatch: QueryMatchResult.nullable().optional(),
type: DataProviderType.nullable().optional(),
});

export type RowRendererId = z.infer<typeof RowRendererId>;
Expand Down Expand Up @@ -154,31 +148,33 @@ export const SerializedFilterQueryResult = z.object({
.object({
kuery: z
.object({
kind: z.string().optional(),
expression: z.string().optional(),
kind: z.string().nullable().optional(),
expression: z.string().nullable().optional(),
})
.nullable()
.optional(),
serializedQuery: z.string().optional(),
serializedQuery: z.string().nullable().optional(),
})
.nullable()
.optional(),
});

export type Sort = z.infer<typeof Sort>;
export const Sort = z.object({
export type SortObject = z.infer<typeof SortObject>;
export const SortObject = z.object({
columnId: z.string().nullable().optional(),
columnType: z.string().nullable().optional(),
sortDirection: z.string().nullable().optional(),
});

export type Sort = z.infer<typeof Sort>;
export const Sort = z.union([SortObject, z.array(SortObject)]);

export type SavedTimeline = z.infer<typeof SavedTimeline>;
export const SavedTimeline = z.object({
columns: ColumnHeaderResult.nullable().optional(),
columns: z.array(ColumnHeaderResult).nullable().optional(),
created: z.number().nullable().optional(),
createdBy: z.string().nullable().optional(),
dataProviders: z
.array(z.lazy(() => DataProviderResult))
.nullable()
.optional(),
dataProviders: z.array(DataProviderResult).nullable().optional(),
dataViewId: z.string().nullable().optional(),
dateRange: z
.object({
Expand All @@ -190,9 +186,11 @@ export const SavedTimeline = z.object({
description: z.string().nullable().optional(),
eqlOptions: z
.object({
eventCategoryField: z.string().optional(),
tiebreakerField: z.string().optional(),
timestampField: z.string().optional(),
eventCategoryField: z.string().nullable().optional(),
query: z.string().nullable().optional(),
size: z.union([z.string().nullable(), z.number().nullable()]).optional(),
tiebreakerField: z.string().nullable().optional(),
timestampField: z.string().nullable().optional(),
})
.nullable()
.optional(),
Expand Down
Loading

0 comments on commit 13b15bd

Please sign in to comment.