Skip to content

Commit

Permalink
updates warning messages and modifies warning message when endpoint s…
Browse files Browse the repository at this point in the history
…ecurity rule is missing index pattern
  • Loading branch information
dhurley14 committed Mar 1, 2021
1 parent 4b42574 commit b3241b0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export const signalRulesAlertType = ({
hasTimestampFields(
wroteStatus,
hasTimestampOverride ? (timestampOverride as string) : '@timestamp',
name,
timestampFieldCaps,
inputIndices,
ruleStatusService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ describe('utils', () => {
const res = await hasTimestampFields(
false,
timestampField,
'myfakerulename',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
timestampFieldCapsResponse as ApiResponse<Record<string, any>>,
['myfa*'],
Expand Down Expand Up @@ -854,6 +855,7 @@ describe('utils', () => {
const res = await hasTimestampFields(
false,
timestampField,
'myfakerulename',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
timestampFieldCapsResponse as ApiResponse<Record<string, any>>,
['myfa*'],
Expand All @@ -866,6 +868,60 @@ describe('utils', () => {
);
expect(res).toBeTruthy();
});

test('returns true when missing logs-endpoint.alerts-* index and rule name is Endpoint Security', async () => {
const timestampField = '@timestamp';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const timestampFieldCapsResponse: Partial<ApiResponse<Record<string, any>, Context>> = {
body: {
indices: [],
fields: {},
},
};
mockLogger.error.mockClear();
const res = await hasTimestampFields(
false,
timestampField,
'Endpoint Security',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
timestampFieldCapsResponse as ApiResponse<Record<string, any>>,
['logs-endpoint.alerts-*'],
ruleStatusServiceMock,
mockLogger,
buildRuleMessage
);
expect(mockLogger.error).toHaveBeenCalledWith(
'This rule is attempting to query data from Elasticsearch indices listed in the "Index pattern" section of the rule definition, however no index matching: ["logs-endpoint.alerts-*"] was found. This warning will continue to appear until a matching index is created or this rule is de-activated. If you have recently enrolled agents enabled with Endpoint Security through Fleet, this warning should stop once an alert is sent from an agent. name: "fake name" id: "fake id" rule id: "fake rule id" signals index: "fakeindex"'
);
expect(res).toBeTruthy();
});

test('returns true when missing logs-endpoint.alerts-* index and rule name is NOT Endpoint Security', async () => {
const timestampField = '@timestamp';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const timestampFieldCapsResponse: Partial<ApiResponse<Record<string, any>, Context>> = {
body: {
indices: [],
fields: {},
},
};
mockLogger.error.mockClear();
const res = await hasTimestampFields(
false,
timestampField,
'NOT Endpoint Security',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
timestampFieldCapsResponse as ApiResponse<Record<string, any>>,
['logs-endpoint.alerts-*'],
ruleStatusServiceMock,
mockLogger,
buildRuleMessage
);
expect(mockLogger.error).toHaveBeenCalledWith(
'This rule is attempting to query data from Elasticsearch indices listed in the "Index pattern" section of the rule definition, however no index matching: ["logs-endpoint.alerts-*"] was found. This warning will continue to appear until a matching index is created or this rule is de-activated. name: "fake name" id: "fake id" rule id: "fake rule id" signals index: "fakeindex"'
);
expect(res).toBeTruthy();
});
});

describe('wrapBuildingBlocks', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const hasReadIndexPrivileges = async (
export const hasTimestampFields = async (
wroteStatus: boolean,
timestampField: string,
ruleName: string,
// any is derived from here
// node_modules/@elastic/elasticsearch/api/kibana.d.ts
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -115,11 +116,15 @@ export const hasTimestampFields = async (
buildRuleMessage: BuildRuleMessage
): Promise<boolean> => {
if (!wroteStatus && isEmpty(timestampFieldCapsResponse.body.indices)) {
const errorString = `The following index patterns did not match any indices: ${JSON.stringify(
const errorString = `This rule is attempting to query data from Elasticsearch indices listed in the "Index pattern" section of the rule definition, however no index matching: ${JSON.stringify(
inputIndices
)}`;
logger.error(buildRuleMessage(errorString));
await ruleStatusService.warning(errorString);
)} was found. This warning will continue to appear until a matching index is created or this rule is de-activated. ${
ruleName === 'Endpoint Security'
? 'If you have recently enrolled agents enabled with Endpoint Security through Fleet, this warning should stop once an alert is sent from an agent.'
: ''
}`;
logger.error(buildRuleMessage(errorString.trimEnd()));
await ruleStatusService.warning(errorString.trimEnd());
return true;
} else if (
!wroteStatus &&
Expand Down

0 comments on commit b3241b0

Please sign in to comment.