Skip to content

Commit

Permalink
[Security Solution] Migrate risk score internal APIs to versioned rou…
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmain authored Oct 2, 2023
1 parent 6589719 commit 3f4e53f
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getRiskScoreIndexStatus = async (params: {
RISK_SCORE_INDEX_STATUS_API_URL,
{
method: 'GET',
version: '1',
query: { indexName, entity },
asSystemRequest: true,
signal: params.signal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const installRiskScore = ({
}) => {
return http
.post<Response[]>(INTERNAL_RISK_SCORE_URL, {
version: '1',
body: JSON.stringify(options),
signal,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const bulkCreatePrebuiltSavedObjects = async ({
body?: Array<{ type: string; title: string; id: string; name: string }>;
}
>
>(prebuiltSavedObjectsBulkCreateUrl(options.templateName))
>(prebuiltSavedObjectsBulkCreateUrl(options.templateName), { version: '1' })
.then((result) => {
const response = result[options.templateName];
const error = response?.error?.message;
Expand Down Expand Up @@ -142,7 +142,7 @@ export const bulkDeletePrebuiltSavedObjects = async ({
options: Options;
}) => {
const res = await http
.post(prebuiltSavedObjectsBulkDeleteUrl(options.templateName))
.post(prebuiltSavedObjectsBulkDeleteUrl(options.templateName), { version: '1' })
.catch((e) => {
notifications?.toasts?.addDanger({
title: errorMessage ?? DELETE_SAVED_OBJECTS_FAILURE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function createStoredScript({
}: CreateStoredScript) {
const res = await http
.put(RISK_SCORE_CREATE_STORED_SCRIPT, {
version: '1',
body: JSON.stringify(options),
signal,
})
Expand All @@ -53,6 +54,7 @@ export async function deleteStoredScript({
}: DeleteStoredScript) {
const res = await http
.delete(RISK_SCORE_DELETE_STORED_SCRIPT, {
version: '1',
body: JSON.stringify(options),
signal,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,53 @@ import { buildSiemResponse } from '../../detection_engine/routes/utils';
import { indexStatusRequestQuery } from '../../../../common/api/risk_score';

export const getRiskScoreIndexStatusRoute = (router: SecuritySolutionPluginRouter) => {
router.get(
{
router.versioned
.get({
access: 'internal',
path: RISK_SCORE_INDEX_STATUS_API_URL,
validate: {
query: buildRouteValidation(indexStatusRequestQuery),
},
options: {
tags: ['access:securitySolution', `access:${APP_ID}-entity-analytics`],
},
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const coreContext = await context.core;
const { indexName, entity } = request.query;
try {
const newFieldName = `${entity}.risk.calculated_level`;
const res = await coreContext.elasticsearch.client.asCurrentUser.fieldCaps({
index: indexName,
fields: newFieldName,
ignore_unavailable: true,
allow_no_indices: false,
});
const isDeprecated = !Object.keys(res.fields).includes(newFieldName);
})
.addVersion(
{
validate: {
request: {
query: buildRouteValidation(indexStatusRequestQuery),
},
},
version: '1',
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const coreContext = await context.core;
const { indexName, entity } = request.query;
try {
const newFieldName = `${entity}.risk.calculated_level`;
const res = await coreContext.elasticsearch.client.asCurrentUser.fieldCaps({
index: indexName,
fields: newFieldName,
ignore_unavailable: true,
allow_no_indices: false,
});
const isDeprecated = !Object.keys(res.fields).includes(newFieldName);

return response.ok({
body: { isDeprecated, isEnabled: true },
});
} catch (err) {
const error = transformError(err);
if (error.statusCode === 404) {
// index does not exist, therefore cannot be deprecated
return response.ok({
body: { isDeprecated: false, isEnabled: false },
body: { isDeprecated, isEnabled: true },
});
} catch (err) {
const error = transformError(err);
if (error.statusCode === 404) {
// index does not exist, therefore cannot be deprecated
return response.ok({
body: { isDeprecated: false, isEnabled: false },
});
}
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
);
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,50 @@ import { createIndex } from './lib/create_index';
import { createEsIndexRequestBody } from '../../../../common/api/risk_score';

export const createEsIndexRoute = (router: SecuritySolutionPluginRouter, logger: Logger) => {
router.put(
{
router.versioned
.put({
access: 'internal',
path: RISK_SCORE_CREATE_INDEX,
validate: { body: createEsIndexRequestBody },
options: {
tags: ['access:securitySolution'],
},
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const { client } = (await context.core).elasticsearch;
const esClient = client.asCurrentUser;
const options = request.body;
})
.addVersion(
{
validate: {
request: { body: createEsIndexRequestBody },
},
version: '1',
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const { client } = (await context.core).elasticsearch;
const esClient = client.asCurrentUser;
const options = request.body;

try {
const result = await createIndex({
esClient,
logger,
options,
});
const error = result[options.index].error;
try {
const result = await createIndex({
esClient,
logger,
options,
});
const error = result[options.index].error;

if (error != null) {
if (error != null) {
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
} else {
return response.ok({ body: options });
}
} catch (e) {
const error = transformError(e);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
} else {
return response.ok({ body: options });
}
} catch (e) {
const error = transformError(e);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
);
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ import { deleteEsIndices } from './lib/delete_indices';
import { deleteIndicesRequestBody } from '../../../../common/api/risk_score';

export const deleteEsIndicesRoute = (router: SecuritySolutionPluginRouter) => {
router.post(
{
router.versioned
.post({
access: 'internal',
path: RISK_SCORE_DELETE_INDICES,
validate: { body: deleteIndicesRequestBody },
options: {
tags: ['access:securitySolution'],
},
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
})
.addVersion(
{ validate: { request: { body: deleteIndicesRequestBody } }, version: '1' },
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);

const { client } = (await context.core).elasticsearch;
const { indices } = request.body;
const { client } = (await context.core).elasticsearch;
const { indices } = request.body;

try {
await deleteEsIndices({ client, indices });
return response.ok({ body: { deleted: indices } });
} catch (err) {
const error = transformError(err);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
try {
await deleteEsIndices({ client, indices });
return response.ok({ body: { deleted: indices } });
} catch (err) {
const error = transformError(err);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
}
);
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,44 @@ export const installRiskScoresRoute = (
logger: Logger,
security: SetupPlugins['security']
) => {
router.post(
{
router.versioned
.post({
access: 'internal',
path: INTERNAL_RISK_SCORE_URL,
validate: onboardingRiskScoreRequestBody,
options: {
tags: ['access:securitySolution', `access:${APP_ID}-entity-analytics`],
},
},
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const { riskScoreEntity } = request.body;
})
.addVersion(
{ validate: { request: onboardingRiskScoreRequestBody }, version: '1' },
async (context, request, response) => {
const siemResponse = buildSiemResponse(response);
const { riskScoreEntity } = request.body;

try {
const securitySolution = await context.securitySolution;
try {
const securitySolution = await context.securitySolution;

const spaceId = securitySolution?.getSpaceId();
const spaceId = securitySolution?.getSpaceId();

const { client } = (await context.core).elasticsearch;
const esClient = client.asCurrentUser;
const res = await installRiskScoreModule({
esClient,
logger,
riskScoreEntity,
spaceId,
});
const { client } = (await context.core).elasticsearch;
const esClient = client.asCurrentUser;
const res = await installRiskScoreModule({
esClient,
logger,
riskScoreEntity,
spaceId,
});

return response.ok({
body: res,
});
} catch (err) {
const error = transformError(err);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
return response.ok({
body: res,
});
} catch (err) {
const error = transformError(err);
return siemResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
}
);
);
};
Loading

0 comments on commit 3f4e53f

Please sign in to comment.