Skip to content

Commit

Permalink
javascript is not python
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley14 committed Jun 30, 2021
1 parent 3377e4b commit 879c0c0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class AlertingAuthorization {
* used by the RAC/Alerts client
*/
public async getAugmentRuleTypesWithAuthorization(
featureIds: string[],
featureIds: readonly string[],
operations: Array<ReadOperations | WriteOperations>,
authorizationEntity: AlertingAuthorizationEntity
): Promise<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};

Expand All @@ -75,7 +78,7 @@ export class AlertsClient {
operations: Array<ReadOperations | WriteOperations>
) {
return this.authorization.getAugmentRuleTypesWithAuthorization(
featureIds.length !== 0 ? featureIds : Object.keys(mapConsumerToIndexName),
featureIds.length !== 0 ? featureIds : validFeatureIds,
operations,
AlertingAuthorizationEntity.Alert
);
Expand Down Expand Up @@ -203,10 +206,10 @@ export class AlertsClient {
}

public async getAuthorizedAlertsIndices(
featureIds: Array<keyof typeof mapConsumerToIndexName>
featureIds: readonly ValidFeatureIds[]
): Promise<string[] | undefined> {
const augmentedRuleTypes = await this.authorization.getAugmentRuleTypesWithAuthorization(
featureIds,
featureIds as string[],
[ReadOperations.Find, ReadOperations.Get, WriteOperations.Update],
AlertingAuthorizationEntity.Alert
);
Expand All @@ -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 [];
});

Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/rule_registry/server/routes/get_alert_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RacRequestHandlerContext>) => {
router.get(
Expand All @@ -22,14 +23,9 @@ export const getAlertsIndexRoute = (router: IRouter<RacRequestHandlerContext>) =
},
},
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 },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSearchRequest extends ESSearchRequest>(
Expand All @@ -38,8 +38,6 @@ export interface IRuleDataClient {
createOrUpdateWriteTarget(options: { namespace?: string }): Promise<void>;
}

type ValidFeatureIds = keyof typeof mapConsumerToIndexName;

export interface RuleDataClientConstructorOptions {
getClusterClient: () => Promise<ElasticsearchClient>;
ready: () => Promise<void>;
Expand Down

0 comments on commit 879c0c0

Please sign in to comment.