Skip to content

Commit

Permalink
refactor cspRuleTemplate to cspRule
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenIdo committed Dec 7, 2023
1 parent fcf536a commit 92a0e64
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 30 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/cloud_security_posture/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const VULNERABILITIES_DASHBOARD_ROUTE_PATH =
export const BENCHMARKS_ROUTE_PATH = '/internal/cloud_security_posture/benchmarks';
export const BENCHMARKS_API_CURRENT_VERSION = '1';

export const FIND_CSP_RULE_ROUTE_PATH = '/internal/cloud_security_posture/rules/_find';
export const FIND_CSP_RULE_API_CURRENT_VERSION = '1';
export const FIND_CSP_BENCHMARK_RULE_ROUTE_PATH = '/internal/cloud_security_posture/rules/_find';
export const FIND_CSP_BENCHMARK_RULE_API_CURRENT_VERSION = '1';

export const DETECTION_RULE_ALERTS_STATUS_API_CURRENT_VERSION = '1';
export const DETECTION_RULE_RULES_API_CURRENT_VERSION = '2023-10-31';
Expand Down Expand Up @@ -85,7 +85,7 @@ export const INTERNAL_FEATURE_FLAGS = {
showFindingFlyoutEvidence: true,
} as const;

export const CSP_RULE_SAVED_OBJECT_TYPE = 'csp-rule-template';
export const CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE = 'csp-rule-template';

export const CLOUDBEAT_VANILLA = 'cloudbeat/cis_k8s';
export const CLOUDBEAT_EKS = 'cloudbeat/cis_eks';
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/cloud_security_posture/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import {
CLOUD_SECURITY_POSTURE_PACKAGE_NAME,
CLOUDBEAT_VANILLA,
CSP_RULE_SAVED_OBJECT_TYPE,
CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
AWS_CREDENTIALS_TYPE_TO_FIELDS_MAP,
GCP_CREDENTIALS_TYPE_TO_FIELDS_MAP,
AZURE_CREDENTIALS_TYPE_TO_FIELDS_MAP,
Expand Down Expand Up @@ -50,8 +50,10 @@ export const extractErrorMessage = (e: unknown, defaultMessage = 'Unknown Error'
};

export const getBenchmarkFilter = (type: BenchmarkId, section?: RuleSection): string =>
`${CSP_RULE_SAVED_OBJECT_TYPE}.attributes.metadata.benchmark.id: "${type}"${
section ? ` AND ${CSP_RULE_SAVED_OBJECT_TYPE}.attributes.metadata.section: "${section}"` : ''
`${CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE}.attributes.metadata.benchmark.id: "${type}"${
section
? ` AND ${CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE}.attributes.metadata.section: "${section}"`
: ''
}`;

export const isEnabledBenchmarkInputType = (input: PackagePolicyInput | NewPackagePolicyInput) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import { RulesContainer } from './rules_container';
import { render, screen } from '@testing-library/react';
import { QueryClient } from '@tanstack/react-query';
import { useFindCspBenchmarkRule } from './use_csp_rules';
import { useFindCspBenchmarkRule } from './use_csp_benchmark_rules';
import * as TEST_SUBJECTS from './test_subjects';
import { Chance } from 'chance';
import { TestProvider } from '../../test/test_provider';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { CspBenchmarkRule } from '@kbn/cloud-security-posture-plugin/common/type
import { extractErrorMessage } from '../../../common/utils/helpers';
import { RulesTable } from './rules_table';
import { RulesTableHeader } from './rules_table_header';
import { useFindCspBenchmarkRule, type RulesQuery, type RulesQueryResult } from './use_csp_rules';
import {
useFindCspBenchmarkRule,
type RulesQuery,
type RulesQueryResult,
} from './use_csp_benchmark_rules';
import * as TEST_SUBJECTS from './test_subjects';
import { RuleFlyout } from './rules_flyout';
import { LOCAL_STORAGE_PAGE_SIZE_RULES_KEY } from '../../common/constants';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { useQuery } from '@tanstack/react-query';
import { useKibana } from '../../common/hooks/use_kibana';

import {
CSP_RULE_SAVED_OBJECT_TYPE,
FIND_CSP_RULE_API_CURRENT_VERSION,
FIND_CSP_RULE_ROUTE_PATH,
CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
FIND_CSP_BENCHMARK_RULE_API_CURRENT_VERSION,
FIND_CSP_BENCHMARK_RULE_ROUTE_PATH,
} from '../../../common/constants';
import {
FindCspBenchmarkRuleRequest,
Expand All @@ -31,11 +31,11 @@ export const useFindCspBenchmarkRule = (
const { http } = useKibana().services;

return useQuery(
[CSP_RULE_SAVED_OBJECT_TYPE, { section, search, page, perPage, packagePolicyId }],
[CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE, { section, search, page, perPage, packagePolicyId }],
() => {
return http.get<FindCspBenchmarkRuleResponse>(FIND_CSP_RULE_ROUTE_PATH, {
return http.get<FindCspBenchmarkRuleResponse>(FIND_CSP_BENCHMARK_RULE_ROUTE_PATH, {
query: { packagePolicyId, page, perPage, search, section },
version: FIND_CSP_RULE_API_CURRENT_VERSION,
version: FIND_CSP_BENCHMARK_RULE_API_CURRENT_VERSION,
});
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { SavedObjectsClientContract } from '@kbn/core/server';
import { transformError } from '@kbn/securitysolution-es-utils';
import type { AgentPolicy, ListResult, PackagePolicy } from '@kbn/fleet-plugin/common';
import { CspBenchmarkRule } from '@kbn/cloud-security-posture-plugin/common/types/latest';
import { CSP_RULE_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE } from '../../../common/constants';
import {
BENCHMARKS_ROUTE_PATH,
CLOUD_SECURITY_POSTURE_PACKAGE_NAME,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const getRulesCountForPolicy = async (
benchmarkId: BenchmarkId
): Promise<number> => {
const rules = await soClient.find<CspBenchmarkRule>({
type: CSP_RULE_SAVED_OBJECT_TYPE,
type: CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
filter: getBenchmarkFilter(benchmarkId),
perPage: 0,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getSortedCspBenchmarkRulesTemplates } from './find_csp_rule';
import { getSortedCspBenchmarkRulesTemplates } from './find_csp_benchmark_rule';
import { CspBenchmarkRule } from '@kbn/cloud-security-posture-plugin/common/types/latest';

describe('getSortedCspBenchmarkRules', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@kbn/cloud-security-posture-plugin/common/types/latest';
import { getBenchmarkFromPackagePolicy } from '../../../../common/utils/helpers';

import { FIND_CSP_RULE_ROUTE_PATH } from '../../../../common/constants';
import { FIND_CSP_BENCHMARK_RULE_ROUTE_PATH } from '../../../../common/constants';
import { CspRouter } from '../../../types';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../benchmarks/benchmarks';
import { findRuleHandler as findRuleHandlerV1 } from './v1';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const defineFindCspBenchmarkRuleRoute = (router: CspRouter) =>
router.versioned
.get({
access: 'internal',
path: FIND_CSP_RULE_ROUTE_PATH,
path: FIND_CSP_BENCHMARK_RULE_ROUTE_PATH,
})
.addVersion(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { getBenchmarkFilter } from '../../../../common/utils/helpers';
import { CSP_RULE_SAVED_OBJECT_TYPE } from '../../../../common/constants';
import { CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE } from '../../../../common/constants';
import {
getBenchmarkIdFromPackagePolicyId,
getSortedCspBenchmarkRulesTemplates,
} from './find_csp_rule';
} from './find_csp_benchmark_rule';

import type {
CspBenchmarkRule,
Expand All @@ -35,7 +35,7 @@ export const findRuleHandler = async (
: await getBenchmarkIdFromPackagePolicyId(soClient, options.packagePolicyId!);

const cspCspBenchmarkRulesSo = await soClient.find<CspBenchmarkRule>({
type: CSP_RULE_SAVED_OBJECT_TYPE,
type: CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
searchFields: options.searchFields,
search: options.search ? `"${options.search}"*` : '',
page: options.page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { defineGetComplianceDashboardRoute } from './compliance_dashboard/compli
import { defineGetVulnerabilitiesDashboardRoute } from './vulnerabilities_dashboard/vulnerabilities_dashboard';
import { defineGetBenchmarksRoute } from './benchmarks/benchmarks';
import { defineGetCspStatusRoute } from './status/status';
import { defineFindCspBenchmarkRuleRoute } from './rules/find/find_csp_rule';
import { defineFindCspBenchmarkRuleRoute } from './rules/find/find_csp_benchmark_rule';
import { defineGetDetectionEngineAlertsStatus } from './detection_engine/get_detection_engine_alerts_count_by_rule_tags';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import { SECURITY_SOLUTION_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { SavedObjectsType } from '@kbn/core/server';
import { CSP_RULE_SAVED_OBJECT_TYPE } from '../../common/constants';
import { CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE } from '../../common/constants';
import { cspBenchmarkRuleMigrations } from './migrations';
import { cspBenchmarkRuleSavedObjectMapping } from './mappings';
import { rulesV1, rulesV2, rulesV3 } from '@kbn/cloud-security-posture-plugin/common/types/';

export const cspBenchmarkRule: SavedObjectsType = {
name: CSP_RULE_SAVED_OBJECT_TYPE,
name: CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
indexPattern: SECURITY_SOLUTION_SAVED_OBJECT_INDEX,
hidden: false,
namespaceType: 'agnostic',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export * from './csp_rule';
export * from './csp_benchmark_rule';
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { cspBenchmarkRuleMigrations } from './migrations';
import { rulesV1, rulesV2, rulesV3 } from '@kbn/cloud-security-posture-plugin/common/types/';
import { CspBenchmarkRule } from '@kbn/cloud-security-posture-plugin/common/types/latest';

import { CSP_RULE_SAVED_OBJECT_TYPE } from '../../common/constants';
import { CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE } from '../../common/constants';

export function setupSavedObjects(savedObjects: SavedObjectsServiceSetup) {
savedObjects.registerType<CspBenchmarkRule>({
name: CSP_RULE_SAVED_OBJECT_TYPE,
name: CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
indexPattern: SECURITY_SOLUTION_SAVED_OBJECT_INDEX,
hidden: false,
namespaceType: 'agnostic',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./status/status_unprivileged'));
loadTestFile(require.resolve('./status/status_indexing'));
loadTestFile(require.resolve('./benchmark'));
loadTestFile(require.resolve('./find_csp_rule'));
loadTestFile(require.resolve('./find_csp_benchmark_rule'));

// Place your tests files under this directory and add the following here:
// loadTestFile(require.resolve('./your test name'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./status/status_indexed'));
loadTestFile(require.resolve('./status/status_indexing'));
loadTestFile(require.resolve('./benchmark'));
loadTestFile(require.resolve('./find_csp_rule'));
loadTestFile(require.resolve('./find_csp_benchmark_rule'));
loadTestFile(require.resolve('./telemetry'));

// TODO: migrate status_unprivileged tests from stateful, if it feasible in serverless with the new security model
Expand Down

0 comments on commit 92a0e64

Please sign in to comment.