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

[Response Ops] Removing usage of deprecated apis #127883

Merged
merged 5 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* 2.0.
*/

import { nodeBuilder } from '@kbn/es-query';
import { Logger, CoreStart } from 'kibana/server';
import { ActionsConfig } from '../config';
import { ActionsPluginsStart } from '../plugin';
import { ActionTypeRegistryContract } from '../types';
import { cleanupTasks, CleanupTasksResult } from './cleanup_tasks';
import { TaskInstance } from '../../../task_manager/server';
import { nodeBuilder } from '../../../../../src/plugins/data/common';

export interface FindAndCleanupTasksOpts {
logger: Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { fromKueryExpression } from '@kbn/es-query';
import { KibanaRequest } from 'kibana/server';
import { ruleTypeRegistryMock } from '../rule_type_registry.mock';
import { securityMock } from '../../../../plugins/security/server/mocks';
Expand All @@ -22,7 +23,6 @@ import {
import uuid from 'uuid';
import { RecoveredActionGroup } from '../../common';
import { RegistryRuleType } from '../rule_type_registry';
import { esKuery } from '../../../../../src/plugins/data/server';
import { AlertingAuthorizationFilterType } from './alerting_authorization_kuery';

const ruleTypeRegistry = ruleTypeRegistryMock.create();
Expand Down Expand Up @@ -839,7 +839,7 @@ describe('AlertingAuthorization', () => {
})
).filter
).toEqual(
esKuery.fromKueryExpression(
fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)) or (path.to.rule_type_id:myOtherAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)) or (path.to.rule_type_id:mySecondAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)))`
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ensureFieldIsSafeForQuery,
asFiltersBySpaceId,
} from './alerting_authorization_kuery';
import { esKuery } from '../../../../../src/plugins/data/server';
import { fromKueryExpression } from '@kbn/es-query';

describe('asKqlFiltersByRuleTypeAndConsumer', () => {
test('constructs KQL filter for single rule type with single authorized consumer', async () => {
Expand Down Expand Up @@ -44,9 +44,7 @@ describe('asKqlFiltersByRuleTypeAndConsumer', () => {
'space1'
)
).toEqual(
esKuery.fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(myApp)))`
)
fromKueryExpression(`((path.to.rule_type_id:myAppAlertType and consumer-field:(myApp)))`)
);
});

Expand Down Expand Up @@ -81,7 +79,7 @@ describe('asKqlFiltersByRuleTypeAndConsumer', () => {
'space1'
)
).toEqual(
esKuery.fromKueryExpression(
fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(alerts or myApp or myOtherApp)))`
)
);
Expand Down Expand Up @@ -153,7 +151,7 @@ describe('asKqlFiltersByRuleTypeAndConsumer', () => {
'space1'
)
).toEqual(
esKuery.fromKueryExpression(
fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)) or (path.to.rule_type_id:myOtherAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)) or (path.to.rule_type_id:mySecondAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)))`
)
);
Expand Down Expand Up @@ -209,7 +207,7 @@ describe('asKqlFiltersByRuleTypeAndConsumer', () => {
'space1'
)
).toEqual(
esKuery.fromKueryExpression(
fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature) and path.to.spaceIds:space1) or (path.to.rule_type_id:myOtherAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature) and path.to.spaceIds:space1))`
)
);
Expand Down Expand Up @@ -265,7 +263,7 @@ describe('asKqlFiltersByRuleTypeAndConsumer', () => {
undefined
)
).toEqual(
esKuery.fromKueryExpression(
fromKueryExpression(
`((path.to.rule_type_id:myAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)) or (path.to.rule_type_id:myOtherAppAlertType and consumer-field:(alerts or myApp or myOtherApp or myAppWithSubFeature)))`
)
);
Expand Down Expand Up @@ -638,7 +636,7 @@ describe('asFiltersBySpaceId', () => {
},
'space1'
)
).toEqual(esKuery.fromKueryExpression('(path.to.space.id: space1)'));
).toEqual(fromKueryExpression('(path.to.space.id: space1)'));
});

test('returns undefined if no path to spaceIds is provided', () => {
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Semver from 'semver';
import Boom from '@hapi/boom';
import { omit, isEqual, map, uniq, pick, truncate, trim, mapValues } from 'lodash';
import { i18n } from '@kbn/i18n';
import { fromKueryExpression, KueryNode, nodeBuilder } from '@kbn/es-query';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import {
Logger,
Expand All @@ -19,7 +20,6 @@ import {
SavedObjectsUtils,
SavedObjectAttributes,
} from '../../../../../src/core/server';
import { esKuery } from '../../../../../src/plugins/data/server';
import { ActionsClient, ActionsAuthorization } from '../../../actions/server';
import {
Alert as Rule,
Expand Down Expand Up @@ -71,7 +71,6 @@ import { retryIfConflicts } from '../lib/retry_if_conflicts';
import { partiallyUpdateAlert } from '../saved_objects';
import { markApiKeyForInvalidation } from '../invalidate_pending_api_keys/mark_api_key_for_invalidation';
import { ruleAuditEvent, RuleAuditAction } from './audit_events';
import { KueryNode, nodeBuilder } from '../../../../../src/plugins/data/common';
import { mapSortField, validateOperationOnAttributes } from './lib';
import { getRuleExecutionStatusPending } from '../lib/rule_execution_status';
import { Alert } from '../alert';
Expand Down Expand Up @@ -652,7 +651,7 @@ export class RulesClient {
}

const { filter: authorizationFilter, ensureRuleTypeIsAuthorized } = authorizationTuple;
const filterKueryNode = options.filter ? esKuery.fromKueryExpression(options.filter) : null;
const filterKueryNode = options.filter ? fromKueryExpression(options.filter) : null;
let sortField = mapSortField(options.sortField);
if (excludeFromPublicApi) {
try {
Expand Down Expand Up @@ -768,7 +767,7 @@ export class RulesClient {
...options,
filter:
(authorizationFilter && filter
? nodeBuilder.and([esKuery.fromKueryExpression(filter), authorizationFilter as KueryNode])
? nodeBuilder.and([fromKueryExpression(filter), authorizationFilter as KueryNode])
: authorizationFilter) ?? filter,
page: 1,
perPage: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { savedObjectsClientMock, loggingSystemMock } from '../../../../../../src
import { taskManagerMock } from '../../../../task_manager/server/mocks';
import { ruleTypeRegistryMock } from '../../rule_type_registry.mock';
import { alertingAuthorizationMock } from '../../authorization/alerting_authorization.mock';
import { nodeTypes } from '@kbn/es-query';
import { esKuery } from '../../../../../../src/plugins/data/server';
import { nodeTypes, fromKueryExpression } from '@kbn/es-query';
import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks';
import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertingAuthorization } from '../../authorization/alerting_authorization';
Expand Down Expand Up @@ -291,7 +290,7 @@ describe('find()', () => {
});

test('should translate filter/sort/search on params to mapped_params', async () => {
const filter = esKuery.fromKueryExpression(
const filter = fromKueryExpression(
'((alert.attributes.alertTypeId:myType and alert.attributes.consumer:myApp) or (alert.attributes.alertTypeId:myOtherType and alert.attributes.consumer:myApp) or (alert.attributes.alertTypeId:myOtherType and alert.attributes.consumer:myOtherApp))'
);
authorization.getFindAuthorizationFilter.mockResolvedValue({
Expand Down Expand Up @@ -650,7 +649,7 @@ describe('find()', () => {

describe('authorization', () => {
test('ensures user is query filter types down to those the user is authorized to find', async () => {
const filter = esKuery.fromKueryExpression(
const filter = fromKueryExpression(
'((alert.attributes.alertTypeId:myType and alert.attributes.consumer:myApp) or (alert.attributes.alertTypeId:myOtherType and alert.attributes.consumer:myApp) or (alert.attributes.alertTypeId:myOtherType and alert.attributes.consumer:myOtherApp))'
);
authorization.getFindAuthorizationFilter.mockResolvedValue({
Expand All @@ -663,7 +662,7 @@ describe('find()', () => {

const [options] = unsecuredSavedObjectsClient.find.mock.calls[0];
expect(options.filter).toEqual(
nodeTypes.function.buildNode('and', [esKuery.fromKueryExpression('someTerm'), filter])
nodeTypes.function.buildNode('and', [fromKueryExpression('someTerm'), filter])
);
expect(authorization.getFindAuthorizationFilter).toHaveBeenCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import { ES_GEO_SHAPE_TYPES, GeoContainmentAlertParams } from '../../types';
import { GeoIndexPatternSelect } from '../util_components/geo_index_pattern_select';
import { SingleFieldSelect } from '../util_components/single_field_select';
import { ExpressionWithPopover } from '../util_components/expression_with_popover';
import { IFieldType } from '../../../../../../../../src/plugins/data/common';
import { IIndexPattern } from '../../../../../../../../src/plugins/data/common';
import { DataViewField, DataView } from '../../../../../../../../src/plugins/data/common';

interface Props {
ruleParams: GeoContainmentAlertParams;
errors: IErrorObject;
boundaryIndexPattern: IIndexPattern;
boundaryIndexPattern: DataView;
boundaryNameField?: string;
setBoundaryIndexPattern: (boundaryIndexPattern?: IIndexPattern) => void;
setBoundaryIndexPattern: (boundaryIndexPattern?: DataView) => void;
setBoundaryGeoField: (boundaryGeoField?: string) => void;
setBoundaryNameField: (boundaryNameField?: string) => void;
data: DataPublicPluginStart;
Expand All @@ -50,10 +49,10 @@ export const BoundaryIndexExpression: FunctionComponent<Props> = ({
const IndexPatternSelect = (data.ui && data.ui.IndexPatternSelect) || null;
const { boundaryGeoField } = ruleParams;
// eslint-disable-next-line react-hooks/exhaustive-deps
const nothingSelected: IFieldType = {
const nothingSelected: DataViewField = {
name: '<nothing selected>',
type: 'string',
};
} as DataViewField;

const usePrevious = <T extends unknown>(value: T): T | undefined => {
const ref = useRef<T>();
Expand All @@ -65,17 +64,18 @@ export const BoundaryIndexExpression: FunctionComponent<Props> = ({

const oldIndexPattern = usePrevious(boundaryIndexPattern);
const fields = useRef<{
geoFields: IFieldType[];
boundaryNameFields: IFieldType[];
geoFields: DataViewField[];
boundaryNameFields: DataViewField[];
}>({
geoFields: [],
boundaryNameFields: [],
});
useEffect(() => {
if (oldIndexPattern !== boundaryIndexPattern) {
fields.current.geoFields =
(boundaryIndexPattern.fields.length &&
boundaryIndexPattern.fields.filter((field: IFieldType) =>
(boundaryIndexPattern.fields &&
boundaryIndexPattern.fields.length &&
boundaryIndexPattern.fields.filter((field: DataViewField) =>
ES_GEO_SHAPE_TYPES.includes(field.type)
)) ||
[];
Expand All @@ -84,7 +84,7 @@ export const BoundaryIndexExpression: FunctionComponent<Props> = ({
}

fields.current.boundaryNameFields = [
...boundaryIndexPattern.fields.filter((field: IFieldType) => {
...(boundaryIndexPattern.fields ?? []).filter((field: DataViewField) => {
return (
BOUNDARY_NAME_ENTITY_TYPES.includes(field.type) &&
!field.name.startsWith('_') &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { EntityByExpression, getValidIndexPatternFields } from './entity_by_expression';
import { DataViewField } from 'src/plugins/data_views/public';

const defaultProps = {
errors: {
Expand Down Expand Up @@ -68,7 +69,7 @@ const defaultProps = {
aggregatable: true,
readFromDocValues: true,
},
],
] as DataViewField[],
isInvalid: false,
};

Expand All @@ -89,6 +90,6 @@ test('should only use valid index fields', async () => {
aggregatable: false,
}));

const noIndexFields = getValidIndexPatternFields(invalidIndexFields);
const noIndexFields = getValidIndexPatternFields(invalidIndexFields as DataViewField[]);
expect(noIndexFields.length).toEqual(0);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import React, { FunctionComponent, useEffect, useRef } from 'react';
import { EuiFormRow } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import _ from 'lodash';
import { DataViewField } from 'src/plugins/data_views/public';
import { IErrorObject } from '../../../../../../triggers_actions_ui/public';
import { SingleFieldSelect } from '../util_components/single_field_select';
import { ExpressionWithPopover } from '../util_components/expression_with_popover';
import { IFieldType } from '../../../../../../../../src/plugins/data/common';

interface Props {
errors: IErrorObject;
entity: string;
setAlertParamsEntity: (entity: string) => void;
indexFields: IFieldType[];
indexFields: DataViewField[];
isInvalid: boolean;
}

const ENTITY_TYPES = ['string', 'number', 'ip'];
export function getValidIndexPatternFields(fields: IFieldType[]): IFieldType[] {
export function getValidIndexPatternFields(fields: DataViewField[]): DataViewField[] {
return fields.filter((field) => {
const isSpecifiedSupportedField = ENTITY_TYPES.includes(field.type);
const hasLeadingUnderscore = field.name.startsWith('_');
Expand All @@ -49,7 +49,7 @@ export const EntityByExpression: FunctionComponent<Props> = ({

const oldIndexFields = usePrevious(indexFields);
const fields = useRef<{
indexFields: IFieldType[];
indexFields: DataViewField[];
}>({
indexFields: [],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { ES_GEO_FIELD_TYPES } from '../../types';
import { GeoIndexPatternSelect } from '../util_components/geo_index_pattern_select';
import { SingleFieldSelect } from '../util_components/single_field_select';
import { ExpressionWithPopover } from '../util_components/expression_with_popover';
import { IFieldType } from '../../../../../../../../src/plugins/data/common';
import { IIndexPattern } from '../../../../../../../../src/plugins/data/common';
import { DataViewField, DataView } from '../../../../../../../../src/plugins/data/common';

interface Props {
dateField: string;
Expand All @@ -30,8 +29,8 @@ interface Props {
setAlertParamsDate: (date: string) => void;
setAlertParamsGeoField: (geoField: string) => void;
setRuleProperty: RuleTypeParamsExpressionProps['setRuleProperty'];
setIndexPattern: (indexPattern: IIndexPattern) => void;
indexPattern: IIndexPattern;
setIndexPattern: (indexPattern: DataView) => void;
indexPattern: DataView;
isInvalid: boolean;
data: DataPublicPluginStart;
}
Expand Down Expand Up @@ -64,17 +63,18 @@ export const EntityIndexExpression: FunctionComponent<Props> = ({

const oldIndexPattern = usePrevious(indexPattern);
const fields = useRef<{
dateFields: IFieldType[];
geoFields: IFieldType[];
dateFields: DataViewField[];
geoFields: DataViewField[];
}>({
dateFields: [],
geoFields: [],
});
useEffect(() => {
if (oldIndexPattern !== indexPattern) {
fields.current.geoFields =
(indexPattern.fields.length &&
indexPattern.fields.filter((field: IFieldType) =>
(indexPattern.fields &&
indexPattern.fields.length &&
indexPattern.fields.filter((field: DataViewField) =>
ES_GEO_FIELD_TYPES.includes(field.type)
)) ||
[];
Expand All @@ -83,8 +83,9 @@ export const EntityIndexExpression: FunctionComponent<Props> = ({
}

fields.current.dateFields =
(indexPattern.fields.length &&
indexPattern.fields.filter((field: IFieldType) => field.type === 'date')) ||
(indexPattern.fields &&
indexPattern.fields.length &&
indexPattern.fields.filter((field: DataViewField) => field.type === 'date')) ||
[];
if (fields.current.dateFields.length) {
setAlertParamsDate(fields.current.dateFields[0].name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { shallow } from 'enzyme';
import { EntityIndexExpression } from './expressions/entity_index_expression';
import { BoundaryIndexExpression } from './expressions/boundary_index_expression';
import { IErrorObject } from '../../../../../triggers_actions_ui/public';
import { IIndexPattern } from '../../../../../../../src/plugins/data/common';
import { DataView } from '../../../../../../../src/plugins/data/common';
import { dataPluginMock } from 'src/plugins/data/public/mocks';

const dataStartMock = dataPluginMock.createStartContract();
Expand All @@ -37,7 +37,7 @@ test('should render EntityIndexExpression', async () => {
setAlertParamsGeoField={() => {}}
setRuleProperty={() => {}}
setIndexPattern={() => {}}
indexPattern={'' as unknown as IIndexPattern}
indexPattern={'' as unknown as DataView}
isInvalid={false}
data={dataStartMock}
/>
Expand All @@ -56,7 +56,7 @@ test('should render EntityIndexExpression w/ invalid flag if invalid', async ()
setAlertParamsGeoField={() => {}}
setRuleProperty={() => {}}
setIndexPattern={() => {}}
indexPattern={'' as unknown as IIndexPattern}
indexPattern={'' as unknown as DataView}
isInvalid={true}
data={dataStartMock}
/>
Expand All @@ -70,7 +70,7 @@ test('should render BoundaryIndexExpression', async () => {
<BoundaryIndexExpression
ruleParams={alertParams}
errors={{} as IErrorObject}
boundaryIndexPattern={'' as unknown as IIndexPattern}
boundaryIndexPattern={'' as unknown as DataView}
setBoundaryIndexPattern={() => {}}
setBoundaryGeoField={() => {}}
setBoundaryNameField={() => {}}
Expand Down
Loading