Skip to content

Commit

Permalink
[Cloud Security] Refactoring tests (#195675)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfirpeled authored Oct 10, 2024
1 parent 2759994 commit e6c2750
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 301 deletions.
69 changes: 5 additions & 64 deletions x-pack/test/api_integration/apis/cloud_security_posture/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,12 @@
*/

import type { Agent as SuperTestAgent } from 'supertest';
import { Client } from '@elastic/elasticsearch';
import expect from '@kbn/expect';

import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { IndexDetails } from '@kbn/cloud-security-posture-common';
import { CLOUD_SECURITY_PLUGIN_VERSION } from '@kbn/cloud-security-posture-plugin/common/constants';
import { SecurityService } from '@kbn/ftr-common-functional-ui-services';
import { RoleCredentials } from '@kbn/ftr-common-functional-services';

export const deleteIndex = async (es: Client, indexToBeDeleted: string[]) => {
return Promise.all([
...indexToBeDeleted.map((indexes) =>
es.deleteByQuery({
index: indexes,
query: {
match_all: {},
},
ignore_unavailable: true,
refresh: true,
})
),
]);
};

export const bulkIndex = async <T>(es: Client, findingsMock: T[], indexName: string) => {
const operations = findingsMock.flatMap((finding) => [
{ create: { _index: indexName } }, // Action description
{
...finding,
'@timestamp': new Date().toISOString(),
}, // Data to index
]);

await es.bulk({
body: operations, // Bulk API expects 'body' for operations
refresh: true,
});
};

export const addIndex = async <T>(es: Client, findingsMock: T[], indexName: string) => {
await Promise.all([
...findingsMock.map((finding) =>
es.index({
index: indexName,
body: {
...finding,
'@timestamp': new Date().toISOString(),
},
refresh: true,
})
),
]);
};

export async function createPackagePolicy(
supertest: SuperTestAgent,
agentPolicyId: string,
Expand Down Expand Up @@ -233,10 +186,10 @@ export const createUser = async (security: SecurityService, userName: string, ro
});
};

export const createCSPOnlyRole = async (
export const createCSPRole = async (
security: SecurityService,
roleName: string,
indicesName: string
indicesName?: string[]
) => {
await security.role.create(roleName, {
kibana: [
Expand All @@ -245,12 +198,12 @@ export const createCSPOnlyRole = async (
spaces: ['*'],
},
],
...(indicesName.length !== 0
...(indicesName && indicesName.length > 0
? {
elasticsearch: {
indices: [
{
names: [indicesName],
names: indicesName,
privileges: ['read'],
},
],
Expand All @@ -267,15 +220,3 @@ export const deleteRole = async (security: SecurityService, roleName: string) =>
export const deleteUser = async (security: SecurityService, userName: string) => {
await security.user.delete(userName);
};

export const assertIndexStatus = (
indicesDetails: IndexDetails[],
indexName: string,
expectedStatus: string
) => {
const actualValue = indicesDetails.find((idx) => idx.index === indexName)?.status;
expect(actualValue).to.eql(
expectedStatus,
`expected ${indexName} status to be ${expectedStatus} but got ${actualValue} instead`
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import {
LATEST_FINDINGS_INDEX_DEFAULT_NS,
VULNERABILITIES_INDEX_DEFAULT_NS,
} from '@kbn/cloud-security-posture-plugin/common/constants';
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
import { generateAgent } from '../../../../fleet_api_integration/helpers';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { deleteIndex, createPackagePolicy } from '../helper';

const INDEX_ARRAY = [
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
VULNERABILITIES_INDEX_DEFAULT_NS,
];
import { createPackagePolicy } from '../helper';

const currentTimeMinusFourHours = new Date(Date.now() - 21600000).toISOString();
const currentTimeMinusTenMinutes = new Date(Date.now() - 600000).toISOString();
Expand All @@ -35,6 +29,13 @@ export default function (providerContext: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const fleetAndAgents = getService('fleetAndAgents');
const findingsIndex = new EsIndexDataProvider(es, FINDINGS_INDEX_DEFAULT_NS);
const latestFindingsIndex = new EsIndexDataProvider(es, LATEST_FINDINGS_INDEX_DEFAULT_NS);
const vulnerabilitiesIndex = new EsIndexDataProvider(es, VULNERABILITIES_INDEX_DEFAULT_NS);
const cdrVulnerabilitiesIndex = new EsIndexDataProvider(
es,
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN
);

describe('GET /internal/cloud_security_posture/status', () => {
let agentPolicyId: string;
Expand Down Expand Up @@ -84,12 +85,20 @@ export default function (providerContext: FtrProviderContext) {
.expect(200);
await generateAgent(providerContext, 'healthy', `Agent policy test 2`, agentPolicyId);

await deleteIndex(es, INDEX_ARRAY);
await findingsIndex.deleteAll();
await latestFindingsIndex.deleteAll();
await vulnerabilitiesIndex.deleteAll();
await cdrVulnerabilitiesIndex.deleteAll();
});

afterEach(async () => {
await kibanaServer.savedObjects.cleanStandardList();
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');

await findingsIndex.deleteAll();
await latestFindingsIndex.deleteAll();
await vulnerabilitiesIndex.deleteAll();
await cdrVulnerabilitiesIndex.deleteAll();
});

it(`Should return index-timeout when installed kspm, has findings only on logs-cloud_security_posture.findings-default* and it has been more than 10 minutes since the installation`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN } from '@kbn/cloud-security-posture-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-common';
import {
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
VULNERABILITIES_INDEX_DEFAULT_NS,
} from '@kbn/cloud-security-posture-plugin/common/constants';
import { LATEST_FINDINGS_INDEX_DEFAULT_NS } from '@kbn/cloud-security-posture-plugin/common/constants';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { deleteIndex, addIndex, createPackagePolicy } from '../helper';
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
import { createPackagePolicy } from '../helper';
import { findingsMockData, vulnerabilityMockData } from '../mock_data';

const INDEX_ARRAY = [
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
VULNERABILITIES_INDEX_DEFAULT_NS,
];

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const latestFindingsIndex = new EsIndexDataProvider(es, LATEST_FINDINGS_INDEX_DEFAULT_NS);
const latestVulnerabilitiesIndex = new EsIndexDataProvider(
es,
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN
);
const mock3PIndex = 'security_solution-mock-3p-integration.misconfiguration_latest';
const _3pIndex = new EsIndexDataProvider(es, mock3PIndex);

describe('GET /internal/cloud_security_posture/status', () => {
let agentPolicyId: string;
Expand All @@ -50,19 +47,21 @@ export default function (providerContext: FtrProviderContext) {

agentPolicyId = agentPolicyResponse.item.id;

await deleteIndex(es, INDEX_ARRAY);
await addIndex(es, findingsMockData, LATEST_FINDINGS_INDEX_DEFAULT_NS);
await addIndex(es, vulnerabilityMockData, CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN);
await latestFindingsIndex.deleteAll();
await latestVulnerabilitiesIndex.deleteAll();
await _3pIndex.deleteAll();
});

afterEach(async () => {
await deleteIndex(es, INDEX_ARRAY);
await latestFindingsIndex.deleteAll();
await latestVulnerabilitiesIndex.deleteAll();
await _3pIndex.destroyIndex();
await kibanaServer.savedObjects.cleanStandardList();
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
});

it(`Return hasMisconfigurationsFindings true when there are latest findings but no installed integrations`, async () => {
await addIndex(es, findingsMockData, LATEST_FINDINGS_INDEX_DEFAULT_NS);
await latestFindingsIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
Expand All @@ -77,9 +76,7 @@ export default function (providerContext: FtrProviderContext) {
});

it(`Return hasMisconfigurationsFindings true when there are only findings in third party index`, async () => {
await deleteIndex(es, INDEX_ARRAY);
const mock3PIndex = 'security_solution-mock-3p-integration.misconfiguration_latest';
await addIndex(es, findingsMockData, mock3PIndex);
await _3pIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
Expand All @@ -91,13 +88,9 @@ export default function (providerContext: FtrProviderContext) {
true,
`expected hasMisconfigurationsFindings to be true but got ${res.hasMisconfigurationsFindings} instead`
);

await deleteIndex(es, [mock3PIndex]);
});

it(`Return hasMisconfigurationsFindings false when there are no findings`, async () => {
await deleteIndex(es, INDEX_ARRAY);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand All @@ -120,6 +113,8 @@ export default function (providerContext: FtrProviderContext) {
'kspm'
);

await latestFindingsIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand All @@ -142,6 +137,8 @@ export default function (providerContext: FtrProviderContext) {
'cspm'
);

await latestFindingsIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand All @@ -164,6 +161,8 @@ export default function (providerContext: FtrProviderContext) {
'vuln_mgmt'
);

await latestVulnerabilitiesIndex.addBulk(vulnerabilityMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-common';
import { CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN } from '@kbn/cloud-security-posture-common';
import {
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
VULNERABILITIES_INDEX_DEFAULT_NS,
} from '@kbn/cloud-security-posture-plugin/common/constants';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { deleteIndex, addIndex, createPackagePolicy } from '../helper';
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
import { createPackagePolicy } from '../helper';
import { findingsMockData, vulnerabilityMockData } from '../mock_data';

const INDEX_ARRAY = [
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
VULNERABILITIES_INDEX_DEFAULT_NS,
];

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const findingsIndex = new EsIndexDataProvider(es, FINDINGS_INDEX_DEFAULT_NS);
const vulnerabilitiesIndex = new EsIndexDataProvider(es, VULNERABILITIES_INDEX_DEFAULT_NS);

describe('GET /internal/cloud_security_posture/status', () => {
let agentPolicyId: string;
Expand All @@ -49,13 +43,13 @@ export default function (providerContext: FtrProviderContext) {
});

agentPolicyId = agentPolicyResponse.item.id;
await deleteIndex(es, INDEX_ARRAY);
await addIndex(es, findingsMockData, FINDINGS_INDEX_DEFAULT_NS);
await addIndex(es, vulnerabilityMockData, VULNERABILITIES_INDEX_DEFAULT_NS);
await findingsIndex.deleteAll();
await vulnerabilitiesIndex.deleteAll();
});

afterEach(async () => {
await deleteIndex(es, INDEX_ARRAY);
await findingsIndex.deleteAll();
await vulnerabilitiesIndex.deleteAll();
await kibanaServer.savedObjects.cleanStandardList();
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
});
Expand All @@ -70,6 +64,8 @@ export default function (providerContext: FtrProviderContext) {
'kspm'
);

await findingsIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand All @@ -92,6 +88,8 @@ export default function (providerContext: FtrProviderContext) {
'cspm'
);

await findingsIndex.addBulk(findingsMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand All @@ -114,6 +112,8 @@ export default function (providerContext: FtrProviderContext) {
'vuln_mgmt'
);

await vulnerabilitiesIndex.addBulk(vulnerabilityMockData);

const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
Expand Down
Loading

0 comments on commit e6c2750

Please sign in to comment.