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

[7.6] [SIEM][Detection Engine] Backend end-to-end tests (#57166) #57392

Merged
merged 4 commits into from
Feb 12, 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 @@ -149,7 +149,7 @@ export const enableRules = async ({ ids, enabled }: EnableRulesProps): Promise<R
const response = await fetch(
`${chrome.getBasePath()}${DETECTION_ENGINE_RULES_URL}/_bulk_update`,
{
method: 'PUT',
method: 'PATCH',
credentials: 'same-origin',
headers: {
'content-type': 'application/json',
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/siem/public/hooks/api/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export const throwIfNotOk = async (response: Response): Promise<void> => {
if (body != null && body.message) {
if (body.statusCode != null) {
throw new ToasterErrors([body.message, `${i18n.STATUS_CODE} ${body.statusCode}`]);
} else if (body.status_code != null) {
throw new ToasterErrors([body.message, `${i18n.STATUS_CODE} ${body.status_code}`]);
} else {
throw new ToasterErrors([body.message]);
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/siem/public/utils/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface MessageBody {
error?: string;
message?: string;
statusCode?: number;
status_code?: number;
}

export const parseJsonFromBody = async (response: Response): Promise<MessageBody | null> => {
Expand Down
8 changes: 6 additions & 2 deletions x-pack/legacy/plugins/siem/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { readIndexRoute } from './lib/detection_engine/routes/index/read_index_r
import { readRulesRoute } from './lib/detection_engine/routes/rules/read_rules_route';
import { findRulesRoute } from './lib/detection_engine/routes/rules/find_rules_route';
import { deleteRulesRoute } from './lib/detection_engine/routes/rules/delete_rules_route';
import { updateRulesRoute } from './lib/detection_engine/routes/rules/update_rules_route';
import { patchRulesRoute } from './lib/detection_engine/routes/rules/patch_rules_route';
import { setSignalsStatusRoute } from './lib/detection_engine/routes/signals/open_close_signals_route';
import { querySignalsRoute } from './lib/detection_engine/routes/signals/query_signals_route';
import { ServerFacade } from './types';
Expand All @@ -23,12 +23,14 @@ import { readTagsRoute } from './lib/detection_engine/routes/tags/read_tags_rout
import { readPrivilegesRoute } from './lib/detection_engine/routes/privileges/read_privileges_route';
import { addPrepackedRulesRoute } from './lib/detection_engine/routes/rules/add_prepackaged_rules_route';
import { createRulesBulkRoute } from './lib/detection_engine/routes/rules/create_rules_bulk_route';
import { updateRulesBulkRoute } from './lib/detection_engine/routes/rules/update_rules_bulk_route';
import { patchRulesBulkRoute } from './lib/detection_engine/routes/rules/patch_rules_bulk_route';
import { deleteRulesBulkRoute } from './lib/detection_engine/routes/rules/delete_rules_bulk_route';
import { importRulesRoute } from './lib/detection_engine/routes/rules/import_rules_route';
import { exportRulesRoute } from './lib/detection_engine/routes/rules/export_rules_route';
import { findRulesStatusesRoute } from './lib/detection_engine/routes/rules/find_rules_status_route';
import { getPrepackagedRulesStatusRoute } from './lib/detection_engine/routes/rules/get_prepackaged_rules_status_route';
import { updateRulesRoute } from './lib/detection_engine/routes/rules/update_rules_route';
import { updateRulesBulkRoute } from './lib/detection_engine/routes/rules/update_rules_bulk_route';

const APP_ID = 'siem';

Expand All @@ -50,12 +52,14 @@ export const initServerWithKibana = (context: PluginInitializerContext, __legacy
updateRulesRoute(__legacy);
deleteRulesRoute(__legacy);
findRulesRoute(__legacy);
patchRulesRoute(__legacy);

addPrepackedRulesRoute(__legacy);
getPrepackagedRulesStatusRoute(__legacy);
createRulesBulkRoute(__legacy);
updateRulesBulkRoute(__legacy);
deleteRulesBulkRoute(__legacy);
patchRulesBulkRoute(__legacy);

importRulesRoute(__legacy);
exportRulesRoute(__legacy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export const getUpdateRequest = (): ServerInjectOptions => ({
},
});

export const getPatchRequest = (): ServerInjectOptions => ({
method: 'PATCH',
url: DETECTION_ENGINE_RULES_URL,
payload: {
...typicalPayload(),
},
});

export const getReadRequest = (): ServerInjectOptions => ({
method: 'GET',
url: `${DETECTION_ENGINE_RULES_URL}?rule_id=rule-1`,
Expand All @@ -130,6 +138,12 @@ export const getUpdateBulkRequest = (): ServerInjectOptions => ({
payload: [typicalPayload()],
});

export const getPatchBulkRequest = (): ServerInjectOptions => ({
method: 'PATCH',
url: `${DETECTION_ENGINE_RULES_URL}/_bulk_update`,
payload: [typicalPayload()],
});

export const getDeleteBulkRequest = (): ServerInjectOptions => ({
method: 'DELETE',
url: `${DETECTION_ENGINE_RULES_URL}/_bulk_delete`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import Hapi from 'hapi';
import Boom from 'boom';

import { DETECTION_ENGINE_INDEX_URL } from '../../../../../common/constants';
import signalsPolicy from './signals_policy.json';
Expand All @@ -31,13 +30,18 @@ export const createCreateIndexRoute = (server: ServerFacade): Hapi.ServerRoute =
},
},
},
async handler(request: RequestFacade) {
async handler(request: RequestFacade, headers) {
try {
const index = getIndex(request, server);
const callWithRequest = callWithRequestFactory(request, server);
const indexExists = await getIndexExists(callWithRequest, index);
if (indexExists) {
return new Boom(`index: "${index}" already exists`, { statusCode: 409 });
return headers
.response({
message: `index: "${index}" already exists`,
status_code: 409,
})
.code(409);
} else {
const policyExists = await getPolicyExists(callWithRequest, index);
if (!policyExists) {
Expand All @@ -52,7 +56,13 @@ export const createCreateIndexRoute = (server: ServerFacade): Hapi.ServerRoute =
return { acknowledged: true };
}
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import Hapi from 'hapi';
import Boom from 'boom';

import { DETECTION_ENGINE_INDEX_URL } from '../../../../../common/constants';
import { ServerFacade, RequestFacade } from '../../../../types';
Expand Down Expand Up @@ -39,13 +38,18 @@ export const createDeleteIndexRoute = (server: ServerFacade): Hapi.ServerRoute =
},
},
},
async handler(request: RequestFacade) {
async handler(request: RequestFacade, headers) {
try {
const index = getIndex(request, server);
const callWithRequest = callWithRequestFactory(request, server);
const indexExists = await getIndexExists(callWithRequest, index);
if (!indexExists) {
return new Boom(`index: "${index}" does not exist`, { statusCode: 404 });
return headers
.response({
message: `index: "${index}" does not exist`,
status_code: 404,
})
.code(404);
} else {
await deleteAllIndex(callWithRequest, `${index}-*`);
const policyExists = await getPolicyExists(callWithRequest, index);
Expand All @@ -59,7 +63,13 @@ export const createDeleteIndexRoute = (server: ServerFacade): Hapi.ServerRoute =
return { acknowledged: true };
}
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import Hapi from 'hapi';
import Boom from 'boom';

import { DETECTION_ENGINE_INDEX_URL } from '../../../../../common/constants';
import { ServerFacade, RequestFacade } from '../../../../types';
Expand Down Expand Up @@ -42,11 +41,22 @@ export const createReadIndexRoute = (server: ServerFacade): Hapi.ServerRoute =>
if (request.method.toLowerCase() === 'head') {
return headers.response().code(404);
} else {
return new Boom('index for this space does not exist', { statusCode: 404 });
return headers
.response({
message: 'index for this space does not exist',
status_code: 404,
})
.code(404);
}
}
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createReadPrivilegesRulesRoute = (server: ServerFacade): Hapi.Serve
},
},
},
async handler(request: RulesRequest) {
async handler(request: RulesRequest, headers) {
try {
const callWithRequest = callWithRequestFactory(request, server);
const index = getIndex(request, server);
Expand All @@ -35,7 +35,13 @@ export const createReadPrivilegesRulesRoute = (server: ServerFacade): Hapi.Serve
has_encryption_key: !usingEphemeralEncryptionKey,
});
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ describe('add_prepackaged_rules_route', () => {
alertsClient.create.mockResolvedValue(getResult());
const { payload } = await server.inject(addPrepackagedRulesRequest());
expect(JSON.parse(payload)).toEqual({
error: 'Bad Request',
message:
'Pre-packaged rules cannot be installed until the space index is created: .siem-signals-default',
statusCode: 400,
status_code: 400,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Hapi from 'hapi';
import { isFunction } from 'lodash/fp';
import Boom from 'boom';

import { DETECTION_ENGINE_PREPACKAGED_URL } from '../../../../../common/constants';
import { ServerFacade, RequestFacade } from '../../../../types';
Expand Down Expand Up @@ -55,9 +54,12 @@ export const createAddPrepackedRulesRoute = (server: ServerFacade): Hapi.ServerR
if (rulesToInstall.length !== 0 || rulesToUpdate.length !== 0) {
const spaceIndexExists = await getIndexExists(callWithRequest, spaceIndex);
if (!spaceIndexExists) {
return Boom.badRequest(
`Pre-packaged rules cannot be installed until the space index is created: ${spaceIndex}`
);
return headers
.response({
message: `Pre-packaged rules cannot be installed until the space index is created: ${spaceIndex}`,
status_code: 400,
})
.code(400);
}
}
await Promise.all(
Expand All @@ -75,7 +77,13 @@ export const createAddPrepackedRulesRoute = (server: ServerFacade): Hapi.ServerR
rules_updated: rulesToUpdate.length,
};
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ describe('create_rules', () => {
alertsClient.create.mockResolvedValue(getResult());
const { payload } = await server.inject(getCreateRequest());
expect(JSON.parse(payload)).toEqual({
error: 'Bad Request',
message: 'To create a rule, the index must exist first. Index .siem-signals does not exist',
statusCode: 400,
status_code: 400,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Hapi from 'hapi';
import { isFunction } from 'lodash/fp';
import Boom from 'boom';
import uuid from 'uuid';
import { DETECTION_ENGINE_RULES_URL } from '../../../../../common/constants';
import { createRules } from '../../rules/create_rules';
Expand All @@ -15,7 +14,7 @@ import { createRulesSchema } from '../schemas/create_rules_schema';
import { ServerFacade } from '../../../../types';
import { readRules } from '../../rules/read_rules';
import { ruleStatusSavedObjectType } from '../../rules/saved_object_mappings';
import { transformOrError } from './utils';
import { transform } from './utils';
import { getIndexExists } from '../../index/get_index_exists';
import { callWithRequestFactory, getIndex, transformError } from '../utils';

Expand Down Expand Up @@ -75,14 +74,22 @@ export const createCreateRulesRoute = (server: ServerFacade): Hapi.ServerRoute =
const callWithRequest = callWithRequestFactory(request, server);
const indexExists = await getIndexExists(callWithRequest, finalIndex);
if (!indexExists) {
return Boom.badRequest(
`To create a rule, the index must exist first. Index ${finalIndex} does not exist`
);
return headers
.response({
message: `To create a rule, the index must exist first. Index ${finalIndex} does not exist`,
status_code: 400,
})
.code(400);
}
if (ruleId != null) {
const rule = await readRules({ alertsClient, ruleId });
if (rule != null) {
return Boom.conflict(`rule_id: "${ruleId}" already exists`);
return headers
.response({
message: `rule_id: "${ruleId}" already exists`,
status_code: 409,
})
.code(409);
}
}
const createdRule = await createRules({
Expand Down Expand Up @@ -125,9 +132,25 @@ export const createCreateRulesRoute = (server: ServerFacade): Hapi.ServerRoute =
search: `${createdRule.id}`,
searchFields: ['alertId'],
});
return transformOrError(createdRule, ruleStatuses.saved_objects[0]);
const transformed = transform(createdRule, ruleStatuses.saved_objects[0]);
if (transformed == null) {
return headers
.response({
message: 'Internal error transforming rules',
status_code: 500,
})
.code(500);
} else {
return transformed;
}
} catch (err) {
return transformError(err);
const error = transformError(err);
return headers
.response({
message: error.message,
status_code: error.statusCode,
})
.code(error.statusCode);
}
},
};
Expand Down
Loading