diff --git a/x-pack/plugins/alerting/server/authorization/alerting_authorization.ts b/x-pack/plugins/alerting/server/authorization/alerting_authorization.ts index de49cdd370585..655e1f7ce095c 100644 --- a/x-pack/plugins/alerting/server/authorization/alerting_authorization.ts +++ b/x-pack/plugins/alerting/server/authorization/alerting_authorization.ts @@ -143,7 +143,7 @@ export class AlertingAuthorization { * used by the RAC/Alerts client */ public async getAugmentRuleTypesWithAuthorization( - featureIds: string[], + featureIds: readonly string[], operations: Array, authorizationEntity: AlertingAuthorizationEntity ): Promise<{ diff --git a/x-pack/plugins/rule_registry/server/alert_data_client/alerts_client.ts b/x-pack/plugins/rule_registry/server/alert_data_client/alerts_client.ts index 81d43b0013767..197ee95fab4ae 100644 --- a/x-pack/plugins/rule_registry/server/alert_data_client/alerts_client.ts +++ b/x-pack/plugins/rule_registry/server/alert_data_client/alerts_client.ts @@ -46,9 +46,12 @@ interface GetAlertParams { index?: string; } -export const mapConsumerToIndexName = { - observability: '.alerts-observability', +export const validFeatureIds = ['apm', 'observability', 'siem'] as const; +export type ValidFeatureIds = typeof validFeatureIds[number]; + +export const mapConsumerToIndexName: { [featureId in ValidFeatureIds]: string | string[] } = { apm: '.alerts-observability-apm', + observability: '.alerts-observability', siem: ['.alerts-security-solution', '.siem-signals'], }; @@ -75,7 +78,7 @@ export class AlertsClient { operations: Array ) { return this.authorization.getAugmentRuleTypesWithAuthorization( - featureIds.length !== 0 ? featureIds : Object.keys(mapConsumerToIndexName), + featureIds.length !== 0 ? featureIds : validFeatureIds, operations, AlertingAuthorizationEntity.Alert ); @@ -203,10 +206,10 @@ export class AlertsClient { } public async getAuthorizedAlertsIndices( - featureIds: Array + featureIds: readonly ValidFeatureIds[] ): Promise { const augmentedRuleTypes = await this.authorization.getAugmentRuleTypesWithAuthorization( - featureIds, + featureIds as string[], [ReadOperations.Find, ReadOperations.Get, WriteOperations.Update], AlertingAuthorizationEntity.Alert ); @@ -219,11 +222,14 @@ export class AlertsClient { authorizedFeatures.add(ruleType.producer); } - const typeguard = (a: string): a is keyof typeof mapConsumerToIndexName => - a in Object.keys(mapConsumerToIndexName); + const isValidFeatureId = (a: string): a is ValidFeatureIds => + // @ts-expect-error Argument of type 'string' is not assignable to parameter of type '"apm" | "observability" | "siem"' + validFeatureIds.includes(a); const toReturn = Array.from(authorizedFeatures).flatMap((feature) => { - if (typeguard(feature)) return mapConsumerToIndexName[feature]; + if (isValidFeatureId(feature)) { + return mapConsumerToIndexName[feature]; + } return []; }); diff --git a/x-pack/plugins/rule_registry/server/routes/get_alert_index.ts b/x-pack/plugins/rule_registry/server/routes/get_alert_index.ts index bfafec919ebb2..a394fc7262570 100644 --- a/x-pack/plugins/rule_registry/server/routes/get_alert_index.ts +++ b/x-pack/plugins/rule_registry/server/routes/get_alert_index.ts @@ -11,6 +11,7 @@ import { transformError } from '@kbn/securitysolution-es-utils'; import { RacRequestHandlerContext } from '../types'; import { BASE_RAC_ALERTS_API_PATH } from '../../common/constants'; +import { validFeatureIds } from '../alert_data_client/alerts_client'; export const getAlertsIndexRoute = (router: IRouter) => { router.get( @@ -22,14 +23,9 @@ export const getAlertsIndexRoute = (router: IRouter) = }, }, async (context, request, response) => { - const APM_SERVER_FEATURE_ID = 'apm'; - const SERVER_APP_ID = 'siem'; try { const alertsClient = await context.rac.getAlertsClient(); - const indexName = await alertsClient.getAuthorizedAlertsIndices([ - APM_SERVER_FEATURE_ID, - SERVER_APP_ID, - ]); + const indexName = await alertsClient.getAuthorizedAlertsIndices(validFeatureIds); return response.ok({ body: { index_name: indexName }, }); diff --git a/x-pack/plugins/rule_registry/server/rule_data_client/types.ts b/x-pack/plugins/rule_registry/server/rule_data_client/types.ts index 5e168df32521f..458f3ccbef9bb 100644 --- a/x-pack/plugins/rule_registry/server/rule_data_client/types.ts +++ b/x-pack/plugins/rule_registry/server/rule_data_client/types.ts @@ -11,7 +11,7 @@ import { ElasticsearchClient } from 'kibana/server'; import { FieldDescriptor } from 'src/plugins/data/server'; import { ESSearchRequest, ESSearchResponse } from 'src/core/types/elasticsearch'; import { TechnicalRuleDataFieldName } from '../../common/technical_rule_data_field_names'; -import { mapConsumerToIndexName } from '../alert_data_client/alerts_client'; +import { ValidFeatureIds } from '../alert_data_client/alerts_client'; export interface RuleDataReader { search( @@ -38,8 +38,6 @@ export interface IRuleDataClient { createOrUpdateWriteTarget(options: { namespace?: string }): Promise; } -type ValidFeatureIds = keyof typeof mapConsumerToIndexName; - export interface RuleDataClientConstructorOptions { getClusterClient: () => Promise; ready: () => Promise;