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

[Rules migration] Add Integrations column (#11387) #204639

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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 @@ -25,6 +25,8 @@ export const SIEM_RULE_MIGRATION_INSTALL_TRANSLATED_PATH =
`${SIEM_RULE_MIGRATION_PATH}/install_translated` as const;
export const SIEM_RULE_MIGRATIONS_PREBUILT_RULES_PATH =
`${SIEM_RULE_MIGRATION_PATH}/prebuilt_rules` as const;
export const SIEM_RULE_MIGRATIONS_INTEGRATIONS_PATH =
`${SIEM_RULE_MIGRATION_PATH}/integrations` as const;

export const SIEM_RULE_MIGRATION_RESOURCES_PATH = `${SIEM_RULE_MIGRATION_PATH}/resources` as const;
export const SIEM_RULE_MIGRATION_RESOURCES_MISSING_PATH =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../../rule_migration.gen';
import { NonEmptyString } from '../../../../api/model/primitives.gen';
import { ConnectorId, LangSmithOptions } from '../../common.gen';
import { RelatedIntegration } from '../../../../api/detection_engine/model/rule_schema/common_attributes.gen';

export type CreateRuleMigrationRequestParams = z.infer<typeof CreateRuleMigrationRequestParams>;
export const CreateRuleMigrationRequestParams = z.object({
Expand Down Expand Up @@ -79,6 +80,24 @@ export const GetRuleMigrationResponse = z.object({
data: z.array(RuleMigration),
});

export type GetRuleMigrationIntegrationsRequestParams = z.infer<
typeof GetRuleMigrationIntegrationsRequestParams
>;
export const GetRuleMigrationIntegrationsRequestParams = z.object({
migration_id: NonEmptyString,
});
export type GetRuleMigrationIntegrationsRequestParamsInput = z.input<
typeof GetRuleMigrationIntegrationsRequestParams
>;

/**
* The map of related integrations, with the integration id as a key
*/
export type GetRuleMigrationIntegrationsResponse = z.infer<
typeof GetRuleMigrationIntegrationsResponse
>;
export const GetRuleMigrationIntegrationsResponse = z.object({}).catchall(RelatedIntegration);

export type GetRuleMigrationPrebuiltRulesRequestParams = z.infer<
typeof GetRuleMigrationPrebuiltRulesRequestParams
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ paths:
additionalProperties:
$ref: '../../rule_migration.schema.yaml#/components/schemas/PrebuiltRuleVersion'

/internal/siem_migrations/rules/{migration_id}/integrations:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have the {migration_id} in the path? I am confused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be /internal/siem_migrations/rules/integrations

get:
summary: Retrieves all related integrations for a specific migration
operationId: GetRuleMigrationIntegrations
x-codegen-enabled: true
x-internal: true
description: Retrieves all related integrations
tags:
- SIEM Rule Migrations
parameters:
- name: migration_id
in: path
required: true
schema:
description: The migration id to retrieve related integrations for
$ref: '../../../../../common/api/model/primitives.schema.yaml#/components/schemas/NonEmptyString'
responses:
200:
description: Indicates that related integrations have been retrieved correctly.
content:
application/json:
schema:
type: object
description: The map of related integrations, with the integration id as a key
additionalProperties:
$ref: '../../../../../common/api/detection_engine/model/rule_schema/common_attributes.schema.yaml#/components/schemas/RelatedIntegration'

# Rule migration resources APIs

/internal/siem_migrations/rules/{migration_id}/resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SIEM_RULE_MIGRATION_RESOURCES_MISSING_PATH,
SIEM_RULE_MIGRATION_RESOURCES_PATH,
SIEM_RULE_MIGRATIONS_PREBUILT_RULES_PATH,
SIEM_RULE_MIGRATIONS_INTEGRATIONS_PATH,
} from '../../../../common/siem_migrations/constants';
import type {
CreateRuleMigrationRequestBody,
Expand All @@ -39,6 +40,7 @@ import type {
UpsertRuleMigrationResourcesResponse,
GetRuleMigrationPrebuiltRulesResponse,
UpdateRuleMigrationResponse,
GetRuleMigrationIntegrationsResponse,
} from '../../../../common/siem_migrations/model/api/rules/rule_migration.gen';

export interface GetRuleMigrationStatsParams {
Expand Down Expand Up @@ -279,6 +281,23 @@ export const getRuleMigrationsPrebuiltRules = async ({
);
};

export interface GetRelatedIntegrationsParams {
/** `id` of the migration to get related integrations for */
migrationId: string;
/** Optional AbortSignal for cancelling request */
signal?: AbortSignal;
}
/** Retrieves related integrations for a specific migration. */
export const getRelatedIntegrations = async ({
migrationId,
signal,
}: GetRelatedIntegrationsParams): Promise<GetRuleMigrationIntegrationsResponse> => {
return KibanaServices.get().http.get<GetRuleMigrationIntegrationsResponse>(
replaceParams(SIEM_RULE_MIGRATIONS_INTEGRATIONS_PATH, { migration_id: migrationId }),
{ version: '1', signal }
);
};

export interface UpdateRulesParams {
/** The list of migration rules data to update */
rulesToUpdate: UpdateRuleMigrationData[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@elastic/eui';
import React, { useCallback, useMemo, useState } from 'react';

import type { RelatedIntegration, RuleResponse } from '../../../../../common/api/detection_engine';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import type { RuleMigration } from '../../../../../common/siem_migrations/model/rule_migration.gen';
import { EmptyMigration } from './empty_migration';
Expand All @@ -33,6 +34,7 @@ import { BulkActions } from './bulk_actions';
import { SearchField } from './search_field';
import { RuleTranslationResult } from '../../../../../common/siem_migrations/constants';
import * as i18n from './translations';
import { useGetRelatedIntegrations } from '../../service/hooks/use_get_integrations';

const DEFAULT_PAGE_SIZE = 10;
const DEFAULT_SORT_FIELD = 'translation_result';
Expand Down Expand Up @@ -64,6 +66,9 @@ export const MigrationRulesTable: React.FC<MigrationRulesTableProps> = React.mem
const { data: prebuiltRules = {}, isLoading: isPrebuiltRulesLoading } =
useGetMigrationPrebuiltRules(migrationId);

const { integrations, isLoading: isIntegrationsLoading } =
useGetRelatedIntegrations(migrationId);

const {
data: { ruleMigrations, total } = { ruleMigrations: [], total: 0 },
isLoading: isDataLoading,
Expand Down Expand Up @@ -180,7 +185,12 @@ export const MigrationRulesTable: React.FC<MigrationRulesTableProps> = React.mem
[addError, installTranslatedMigrationRules]
);

const isLoading = isStatsLoading || isPrebuiltRulesLoading || isDataLoading || isTableLoading;
const isLoading =
isStatsLoading ||
isPrebuiltRulesLoading ||
isIntegrationsLoading ||
isDataLoading ||
isTableLoading;

const ruleActionsFactory = useCallback(
(ruleMigration: RuleMigration, closeRulePreview: () => void) => {
Expand Down Expand Up @@ -221,29 +231,49 @@ export const MigrationRulesTable: React.FC<MigrationRulesTableProps> = React.mem
[installSingleRule, isLoading]
);

const getMigrationRule = useCallback(
const getMigrationRuleData = useCallback(
(ruleId: string) => {
if (!isLoading && ruleMigrations.length) {
return ruleMigrations.find((item) => item.id === ruleId);
if (!isLoading && ruleMigrations.length && integrations) {
const ruleMigration = ruleMigrations.find((item) => item.id === ruleId);
let matchedPrebuiltRule: RuleResponse | undefined;
const relatedIntegrations: RelatedIntegration[] = [];
if (ruleMigration) {
// Find matched prebuilt rule if any and prioritize its installed version
const matchedPrebuiltRuleVersion = ruleMigration.elastic_rule?.prebuilt_rule_id
? prebuiltRules[ruleMigration.elastic_rule.prebuilt_rule_id]
: undefined;
matchedPrebuiltRule =
matchedPrebuiltRuleVersion?.current ?? matchedPrebuiltRuleVersion?.target;

if (matchedPrebuiltRule?.related_integrations) {
relatedIntegrations.push(...matchedPrebuiltRule.related_integrations);
} else if (ruleMigration.elastic_rule?.integration_id) {
const integration = integrations[ruleMigration.elastic_rule.integration_id];
if (integration) {
relatedIntegrations.push(integration);
}
}
}
return { ruleMigration, matchedPrebuiltRule, relatedIntegrations };
}
},
[isLoading, ruleMigrations]
[integrations, isLoading, prebuiltRules, ruleMigrations]
);

const {
migrationRuleDetailsFlyout: rulePreviewFlyout,
openMigrationRuleDetails: openRulePreview,
} = useMigrationRuleDetailsFlyout({
isLoading,
prebuiltRules,
getMigrationRule,
getMigrationRuleData,
ruleActionsFactory,
});

const rulesColumns = useMigrationRulesTableColumns({
disableActions: isTableLoading,
openMigrationRuleDetails: openRulePreview,
installMigrationRule: installSingleRule,
getMigrationRuleData,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const createAuthorColumn = (): TableColumn => {
return <Author isPrebuiltRule={!!rule.elastic_rule?.prebuilt_rule_id} />;
},
sortable: true,
truncateText: true,
width: '10%',
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 React from 'react';
import type { RelatedIntegration } from '../../../../../common/api/detection_engine';
import { IntegrationsPopover } from '../../../../detections/components/rules/related_integrations/integrations_popover';
import type { RuleMigration } from '../../../../../common/siem_migrations/model/rule_migration.gen';
import * as i18n from './translations';
import type { TableColumn } from './constants';

export const createIntegrationsColumn = ({
getMigrationRuleData,
}: {
getMigrationRuleData: (
ruleId: string
) => { relatedIntegrations?: RelatedIntegration[] } | undefined;
}): TableColumn => {
return {
field: 'elastic_rule.integration_id',
name: i18n.COLUMN_INTEGRATIONS,
render: (_, rule: RuleMigration) => {
const migrationRuleData = getMigrationRuleData(rule.id);
const relatedIntegrations = migrationRuleData?.relatedIntegrations;
if (relatedIntegrations == null || relatedIntegrations.length === 0) {
return null;
}
return <IntegrationsPopover relatedIntegrations={relatedIntegrations} />;
},
truncateText: true,
width: '143px',
align: 'center',
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,10 @@ export const COLUMN_UPDATED = i18n.translate(
defaultMessage: 'Updated',
}
);

export const COLUMN_INTEGRATIONS = i18n.translate(
'xpack.securitySolution.siemMigrations.rules.tableColumn.integrationsLabel',
{
defaultMessage: 'Integrations',
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const createUpdatedColumn = (): TableColumn => {
<FormattedRelativePreferenceDate value={value} dateFormat="M/D/YY" />
),
sortable: true,
truncateText: false,
truncateText: true,
align: 'center',
width: '10%',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import type { ReactNode } from 'react';
import React, { useCallback, useState, useMemo } from 'react';
import type { EuiTabbedContentTab } from '@elastic/eui';
import type {
PrebuiltRuleVersion,
RuleMigration,
} from '../../../../common/siem_migrations/model/rule_migration.gen';
import type { RuleResponse } from '../../../../common/api/detection_engine';
import type { RuleMigration } from '../../../../common/siem_migrations/model/rule_migration.gen';
import { MigrationRuleDetailsFlyout } from '../components/rule_details_flyout';

interface UseMigrationRuleDetailsFlyoutParams {
isLoading?: boolean;
prebuiltRules: Record<string, PrebuiltRuleVersion>;
getMigrationRule: (ruleId: string) => RuleMigration | undefined;
getMigrationRuleData: (ruleId: string) =>
| {
ruleMigration?: RuleMigration;
matchedPrebuiltRule?: RuleResponse;
}
| undefined;
ruleActionsFactory: (ruleMigration: RuleMigration, closeRulePreview: () => void) => ReactNode;
extraTabsFactory?: (ruleMigration: RuleMigration) => EuiTabbedContentTab[];
}
Expand All @@ -30,47 +32,42 @@ interface UseMigrationRuleDetailsFlyoutResult {

export function useMigrationRuleDetailsFlyout({
isLoading,
prebuiltRules,
getMigrationRule,
getMigrationRuleData,
extraTabsFactory,
ruleActionsFactory,
}: UseMigrationRuleDetailsFlyoutParams): UseMigrationRuleDetailsFlyoutResult {
const [migrationRuleId, setMigrationRuleId] = useState<string | undefined>();

const ruleMigration = useMemo(() => {
const migrationRuleData = useMemo(() => {
if (migrationRuleId) {
return getMigrationRule(migrationRuleId);
return getMigrationRuleData(migrationRuleId);
}
}, [getMigrationRule, migrationRuleId]);
const matchedPrebuiltRule = useMemo(() => {
if (ruleMigration) {
// Find matched prebuilt rule if any and prioritize its installed version
const matchedPrebuiltRuleVersion = ruleMigration.elastic_rule?.prebuilt_rule_id
? prebuiltRules[ruleMigration.elastic_rule.prebuilt_rule_id]
: undefined;
return matchedPrebuiltRuleVersion?.current ?? matchedPrebuiltRuleVersion?.target;
}
}, [prebuiltRules, ruleMigration]);
}, [getMigrationRuleData, migrationRuleId]);

const openMigrationRuleDetails = useCallback((rule: RuleMigration) => {
setMigrationRuleId(rule.id);
}, []);
const closeMigrationRuleDetails = useCallback(() => setMigrationRuleId(undefined), []);

const ruleActions = useMemo(
() => ruleMigration && ruleActionsFactory(ruleMigration, closeMigrationRuleDetails),
[ruleMigration, ruleActionsFactory, closeMigrationRuleDetails]
() =>
migrationRuleData?.ruleMigration &&
ruleActionsFactory(migrationRuleData.ruleMigration, closeMigrationRuleDetails),
[migrationRuleData?.ruleMigration, ruleActionsFactory, closeMigrationRuleDetails]
);
const extraTabs = useMemo(
() => (ruleMigration && extraTabsFactory ? extraTabsFactory(ruleMigration) : []),
[ruleMigration, extraTabsFactory]
() =>
migrationRuleData?.ruleMigration && extraTabsFactory
? extraTabsFactory(migrationRuleData.ruleMigration)
: [],
[extraTabsFactory, migrationRuleData?.ruleMigration]
);

return {
migrationRuleDetailsFlyout: ruleMigration && (
migrationRuleDetailsFlyout: migrationRuleData?.ruleMigration && (
<MigrationRuleDetailsFlyout
ruleMigration={ruleMigration}
matchedPrebuiltRule={matchedPrebuiltRule}
ruleMigration={migrationRuleData.ruleMigration}
matchedPrebuiltRule={migrationRuleData.matchedPrebuiltRule}
size="l"
closeFlyout={closeMigrationRuleDetails}
ruleActions={ruleActions}
Expand Down
Loading