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] only import beat_schema when needed #78708

Merged
merged 5 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -12,10 +12,13 @@ import { mockAuditbeatIndexField, mockFilebeatIndexField, mockPacketbeatIndexFie
describe('Index Fields', () => {
describe('formatIndexFields', () => {
test('Basic functionality', async () => {
const { fieldsBeat } = await import('../../utils/beat_schema/fields');

expect(
sortBy(
'name',
await formatIndexFields(
fieldsBeat,
[mockAuditbeatIndexField, mockFilebeatIndexField, mockPacketbeatIndexField],
['auditbeat', 'filebeat', 'packetbeat']
)
Expand Down Expand Up @@ -166,7 +169,10 @@ describe('Index Fields', () => {

describe('formatFirstFields', () => {
test('Basic functionality', async () => {
const { fieldsBeat } = await import('../../utils/beat_schema/fields');

const fields = await formatFirstFields(
fieldsBeat,
[mockAuditbeatIndexField, mockFilebeatIndexField, mockPacketbeatIndexField],
['auditbeat', 'filebeat', 'packetbeat']
);
Expand Down Expand Up @@ -747,8 +753,11 @@ describe('Index Fields', () => {
});

describe('createFieldItem', () => {
test('Basic functionality', () => {
const item = createFieldItem(
test('Basic functionality', async () => {
const { fieldsBeat } = await import('../../utils/beat_schema/fields');

const item = await createFieldItem(
fieldsBeat,
['auditbeat'],
{
name: '_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import {
IndexFieldsStrategyResponse,
IndexField,
IndexFieldsStrategyRequest,
BeatFields,
} from '../../../common/search_strategy/index_fields';

import { fieldsBeat } from '../../utils/beat_schema/fields';

export const securitySolutionIndexFieldsProvider = (): ISearchStrategy<
IndexFieldsStrategyRequest,
IndexFieldsStrategyResponse
> => {
// require the fields once we actually need them, rather than ahead of time, and pass
// them to createFieldItem to reduce the amount of work done as much as possible
// eslint-disable-next-line @typescript-eslint/no-var-requires
const beatFields: BeatFields = require('../../utils/beat_schema/fields').fieldsBeat;

return {
search: async (context, request) => {
const { elasticsearch } = context.core;
Expand All @@ -41,6 +45,7 @@ export const securitySolutionIndexFieldsProvider = (): ISearchStrategy<

if (!request.onlyCheckIfIndicesExist) {
indexFields = await formatIndexFields(
beatFields,
responsesIndexFields.filter((rif) => rif !== false) as FieldDescriptor[][],
dedupeIndices
);
Expand Down Expand Up @@ -116,6 +121,7 @@ const missingFields: FieldDescriptor[] = [
* @param indexesAliasIdx The index within the alias
*/
export const createFieldItem = (
beatFields: BeatFields,
indexesAlias: string[],
index: FieldDescriptor,
indexesAliasIdx: number
Expand All @@ -126,7 +132,7 @@ export const createFieldItem = (
splitIndexName[splitIndexName.length - 1] === 'text'
? splitIndexName.slice(0, splitIndexName.length - 1).join('.')
: index.name;
const beatIndex = fieldsBeat[indexName] ?? {};
const beatIndex = beatFields[indexName] ?? {};
if (isEmpty(beatIndex.category)) {
beatIndex.category = splitIndexName[0];
}
Expand All @@ -151,6 +157,7 @@ export const createFieldItem = (
* @param indexesAlias The index aliases such as filebeat-*
*/
export const formatFirstFields = async (
beatFields: BeatFields,
responsesIndexFields: FieldDescriptor[][],
indexesAlias: string[]
): Promise<IndexField[]> => {
Expand All @@ -160,11 +167,11 @@ export const formatFirstFields = async (
responsesIndexFields.reduce(
(accumulator: IndexField[], indexFields: FieldDescriptor[], indexesAliasIdx: number) => {
missingFields.forEach((index) => {
const item = createFieldItem(indexesAlias, index, indexesAliasIdx);
const item = createFieldItem(beatFields, indexesAlias, index, indexesAliasIdx);
accumulator.push(item);
});
indexFields.forEach((index) => {
const item = createFieldItem(indexesAlias, index, indexesAliasIdx);
const item = createFieldItem(beatFields, indexesAlias, index, indexesAliasIdx);
accumulator.push(item);
});
return accumulator;
Expand Down Expand Up @@ -224,10 +231,11 @@ export const formatSecondFields = async (fields: IndexField[]): Promise<IndexFie
* @param indexesAlias The index alias
*/
export const formatIndexFields = async (
beatFields: BeatFields,
responsesIndexFields: FieldDescriptor[][],
indexesAlias: string[]
): Promise<IndexField[]> => {
const fields = await formatFirstFields(responsesIndexFields, indexesAlias);
const fields = await formatFirstFields(beatFields, responsesIndexFields, indexesAlias);
const secondFields = await formatSecondFields(fields);
return secondFields;
};