Skip to content

Commit

Permalink
[Cloud Security] Evaluated column buttons (elastic#176498)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSh authored and CoenWarmer committed Feb 15, 2024
1 parent 4bd460e commit becf69a
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 { useBenchmarkDynamicValues } from './use_benchmark_dynamic_values';
import { renderHook } from '@testing-library/react-hooks/dom';
import { useCspIntegrationLink } from '../navigation/use_csp_integration_link';
import { BenchmarksCisId } from '../../../common/types/benchmarks/v2';

jest.mock('../navigation/use_csp_integration_link');

describe('useBenchmarkDynamicValues', () => {
const setupMocks = (cspmIntegrationLink: string, kspmIntegrationLink: string) => {
(useCspIntegrationLink as jest.Mock)
.mockReturnValueOnce(cspmIntegrationLink)
.mockReturnValueOnce(kspmIntegrationLink);
};

it('should return the correct dynamic benchmark values for each provided benchmark ID', () => {
setupMocks('cspm-integration-link', 'kspm-integration-link');
const { result } = renderHook(() => useBenchmarkDynamicValues());

const benchmarkValuesCisAws = result.current.getBenchmarkDynamicValues('cis_aws', 3);
expect(benchmarkValuesCisAws).toEqual({
integrationType: 'CSPM',
integrationName: 'AWS',
resourceName: 'Accounts',
resourceCountLabel: 'accounts',
integrationLink: 'cspm-integration-link',
learnMoreLink: 'https://ela.st/cspm-get-started',
});

const benchmarkValuesCisGcp = result.current.getBenchmarkDynamicValues('cis_gcp', 0);
expect(benchmarkValuesCisGcp).toEqual({
integrationType: 'CSPM',
integrationName: 'GCP',
resourceName: 'Projects',
resourceCountLabel: 'projects',
integrationLink: 'cspm-integration-link',
learnMoreLink: 'https://ela.st/cspm-get-started',
});

const benchmarkValuesCisAzure = result.current.getBenchmarkDynamicValues('cis_azure', 1);
expect(benchmarkValuesCisAzure).toEqual({
integrationType: 'CSPM',
integrationName: 'Azure',
resourceName: 'Subscriptions',
resourceCountLabel: 'subscription',
integrationLink: 'cspm-integration-link',
learnMoreLink: 'https://ela.st/cspm-get-started',
});

const benchmarkValuesCisK8s = result.current.getBenchmarkDynamicValues('cis_k8s', 0);
expect(benchmarkValuesCisK8s).toEqual({
integrationType: 'KSPM',
integrationName: 'Kubernetes',
resourceName: 'Clusters',
resourceCountLabel: 'clusters',
integrationLink: 'kspm-integration-link',
learnMoreLink: 'https://ela.st/kspm-get-started',
});

const benchmarkValuesCisEks = result.current.getBenchmarkDynamicValues('cis_eks');
expect(benchmarkValuesCisEks).toEqual({
integrationType: 'KSPM',
integrationName: 'EKS',
resourceName: 'Clusters',
resourceCountLabel: 'clusters',
integrationLink: 'kspm-integration-link',
learnMoreLink: 'https://ela.st/kspm-get-started',
});

const benchmarkValuesInvalid = result.current.getBenchmarkDynamicValues(
'invalid_benchmark' as BenchmarksCisId
);
expect(benchmarkValuesInvalid).toEqual({});
});

it('should return the correct resource plurals based on the provided resource count', () => {
const { result } = renderHook(() => useBenchmarkDynamicValues());

const benchmarkValuesCisAws = result.current.getBenchmarkDynamicValues('cis_aws', 3);
expect(benchmarkValuesCisAws.resourceCountLabel).toBe('accounts');

const benchmarkValuesCisGcp = result.current.getBenchmarkDynamicValues('cis_gcp', 0);
expect(benchmarkValuesCisGcp.resourceCountLabel).toBe('projects');

const benchmarkValuesCisAzure = result.current.getBenchmarkDynamicValues('cis_azure', 1);
expect(benchmarkValuesCisAzure.resourceCountLabel).toBe('subscription');

const benchmarkValuesCisK8s = result.current.getBenchmarkDynamicValues('cis_k8s', 0);
expect(benchmarkValuesCisK8s.resourceCountLabel).toBe('clusters');

const benchmarkValuesCisEks = result.current.getBenchmarkDynamicValues('cis_eks');
expect(benchmarkValuesCisEks.resourceCountLabel).toBe('clusters');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { useCspIntegrationLink } from '../navigation/use_csp_integration_link';
import { CSPM_POLICY_TEMPLATE, KSPM_POLICY_TEMPLATE } from '../../../common/constants';
import { BenchmarksCisId } from '../../../common/types/benchmarks/v2';

type BenchmarkDynamicNames =
| {
integrationType: 'CSPM';
integrationName: 'AWS';
resourceName: 'Accounts';
}
| {
integrationType: 'CSPM';
integrationName: 'GCP';
resourceName: 'Projects';
}
| {
integrationType: 'CSPM';
integrationName: 'Azure';
resourceName: 'Subscriptions';
}
| {
integrationType: 'KSPM';
integrationName: 'Kubernetes';
resourceName: 'Clusters';
}
| {
integrationType: 'KSPM';
integrationName: 'EKS';
resourceName: 'Clusters';
};

export type BenchmarkDynamicValues = BenchmarkDynamicNames & {
resourceCountLabel: string;
integrationLink: string;
learnMoreLink: string;
};

export type GetBenchmarkDynamicValues = (
benchmarkId: BenchmarksCisId,
resourceCount?: number
) => BenchmarkDynamicValues;

export const useBenchmarkDynamicValues = () => {
const cspmIntegrationLink = useCspIntegrationLink(CSPM_POLICY_TEMPLATE) || '';
const kspmIntegrationLink = useCspIntegrationLink(KSPM_POLICY_TEMPLATE) || '';

/**
* Retrieves dynamic benchmark values based on the provided benchmark ID and resource count.
*
* @param {BenchmarksCisId} benchmarkId - The benchmark ID.
* @param {number} [resourceCount] - The count of resources (optional).
* @returns {BenchmarkDynamicValues} The dynamic benchmark values including integration details,
* resource name, resource count label in plurals/singular, integration link, and learn more link.
*
* @example
* const benchmarkValues = getBenchmarkDynamicValues('cis_aws', 3);
* // Returns:
* // {
* // integrationType: 'CSPM',
* // integrationName: 'AWS',
* // resourceName: 'Accounts',
* // resourceCountLabel: 'accounts',
* // integrationLink: 'cspm-integration-link',
* // learnMoreLink: 'https://ela.st/cspm-get-started'
* // }
*/
const getBenchmarkDynamicValues: GetBenchmarkDynamicValues = (
benchmarkId: BenchmarksCisId,
resourceCount?: number
): BenchmarkDynamicValues => {
switch (benchmarkId) {
case 'cis_aws':
return {
integrationType: 'CSPM',
integrationName: 'AWS',
resourceName: 'Accounts',
resourceCountLabel: i18n.translate('xpack.csp.benchmarkDynamicValues.AwsAccountPlural', {
defaultMessage: '{resourceCount, plural, one {account} other {accounts}}',
values: { resourceCount: resourceCount || 0 },
}),
integrationLink: cspmIntegrationLink,
learnMoreLink: 'https://ela.st/cspm-get-started',
};
case 'cis_gcp':
return {
integrationType: 'CSPM',
integrationName: 'GCP',
resourceName: 'Projects',
resourceCountLabel: i18n.translate('xpack.csp.benchmarkDynamicValues.GcpAccountPlural', {
defaultMessage: '{resourceCount, plural, one {project} other {projects}}',
values: { resourceCount: resourceCount || 0 },
}),
integrationLink: cspmIntegrationLink,
learnMoreLink: 'https://ela.st/cspm-get-started',
};
case 'cis_azure':
return {
integrationType: 'CSPM',
integrationName: 'Azure',
resourceName: 'Subscriptions',
resourceCountLabel: i18n.translate(
'xpack.csp.benchmarkDynamicValues.AzureAccountPlural',
{
defaultMessage: '{resourceCount, plural, one {subscription} other {subscriptions}}',
values: { resourceCount: resourceCount || 0 },
}
),
integrationLink: cspmIntegrationLink,
learnMoreLink: 'https://ela.st/cspm-get-started',
};
case 'cis_k8s':
return {
integrationType: 'KSPM',
integrationName: 'Kubernetes',
resourceName: 'Clusters',
resourceCountLabel: i18n.translate('xpack.csp.benchmarkDynamicValues.K8sAccountPlural', {
defaultMessage: '{resourceCount, plural, one {cluster} other {clusters}}',
values: { resourceCount: resourceCount || 0 },
}),
integrationLink: kspmIntegrationLink,
learnMoreLink: 'https://ela.st/kspm-get-started',
};
case 'cis_eks':
return {
integrationType: 'KSPM',
integrationName: 'EKS',
resourceName: 'Clusters',
resourceCountLabel: i18n.translate('xpack.csp.benchmarkDynamicValues.EksAccountPlural', {
defaultMessage: '{resourceCount, plural, one {cluster} other {clusters}}',
values: { resourceCount: resourceCount || 0 },
}),
integrationLink: kspmIntegrationLink,
learnMoreLink: 'https://ela.st/kspm-get-started',
};
default:
return {} as BenchmarkDynamicValues;
}
};

return { getBenchmarkDynamicValues };
};
Loading

0 comments on commit becf69a

Please sign in to comment.