Skip to content

Commit

Permalink
[Security Solution] Remove @ts-expect-error from v4.9.5 upgrade in De…
Browse files Browse the repository at this point in the history
…tection and Response areas (elastic#179273)

**Resolves: elastic#176287
**Resolves: elastic#176126

## Summary

In elastic#175178 Kibana was upgraded to
TypeScript v4.9.5. That PR introduced a few type errors which were
skipped by adding `@ts-expect-error` comments. This PR attempts to fix
those type errors and remove the comments.
  • Loading branch information
banderror authored Mar 27, 2024
1 parent 1837fa3 commit f96818a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ describe('threat_mapping', () => {
});

test('it should fail validation with an extra entry item', () => {
const payload: ThreatMappingEntries & Array<{ extra: string }> = [
const payload: Array<ThreatMappingEntries[0] & { extra: string }> = [
{
field: 'field.one',
type: 'mapping',
value: 'field.one',
// @ts-expect-error upgrade typescript v4.9.5
extra: 'blah',
},
];
Expand Down Expand Up @@ -112,7 +111,7 @@ describe('threat_mapping', () => {
});

test('it should fail validate with an extra key', () => {
const payload: ThreatMapping & Array<{ extra: string }> = [
const payload: Array<ThreatMapping[0] & { extra: string }> = [
{
entries: [
{
Expand All @@ -121,7 +120,6 @@ describe('threat_mapping', () => {
value: 'field.one',
},
],
// @ts-expect-error upgrade typescript v4.9.5
extra: 'invalid',
},
];
Expand All @@ -135,14 +133,13 @@ describe('threat_mapping', () => {
});

test('it should fail validate with an extra inner entry', () => {
const payload: ThreatMapping & Array<{ entries: Array<{ extra: string }> }> = [
const payload: Array<ThreatMapping[0] & { entries: Array<{ extra: string }> }> = [
{
entries: [
{
field: 'field.one',
type: 'mapping',
value: 'field.one',
// @ts-expect-error upgrade typescript v4.9.5
extra: 'blah',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { v4 as uuidv4 } from 'uuid';
*/
type NotArray<T> = T extends unknown[] ? never : T;
export const addIdToItem = <T>(item: NotArray<T>): T => {
// @ts-expect-error upgrade typescript v4.9.5
const maybeId: typeof item & { id?: string } = item;
const maybeId = item as typeof item & { id?: string };
if (maybeId.id != null) {
return item;
} else {
Expand All @@ -42,8 +41,7 @@ export const removeIdFromItem = <T>(
},
Exclude<keyof T, 'id'>
> => {
// @ts-expect-error upgrade typescript v4.9.5
const maybeId: typeof item & { id?: string } = item;
const maybeId = item as typeof item & { id?: string };
if (maybeId.id != null) {
const { id, ...noId } = maybeId;
return noId;
Expand Down
19 changes: 13 additions & 6 deletions x-pack/plugins/lists/server/routes/utils/build_siem_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* 2.0.
*/

import { CustomHttpResponseOptions, KibanaResponseFactory } from '@kbn/core/server';
import {
CustomHttpResponseOptions,
HttpResponsePayload,
KibanaResponseFactory,
ResponseError,
} from '@kbn/core/server';

/**
* Copied from x-pack/plugins/security_solution/server/lib/detection_engine/routes/utils.ts
Expand Down Expand Up @@ -48,22 +53,24 @@ const statusToErrorMessage = (
export class SiemResponseFactory {
constructor(private response: KibanaResponseFactory) {}

// @ts-expect-error upgrade typescript v4.9.5
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
error<T>({ statusCode, body, headers, bypassErrorFormat }: CustomHttpResponseOptions<T>) {
error<T extends HttpResponsePayload | ResponseError>({
statusCode,
body,
headers,
bypassErrorFormat,
}: CustomHttpResponseOptions<T>) {
// KibanaResponse is not exported so we cannot use a return type here and that is why the linter is turned off above
// @ts-expect-error upgrade typescript v4.9.5
const contentType: CustomHttpResponseOptions<T>['headers'] = {
'content-type': 'application/json',
};
// @ts-expect-error upgrade typescript v4.9.5
const defaultedHeaders: CustomHttpResponseOptions<T>['headers'] = {
...contentType,
...(headers ?? {}),
};

const formattedBody = bypassErrorFormat
? body
? Object.assign<{}, unknown>({}, body) // eslint-disable-line prefer-object-spread
: { message: body ?? statusToErrorMessage(statusCode) };

return this.response.custom({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
RouteValidationFunction,
KibanaResponseFactory,
CustomHttpResponseOptions,
HttpResponsePayload,
ResponseError,
} from '@kbn/core/server';

import { CustomHttpRequestError } from '../../../utils/custom_http_request_error';
Expand Down Expand Up @@ -160,13 +162,14 @@ const statusToErrorMessage = (statusCode: number) => {
export class SiemResponseFactory {
constructor(private response: KibanaResponseFactory) {}

// @ts-expect-error upgrade typescript v4.9.5
error<T>({ statusCode, body, headers }: CustomHttpResponseOptions<T>) {
// @ts-expect-error upgrade typescript v4.9.5
error<T extends HttpResponsePayload | ResponseError>({
statusCode,
body,
headers,
}: CustomHttpResponseOptions<T>) {
const contentType: CustomHttpResponseOptions<T>['headers'] = {
'content-type': 'application/json',
};
// @ts-expect-error upgrade typescript v4.9.5
const defaultedHeaders: CustomHttpResponseOptions<T>['headers'] = {
...contentType,
...(headers ?? {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { BulkActionEditTypeEnum } from '../../../../../../common/api/detection_e
* @param editAction {@link BulkActionEditType}
* @returns {boolean}
*/
export const isIndexPatternsBulkEditAction = (editAction: BulkActionEditType) =>
[
export const isIndexPatternsBulkEditAction = (editAction: BulkActionEditType) => {
const indexPatternsActions: BulkActionEditType[] = [
BulkActionEditTypeEnum.add_index_patterns,
BulkActionEditTypeEnum.delete_index_patterns,
BulkActionEditTypeEnum.set_index_patterns,
// @ts-expect-error upgrade typescript v4.9.5
].includes(editAction);
];
return indexPatternsActions.includes(editAction);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { invariant } from '../../../../../../common/utils/invariant';
import { isMlRule } from '../../../../../../common/machine_learning/helpers';
import { isEsqlRule } from '../../../../../../common/detection_engine/utils';
import { BulkActionsDryRunErrCode } from '../../../../../../common/constants';
import type { BulkActionEditPayload } from '../../../../../../common/api/detection_engine/rule_management';
import type {
BulkActionEditPayload,
BulkActionEditType,
} from '../../../../../../common/api/detection_engine/rule_management';
import { BulkActionEditTypeEnum } from '../../../../../../common/api/detection_engine/rule_management';
import type { RuleAlertType } from '../../../rule_schema';
import { isIndexPatternsBulkEditAction } from './utils';
Expand Down Expand Up @@ -99,12 +102,11 @@ export const validateBulkEditRule = async ({
* add_rule_actions, set_rule_actions can be applied to prebuilt/immutable rules
*/
const istEditApplicableToImmutableRule = (edit: BulkActionEditPayload[]): boolean => {
return edit.every(({ type }) =>
[BulkActionEditTypeEnum.set_rule_actions, BulkActionEditTypeEnum.add_rule_actions].includes(
// @ts-expect-error upgrade typescript v4.9.5
type
)
);
const applicableActions: BulkActionEditType[] = [
BulkActionEditTypeEnum.set_rule_actions,
BulkActionEditTypeEnum.add_rule_actions,
];
return edit.every(({ type }) => applicableActions.includes(type));
};

/**
Expand Down

0 comments on commit f96818a

Please sign in to comment.