Skip to content

Commit

Permalink
[Entity Analytics] Move routes and constants into folders owned by th…
Browse files Browse the repository at this point in the history
…e entity analytics team (#180702)

## Summary

Closes #180531
This pull request moves entity analytics route registration and url
definition into files owned by our team.

Currently, to add a new route we require a code owners review from both
the `security-detections-response` and `security-threat-hunting` teams
unnecessarily. This is because we needed to change the following files:

- `x-pack/plugins/security_solution/common/constants.ts`
- `x-pack/plugins/security_solution/server/routes/index.ts`

As recommended by @maximpn
[here](#179930 (review))

I have also removed redundant feature flag checks for enabling risk
scoring and risk engine privileges routes, these feature flags are
enabled now.

---------

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
hop-dev and kibanamachine authored Apr 15, 2024
1 parent bf73f17 commit 47582c4
Show file tree
Hide file tree
Showing 29 changed files with 204 additions and 149 deletions.
40 changes: 1 addition & 39 deletions x-pack/plugins/security_solution/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { AddOptionsListControlProps } from '@kbn/controls-plugin/public';
import * as i18n from './translations';

export { SecurityPageName } from '@kbn/security-solution-navigation';

/**
* as const
*
Expand Down Expand Up @@ -247,44 +246,7 @@ export const DETECTION_ENGINE_RULES_BULK_CREATE =
export const DETECTION_ENGINE_RULES_BULK_UPDATE =
`${DETECTION_ENGINE_RULES_URL}/_bulk_update` as const;

/**
* Internal Risk Score routes
*/
export const INTERNAL_RISK_SCORE_URL = '/internal/risk_score' as const;
export const DEV_TOOL_PREBUILT_CONTENT =
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/dev_tool/{console_id}` as const;
export const devToolPrebuiltContentUrl = (spaceId: string, consoleId: string) =>
`/s/${spaceId}${INTERNAL_RISK_SCORE_URL}/prebuilt_content/dev_tool/${consoleId}` as const;
export const PREBUILT_SAVED_OBJECTS_BULK_CREATE = `${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_create/{template_name}`;
export const prebuiltSavedObjectsBulkCreateUrl = (templateName: string) =>
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_create/${templateName}` as const;
export const PREBUILT_SAVED_OBJECTS_BULK_DELETE = `${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_delete/{template_name}`;
export const prebuiltSavedObjectsBulkDeleteUrl = (templateName: string) =>
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_delete/${templateName}` as const;
export const RISK_SCORE_CREATE_INDEX = `${INTERNAL_RISK_SCORE_URL}/indices/create`;
export const RISK_SCORE_DELETE_INDICES = `${INTERNAL_RISK_SCORE_URL}/indices/delete`;
export const RISK_SCORE_CREATE_STORED_SCRIPT = `${INTERNAL_RISK_SCORE_URL}/stored_scripts/create`;
export const RISK_SCORE_DELETE_STORED_SCRIPT = `${INTERNAL_RISK_SCORE_URL}/stored_scripts/delete`;
export const RISK_SCORE_PREVIEW_URL = `${INTERNAL_RISK_SCORE_URL}/preview`;

export const RISK_ENGINE_URL = `${INTERNAL_RISK_SCORE_URL}/engine`;
export const RISK_ENGINE_STATUS_URL = `${RISK_ENGINE_URL}/status`;
export const RISK_ENGINE_INIT_URL = `${RISK_ENGINE_URL}/init`;
export const RISK_ENGINE_ENABLE_URL = `${RISK_ENGINE_URL}/enable`;
export const RISK_ENGINE_DISABLE_URL = `${RISK_ENGINE_URL}/disable`;
export const RISK_ENGINE_PRIVILEGES_URL = `${RISK_ENGINE_URL}/privileges`;
export const RISK_ENGINE_SETTINGS_URL = `${RISK_ENGINE_URL}/settings`;

export const ASSET_CRITICALITY_URL = `/internal/asset_criticality`;
export const ASSET_CRITICALITY_PRIVILEGES_URL = `/internal/asset_criticality/privileges`;
export const ASSET_CRITICALITY_STATUS_URL = `${ASSET_CRITICALITY_URL}/status`;
export const ASSET_CRITICALITY_CSV_UPLOAD_URL = `${ASSET_CRITICALITY_URL}/upload_csv`;

/**
* Public Risk Score routes
*/
export const RISK_ENGINE_PUBLIC_PREFIX = '/api/risk_scores' as const;
export const RISK_SCORE_CALCULATION_URL = `${RISK_ENGINE_PUBLIC_PREFIX}/calculation` as const;
export * from './entity_analytics/constants';

export const INTERNAL_DASHBOARDS_URL = `/internal/dashboards` as const;
export const INTERNAL_TAGS_URL = `/internal/tags`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* 2.0.
*/

export const ASSET_CRITICALITY_URL = `/internal/asset_criticality`;
export const ASSET_CRITICALITY_PRIVILEGES_URL = `/internal/asset_criticality/privileges`;
export const ASSET_CRITICALITY_STATUS_URL = `${ASSET_CRITICALITY_URL}/status`;
export const ASSET_CRITICALITY_CSV_UPLOAD_URL = `${ASSET_CRITICALITY_URL}/upload_csv`;

export const ASSET_CRITICALITY_INDEX_PATTERN = '.asset-criticality.asset-criticality-*';

type AssetCriticalityIndexPrivilege = 'read' | 'write';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './asset_criticality/constants';
export * from './risk_engine/constants';
export * from './risk_score/constants';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { INTERNAL_RISK_SCORE_URL } from '../risk_score/constants';
export const RISK_ENGINE_URL = `${INTERNAL_RISK_SCORE_URL}/engine`;
export const RISK_ENGINE_STATUS_URL = `${RISK_ENGINE_URL}/status`;
export const RISK_ENGINE_INIT_URL = `${RISK_ENGINE_URL}/init`;
export const RISK_ENGINE_ENABLE_URL = `${RISK_ENGINE_URL}/enable`;
export const RISK_ENGINE_DISABLE_URL = `${RISK_ENGINE_URL}/disable`;
export const RISK_ENGINE_PRIVILEGES_URL = `${RISK_ENGINE_URL}/privileges`;
export const RISK_ENGINE_SETTINGS_URL = `${RISK_ENGINE_URL}/settings`;

export const MAX_SPACES_COUNT = 1;

type ClusterPrivilege = 'manage_index_templates' | 'manage_transform';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
/**
* Public Risk Score routes
*/
export const RISK_ENGINE_PUBLIC_PREFIX = '/api/risk_scores' as const;
export const RISK_SCORE_CALCULATION_URL = `${RISK_ENGINE_PUBLIC_PREFIX}/calculation` as const;

/**
* Internal Risk Score routes
*/
export const INTERNAL_RISK_SCORE_URL = '/internal/risk_score' as const;
export const DEV_TOOL_PREBUILT_CONTENT =
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/dev_tool/{console_id}` as const;
export const devToolPrebuiltContentUrl = (spaceId: string, consoleId: string) =>
`/s/${spaceId}${INTERNAL_RISK_SCORE_URL}/prebuilt_content/dev_tool/${consoleId}` as const;
export const PREBUILT_SAVED_OBJECTS_BULK_CREATE = `${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_create/{template_name}`;
export const prebuiltSavedObjectsBulkCreateUrl = (templateName: string) =>
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_create/${templateName}` as const;
export const PREBUILT_SAVED_OBJECTS_BULK_DELETE = `${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_delete/{template_name}`;
export const prebuiltSavedObjectsBulkDeleteUrl = (templateName: string) =>
`${INTERNAL_RISK_SCORE_URL}/prebuilt_content/saved_objects/_bulk_delete/${templateName}` as const;
export const RISK_SCORE_CREATE_INDEX = `${INTERNAL_RISK_SCORE_URL}/indices/create`;
export const RISK_SCORE_DELETE_INDICES = `${INTERNAL_RISK_SCORE_URL}/indices/delete`;
export const RISK_SCORE_CREATE_STORED_SCRIPT = `${INTERNAL_RISK_SCORE_URL}/stored_scripts/create`;
export const RISK_SCORE_DELETE_STORED_SCRIPT = `${INTERNAL_RISK_SCORE_URL}/stored_scripts/delete`;
export const RISK_SCORE_PREVIEW_URL = `${INTERNAL_RISK_SCORE_URL}/preview`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
APP_ID,
ENABLE_ASSET_CRITICALITY_SETTING,
} from '../../../../../common/constants';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { AssetCriticalityRecordIdParts } from '../../../../../common/api/entity_analytics/asset_criticality';
import { buildRouteValidationWithZod } from '../../../../utils/build_validation/route_validation';
import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_criticality_resources';
import { assertAdvancedSettingsEnabled } from '../../utils/assert_advanced_setting_enabled';
import type { EntityAnalyticsRoutesDeps } from '../../types';
export const assetCriticalityDeleteRoute = (
router: SecuritySolutionPluginRouter,
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger
) => {
router.versioned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import {
APP_ID,
ENABLE_ASSET_CRITICALITY_SETTING,
} from '../../../../../common/constants';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_criticality_resources';
import { buildRouteValidationWithZod } from '../../../../utils/build_validation/route_validation';
import { AssetCriticalityRecordIdParts } from '../../../../../common/api/entity_analytics/asset_criticality';
import { assertAdvancedSettingsEnabled } from '../../utils/assert_advanced_setting_enabled';
export const assetCriticalityGetRoute = (router: SecuritySolutionPluginRouter, logger: Logger) => {
import type { EntityAnalyticsRoutesDeps } from '../../types';
export const assetCriticalityGetRoute = (
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger
) => {
router.versioned
.get({
access: 'internal',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
* 2.0.
*/

export { assetCriticalityStatusRoute } from './status';
export { assetCriticalityUpsertRoute } from './upsert';
export { assetCriticalityGetRoute } from './get';
export { assetCriticalityDeleteRoute } from './delete';
export { assetCriticalityPrivilegesRoute } from './privileges';
export { assetCriticalityCSVUploadRoute } from './upload_csv';
export { registerAssetCriticalityRoutes } from './register_asset_criticality_routes';
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { Logger, StartServicesAccessor } from '@kbn/core/server';
import type { Logger } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { transformError } from '@kbn/securitysolution-es-utils';
import {
ASSET_CRITICALITY_PRIVILEGES_URL,
APP_ID,
ENABLE_ASSET_CRITICALITY_SETTING,
} from '../../../../../common/constants';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_criticality_resources';
import { getUserAssetCriticalityPrivileges } from '../get_user_asset_criticality_privileges';

import type { StartPlugins } from '../../../../plugin';
import { assertAdvancedSettingsEnabled } from '../../utils/assert_advanced_setting_enabled';
import type { EntityAnalyticsRoutesDeps } from '../../types';

export const assetCriticalityPrivilegesRoute = (
router: SecuritySolutionPluginRouter,
getStartServices: StartServicesAccessor<StartPlugins>,
logger: Logger
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger,
getStartServices: EntityAnalyticsRoutesDeps['getStartServices']
) => {
router.versioned
.get({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { assetCriticalityStatusRoute } from './status';
import { assetCriticalityUpsertRoute } from './upsert';
import { assetCriticalityGetRoute } from './get';
import { assetCriticalityDeleteRoute } from './delete';
import { assetCriticalityPrivilegesRoute } from './privileges';
import { assetCriticalityCSVUploadRoute } from './upload_csv';
import type { EntityAnalyticsRoutesDeps } from '../../types';

export const registerAssetCriticalityRoutes = ({
router,
logger,
config,
getStartServices,
}: EntityAnalyticsRoutesDeps) => {
assetCriticalityStatusRoute(router, logger);
assetCriticalityUpsertRoute(router, logger);
assetCriticalityGetRoute(router, logger);
assetCriticalityDeleteRoute(router, logger);
assetCriticalityPrivilegesRoute(router, logger, getStartServices);
assetCriticalityCSVUploadRoute(router, logger, config, getStartServices);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
APP_ID,
ENABLE_ASSET_CRITICALITY_SETTING,
} from '../../../../../common/constants';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import type { EntityAnalyticsRoutesDeps } from '../../types';
import { assertAdvancedSettingsEnabled } from '../../utils/assert_advanced_setting_enabled';
import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_criticality_resources';

export const assetCriticalityStatusRoute = (
router: SecuritySolutionPluginRouter,
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger
) => {
router.versioned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { Logger, StartServicesAccessor } from '@kbn/core/server';
import type { Logger } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { schema } from '@kbn/config-schema';
import Papa from 'papaparse';
import { transformError } from '@kbn/securitysolution-es-utils';
import type { StartPlugins } from '../../../../plugin';
import type { AssetCriticalityCsvUploadResponse } from '../../../../../common/api/entity_analytics';
import { CRITICALITY_CSV_MAX_SIZE_BYTES_WITH_TOLERANCE } from '../../../../../common/entity_analytics/asset_criticality';
import type { ConfigType } from '../../../../config';
import type { HapiReadableStream, SecuritySolutionPluginRouter } from '../../../../types';
import type { HapiReadableStream } from '../../../../types';
import {
ASSET_CRITICALITY_CSV_UPLOAD_URL,
APP_ID,
Expand All @@ -23,12 +22,13 @@ import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_c
import { transformCSVToUpsertRecords } from '../transform_csv_to_upsert_records';
import { createAssetCriticalityProcessedFileEvent } from '../../../telemetry/event_based/events';
import { assertAdvancedSettingsEnabled } from '../../utils/assert_advanced_setting_enabled';
import type { EntityAnalyticsRoutesDeps } from '../../types';

export const assetCriticalityCSVUploadRoute = (
router: SecuritySolutionPluginRouter,
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger,
config: ConfigType,
getStartServices: StartServicesAccessor<StartPlugins>
getStartServices: EntityAnalyticsRoutesDeps['getStartServices']
) => {
const { errorRetries, maxBulkRequestBodySizeBytes } =
config.entityAnalytics.assetCriticality.csvUpload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { Logger } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { transformError } from '@kbn/securitysolution-es-utils';
import { ASSET_CRITICALITY_URL, APP_ID } from '../../../../../common/constants';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { checkAndInitAssetCriticalityResources } from '../check_and_init_asset_criticality_resources';
import { buildRouteValidationWithZod } from '../../../../utils/build_validation/route_validation';
import { CreateAssetCriticalityRecord } from '../../../../../common/api/entity_analytics/asset_criticality';
import type { EntityAnalyticsRoutesDeps } from '../../types';
export const assetCriticalityUpsertRoute = (
router: SecuritySolutionPluginRouter,
router: EntityAnalyticsRoutesDeps['router'],
logger: Logger
) => {
router.versioned
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { registerAssetCriticalityRoutes } from './asset_criticality/routes';
import { registerRiskScoreRoutes } from './risk_score/routes';
import { registerRiskEngineRoutes } from './risk_engine/routes';
import type { EntityAnalyticsRoutesDeps } from './types';

export const registerEntityAnalyticsRoutes = (routeDeps: EntityAnalyticsRoutesDeps) => {
registerAssetCriticalityRoutes(routeDeps);
registerRiskScoreRoutes(routeDeps);
registerRiskEngineRoutes(routeDeps);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
* 2.0.
*/

import type { StartServicesAccessor } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { transformError } from '@kbn/securitysolution-es-utils';
import { RISK_ENGINE_DISABLE_URL, APP_ID } from '../../../../../common/constants';
import type { StartPlugins } from '../../../../plugin';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { TASK_MANAGER_UNAVAILABLE_ERROR } from './translations';
import { withRiskEnginePrivilegeCheck } from '../risk_engine_privileges';
import type { EntityAnalyticsRoutesDeps } from '../../types';

export const riskEngineDisableRoute = (
router: SecuritySolutionPluginRouter,
getStartServices: StartServicesAccessor<StartPlugins>
router: EntityAnalyticsRoutesDeps['router'],
getStartServices: EntityAnalyticsRoutesDeps['getStartServices']
) => {
router.versioned
.post({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
* 2.0.
*/

import type { StartServicesAccessor } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { transformError } from '@kbn/securitysolution-es-utils';
import { RISK_ENGINE_ENABLE_URL, APP_ID } from '../../../../../common/constants';
import { TASK_MANAGER_UNAVAILABLE_ERROR } from './translations';
import type { StartPlugins } from '../../../../plugin';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { withRiskEnginePrivilegeCheck } from '../risk_engine_privileges';
import type { EntityAnalyticsRoutesDeps } from '../../types';

export const riskEngineEnableRoute = (
router: SecuritySolutionPluginRouter,
getStartServices: StartServicesAccessor<StartPlugins>
router: EntityAnalyticsRoutesDeps['router'],
getStartServices: EntityAnalyticsRoutesDeps['getStartServices']
) => {
router.versioned
.post({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
* 2.0.
*/

export { riskEngineInitRoute } from './init';
export { riskEngineEnableRoute } from './enable';
export { riskEngineDisableRoute } from './disable';
export { riskEngineStatusRoute } from './status';
export { riskEnginePrivilegesRoute } from './privileges';
export { riskEngineSettingsRoute } from './settings';
export { registerRiskEngineRoutes } from './register_risk_engine_routes';
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
* 2.0.
*/

import type { StartServicesAccessor } from '@kbn/core/server';
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
import { transformError } from '@kbn/securitysolution-es-utils';
import { RISK_ENGINE_INIT_URL, APP_ID } from '../../../../../common/constants';
import type { StartPlugins } from '../../../../plugin';
import { TASK_MANAGER_UNAVAILABLE_ERROR } from './translations';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import type { InitRiskEngineResultResponse } from '../../types';
import type { EntityAnalyticsRoutesDeps, InitRiskEngineResultResponse } from '../../types';
import { withRiskEnginePrivilegeCheck } from '../risk_engine_privileges';
export const riskEngineInitRoute = (
router: SecuritySolutionPluginRouter,
getStartServices: StartServicesAccessor<StartPlugins>
router: EntityAnalyticsRoutesDeps['router'],
getStartServices: EntityAnalyticsRoutesDeps['getStartServices']
) => {
router.versioned
.post({
Expand Down
Loading

0 comments on commit 47582c4

Please sign in to comment.