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

[Lens] Do not crash data panel on invalid KQL query #70712

Merged
merged 5 commits into from
Jul 9, 2020
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 @@ -324,8 +324,11 @@ describe('IndexPattern Data Panel', () => {
};
}

async function testExistenceLoading(stateChanges?: unknown, propChanges?: unknown) {
const props = testProps();
async function testExistenceLoading(
stateChanges?: unknown,
propChanges?: unknown,
props = testProps()
) {
const inst = mountWithIntl(<IndexPatternDataPanel {...props} />);

await act(async () => {
Expand Down Expand Up @@ -536,6 +539,25 @@ describe('IndexPattern Data Panel', () => {
expect(core.http.post).toHaveBeenCalledTimes(2);
expect(overlapCount).toEqual(0);
});

it("should default to empty dsl if query can't be parsed", async () => {
const props = {
...testProps(),
query: {
language: 'kuery',
query: '@timestamp : NOT *',
},
};
await testExistenceLoading(undefined, undefined, props);

expect((props.core.http.post as jest.Mock).mock.calls[0][1].body).toContain(
JSON.stringify({
must_not: {
match_all: {},
},
})
);
});
});

describe('displaying field list', () => {
Expand Down
25 changes: 23 additions & 2 deletions x-pack/plugins/lens/public/indexpattern_datasource/datapanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { DataPublicPluginStart, EsQueryConfig, Query, Filter } from 'src/plugins/data/public';
import { DatasourceDataPanelProps, DataType, StateSetter } from '../types';
import { ChildDragDropProvider, DragContextState } from '../drag_drop';
import { FieldItem } from './field_item';
Expand Down Expand Up @@ -74,6 +74,27 @@ const fieldTypeNames: Record<DataType, string> = {
ip: i18n.translate('xpack.lens.datatypes.ipAddress', { defaultMessage: 'IP' }),
};

// Wrapper around esQuery.buildEsQuery, handling errors (e.g. because a query can't be parsed) by
// returning a query dsl object not matching anything
function buildSafeEsQuery(
indexPattern: IIndexPattern,
query: Query,
filters: Filter[],
queryConfig: EsQueryConfig
) {
try {
return esQuery.buildEsQuery(indexPattern, query, filters, queryConfig);
} catch (e) {
return {
bool: {
must_not: {
match_all: {},
},
},
};
}
}

export function IndexPatternDataPanel({
setState,
state,
Expand Down Expand Up @@ -106,7 +127,7 @@ export function IndexPatternDataPanel({
timeFieldName: indexPatterns[id].timeFieldName,
}));

const dslQuery = esQuery.buildEsQuery(
const dslQuery = buildSafeEsQuery(
indexPatterns[currentIndexPatternId] as IIndexPattern,
query,
filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ export const InnerFieldItem = function InnerFieldItem(props: FieldItemProps) {
className={`lnsFieldItem__info ${infoIsOpen ? 'lnsFieldItem__info-isOpen' : ''}`}
data-test-subj={`lnsFieldListPanelField-${field.name}`}
onClick={() => {
togglePopover();
if (exists) {
togglePopover();
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous behavior here was confusing, but actually sort of useful, where the click state could be used to debug issues with the exists logic. I don't think it needs to be changed here.

}
}}
onKeyPress={(event) => {
if (event.key === 'ENTER') {
if (exists && event.key === 'ENTER') {
togglePopover();
}
}}
Expand Down