Skip to content

Commit a9be1a2

Browse files
committed
[Cloud Security] Refactoring tests (#195675)
(cherry picked from commit e6c2750)
1 parent 38de3b9 commit a9be1a2

11 files changed

Lines changed: 220 additions & 301 deletions

File tree

x-pack/test/api_integration/apis/cloud_security_posture/helper.ts

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,12 @@
66
*/
77

88
import type { Agent as SuperTestAgent } from 'supertest';
9-
import { Client } from '@elastic/elasticsearch';
10-
import expect from '@kbn/expect';
9+
1110
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
12-
import type { IndexDetails } from '@kbn/cloud-security-posture-common';
1311
import { CLOUD_SECURITY_PLUGIN_VERSION } from '@kbn/cloud-security-posture-plugin/common/constants';
1412
import { SecurityService } from '@kbn/ftr-common-functional-ui-services';
1513
import { RoleCredentials } from '@kbn/ftr-common-functional-services';
1614

17-
export const deleteIndex = async (es: Client, indexToBeDeleted: string[]) => {
18-
return Promise.all([
19-
...indexToBeDeleted.map((indexes) =>
20-
es.deleteByQuery({
21-
index: indexes,
22-
query: {
23-
match_all: {},
24-
},
25-
ignore_unavailable: true,
26-
refresh: true,
27-
})
28-
),
29-
]);
30-
};
31-
32-
export const bulkIndex = async <T>(es: Client, findingsMock: T[], indexName: string) => {
33-
const operations = findingsMock.flatMap((finding) => [
34-
{ create: { _index: indexName } }, // Action description
35-
{
36-
...finding,
37-
'@timestamp': new Date().toISOString(),
38-
}, // Data to index
39-
]);
40-
41-
await es.bulk({
42-
body: operations, // Bulk API expects 'body' for operations
43-
refresh: true,
44-
});
45-
};
46-
47-
export const addIndex = async <T>(es: Client, findingsMock: T[], indexName: string) => {
48-
await Promise.all([
49-
...findingsMock.map((finding) =>
50-
es.index({
51-
index: indexName,
52-
body: {
53-
...finding,
54-
'@timestamp': new Date().toISOString(),
55-
},
56-
refresh: true,
57-
})
58-
),
59-
]);
60-
};
61-
6215
export async function createPackagePolicy(
6316
supertest: SuperTestAgent,
6417
agentPolicyId: string,
@@ -233,10 +186,10 @@ export const createUser = async (security: SecurityService, userName: string, ro
233186
});
234187
};
235188

236-
export const createCSPOnlyRole = async (
189+
export const createCSPRole = async (
237190
security: SecurityService,
238191
roleName: string,
239-
indicesName: string
192+
indicesName?: string[]
240193
) => {
241194
await security.role.create(roleName, {
242195
kibana: [
@@ -245,12 +198,12 @@ export const createCSPOnlyRole = async (
245198
spaces: ['*'],
246199
},
247200
],
248-
...(indicesName.length !== 0
201+
...(indicesName && indicesName.length > 0
249202
? {
250203
elasticsearch: {
251204
indices: [
252205
{
253-
names: [indicesName],
206+
names: indicesName,
254207
privileges: ['read'],
255208
},
256209
],
@@ -267,15 +220,3 @@ export const deleteRole = async (security: SecurityService, roleName: string) =>
267220
export const deleteUser = async (security: SecurityService, userName: string) => {
268221
await security.user.delete(userName);
269222
};
270-
271-
export const assertIndexStatus = (
272-
indicesDetails: IndexDetails[],
273-
indexName: string,
274-
expectedStatus: string
275-
) => {
276-
const actualValue = indicesDetails.find((idx) => idx.index === indexName)?.status;
277-
expect(actualValue).to.eql(
278-
expectedStatus,
279-
`expected ${indexName} status to be ${expectedStatus} but got ${actualValue} instead`
280-
);
281-
};

x-pack/test/api_integration/apis/cloud_security_posture/status/status_index_timeout.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ import {
1313
LATEST_FINDINGS_INDEX_DEFAULT_NS,
1414
VULNERABILITIES_INDEX_DEFAULT_NS,
1515
} from '@kbn/cloud-security-posture-plugin/common/constants';
16+
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
1617
import { generateAgent } from '../../../../fleet_api_integration/helpers';
1718
import { FtrProviderContext } from '../../../ftr_provider_context';
18-
import { deleteIndex, createPackagePolicy } from '../helper';
19-
20-
const INDEX_ARRAY = [
21-
FINDINGS_INDEX_DEFAULT_NS,
22-
LATEST_FINDINGS_INDEX_DEFAULT_NS,
23-
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
24-
VULNERABILITIES_INDEX_DEFAULT_NS,
25-
];
19+
import { createPackagePolicy } from '../helper';
2620

2721
const currentTimeMinusFourHours = new Date(Date.now() - 21600000).toISOString();
2822
const currentTimeMinusTenMinutes = new Date(Date.now() - 600000).toISOString();
@@ -35,6 +29,13 @@ export default function (providerContext: FtrProviderContext) {
3529
const esArchiver = getService('esArchiver');
3630
const kibanaServer = getService('kibanaServer');
3731
const fleetAndAgents = getService('fleetAndAgents');
32+
const findingsIndex = new EsIndexDataProvider(es, FINDINGS_INDEX_DEFAULT_NS);
33+
const latestFindingsIndex = new EsIndexDataProvider(es, LATEST_FINDINGS_INDEX_DEFAULT_NS);
34+
const vulnerabilitiesIndex = new EsIndexDataProvider(es, VULNERABILITIES_INDEX_DEFAULT_NS);
35+
const cdrVulnerabilitiesIndex = new EsIndexDataProvider(
36+
es,
37+
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN
38+
);
3839

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

87-
await deleteIndex(es, INDEX_ARRAY);
88+
await findingsIndex.deleteAll();
89+
await latestFindingsIndex.deleteAll();
90+
await vulnerabilitiesIndex.deleteAll();
91+
await cdrVulnerabilitiesIndex.deleteAll();
8892
});
8993

9094
afterEach(async () => {
9195
await kibanaServer.savedObjects.cleanStandardList();
9296
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
97+
98+
await findingsIndex.deleteAll();
99+
await latestFindingsIndex.deleteAll();
100+
await vulnerabilitiesIndex.deleteAll();
101+
await cdrVulnerabilitiesIndex.deleteAll();
93102
});
94103

95104
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 () => {

x-pack/test/api_integration/apis/cloud_security_posture/status/status_indexed.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,25 @@ import expect from '@kbn/expect';
88
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
99
import { CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN } from '@kbn/cloud-security-posture-common';
1010
import type { CspSetupStatus } from '@kbn/cloud-security-posture-common';
11-
import {
12-
FINDINGS_INDEX_DEFAULT_NS,
13-
LATEST_FINDINGS_INDEX_DEFAULT_NS,
14-
VULNERABILITIES_INDEX_DEFAULT_NS,
15-
} from '@kbn/cloud-security-posture-plugin/common/constants';
11+
import { LATEST_FINDINGS_INDEX_DEFAULT_NS } from '@kbn/cloud-security-posture-plugin/common/constants';
1612
import { FtrProviderContext } from '../../../ftr_provider_context';
17-
import { deleteIndex, addIndex, createPackagePolicy } from '../helper';
13+
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
14+
import { createPackagePolicy } from '../helper';
1815
import { findingsMockData, vulnerabilityMockData } from '../mock_data';
1916

20-
const INDEX_ARRAY = [
21-
FINDINGS_INDEX_DEFAULT_NS,
22-
LATEST_FINDINGS_INDEX_DEFAULT_NS,
23-
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
24-
VULNERABILITIES_INDEX_DEFAULT_NS,
25-
];
26-
2717
export default function (providerContext: FtrProviderContext) {
2818
const { getService } = providerContext;
2919
const supertest = getService('supertest');
3020
const es = getService('es');
3121
const esArchiver = getService('esArchiver');
3222
const kibanaServer = getService('kibanaServer');
23+
const latestFindingsIndex = new EsIndexDataProvider(es, LATEST_FINDINGS_INDEX_DEFAULT_NS);
24+
const latestVulnerabilitiesIndex = new EsIndexDataProvider(
25+
es,
26+
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN
27+
);
28+
const mock3PIndex = 'security_solution-mock-3p-integration.misconfiguration_latest';
29+
const _3pIndex = new EsIndexDataProvider(es, mock3PIndex);
3330

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

5148
agentPolicyId = agentPolicyResponse.item.id;
5249

53-
await deleteIndex(es, INDEX_ARRAY);
54-
await addIndex(es, findingsMockData, LATEST_FINDINGS_INDEX_DEFAULT_NS);
55-
await addIndex(es, vulnerabilityMockData, CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN);
50+
await latestFindingsIndex.deleteAll();
51+
await latestVulnerabilitiesIndex.deleteAll();
52+
await _3pIndex.deleteAll();
5653
});
5754

5855
afterEach(async () => {
59-
await deleteIndex(es, INDEX_ARRAY);
56+
await latestFindingsIndex.deleteAll();
57+
await latestVulnerabilitiesIndex.deleteAll();
58+
await _3pIndex.destroyIndex();
6059
await kibanaServer.savedObjects.cleanStandardList();
6160
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
6261
});
6362

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

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

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

8481
const { body: res }: { body: CspSetupStatus } = await supertest
8582
.get(`/internal/cloud_security_posture/status`)
@@ -91,13 +88,9 @@ export default function (providerContext: FtrProviderContext) {
9188
true,
9289
`expected hasMisconfigurationsFindings to be true but got ${res.hasMisconfigurationsFindings} instead`
9390
);
94-
95-
await deleteIndex(es, [mock3PIndex]);
9691
});
9792

9893
it(`Return hasMisconfigurationsFindings false when there are no findings`, async () => {
99-
await deleteIndex(es, INDEX_ARRAY);
100-
10194
const { body: res }: { body: CspSetupStatus } = await supertest
10295
.get(`/internal/cloud_security_posture/status`)
10396
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
@@ -120,6 +113,8 @@ export default function (providerContext: FtrProviderContext) {
120113
'kspm'
121114
);
122115

116+
await latestFindingsIndex.addBulk(findingsMockData);
117+
123118
const { body: res }: { body: CspSetupStatus } = await supertest
124119
.get(`/internal/cloud_security_posture/status`)
125120
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
@@ -142,6 +137,8 @@ export default function (providerContext: FtrProviderContext) {
142137
'cspm'
143138
);
144139

140+
await latestFindingsIndex.addBulk(findingsMockData);
141+
145142
const { body: res }: { body: CspSetupStatus } = await supertest
146143
.get(`/internal/cloud_security_posture/status`)
147144
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
@@ -164,6 +161,8 @@ export default function (providerContext: FtrProviderContext) {
164161
'vuln_mgmt'
165162
);
166163

164+
await latestVulnerabilitiesIndex.addBulk(vulnerabilityMockData);
165+
167166
const { body: res }: { body: CspSetupStatus } = await supertest
168167
.get(`/internal/cloud_security_posture/status`)
169168
.set(ELASTIC_HTTP_VERSION_HEADER, '1')

x-pack/test/api_integration/apis/cloud_security_posture/status/status_indexing.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@
77
import expect from '@kbn/expect';
88
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
99
import type { CspSetupStatus } from '@kbn/cloud-security-posture-common';
10-
import { CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN } from '@kbn/cloud-security-posture-common';
1110
import {
1211
FINDINGS_INDEX_DEFAULT_NS,
13-
LATEST_FINDINGS_INDEX_DEFAULT_NS,
1412
VULNERABILITIES_INDEX_DEFAULT_NS,
1513
} from '@kbn/cloud-security-posture-plugin/common/constants';
1614
import { FtrProviderContext } from '../../../ftr_provider_context';
17-
import { deleteIndex, addIndex, createPackagePolicy } from '../helper';
15+
import { EsIndexDataProvider } from '../../../../cloud_security_posture_api/utils';
16+
import { createPackagePolicy } from '../helper';
1817
import { findingsMockData, vulnerabilityMockData } from '../mock_data';
1918

20-
const INDEX_ARRAY = [
21-
FINDINGS_INDEX_DEFAULT_NS,
22-
LATEST_FINDINGS_INDEX_DEFAULT_NS,
23-
CDR_LATEST_NATIVE_VULNERABILITIES_INDEX_PATTERN,
24-
VULNERABILITIES_INDEX_DEFAULT_NS,
25-
];
26-
2719
export default function (providerContext: FtrProviderContext) {
2820
const { getService } = providerContext;
2921
const supertest = getService('supertest');
3022
const es = getService('es');
3123
const esArchiver = getService('esArchiver');
3224
const kibanaServer = getService('kibanaServer');
25+
const findingsIndex = new EsIndexDataProvider(es, FINDINGS_INDEX_DEFAULT_NS);
26+
const vulnerabilitiesIndex = new EsIndexDataProvider(es, VULNERABILITIES_INDEX_DEFAULT_NS);
3327

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

5145
agentPolicyId = agentPolicyResponse.item.id;
52-
await deleteIndex(es, INDEX_ARRAY);
53-
await addIndex(es, findingsMockData, FINDINGS_INDEX_DEFAULT_NS);
54-
await addIndex(es, vulnerabilityMockData, VULNERABILITIES_INDEX_DEFAULT_NS);
46+
await findingsIndex.deleteAll();
47+
await vulnerabilitiesIndex.deleteAll();
5548
});
5649

5750
afterEach(async () => {
58-
await deleteIndex(es, INDEX_ARRAY);
51+
await findingsIndex.deleteAll();
52+
await vulnerabilitiesIndex.deleteAll();
5953
await kibanaServer.savedObjects.cleanStandardList();
6054
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
6155
});
@@ -70,6 +64,8 @@ export default function (providerContext: FtrProviderContext) {
7064
'kspm'
7165
);
7266

67+
await findingsIndex.addBulk(findingsMockData);
68+
7369
const { body: res }: { body: CspSetupStatus } = await supertest
7470
.get(`/internal/cloud_security_posture/status`)
7571
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
@@ -92,6 +88,8 @@ export default function (providerContext: FtrProviderContext) {
9288
'cspm'
9389
);
9490

91+
await findingsIndex.addBulk(findingsMockData);
92+
9593
const { body: res }: { body: CspSetupStatus } = await supertest
9694
.get(`/internal/cloud_security_posture/status`)
9795
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
@@ -114,6 +112,8 @@ export default function (providerContext: FtrProviderContext) {
114112
'vuln_mgmt'
115113
);
116114

115+
await vulnerabilitiesIndex.addBulk(vulnerabilityMockData);
116+
117117
const { body: res }: { body: CspSetupStatus } = await supertest
118118
.get(`/internal/cloud_security_posture/status`)
119119
.set(ELASTIC_HTTP_VERSION_HEADER, '1')

0 commit comments

Comments
 (0)