Skip to content

Commit

Permalink
Merge branch 'main' into task/unskip-tests-after-es-promotion-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Apr 12, 2023
2 parents 1b2ed2e + 263af61 commit ad25b21
Show file tree
Hide file tree
Showing 27 changed files with 881 additions and 466 deletions.
7 changes: 7 additions & 0 deletions src/dev/build/tasks/fetch_agent_versions_list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import fetch from 'node-fetch';
import pRetry from 'p-retry';

import { REPO_ROOT } from '@kbn/repo-info';
import { ToolingLog } from '@kbn/tooling-log';
Expand All @@ -14,6 +15,7 @@ import { FetchAgentVersionsList } from './fetch_agent_versions_list';
import { Build, Config, write } from '../lib';

jest.mock('node-fetch');
jest.mock('p-retry');
jest.mock('../lib');

const config = new Config(
Expand Down Expand Up @@ -46,9 +48,14 @@ const config = new Config(
);

const mockedFetch = fetch as jest.MockedFunction<typeof fetch>;
const mockedPRetry = pRetry as jest.MockedFunction<typeof pRetry>;
const mockedWrite = write as jest.MockedFunction<typeof write>;
const mockedBuild = new Build(config);

mockedPRetry.mockImplementation((fn: any) => {
return fn();
});

const processEnv = process.env;

describe('FetchAgentVersionsList', () => {
Expand Down
9 changes: 5 additions & 4 deletions src/dev/build/tasks/fetch_agent_versions_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
*/

import fetch from 'node-fetch';
import pRetry from 'p-retry';

import { ToolingLog } from '@kbn/tooling-log';
import { write, Task } from '../lib';

// Endpoint maintained by the web-team and hosted on the elastic website
const PRODUCT_VERSIONS_URL = 'https://www.elastic.co/api/product_versions';

const isPr = () =>
!!process.env.BUILDKITE_PULL_REQUEST && process.env.BUILDKITE_PULL_REQUEST !== 'false';

Expand All @@ -20,13 +24,10 @@ const getAvailableVersions = async (log: ToolingLog) => {
'Content-Type': 'application/json',
},
};
// Endpoint maintained by the web-team and hosted on the elastic website
// See https://github.com/elastic/website-development/issues/9331
const url = 'https://www.elastic.co/content/product_versions';
log.info('Fetching Elastic Agent versions list');

try {
const results = await fetch(url, options);
const results = await pRetry(() => fetch(PRODUCT_VERSIONS_URL, options), { retries: 3 });
const rawBody = await results.text();

if (results.status >= 400) {
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/cloud_security_posture/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const STATS_ROUTE_PATH = '/internal/cloud_security_posture/stats/{policy_
export const BENCHMARKS_ROUTE_PATH = '/internal/cloud_security_posture/benchmarks';

export const CLOUD_SECURITY_POSTURE_PACKAGE_NAME = 'cloud_security_posture';

// TODO: REMOVE CSP_LATEST_FINDINGS_DATA_VIEW and replace it with LATEST_FINDINGS_INDEX_PATTERN
export const CSP_LATEST_FINDINGS_DATA_VIEW = 'logs-cloud_security_posture.findings_latest-*';

export const FINDINGS_INDEX_NAME = 'logs-cloud_security_posture.findings';
Expand Down Expand Up @@ -96,4 +96,7 @@ export const POSTURE_TYPES: { [x: string]: PostureTypes } = {
[CSPM_POLICY_TEMPLATE]: CSPM_POLICY_TEMPLATE,
[VULN_MGMT_POLICY_TEMPLATE]: VULN_MGMT_POLICY_TEMPLATE,
[POSTURE_TYPE_ALL]: POSTURE_TYPE_ALL,
} as const;
};

export const VULNERABILITIES = 'vulnerabilities';
export const CONFIGURATIONS = 'configurations';
1 change: 1 addition & 0 deletions x-pack/plugins/cloud_security_posture/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface BaseCspSetupStatus {
kspm: BaseCspSetupBothPolicy;
vuln_mgmt: BaseCspSetupBothPolicy;
isPluginInitialized: boolean;
installedPackageVersion?: string | undefined;
}

export type CspSetupStatus = BaseCspSetupStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
import { useQuery } from '@tanstack/react-query';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { DataView } from '@kbn/data-plugin/common';
import { CSP_LATEST_FINDINGS_DATA_VIEW } from '../../../common/constants';
import { CspClientPluginStartDeps } from '../../types';

/**
* TODO: use perfected kibana data views
*/
export const useLatestFindingsDataView = () => {
export const useLatestFindingsDataView = (dataView: string) => {
const {
data: { dataViews },
} = useKibana<CspClientPluginStartDeps>().services;

const findDataView = async (): Promise<DataView> => {
const dataView = (await dataViews.find(CSP_LATEST_FINDINGS_DATA_VIEW))?.[0];
if (!dataView) {
throw new Error('Findings data view not found');
const dataViewObj = (await dataViews.find(dataView))?.[0];
if (!dataViewObj) {
throw new Error(`Data view not found [Name: {${dataView}}]`);
}

return dataView;
return dataViewObj;
};

return useQuery(['latest_findings_data_view'], findDataView);
return useQuery([`useDataView-${dataView}`], findDataView);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { STATUS_ROUTE_PATH } from '../../../common/constants';

const getCspSetupStatusQueryKey = 'csp_status_key';

export const useCspSetupStatusApi = ({
options,
}: { options?: UseQueryOptions<CspSetupStatus, unknown, CspSetupStatus> } = {}) => {
export const useCspSetupStatusApi = (
options?: UseQueryOptions<CspSetupStatus, unknown, CspSetupStatus>
) => {
const { http } = useKibana().services;
return useQuery<CspSetupStatus, unknown, CspSetupStatus>(
[getCspSetupStatusQueryKey],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/

import { pagePathGetters, pkgKeyFromPackageInfo } from '@kbn/fleet-plugin/public';
import type { PosturePolicyTemplate } from '../../../common/types';
import type { CloudSecurityPolicyTemplate } from '../../../common/types';
import { useCisKubernetesIntegration } from '../api/use_cis_kubernetes_integration';
import { useKibana } from '../hooks/use_kibana';

export const useCspIntegrationLink = (
policyTemplate: PosturePolicyTemplate
policyTemplate: CloudSecurityPolicyTemplate
): string | undefined => {
const { http } = useKibana().services;
const cisIntegration = useCisKubernetesIntegration();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import React, { ComponentProps } from 'react';
import { UseQueryResult } from '@tanstack/react-query';
import { CloudPosturePage } from './cloud_posture_page';
import { NoDataPage } from '@kbn/kibana-react-plugin/public';
import { useCspSetupStatusApi } from '../common/api/use_setup_status_api';
import { useCspIntegrationLink } from '../common/navigation/use_csp_integration_link';

const chance = new Chance();

Expand All @@ -35,19 +33,6 @@ jest.mock('../common/navigation/use_csp_integration_link');
describe('<CloudPosturePage />', () => {
beforeEach(() => {
jest.resetAllMocks();
(useCspSetupStatusApi as jest.Mock).mockImplementation(() =>
createReactQueryResponse({
status: 'success',
data: {
cspm: { status: 'indexed' },
kspm: { status: 'indexed' },
indicesDetails: [
{ index: 'logs-cloud_security_posture.findings_latest-default', status: 'not-empty' },
{ index: 'logs-cloud_security_posture.findings-default*', status: 'not-empty' },
],
},
})
);

(useSubscriptionStatus as jest.Mock).mockImplementation(() =>
createReactQueryResponse({
Expand All @@ -58,7 +43,9 @@ describe('<CloudPosturePage />', () => {
});

const renderCloudPosturePage = (
props: ComponentProps<typeof CloudPosturePage> = { children: null }
props: ComponentProps<typeof CloudPosturePage> = {
children: null,
}
) => {
const mockCore = coreMock.createStart();

Expand Down Expand Up @@ -147,69 +134,6 @@ describe('<CloudPosturePage />', () => {
expect(screen.queryByTestId(ERROR_STATE_TEST_SUBJECT)).not.toBeInTheDocument();
});

it('renders integrations installation prompt if integration is not installed', () => {
(useCspSetupStatusApi as jest.Mock).mockImplementation(() =>
createReactQueryResponse({
status: 'success',
data: {
kspm: { status: 'not-installed' },
cspm: { status: 'not-installed' },
indicesDetails: [
{ index: 'logs-cloud_security_posture.findings_latest-default', status: 'empty' },
{ index: 'logs-cloud_security_posture.findings-default*', status: 'empty' },
],
},
})
);
(useCspIntegrationLink as jest.Mock).mockImplementation(() => chance.url());

const children = chance.sentence();
renderCloudPosturePage({ children });

expect(screen.getByTestId(PACKAGE_NOT_INSTALLED_TEST_SUBJECT)).toBeInTheDocument();
expect(screen.queryByText(children)).not.toBeInTheDocument();
expect(screen.queryByTestId(LOADING_STATE_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByTestId(SUBSCRIPTION_NOT_ALLOWED_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByTestId(ERROR_STATE_TEST_SUBJECT)).not.toBeInTheDocument();
});

it('renders default loading state when the integration query is loading', () => {
(useCspSetupStatusApi as jest.Mock).mockImplementation(
() =>
createReactQueryResponse({
status: 'loading',
}) as unknown as UseQueryResult
);

const children = chance.sentence();
renderCloudPosturePage({ children });

expect(screen.getByTestId(LOADING_STATE_TEST_SUBJECT)).toBeInTheDocument();
expect(screen.queryByText(children)).not.toBeInTheDocument();
expect(screen.queryByTestId(ERROR_STATE_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByTestId(SUBSCRIPTION_NOT_ALLOWED_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByTestId(PACKAGE_NOT_INSTALLED_TEST_SUBJECT)).not.toBeInTheDocument();
});

it('renders default error state when the integration query has an error', () => {
(useCspSetupStatusApi as jest.Mock).mockImplementation(
() =>
createReactQueryResponse({
status: 'error',
error: new Error('error'),
}) as unknown as UseQueryResult
);

const children = chance.sentence();
renderCloudPosturePage({ children });

expect(screen.getByTestId(ERROR_STATE_TEST_SUBJECT)).toBeInTheDocument();
expect(screen.queryByTestId(LOADING_STATE_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByTestId(SUBSCRIPTION_NOT_ALLOWED_TEST_SUBJECT)).not.toBeInTheDocument();
expect(screen.queryByText(children)).not.toBeInTheDocument();
expect(screen.queryByTestId(PACKAGE_NOT_INSTALLED_TEST_SUBJECT)).not.toBeInTheDocument();
});

it('renders default loading text when query isLoading', () => {
const query = createReactQueryResponse({
status: 'loading',
Expand Down
Loading

0 comments on commit ad25b21

Please sign in to comment.